LightJason - AgentSpeak(L++)
TestCActionMathBlasVector.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.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;
53 
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;
61 
62 
66 @RunWith( DataProviderRunner.class )
67 public final class TestCActionMathBlasVector extends IBaseTest
68 {
69 
74  private static final DoubleMatrix1D VECTOR1 = new DenseDoubleMatrix1D( new double[]{2, 5, 3, 8} );
75 
80  private static final DoubleMatrix1D VECTOR2 = new DenseDoubleMatrix1D( new double[]{8, 6, 2, 1} );
81 
82 
87  @DataProvider
88  public static Object[] generator()
89  {
90  return testcase(
91 
92  Stream.of( VECTOR1, VECTOR2 ),
93 
94  Stream.of(
95  CNonZero.class,
96  CSum.class,
97  CDotProduct.class
98  ),
99  Stream.of( 4D, 4D ),
100  Stream.of( VECTOR1.zSum(), VECTOR2.zSum() ),
101  Stream.of( 60.0 )
102 
103  ).toArray();
104  }
105 
106 
115  @SafeVarargs
116  @SuppressWarnings( "varargs" )
117  private static Stream<Object> testcase( final Stream<Object> p_input, final Stream<Class<?>> p_classes, final Stream<Object>... p_classresult )
118  {
119  final List<ITerm> l_input = p_input.map( CRawTerm::from ).collect( Collectors.toList() );
120 
121  return StreamUtils.zip(
122  p_classes,
123  Arrays.stream( p_classresult ),
124  ( i, j ) -> new ImmutableTriple<>( l_input, i, j )
125  );
126  }
127 
128 
138  @Test
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
142  {
143  final List<ITerm> l_return = new ArrayList<>();
144 
145  p_input.getMiddle().getConstructor().newInstance().execute(
146  false, IContext.EMPTYPLAN,
147  p_input.getLeft(),
148  l_return
149  );
150 
151  Assert.assertArrayEquals(
152  p_input.getMiddle().toGenericString(),
153  l_return.stream().map( ITerm::raw ).toArray(),
154  p_input.getRight().toArray()
155  );
156  }
157 
161  @Test
162  public final void create()
163  {
164  final List<ITerm> l_return = new ArrayList<>();
165 
166  new CCreate().execute(
167  false, IContext.EMPTYPLAN,
168  Stream.of( 2, "dense" ).map( CRawTerm::from ).collect( Collectors.toList() ),
169  l_return
170  );
171 
172  new CCreate().execute(
173  false, IContext.EMPTYPLAN,
174  Stream.of( 4, "sparse" ).map( CRawTerm::from ).collect( Collectors.toList() ),
175  l_return
176  );
177 
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 );
181  }
182 
186  @Test
187  public final void set()
188  {
189  final DoubleMatrix1D l_vector = new DenseDoubleMatrix1D( 4 );
190 
191  new CSet().execute(
192  false, IContext.EMPTYPLAN,
193  Stream.of( 0, 6.0, l_vector ).map( CRawTerm::from ).collect( Collectors.toList() ),
194  Collections.emptyList()
195  );
196 
197  Assert.assertEquals( l_vector.get( 0 ), 6, 0 );
198  }
199 
203  @Test
204  public final void tolist()
205  {
206  final List<ITerm> l_return = new ArrayList<>();
207 
208  new CToList().execute(
209  false, IContext.EMPTYPLAN,
210  Stream.of( VECTOR1 ).map( CRawTerm::from ).collect( Collectors.toList() ),
211  l_return
212  );
213 
214  Assert.assertEquals( l_return.size(), 1 );
215  Assert.assertTrue( l_return.get( 0 ).raw() instanceof List );
216 
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() );
219  }
220 
224  @Test
225  public final void assignscalar()
226  {
227  final DoubleMatrix1D l_vector = new DenseDoubleMatrix1D( 4 );
228 
229  new CAssign().execute(
230  false, IContext.EMPTYPLAN,
231  Stream.of( 2, l_vector ).map( CRawTerm::from ).collect( Collectors.toList() ),
232  Collections.emptyList()
233  );
234 
235  Assert.assertArrayEquals( l_vector.toArray(), Stream.of( 2, 2, 2, 2 ).mapToDouble( i -> i ).toArray(), 0 );
236  }
237 
241  @Test
242  public final void assignvector()
243  {
244  final DoubleMatrix1D l_vector = new DenseDoubleMatrix1D( 4 );
245 
246  new CAssign().execute(
247  false, IContext.EMPTYPLAN,
248  Stream.of( VECTOR2, l_vector ).map( CRawTerm::from ).collect( Collectors.toList() ),
249  Collections.emptyList()
250  );
251 
252  Assert.assertArrayEquals( l_vector.toArray(), VECTOR2.toArray(), 0 );
253  }
254 
255 
259  @Test
260  public final void get()
261  {
262  final List<ITerm> l_return = new ArrayList<>();
263 
264  new CGet().execute(
265  false, IContext.EMPTYPLAN,
266  Stream.of( VECTOR1, 0 ).map( CRawTerm::from ).collect( Collectors.toList() ),
267  l_return
268  );
269 
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 );
273  }
274 
278  @Test
279  public final void copy()
280  {
281  final List<ITerm> l_return = new ArrayList<>();
282 
283  new CCopy().execute(
284  false, IContext.EMPTYPLAN,
285  Stream.of( VECTOR1, VECTOR2 ).map( CRawTerm::from ).collect( Collectors.toList() ),
286  l_return
287  );
288 
289  Assert.assertEquals( l_return.size(), 2 );
290  Assert.assertArrayEquals( l_return.stream().map( ITerm::raw ).toArray(), Stream.of( VECTOR1, VECTOR2 ).toArray() );
291  }
292 
296  @Test
297  public final void parse()
298  {
299  final List<ITerm> l_return = new ArrayList<>();
300 
301  new CParse().execute(
302  false, IContext.EMPTYPLAN,
303  Stream.of( "1,2,3", "dense" ).map( CRawTerm::from ).collect( Collectors.toList() ),
304  l_return
305  );
306 
307  new CParse().execute(
308  false, IContext.EMPTYPLAN,
309  Stream.of( "4,3,4", "sparse" ).map( CRawTerm::from ).collect( Collectors.toList() ),
310  l_return
311  );
312 
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 );
316  }
317 
321  @Test
322  public final void fromlist()
323  {
324  final List<ITerm> l_return = new ArrayList<>();
325 
326  new CFromList().execute(
327  false, IContext.EMPTYPLAN,
328  Stream.of( Stream.of( 1, 2, 3 ).collect( Collectors.toList() ), "dense" ).map( CRawTerm::from ).collect( Collectors.toList() ),
329  l_return
330  );
331 
332  new CFromList().execute(
333  false, IContext.EMPTYPLAN,
334  Stream.of( Stream.of( 4, 3, 4 ).collect( Collectors.toList() ), "sparse" ).map( CRawTerm::from ).collect( Collectors.toList() ),
335  l_return
336  );
337 
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 );
341  }
342 
343 }
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
Definition: IBaseTest.java:33
creates a dense- or sparse-matrix from a string.
IContext EMPTYPLAN
empty context with plan
Definition: IContext.java:47
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: CFromList.java:76
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
execution context with local data
Definition: IContext.java:42
external action interface
Definition: IAction.java:38
creates a dense- or sparse-vector from as string.
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
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
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
creates a dense- or sparse-vector from a list.
Definition: CFromList.java:52
term structure for simple datatypes
Definition: CRawTerm.java:45