LightJason - AgentSpeak(L++)
TestCActionStorage.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 org.apache.commons.lang3.RandomStringUtils;
27 import org.junit.Assert;
28 import org.junit.Assume;
29 import org.junit.Before;
30 import org.junit.Test;
48 
49 import java.io.ByteArrayInputStream;
50 import java.io.InputStream;
51 import java.nio.charset.StandardCharsets;
52 import java.text.MessageFormat;
53 import java.util.ArrayList;
54 import java.util.Collections;
55 import java.util.List;
56 import java.util.Set;
57 import java.util.logging.LogManager;
58 import java.util.stream.Collectors;
59 import java.util.stream.IntStream;
60 import java.util.stream.Stream;
61 
62 
66 public final class TestCActionStorage extends IBaseTest
67 {
72 
73  static
74  {
75  LogManager.getLogManager().reset();
76  }
77 
83  @Before
84  public final void initialize() throws Exception
85  {
86  m_context = new CContext(
87  new CGenerator( new ByteArrayInputStream( "".getBytes( StandardCharsets.UTF_8 ) ), Collections.emptySet() ).generatesingle(),
88  new CPlan( CTrigger.from( ITrigger.EType.ADDGOAL, CLiteral.from( "nothing" ) ), Collections.emptyList(), Collections.emptySet() ),
89  Collections.emptyList()
90  );
91  }
92 
96  @Test
97  public final void addwithoutkeys()
98  {
99  Assume.assumeNotNull( m_context );
100 
101  new CAdd().execute(
102  false, m_context,
103  Stream.of( "testnumber", 123, "teststring", "foobar" ).map( CRawTerm::from ).collect( Collectors.toList() ),
104  Collections.emptyList()
105  );
106 
107  Assert.assertEquals( m_context.agent().storage().size(), 2 );
108  Assert.assertEquals( m_context.agent().storage().get( "testnumber" ), 123 );
109  Assert.assertEquals( m_context.agent().storage().get( "teststring" ), "foobar" );
110  }
111 
115  @Test
116  public final void addwithkeys()
117  {
118  new CAdd( "bar" ).execute(
119  false, m_context,
120  Stream.of( "bar", 123 ).map( CRawTerm::from ).collect( Collectors.toList() ),
121  Collections.emptyList()
122  );
123 
124  Assert.assertEquals( m_context.agent().storage().size(), 0 );
125  Assert.assertNull( m_context.agent().storage().get( "bar" ) );
126  }
127 
128 
132  @Test
133  public final void addwithkeystrean()
134  {
135  new CAdd( Stream.of( "abc" ) ).execute(
136  false, m_context,
137  Stream.of( "abc", 123 ).map( CRawTerm::from ).collect( Collectors.toList() ),
138  Collections.emptyList()
139  );
140 
141  Assert.assertEquals( m_context.agent().storage().size(), 0 );
142  Assert.assertNull( m_context.agent().storage().get( "abc" ) );
143  }
144 
145 
149  @Test
150  public final void removewithoutkeys()
151  {
152  Assume.assumeNotNull( m_context );
153 
154  m_context.agent().storage().put( "xxx", 123 );
155 
156  final List<ITerm> l_return = new ArrayList<>();
157  new CRemove().execute(
158  false, m_context,
159  Stream.of( "xxx" ).map( CRawTerm::from ).collect( Collectors.toList() ),
160  l_return
161  );
162 
163  Assert.assertTrue( m_context.agent().storage().isEmpty() );
164  Assert.assertNull( m_context.agent().storage().get( "xxx" ) );
165  Assert.assertEquals( l_return.size(), 1 );
166  Assert.assertEquals( l_return.get( 0 ).<Integer>raw(), Integer.valueOf( 123 ) );
167  }
168 
169 
173  @Test
174  public final void clearwithoutkeys()
175  {
176  Assume.assumeNotNull( m_context );
177 
178  IntStream.range( 0, 100 )
179  .mapToObj( i -> RandomStringUtils.random( 25 ) )
180  .forEach( i -> m_context.agent().storage().put( i, RandomStringUtils.random( 5 ) ) );
181 
182  Assert.assertEquals( m_context.agent().storage().size(), 100 );
183 
184  new CClear().execute(
185  false, m_context,
186  Collections.emptyList(),
187  Collections.emptyList()
188  );
189 
190  Assert.assertTrue( m_context.agent().storage().isEmpty() );
191  }
192 
193 
197  @Test
198  public final void removewithkeys()
199  {
200  Assume.assumeNotNull( m_context );
201 
202  m_context.agent().storage().put( "foo", 123 );
203  m_context.agent().storage().put( "bar", 456 );
204 
205  final List<ITerm> l_return = new ArrayList<>();
206  new CRemove( "foo" ).execute(
207  false, m_context,
208  Stream.of( "foo", "bar" ).map( CRawTerm::from ).collect( Collectors.toList() ),
209  l_return
210  );
211 
212  Assert.assertEquals( m_context.agent().storage().size(), 1 );
213  Assert.assertNotNull( m_context.agent().storage().get( "foo" ) );
214  Assert.assertEquals( l_return.size(), 1 );
215  Assert.assertEquals( l_return.get( 0 ).<Integer>raw(), Integer.valueOf( 456 ) );
216  }
217 
218 
222  @Test
223  public final void removewithkeystream()
224  {
225  Assume.assumeNotNull( m_context );
226 
227  m_context.agent().storage().put( "xx", 189 );
228  m_context.agent().storage().put( "yy", 267 );
229 
230  final List<ITerm> l_return = new ArrayList<>();
231  new CRemove( Stream.of( "xx" ) ).execute(
232  false, m_context,
233  Stream.of( "xx", "yy" ).map( CRawTerm::from ).collect( Collectors.toList() ),
234  l_return
235  );
236 
237  Assert.assertEquals( m_context.agent().storage().size(), 1 );
238  Assert.assertNotNull( m_context.agent().storage().get( "xx" ) );
239  Assert.assertEquals( l_return.size(), 1 );
240  Assert.assertEquals( l_return.get( 0 ).<Integer>raw(), Integer.valueOf( 267 ) );
241  }
242 
243 
247  @Test
248  public final void clearwithkeys()
249  {
250  Assume.assumeNotNull( m_context );
251 
252  IntStream.range( 0, 100 )
253  .forEach( i -> m_context.agent().storage().put( MessageFormat.format( "value {0}", i ), i ) );
254 
255  Assert.assertEquals( m_context.agent().storage().size(), 100 );
256 
257  new CClear( "value 1", "value 5", "value 73" ).execute(
258  false, m_context,
259  Collections.emptyList(),
260  Collections.emptyList()
261  );
262 
263  Assert.assertEquals( m_context.agent().storage().size(), 3 );
264  Assert.assertArrayEquals( m_context.agent().storage().keySet().toArray(), Stream.of( "value 73", "value 5", "value 1" ).toArray() );
265  Assert.assertArrayEquals( m_context.agent().storage().values().toArray(), Stream.of( 73, 5, 1 ).toArray() );
266  }
267 
268 
272  @Test
273  public final void clearwithkeystream()
274  {
275  Assume.assumeNotNull( m_context );
276 
277  IntStream.range( 0, 100 )
278  .forEach( i -> m_context.agent().storage().put( MessageFormat.format( "value {0}", i ), i ) );
279 
280  Assert.assertEquals( m_context.agent().storage().size(), 100 );
281 
282  new CClear( Stream.of( "value 7", "value 23", "value 91" ) ).execute(
283  false, m_context,
284  Collections.emptyList(),
285  Collections.emptyList()
286  );
287 
288  Assert.assertEquals( m_context.agent().storage().size(), 3 );
289  Assert.assertArrayEquals( m_context.agent().storage().keySet().toArray(), Stream.of( "value 7", "value 23", "value 91" ).toArray() );
290  Assert.assertArrayEquals( m_context.agent().storage().values().toArray(), Stream.of( 7, 23, 91 ).toArray() );
291  }
292 
293 
297  @Test
298  public final void existswithoutkeys()
299  {
300  Assume.assumeNotNull( m_context );
301 
302  final List<ITerm> l_content = IntStream.range( 0, 100 )
303  .mapToObj( i -> RandomStringUtils.random( 25 ) )
304  .peek( i -> m_context.agent().storage().put( i, RandomStringUtils.random( 5 ) ) )
305  .map( CRawTerm::from )
306  .collect( Collectors.toList() );
307 
308  final List<ITerm> l_return = new ArrayList<>();
309  new CExists().execute(
310  false, m_context,
311  l_content,
312  l_return
313  );
314 
315  Assert.assertEquals( l_return.size(), 100 );
316  Assert.assertTrue( l_return.stream().allMatch( ITerm::<Boolean>raw ) );
317  }
318 
319 
323  @Test
324  public final void existswithkeys()
325  {
326  Assume.assumeNotNull( m_context );
327 
328  m_context.agent().storage().put( "value 9", 5 );
329  m_context.agent().storage().put( "value 7", 5 );
330 
331  final List<ITerm> l_return = new ArrayList<>();
332  new CExists( "value 9", "value 77", "57" ).execute(
333  false, m_context,
334  Stream.of( "value 9", "value 7", "value 23", "value 77", "57", "123" ).map( CRawTerm::from ).collect( Collectors.toList() ),
335  l_return
336  );
337 
338  Assert.assertArrayEquals(
339  l_return.stream().map( ITerm::raw ).toArray(),
340  Stream.of( false, true, false, false, false, false ).toArray()
341  );
342  }
343 
344 
348  @Test
349  public final void existswithkeystream()
350  {
351  Assume.assumeNotNull( m_context );
352 
353  m_context.agent().storage().put( "value 33", 5 );
354  m_context.agent().storage().put( "value 177", 5 );
355  m_context.agent().storage().put( "value 23", 19 );
356 
357  final List<ITerm> l_return = new ArrayList<>();
358  new CExists( Stream.of( "value 33", "value 88", "23" ) ).execute(
359  false, m_context,
360  Stream.of( "value 33", "value 177", "value 23", "value 137" ).map( CRawTerm::from ).collect( Collectors.toList() ),
361  l_return
362  );
363 
364  Assert.assertArrayEquals(
365  l_return.stream().map( ITerm::raw ).toArray(),
366  Stream.of( false, true, true, false ).toArray()
367  );
368  }
369 
370 
371 
375  @Test
376  public final void arguments()
377  {
378  Assert.assertArrayEquals(
379 
380  Stream.of(
381  new CAdd().minimalArgumentNumber(),
382  new CClear().minimalArgumentNumber(),
383  new CExists().minimalArgumentNumber(),
384  new CRemove().minimalArgumentNumber()
385  ).toArray(),
386 
387  Stream.of( 1, 0, 1, 1 ).toArray()
388 
389  );
390  }
391 
392 
393 
397  @Test
398  public final void resolver()
399  {
400  final Set<String> l_keys = Stream.of( "a", "x", "y" ).collect( Collectors.toSet() );
401 
402  Assert.assertArrayEquals(
403  new CAdd( l_keys::contains ).forbiddenkeys( "x", "z" ).toArray(),
404  Stream.of( true, false ).toArray()
405  );
406 
407  Assert.assertArrayEquals(
408  new CRemove( l_keys::contains ).forbiddenkeys( "x", "z" ).toArray(),
409  Stream.of( true, false ).toArray()
410  );
411 
412  Assert.assertArrayEquals(
413  new CClear( l_keys::contains ).forbiddenkeys( "x", "z" ).toArray(),
414  Stream.of( true, false ).toArray()
415  );
416 
417  Assert.assertArrayEquals(
418  new CExists( l_keys::contains ).forbiddenkeys( "x", "z" ).toArray(),
419  Stream.of( true, false ).toArray()
420  );
421  }
422 
423 
427  private static final class CAgent extends IBaseAgent<CAgent>
428  {
432  private static final long serialVersionUID = 5077720620041316609L;
433 
439  CAgent( final IAgentConfiguration<CAgent> p_configuration )
440  {
441  super( p_configuration );
442  }
443  }
444 
448  private static final class CGenerator extends IBaseAgentGenerator<CAgent>
449  {
457  CGenerator( final InputStream p_stream, final Set<IAction> p_actions ) throws Exception
458  {
459  super( p_stream, p_actions );
460  }
461 
462  @Override
463  public final CAgent generatesingle( final Object... p_data )
464  {
465  return new CAgent( m_configuration );
466  }
467  }
468 
469 }
final void existswithoutkeys()
test exists action without keys
final void removewithkeystream()
test remove action without key stream
base test class with helpers
Definition: IBaseTest.java:33
final void removewithkeys()
test remove action without keys
final void clearwithoutkeys()
test clear action without keys
removes an element by name from the storage.
static ITrigger from( @Nonnull final EType p_event, @Nonnull final ILiteral p_literal)
creates a trigger event^
Definition: CTrigger.java:87
final void existswithkeys()
test exists action with keys
final void arguments()
test for checking minimal arguments
execution context with local data
Definition: IContext.java:42
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
external action interface
Definition: IAction.java:38
check if an element exists within the agent-storage.
Definition: CExists.java:48
CAgent(final IAgentConfiguration< CAgent > p_configuration)
ctor
final void clearwithkeystream()
test clear action with key stream
default generic literal class for agent beliefs a literal consists of a functor, an optional list of ...
Definition: CLiteral.java:64
< T > T raw()
cast to any raw value type
Map< String, Object > storage()
storage access
final void existswithkeystream()
test exists action with key stream
CGenerator(final InputStream p_stream, final Set< IAction > p_actions)
ctor
final void addwithkeystrean()
test add action with forbidden key strean
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
static ILiteral from( @Nonnull final String p_functor, @Nullable final ITerm... p_values)
factory
Definition: CLiteral.java:161
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: CExists.java:102
adds or overwrites an element in the agent-storage.
final void addwithoutkeys()
test add action without forbidden keys
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
term structure for simple datatypes
Definition: CRawTerm.java:45
final void removewithoutkeys()
test remove action without keys
IAgent<?> agent()
returns the agent of the context
final void addwithkeys()
test add action with forbidden keys