24 package org.lightjason.agentspeak.action.builtin;
26 import cern.colt.matrix.tdouble.DoubleMatrix1D;
27 import cern.colt.matrix.tdouble.impl.DenseDoubleMatrix1D;
28 import com.codepoetics.protonpack.StreamUtils;
29 import com.tngtech.java.junit.dataprovider.DataProvider;
30 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
31 import com.tngtech.java.junit.dataprovider.UseDataProvider;
32 import org.apache.commons.lang3.tuple.ImmutableTriple;
33 import org.apache.commons.lang3.tuple.Triple;
34 import org.junit.Assert;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
54 import java.lang.reflect.InvocationTargetException;
55 import java.util.ArrayList;
56 import java.util.Arrays;
57 import java.util.Collections;
58 import java.util.List;
59 import java.util.stream.Collectors;
60 import java.util.stream.Stream;
66 @RunWith( DataProviderRunner.class )
74 private static final DoubleMatrix1D VECTOR1 =
new DenseDoubleMatrix1D(
new double[]{2, 5, 3, 8} );
80 private static final DoubleMatrix1D VECTOR2 =
new DenseDoubleMatrix1D(
new double[]{8, 6, 2, 1} );
92 Stream.of( VECTOR1, VECTOR2 ),
100 Stream.of( VECTOR1.zSum(), VECTOR2.zSum() ),
116 @SuppressWarnings(
"varargs" )
117 private static Stream<Object> testcase( final Stream<Object> p_input, final Stream<Class<?>> p_classes, final Stream<Object>... p_classresult )
119 final List<ITerm> l_input = p_input.map(
CRawTerm::from ).collect( Collectors.toList() );
121 return StreamUtils.zip(
123 Arrays.stream( p_classresult ),
124 ( i, j ) ->
new ImmutableTriple<>( l_input, i, j )
139 @UseDataProvider(
"generator" )
140 public final
void action( final Triple<List<
ITerm>, Class<? extends
IAction>, Stream<Object>> p_input )
141 throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException
143 final List<ITerm> l_return =
new ArrayList<>();
145 p_input.getMiddle().getConstructor().newInstance().execute(
151 Assert.assertArrayEquals(
152 p_input.getMiddle().toGenericString(),
153 l_return.stream().map(
ITerm::raw ).toArray(),
154 p_input.getRight().toArray()
164 final List<ITerm> l_return =
new ArrayList<>();
168 Stream.of( 2,
"dense" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
174 Stream.of( 4,
"sparse" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
178 Assert.assertEquals( l_return.size(), 2 );
179 Assert.assertEquals( l_return.get( 0 ).<DoubleMatrix1D>raw().size(), 2 );
180 Assert.assertEquals( l_return.get( 1 ).<DoubleMatrix1D>raw().size(), 4 );
187 public final void set()
189 final DoubleMatrix1D l_vector =
new DenseDoubleMatrix1D( 4 );
193 Stream.of( 0, 6.0, l_vector ).map(
CRawTerm::from ).collect( Collectors.toList() ),
194 Collections.emptyList()
197 Assert.assertEquals( l_vector.get( 0 ), 6, 0 );
206 final List<ITerm> l_return =
new ArrayList<>();
210 Stream.of( VECTOR1 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
214 Assert.assertEquals( l_return.size(), 1 );
215 Assert.assertTrue( l_return.get( 0 ).raw() instanceof List );
217 final List<Number> l_tolist = l_return.get( 0 ).raw();
218 Assert.assertArrayEquals( l_tolist.toArray(), Stream.of( 2.0, 5.0, 3.0, 8.0 ).collect( Collectors.toList() ).toArray() );
227 final DoubleMatrix1D l_vector =
new DenseDoubleMatrix1D( 4 );
231 Stream.of( 2, l_vector ).map(
CRawTerm::from ).collect( Collectors.toList() ),
232 Collections.emptyList()
235 Assert.assertArrayEquals( l_vector.toArray(), Stream.of( 2, 2, 2, 2 ).mapToDouble( i -> i ).toArray(), 0 );
244 final DoubleMatrix1D l_vector =
new DenseDoubleMatrix1D( 4 );
248 Stream.of( VECTOR2, l_vector ).map(
CRawTerm::from ).collect( Collectors.toList() ),
249 Collections.emptyList()
252 Assert.assertArrayEquals( l_vector.toArray(), VECTOR2.toArray(), 0 );
260 public final void get()
262 final List<ITerm> l_return =
new ArrayList<>();
266 Stream.of( VECTOR1, 0 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
270 Assert.assertEquals( l_return.size(), 1 );
271 Assert.assertTrue( l_return.get( 0 ).raw() instanceof Double );
272 Assert.assertEquals( l_return.get( 0 ).<Double>raw(), 2, 0 );
281 final List<ITerm> l_return =
new ArrayList<>();
285 Stream.of( VECTOR1, VECTOR2 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
289 Assert.assertEquals( l_return.size(), 2 );
290 Assert.assertArrayEquals( l_return.stream().map(
ITerm::raw ).toArray(), Stream.of( VECTOR1, VECTOR2 ).toArray() );
299 final List<ITerm> l_return =
new ArrayList<>();
303 Stream.of(
"1,2,3",
"dense" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
309 Stream.of(
"4,3,4",
"sparse" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
313 Assert.assertEquals( l_return.size(), 2 );
314 Assert.assertArrayEquals( l_return.get( 0 ).<DoubleMatrix1D>raw().toArray(),
new double[]{1, 2, 3}, 0 );
315 Assert.assertArrayEquals( l_return.get( 1 ).<DoubleMatrix1D>raw().toArray(),
new double[]{4, 3, 4}, 0 );
324 final List<ITerm> l_return =
new ArrayList<>();
328 Stream.of( Stream.of( 1, 2, 3 ).collect( Collectors.toList() ),
"dense" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
334 Stream.of( Stream.of( 4, 3, 4 ).collect( Collectors.toList() ),
"sparse" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
338 Assert.assertEquals( l_return.size(), 2 );
339 Assert.assertArrayEquals( l_return.get( 0 ).<DoubleMatrix1D>raw().toArray(),
new double[]{1, 2, 3}, 0 );
340 Assert.assertArrayEquals( l_return.get( 1 ).<DoubleMatrix1D>raw().toArray(),
new double[]{4, 3, 4}, 0 );
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
base test class with helpers
creates a dense- or sparse-matrix from a string.
IContext EMPTYPLAN
empty context with plan
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 assignscalar()
test assign scalar
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
returns the number of non-zero cells.
converts a vector to a list.
final void create()
test create
creates a dense- or sparse-vector.
returns a copy of the vector.
execution context with local data
external action interface
adds an element to the list.
test math blas vector functions
final void copy()
test copy
converts a set to a list.
creates a dense- or sparse-vector from as string.
returns the number of non-zero cells.
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
returns a single element of a vector.
returns an element of the list by the index.
< T > T raw()
cast to any raw value type
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
final void parse()
test parse
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
assigns a value or vector to all elements.
static Object [] generator()
data provider generator
final void assignvector()
test assign vector
final void tolist()
test toList
set a single element of a vector.
final void fromlist()
test fromlist
returns dot-product of vectors.
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
assigns a value or matrix to all elements.
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
returns a copy of the matrix.
creates a dense- or sparse-vector from a list.
term structure for simple datatypes