24 package org.lightjason.agentspeak.action.builtin;
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;
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;
57 import java.util.logging.LogManager;
58 import java.util.stream.Collectors;
59 import java.util.stream.IntStream;
60 import java.util.stream.Stream;
75 LogManager.getLogManager().reset();
87 new CGenerator(
new ByteArrayInputStream(
"".getBytes( StandardCharsets.UTF_8 ) ), Collections.emptySet() ).generatesingle(),
89 Collections.emptyList()
99 Assume.assumeNotNull( m_context );
103 Stream.of(
"testnumber", 123,
"teststring",
"foobar" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
104 Collections.emptyList()
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" );
120 Stream.of(
"bar", 123 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
121 Collections.emptyList()
124 Assert.assertEquals( m_context.
agent().
storage().size(), 0 );
125 Assert.assertNull( m_context.
agent().
storage().get(
"bar" ) );
135 new CAdd( Stream.of(
"abc" ) ).execute(
137 Stream.of(
"abc", 123 ).map(
CRawTerm::from ).collect( Collectors.toList() ),
138 Collections.emptyList()
141 Assert.assertEquals( m_context.
agent().
storage().size(), 0 );
142 Assert.assertNull( m_context.
agent().
storage().get(
"abc" ) );
152 Assume.assumeNotNull( m_context );
156 final List<ITerm> l_return =
new ArrayList<>();
159 Stream.of(
"xxx" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
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 ) );
176 Assume.assumeNotNull( m_context );
178 IntStream.range( 0, 100 )
179 .mapToObj( i -> RandomStringUtils.random( 25 ) )
180 .forEach( i -> m_context.
agent().
storage().put( i, RandomStringUtils.random( 5 ) ) );
182 Assert.assertEquals( m_context.
agent().
storage().size(), 100 );
186 Collections.emptyList(),
187 Collections.emptyList()
190 Assert.assertTrue( m_context.
agent().
storage().isEmpty() );
200 Assume.assumeNotNull( m_context );
205 final List<ITerm> l_return =
new ArrayList<>();
208 Stream.of(
"foo",
"bar" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
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 ) );
225 Assume.assumeNotNull( m_context );
230 final List<ITerm> l_return =
new ArrayList<>();
231 new CRemove( Stream.of(
"xx" ) ).execute(
233 Stream.of(
"xx",
"yy" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
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 ) );
250 Assume.assumeNotNull( m_context );
252 IntStream.range( 0, 100 )
253 .forEach( i -> m_context.
agent().
storage().put( MessageFormat.format(
"value {0}", i ), i ) );
255 Assert.assertEquals( m_context.
agent().
storage().size(), 100 );
259 Collections.emptyList(),
260 Collections.emptyList()
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() );
275 Assume.assumeNotNull( m_context );
277 IntStream.range( 0, 100 )
278 .forEach( i -> m_context.
agent().
storage().put( MessageFormat.format(
"value {0}", i ), i ) );
280 Assert.assertEquals( m_context.
agent().
storage().size(), 100 );
282 new CClear( Stream.of(
"value 7",
"value 23",
"value 91" ) ).execute(
284 Collections.emptyList(),
285 Collections.emptyList()
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() );
300 Assume.assumeNotNull( m_context );
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 ) ) )
306 .collect( Collectors.toList() );
308 final List<ITerm> l_return =
new ArrayList<>();
315 Assert.assertEquals( l_return.size(), 100 );
316 Assert.assertTrue( l_return.stream().allMatch(
ITerm::<Boolean>raw ) );
326 Assume.assumeNotNull( m_context );
331 final List<ITerm> l_return =
new ArrayList<>();
334 Stream.of(
"value 9",
"value 7",
"value 23",
"value 77",
"57",
"123" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
338 Assert.assertArrayEquals(
339 l_return.stream().map(
ITerm::raw ).toArray(),
340 Stream.of(
false,
true,
false,
false,
false,
false ).toArray()
351 Assume.assumeNotNull( m_context );
357 final List<ITerm> l_return =
new ArrayList<>();
358 new CExists( Stream.of(
"value 33",
"value 88",
"23" ) ).execute(
360 Stream.of(
"value 33",
"value 177",
"value 23",
"value 137" ).map(
CRawTerm::from ).collect( Collectors.toList() ),
364 Assert.assertArrayEquals(
365 l_return.stream().map(
ITerm::raw ).toArray(),
366 Stream.of(
false,
true,
true,
false ).toArray()
378 Assert.assertArrayEquals(
381 new CAdd().minimalArgumentNumber(),
382 new CClear().minimalArgumentNumber(),
383 new CExists().minimalArgumentNumber(),
384 new CRemove().minimalArgumentNumber()
387 Stream.of( 1, 0, 1, 1 ).toArray()
400 final Set<String> l_keys = Stream.of(
"a",
"x",
"y" ).collect( Collectors.toSet() );
402 Assert.assertArrayEquals(
403 new CAdd( l_keys::contains ).forbiddenkeys(
"x",
"z" ).toArray(),
404 Stream.of(
true,
false ).toArray()
407 Assert.assertArrayEquals(
408 new CRemove( l_keys::contains ).forbiddenkeys(
"x",
"z" ).toArray(),
409 Stream.of(
true,
false ).toArray()
412 Assert.assertArrayEquals(
413 new CClear( l_keys::contains ).forbiddenkeys(
"x",
"z" ).toArray(),
414 Stream.of(
true,
false ).toArray()
417 Assert.assertArrayEquals(
418 new CExists( l_keys::contains ).forbiddenkeys(
"x",
"z" ).toArray(),
419 Stream.of(
true,
false ).toArray()
441 super( p_configuration );
457 CGenerator(
final InputStream p_stream,
final Set<IAction> p_actions )
throws Exception
459 super( p_stream, p_actions );
465 return new CAgent( m_configuration );
final void existswithoutkeys()
test exists action without keys
final void removewithkeystream()
test remove action without key stream
base test class with helpers
final void removewithkeys()
test remove action without keys
final void clearwithoutkeys()
test clear action without keys
final void clearwithkeys()
test clear action with keys
removes all elements of the storage.
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^
IContext m_context
execution context
final void existswithkeys()
test exists action with keys
final void arguments()
test for checking minimal arguments
final CAgent generatesingle(final Object... p_data)
execution context with local data
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 resolver()
test resolver access
external action interface
check if an element exists within the agent-storage.
CAgent(final IAgentConfiguration< CAgent > p_configuration)
ctor
clears all elements of the collection.
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 ...
interface to define the agent configuration
< 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
static final long serialVersionUID
serial id
static ILiteral from( @Nonnull final String p_functor, @Nullable final ITerm... p_values)
factory
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 initialize()
initialize
removes an element of the list by the index.
action to add elements to a list.
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
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