24 package org.lightjason.agentspeak.action.builtin;
26 import com.codepoetics.protonpack.StreamUtils;
27 import com.tngtech.java.junit.dataprovider.DataProvider;
28 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
29 import com.tngtech.java.junit.dataprovider.UseDataProvider;
30 import org.apache.commons.lang3.tuple.ImmutableTriple;
31 import org.apache.commons.lang3.tuple.Triple;
32 import org.apache.commons.math3.primes.Primes;
33 import org.junit.Assert;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
79 import java.lang.reflect.InvocationTargetException;
80 import java.util.ArrayList;
81 import java.util.Arrays;
82 import java.util.List;
83 import java.util.Random;
84 import java.util.function.Function;
85 import java.util.stream.Collectors;
86 import java.util.stream.IntStream;
87 import java.util.stream.Stream;
93 @RunWith( DataProviderRunner.class )
104 return Stream.concat(
108 Stream.of( 2.5, 9.1, 111.7, 889.9 ),
114 i -> (double) Primes.nextPrime( i.intValue() )
119 Stream.of( -2, -6, 4, -1, -5, 3, 49, 30, 6, 5, 1.3, 2.8, 9.7, 1, 8, 180, Math.PI ),
145 i -> Math.abs( i.doubleValue() ),
146 i -> Math.acos( i.doubleValue() ),
147 i -> Math.asin( i.doubleValue() ),
148 i -> Math.atan( i.doubleValue() ),
149 i -> Math.ceil( i.doubleValue() ),
150 i -> Math.cos( i.doubleValue() ),
151 i -> Math.cosh( i.doubleValue() ),
152 i -> Math.toDegrees( i.doubleValue() ),
153 i -> Math.exp( i.doubleValue() ),
154 i -> Primes.isPrime( i.intValue() ),
155 i -> Math.log( i.doubleValue() ),
156 i -> Math.log10( i.doubleValue() ),
157 i -> Math.floor( i.doubleValue() ),
158 i -> Math.toRadians( i.doubleValue() ),
159 i -> Math.round( i.doubleValue() ),
160 i -> Math.signum( i.doubleValue() ),
161 i -> Math.sin( i.doubleValue() ),
162 i -> Math.sinh( i.doubleValue() ),
163 i -> Math.sqrt( i.doubleValue() ),
164 i -> Math.tan( i.doubleValue() ),
165 i -> Math.tanh( i.doubleValue() )
179 final Random l_random =
new Random();
181 return aggregationvaluetestcase(
183 IntStream.range( 0, 100 ).boxed().map( i -> l_random.nextGaussian() ),
192 i -> i.mapToDouble( Number::doubleValue ).average().getAsDouble(),
193 i -> i.mapToDouble( Number::doubleValue ).sum(),
194 i -> i.mapToDouble( Number::doubleValue ).min().getAsDouble(),
195 i -> i.mapToDouble( Number::doubleValue ).max().getAsDouble()
210 @SuppressWarnings(
"varargs" )
211 private static Stream<Object> singlevaluetestcase( final Stream<Number> p_input, final Stream<Class<? extends
IAction>> p_class,
212 final Function<Number, ?>... p_result )
214 final List<ITerm> l_input = p_input.map(
CRawTerm::from ).collect( Collectors.toList() );
216 return StreamUtils.zip(
218 Arrays.stream( p_result ),
219 ( i, j ) ->
new ImmutableTriple<>( l_input, i, j )
233 @SuppressWarnings(
"varargs" )
234 private static Stream<Object> aggregationvaluetestcase( final Stream<Number> p_input, final Stream<Class<? extends
IAction>> p_class,
235 final Function<Stream<Number>, ?>... p_result )
237 final List<ITerm> l_input = p_input.map(
CRawTerm::from ).collect( Collectors.toList() );
239 return StreamUtils.zip(
241 Arrays.stream( p_result ),
242 ( i, j ) ->
new ImmutableTriple<>( l_input, i, j )
258 @UseDataProvider(
"aggregationvaluegenerate" )
259 public final
void aggregationvalueaction( final Triple<List<
ITerm>, Class<? extends
IAction>, Function<Stream<Number>, ?>> p_input )
260 throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException
262 final List<ITerm> l_return =
new ArrayList<>();
264 p_input.getMiddle().getConstructor().newInstance().execute(
270 Assert.assertEquals( l_return.size(), 1 );
271 Assert.assertEquals( l_return.get( 0 ).raw(), p_input.getRight().apply( p_input.getLeft().stream().map(
ITerm::<Number>raw ) ) );
286 @UseDataProvider(
"singlevaluegenerate" )
287 public final
void singlevalueaction( final Triple<List<
ITerm>, Class<? extends
IAction>, Function<Number, ?>> p_input )
288 throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException
290 final List<ITerm> l_return =
new ArrayList<>();
292 p_input.getMiddle().getConstructor().newInstance().execute(
298 Assert.assertArrayEquals(
299 p_input.getMiddle().toGenericString(),
300 l_return.stream().map(
ITerm::raw ).toArray(),
301 p_input.getLeft().stream().map(
ITerm::<Number>raw ).map( p_input.getRight() ).toArray()
312 final List<ITerm> l_return =
new ArrayList<>();
316 Stream.of( 49, 30, 6, 5 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
320 Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 18851684897584L );
321 Assert.assertEquals( l_return.get( 1 ).<Number>raw(), 6L );
331 final List<ITerm> l_return =
new ArrayList<>();
335 Stream.of( 5, 1, 2, 3, 4 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
339 Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 120L );
340 Assert.assertEquals( l_return.get( 1 ).<Number>raw(), 1L );
341 Assert.assertEquals( l_return.get( 2 ).<Number>raw(), 2L );
342 Assert.assertEquals( l_return.get( 3 ).<Number>raw(), 6L );
343 Assert.assertEquals( l_return.get( 4 ).<Number>raw(), 24L );
353 final List<ITerm> l_return =
new ArrayList<>();
357 Stream.of( 8, 120 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
361 Assert.assertArrayEquals( l_return.get( 0 ).<List<?>>raw().toArray(), Stream.of( 2D, 2D, 2D ).toArray() );
362 Assert.assertArrayEquals( l_return.get( 1 ).<List<?>>raw().toArray(), Stream.of( 2D, 2D, 2D, 3D, 5D ).toArray() );
372 final List<ITerm> l_return =
new ArrayList<>();
376 Stream.of( 1, 1, 1, 10, 20, 30 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
380 Assert.assertEquals( l_return.get( 1 ).<Number>raw(), 0.9999546021312976 );
381 Assert.assertEquals( l_return.get( 2 ).<Number>raw(), 0.9999999979388463 );
382 Assert.assertEquals( l_return.get( 3 ).<Number>raw(), 0.9999999999999065 );
392 final List<ITerm> l_return =
new ArrayList<>();
396 Stream.of( 3, 2, 8, 3 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
400 Assert.assertArrayEquals( l_return.stream().map(
ITerm::<Number>raw ).toArray(), Stream.of( 3L, 966L ).toArray() );
410 final List<ITerm> l_return =
new ArrayList<>();
414 Stream.of( 2, 3, 4, 0.5 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
418 Assert.assertArrayEquals(
419 l_return.stream().map(
ITerm::raw ).toArray(),
420 Stream.of( 9.0, 16.0, 0.25 ).toArray()
431 final List<ITerm> l_return =
new ArrayList<>();
435 Stream.of( 1.05, 1.03, 0.94, 1.02, 1.04 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
439 Assert.assertEquals( l_return.size(), 1 );
440 Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 1.0152139522031014 );
450 final List<ITerm> l_return =
new ArrayList<>();
454 Stream.of( 150, 50 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
458 Assert.assertEquals( l_return.size(), 1 );
459 Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 75.0 );
468 final Random l_random =
new Random();
469 final List<Double> l_input = IntStream.range( 0, 100 ).mapToDouble( i -> l_random.nextGaussian() ).boxed().collect( Collectors.toList() );
471 final List<ITerm> l_return =
new ArrayList<>();
475 l_input.stream().map(
CRawTerm::from ).collect( Collectors.toList() ),
479 Assert.assertArrayEquals(
480 l_return.stream().map(
ITerm::<Number>raw ).toArray(),
481 StreamUtils.windowed( l_input.stream(), 2, 2 )
482 .mapToDouble( i -> Math.hypot( i.get( 0 ), i.get( 1 ) ) )
495 final List<ITerm> l_return =
new ArrayList<>();
499 Stream.of( 3, 4, 9, 1, 7, 8, 4 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
503 Assert.assertEquals( l_return.size(), 1 );
504 Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 2D );
513 final List<ITerm> l_return =
new ArrayList<>();
517 Stream.of( 3, 4, 9, 1, 7, 8, 4 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
521 Assert.assertEquals( l_return.size(), 1 );
522 Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 3D );
final void pow()
test power
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
final void minIndex()
test min index
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
final void factorial()
test factorial
action for index of minimum.
action for checking for a prime number.
base test class with helpers
final void harmonicmean()
test harmonicmean
IContext EMPTYPLAN
empty context with plan
final void maxIndex()
test max index
action for sinus hyperbolic value.
action for converting degree value into radian.
static Object [] aggregationvaluegenerate()
data provider generator for aggregation-value tests
final void sigmoid()
test sigmoid
action for calculating a parameterized sigmoid function.
action for calculating the euclidian length.
action for converting angle value to degree value.
action for arc cosinus value.
action for exponential value.
final void hypot()
test hypot
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
action for calculating factorial.
action for absolute value.
execution context with local data
final void geometricmean()
test geometricmean
external action interface
action for harmonic mean.
action for geometric mean.
final void primefactors()
test primefactors
action for creating prime factors list.
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
action for calculating binomial coefficient.
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
action for calculating stirling number.
action for tangens hyperbolic value.
action for cosinus hyperbolic value.
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
< T > T raw()
cast to any raw value type
action for arc sinus value.
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
action for create the next prime number.
action for rounded value.
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
action for logarithm (base 10) value.
action for tangens value.
final void stirling()
test stirling
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
action for tangens value.
action for index of maximum.
action for sum of elements.
action for cosinus value.
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
action for logarithm value.
static Object [] singlevaluegenerate()
data provider generator for single-value tests
term structure for simple datatypes
final void binomial()
test binomial
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation