24 package org.lightjason.agentspeak.agent;
26 import com.google.common.collect.HashMultimap;
27 import com.google.common.collect.Multimap;
28 import com.google.common.collect.Multimaps;
29 import org.junit.Assume;
30 import org.junit.Before;
31 import org.junit.Test;
44 import javax.annotation.Nonnegative;
45 import javax.annotation.Nonnull;
46 import java.io.FileInputStream;
47 import java.io.InputStream;
48 import java.text.MessageFormat;
49 import java.util.Collections;
50 import java.util.List;
51 import java.util.concurrent.atomic.AtomicBoolean;
52 import java.util.concurrent.atomic.AtomicLong;
53 import java.util.logging.LogManager;
54 import java.util.stream.Collectors;
55 import java.util.stream.LongStream;
56 import java.util.stream.Stream;
58 import static org.junit.Assert.assertTrue;
69 private static final String
ASL =
"src/test/resources/agent/execution.asl";
81 private final Multimap<Long, String>
m_log = Multimaps.synchronizedMultimap( HashMultimap.create() );
85 private final Multimap<Long, String>
m_result = HashMultimap.create();
91 LogManager.getLogManager().reset();
101 m_running =
new AtomicBoolean(
true );
104 final InputStream l_asl =
new FileInputStream( ASL )
109 catch (
final Exception l_exception )
111 l_exception.printStackTrace();
112 assertTrue(
"asl could not be read: {0}",
true );
117 m_result.put( 0L,
"main" );
118 m_result.put( 1L,
"single run" );
119 m_result.put( 1L,
"first" );
120 m_result.put( 1L,
"second" );
121 m_result.put( 1L,
"twovalues equal type" );
122 m_result.put( 1L,
"twovalues different type" );
123 m_result.put( 1L,
"twovalues with literal" );
124 m_result.put( 2L,
"single" );
135 Assume.assumeNotNull( m_agent );
136 Assume.assumeNotNull( m_running );
139 while ( m_running.get() )
145 MessageFormat.format(
"number of cycles are incorrect, excpected [{0}] contains [{1}]", m_result.asMap().size(), m_log.asMap().size() ),
146 LongStream.range( 0, m_result.asMap().size() ).allMatch( m_log::containsKey )
150 MessageFormat.format(
"number of log elements during execution are incorrect, expected {0} result {1}", m_result.asMap(), m_log.asMap() ),
151 LongStream.range( 0, m_result.asMap().size() )
152 .allMatch( i -> m_result.get( i ).size() == m_log.asMap().getOrDefault( i, Collections.emptyList() ).size() )
155 LongStream.range( 0, m_result.asMap().size() ).forEach( i -> assertTrue(
156 MessageFormat.format(
"expected result {0} for index {2} is not equal to log {1}", m_result.get( i ), m_log.get( i ), i ),
157 m_result.get( i ).stream().allMatch( j -> m_log.get( i ).contains( j ) )
184 ).collect( Collectors.toSet() )
204 private static final long serialVersionUID = -7467073439000881088L;
208 private final AtomicLong m_cycle =
new AtomicLong();
217 super( p_configuration );
224 m_cycle.incrementAndGet();
235 return m_cycle.get();
248 private static final long serialVersionUID = 5466369414656444520L;
267 @Nonnull
final List<ITerm> p_argument, @Nonnull
final List<ITerm> p_return )
269 m_running.set(
false );
282 private static final long serialVersionUID = 4536335097194230205L;
301 @Nonnull
final List<ITerm> p_argument, @Nonnull
final List<ITerm> p_return )
303 m_log.put( p_context.agent().<
CAgent>raw().
cycle(), p_argument.get( 0 ).<String>raw() );
final long cycle()
returns the cycle
default implementation of an action
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
CGenerator(final InputStream p_stream)
ctor
final Multimap< Long, String > m_log
logs for plan execution
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
final void executionorder()
execution ordering test
static IPath from( @Nonnull final String p_string)
factor method to build path
class to create a path structure
test for agent execution ordering
AtomicBoolean m_running
running flag (agent can disable execution)
execution context with local data
IAgent<?> m_agent
agent reference
final int minimalArgumentNumber()
minimum number of arguments
static Stream< IAction > actionsFromPackage( @Nullable final String... p_package)
get all classes within an Java package as action
final Multimap< Long, String > m_result
log results
void initialize()
initializing
result for an immutable fuzzy value
final IPath name()
returns the name with path of the action
final IPath name()
returns the name with path of the action
interface to define the agent configuration
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
static final String ASL
asl source
class for any helper calls
final CAgent generatesingle(final Object... p_data)
final int minimalArgumentNumber()
minimum number of arguments
CAgent(final IAgentConfiguration< CAgent > p_configuration)
ctor
final IAgentConfiguration< T > m_configuration
configuration of an agent