LightJason - AgentSpeak(L++)
TestCActionMathBitMatrix.java
Go to the documentation of this file.
1 /*
2  * @cond LICENSE
3  * ######################################################################################
4  * # LGPL License #
5  * # #
6  * # This file is part of the LightJason AgentSpeak(L++) #
7  * # Copyright (c) 2015-19, LightJason (info@lightjason.org) #
8  * # This program is free software: you can redistribute it and/or modify #
9  * # it under the terms of the GNU Lesser General Public License as #
10  * # published by the Free Software Foundation, either version 3 of the #
11  * # License, or (at your option) any later version. #
12  * # #
13  * # This program is distributed in the hope that it will be useful, #
14  * # but WITHOUT ANY WARRANTY; without even the implied warranty of #
15  * # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
16  * # GNU Lesser General Public License for more details. #
17  * # #
18  * # You should have received a copy of the GNU Lesser General Public License #
19  * # along with this program. If not, see http://www.gnu.org/licenses/ #
20  * ######################################################################################
21  * @endcond
22  */
23 
24 package org.lightjason.agentspeak.action.builtin;
25 
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;
64 
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;
71 
72 import static org.junit.Assert.assertTrue;
73 
77 @RunWith( DataProviderRunner.class )
78 public class TestCActionMathBitMatrix extends IBaseTest
79 {
84  private static final BitMatrix MATRIX1 = new BitMatrix( 2, 2 );
89  private static final BitMatrix MATRIX2 = new BitMatrix( 2, 2 );
90 
91 
95  @Before
96  public final void initialize()
97  {
98  MATRIX1.put( 0, 1, false );
99  MATRIX1.put( 1, 0, false );
100  MATRIX1.put( 1, 1, true );
101  MATRIX1.put( 0, 0, true );
102 
103  MATRIX2.put( 0, 1, true );
104  MATRIX2.put( 1, 0, true );
105  MATRIX2.put( 1, 1, true );
106  MATRIX2.put( 0, 0, false );
107  }
108 
113  @DataProvider
114  public static Object[] generator()
115  {
116  return testcase(
117 
118  Stream.of( MATRIX1, MATRIX2 ),
119 
120  Stream.of(
121  CColumns.class,
122  CFalseCount.class,
123  CDimension.class,
124  CCopy.class,
125  CTrueCount.class,
126  CSize.class,
127  CRows.class,
128  CNot.class,
129  COr.class,
130  CAnd.class,
131  CXor.class,
132  CNAnd.class,
133  CHammingDistance.class
134  ),
135  Stream.of( 2D, 2D ),
136  Stream.of( 2D, 1D ),
137  Stream.of( 2D, 2D, 2D, 2D ),
138  Stream.of( MATRIX1, MATRIX2 ),
139  Stream.of( 2D, 3D ),
140  Stream.of( 4, 4 ),
141  Stream.of( 2D, 2D ),
142  Stream.of(),
143  Stream.of(),
144  Stream.of(),
145  Stream.of(),
146  Stream.of(),
147  Stream.of( 3D )
148 
149  ).toArray();
150  }
151 
152 
161  @SafeVarargs
162  @SuppressWarnings( "varargs" )
163  private static Stream<Object> testcase( final Stream<Object> p_input, final Stream<Class<?>> p_classes, final Stream<Object>... p_classresult )
164  {
165  final List<ITerm> l_input = p_input.map( CRawTerm::from ).collect( Collectors.toList() );
166 
167  return StreamUtils.zip(
168  p_classes,
169  Arrays.stream( p_classresult ),
170  ( i, j ) -> new ImmutableTriple<>( l_input, i, j )
171  );
172  }
173 
174 
184  @Test
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
188  {
189  final List<ITerm> l_return = new ArrayList<>();
190 
191  p_input.getMiddle().getConstructor().newInstance().execute(
192  false,
194  p_input.getLeft(),
195  l_return
196  );
197 
198  Assert.assertArrayEquals(
199  p_input.getMiddle().toGenericString(),
200  l_return.stream().map( ITerm::raw ).toArray(),
201  p_input.getRight().toArray()
202  );
203  }
204 
208  @Test
209  public final void create()
210  {
211  final List<ITerm> l_return = new ArrayList<>();
212 
213  new CCreate().execute(
214  false, IContext.EMPTYPLAN,
215  Stream.of( 2, 2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
216  l_return
217  );
218 
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 );
224  }
225 
229  @Test
230  public final void tobitvector()
231  {
232  final List<ITerm> l_return = new ArrayList<>();
233 
234  new CToVector().execute(
235  false, IContext.EMPTYPLAN,
236  Stream.of( MATRIX2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
237  l_return
238  );
239 
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 );
243 
244  final BitVector l_bitvector = l_return.get( 0 ).raw();
245 
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 );
250  }
251 
255  @Test
256  public final void column()
257  {
258  final List<ITerm> l_return = new ArrayList<>();
259 
260  new CColumn().execute(
261  false, IContext.EMPTYPLAN,
262  Stream.of( 1, MATRIX2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
263  l_return
264  );
265 
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 );
269 
270  final BitVector l_bitvector = l_return.get( 0 ).raw();
271 
272  Assert.assertEquals( l_bitvector.get( 0 ), true );
273  Assert.assertEquals( l_bitvector.get( 1 ), true );
274  }
275 
279  @Test
280  public final void row()
281  {
282  final List<ITerm> l_return = new ArrayList<>();
283 
284  new CRow().execute(
285  false, IContext.EMPTYPLAN,
286  Stream.of( 1, MATRIX2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
287  l_return
288  );
289 
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 );
293 
294  final BitVector l_bitvector = l_return.get( 0 ).raw();
295 
296  Assert.assertEquals( l_bitvector.get( 0 ), true );
297  Assert.assertEquals( l_bitvector.get( 1 ), true );
298  }
299 
303  @Test
304  public final void numericvalue()
305  {
306  final List<ITerm> l_return = new ArrayList<>();
307 
308  new CNumericValue().execute(
309  false, IContext.EMPTYPLAN,
310  Stream.of( MATRIX1, 1, 0 ).map( CRawTerm::from ).collect( Collectors.toList() ),
311  l_return
312  );
313 
314  Assert.assertEquals( l_return.get( 0 ).<Number>raw(), 0D );
315  }
316 
320  @Test
321  public final void boolValue()
322  {
323  final List<ITerm> l_return = new ArrayList<>();
324 
325  new CBoolValue().execute(
326  false, IContext.EMPTYPLAN,
327  Stream.of( MATRIX2, 0, 0 ).map( CRawTerm::from ).collect( Collectors.toList() ),
328  l_return
329  );
330 
331  Assert.assertEquals( l_return.get( 0 ).<Boolean>raw(), false );
332  }
333 
337  @Test
338  public final void toblas()
339  {
340  final List<ITerm> l_return = new ArrayList<>();
341  final Double[][] l_result = {{0.0, 1.0}, {1.0, 1.0}};
342 
343  new CToBlas().execute(
344  false, IContext.EMPTYPLAN,
345  Stream.of( MATRIX2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
346  l_return
347  );
348 
349  Assert.assertEquals( l_return.size(), 1 );
350  Assert.assertTrue( l_return.get( 0 ).raw() instanceof DoubleMatrix2D );
351 
352  final DoubleMatrix2D l_blas = l_return.get( 0 ).raw();
353 
354  Assert.assertEquals( l_blas.size(), 4 );
355  Assert.assertArrayEquals( l_blas.toArray(), l_result );
356  }
357 
358 }
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
Definition: IRowColumn.java:70
returns the size (rows * columns) of the matrix.
base test class with helpers
Definition: IBaseTest.java:33
IContext EMPTYPLAN
empty context with plan
Definition: IContext.java:47
combines all arguments to a single result with the and-operator.
Definition: bool/CAnd.java:48
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
execution context with local data
Definition: IContext.java:42
external action interface
Definition: IAction.java:38
performs the logical xor operation to all bit matrices.
combines all arguments to a single result with the xor-operator.
Definition: bool/CXor.java:48
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
Definition: CToVector.java:67
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
performs the logical not operation to all bit matrices.
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
performs the logical or operation to all bit matrices.
performs the logical and operation to all bit matrices.
combines all arguments to a single result with the or-operator.
Definition: bool/COr.java:48
performs the logical not-and operation to all bit matrices.
term structure for simple datatypes
Definition: CRawTerm.java:45
returns the dimension (rows / columns) of a bit matrix.