24 package org.lightjason.agentspeak.action.builtin;
26 import cern.colt.matrix.tbit.BitMatrix;
27 import cern.colt.matrix.tbit.BitVector;
28 import cern.colt.matrix.tdouble.DoubleMatrix2D;
29 import com.codepoetics.protonpack.StreamUtils;
30 import com.tngtech.java.junit.dataprovider.DataProvider;
31 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
32 import com.tngtech.java.junit.dataprovider.UseDataProvider;
33 import org.apache.commons.lang3.tuple.ImmutableTriple;
34 import org.apache.commons.lang3.tuple.Triple;
35 import org.junit.Assert;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
65 import java.lang.reflect.InvocationTargetException;
66 import java.util.ArrayList;
67 import java.util.Arrays;
68 import java.util.List;
69 import java.util.stream.Collectors;
70 import java.util.stream.Stream;
72 import static org.junit.Assert.assertTrue;
77 @RunWith( DataProviderRunner.class )
84 private static final BitMatrix MATRIX1 =
new BitMatrix( 2, 2 );
89 private static final BitMatrix MATRIX2 =
new BitMatrix( 2, 2 );
98 MATRIX1.put( 0, 1,
false );
99 MATRIX1.put( 1, 0,
false );
100 MATRIX1.put( 1, 1,
true );
101 MATRIX1.put( 0, 0,
true );
103 MATRIX2.put( 0, 1,
true );
104 MATRIX2.put( 1, 0,
true );
105 MATRIX2.put( 1, 1,
true );
106 MATRIX2.put( 0, 0,
false );
118 Stream.of( MATRIX1, MATRIX2 ),
137 Stream.of( 2D, 2D, 2D, 2D ),
138 Stream.of( MATRIX1, MATRIX2 ),
162 @SuppressWarnings(
"varargs" )
163 private static Stream<Object> testcase( final Stream<Object> p_input, final Stream<Class<?>> p_classes, final Stream<Object>... p_classresult )
165 final List<ITerm> l_input = p_input.map(
CRawTerm::from ).collect( Collectors.toList() );
167 return StreamUtils.zip(
169 Arrays.stream( p_classresult ),
170 ( i, j ) ->
new ImmutableTriple<>( l_input, i, j )
185 @UseDataProvider(
"generator" )
186 public final
void action( final Triple<List<
ITerm>, Class<? extends
IAction>, Stream<Object>> p_input )
187 throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException
189 final List<ITerm> l_return =
new ArrayList<>();
191 p_input.getMiddle().getConstructor().newInstance().execute(
198 Assert.assertArrayEquals(
199 p_input.getMiddle().toGenericString(),
200 l_return.stream().map(
ITerm::raw ).toArray(),
201 p_input.getRight().toArray()
211 final List<ITerm> l_return =
new ArrayList<>();
215 Stream.of( 2, 2 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
219 Assert.assertEquals( l_return.size(), 1 );
220 assertTrue( l_return.get( 0 ).raw() instanceof BitMatrix );
221 Assert.assertEquals( l_return.get( 0 ).<BitMatrix>raw().size(), 4 );
222 Assert.assertEquals( l_return.get( 0 ).<BitMatrix>raw().rows(), 2 );
223 Assert.assertEquals( l_return.get( 0 ).<BitMatrix>raw().columns(), 2 );
232 final List<ITerm> l_return =
new ArrayList<>();
236 Stream.of( MATRIX2 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
240 Assert.assertEquals( l_return.size(), 1 );
241 Assert.assertTrue( l_return.get( 0 ).raw() instanceof BitVector );
242 Assert.assertEquals( l_return.get( 0 ).<BitVector>raw().size(), 4 );
244 final BitVector l_bitvector = l_return.get( 0 ).raw();
246 Assert.assertEquals( l_bitvector.get( 0 ), true );
247 Assert.assertEquals( l_bitvector.get( 1 ), true );
248 Assert.assertEquals( l_bitvector.get( 2 ), true );
249 Assert.assertEquals( l_bitvector.get( 3 ), false );
258 final List<ITerm> l_return =
new ArrayList<>();
262 Stream.of( 1, MATRIX2 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
266 Assert.assertEquals( l_return.size(), 1 );
267 Assert.assertTrue( l_return.get( 0 ).raw() instanceof BitVector );
268 Assert.assertEquals( l_return.get( 0 ).<BitVector>raw().size(), 2 );
270 final BitVector l_bitvector = l_return.get( 0 ).raw();
272 Assert.assertEquals( l_bitvector.get( 0 ), true );
273 Assert.assertEquals( l_bitvector.get( 1 ), true );
282 final List<ITerm> l_return =
new ArrayList<>();
286 Stream.of( 1, MATRIX2 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
290 Assert.assertEquals( l_return.size(), 1 );
291 Assert.assertTrue( l_return.get( 0 ).raw() instanceof BitVector );
292 Assert.assertEquals( l_return.get( 0 ).<BitVector>raw().size(), 2 );
294 final BitVector l_bitvector = l_return.get( 0 ).raw();
296 Assert.assertEquals( l_bitvector.get( 0 ), true );
297 Assert.assertEquals( l_bitvector.get( 1 ), true );
306 final List<ITerm> l_return =
new ArrayList<>();
310 Stream.of( MATRIX1, 1, 0 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
314 Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 0D );
323 final List<ITerm> l_return =
new ArrayList<>();
327 Stream.of( MATRIX2, 0, 0 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
331 Assert.assertEquals( l_return.get( 0 ).<Boolean>raw(), false );
340 final List<ITerm> l_return =
new ArrayList<>();
341 final Double[][] l_result = {{0.0, 1.0}, {1.0, 1.0}};
345 Stream.of( MATRIX2 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
349 Assert.assertEquals( l_return.size(), 1 );
350 Assert.assertTrue( l_return.get( 0 ).raw() instanceof DoubleMatrix2D );
352 final DoubleMatrix2D l_blas = l_return.get( 0 ).raw();
354 Assert.assertEquals( l_blas.size(), 4 );
355 Assert.assertArrayEquals( l_blas.toArray(), l_result );
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
calculates the hamming distance.
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 size (rows * columns) of the matrix.
converts the bit matrix to a blas matrix.
base test class with helpers
returns the column number of a bit matrix.
returns a single row of a bit matrix.
IContext EMPTYPLAN
empty context with plan
final void tobitvector()
test toBitVector
combines all arguments to a single result with the and-operator.
static Object [] generator()
data provider generator
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 for the index tuple a numeric value.
execution context with local data
external action interface
returns the number of true values.
final void numericvalue()
test numericvalue
performs the logical xor operation to all bit matrices.
combines all arguments to a single result with the xor-operator.
returns the row number of a matrix.
final void boolValue()
test boolean 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
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
returns the number of false values.
returns a single column of a bit matrix.
< T > T raw()
cast to any raw value type
final void toblas()
test toblas
performs the logical not operation to all bit matrices.
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
final void column()
test column
returns for the index tuple a boolean value.
converts the bit matrix into a bit vector.
final void create()
test create
performs the logical or operation to all bit matrices.
performs the logical and operation to all bit matrices.
test math bit matrix functions
combines all arguments to a single result with the or-operator.
final void initialize()
initialize
returns a copy of the matrix.
returns the size of the collection.
performs the logical not-and operation to all bit matrices.
term structure for simple datatypes
returns the dimension (rows / columns) of a bit matrix.