24 package org.lightjason.agentspeak.agent;
26 import org.junit.Before;
27 import org.junit.Test;
46 import javax.annotation.Nonnegative;
47 import javax.annotation.Nonnull;
48 import java.io.FileInputStream;
49 import java.io.IOException;
50 import java.io.InputStream;
51 import java.text.MessageFormat;
52 import java.util.Collections;
53 import java.util.List;
55 import java.util.Objects;
56 import java.util.Stack;
57 import java.util.concurrent.ConcurrentHashMap;
58 import java.util.concurrent.atomic.AtomicBoolean;
59 import java.util.logging.LogManager;
60 import java.util.stream.Collectors;
61 import java.util.stream.IntStream;
62 import java.util.stream.Stream;
64 import static org.junit.Assert.assertTrue;
98 LogManager.getLogManager().reset();
112 "src/test/resources/agent/hanoi.asl", Stream.of() );
122 public void play() throws InterruptedException
137 private void setup(
final int p_agentnumber,
final long p_towernumber,
final long p_slicenumber,
138 final String p_asl,
final Stream<IAction> p_action )
throws Exception
140 m_towernumber = p_towernumber;
141 m_slicenumber = p_slicenumber;
142 m_running =
new AtomicBoolean(
true );
144 final Map<Integer, CTower> l_towermap =
new ConcurrentHashMap<>();
145 IntStream.range( 0, m_towernumber.intValue() )
149 l_towermap.put( i, l_tower );
151 IntStream.range( 0, m_slicenumber.intValue() ).forEach( j -> l_tower.
push(
new CSlice( m_slicenumber.intValue() - j ) ) );
153 m_tower = Collections.unmodifiableMap( l_towermap );
156 final Map<Integer, CAgent> l_agentmap =
new ConcurrentHashMap<>();
159 final InputStream l_asl =
new FileInputStream( p_asl )
163 IntStream.range( 0, p_agentnumber )
164 .forEach( i -> l_agentmap.put( i, l_generator.
generatesingle( i ) ) );
166 catch (
final IOException l_exception )
168 l_exception.printStackTrace();
169 assertTrue(
"asl could not be read",
true );
171 m_agents = Collections.unmodifiableMap( l_agentmap );
181 while ( m_running.get() )
184 System.out.println( MessageFormat.format(
"\ntower configuration: {0}", m_tower ) );
193 catch (
final Exception l_exception )
195 l_exception.printStackTrace();
224 super( p_configuration );
256 CGenerator(
final InputStream p_stream,
final Stream<IAction> p_action )
throws Exception
274 ).collect( Collectors.toSet() ),
275 Collections.emptySet(),
294 @SuppressWarnings(
"unchecked" )
295 public final
CAgent generatesingle( final Object... p_data )
297 return new CAgent( m_configuration, (
int) p_data[0] );
328 @Nonnull
final List<ITerm> p_argument, @Nonnull
final List<ITerm> p_return )
361 @Nonnull
final List<ITerm> p_argument, @Nonnull
final List<ITerm> p_return
397 @Nonnull
final List<ITerm> p_argument, @Nonnull
final List<ITerm> p_return )
399 final CTower l_tower =
m_tower.get( p_argument.get( 0 ).<Number>
raw().intValue() );
400 if ( Objects.isNull( l_tower ) )
430 m_failprobability = p_failprobability;
450 @Nonnull
final List<ITerm> p_argument, @Nonnull
final List<ITerm> p_return )
452 final CTower l_tower =
m_tower.get( p_argument.get( 0 ).<Number>
raw().intValue() );
453 if ( ( Objects.isNull( l_tower ) ) || ( Math.random() < m_failprobability ) )
461 catch (
final IllegalStateException l_exception )
495 @Nonnull
final List<ITerm> p_argument, @Nonnull
final List<ITerm> p_return )
497 final CTower l_tower =
m_tower.get( p_argument.get( 0 ).<Number>
raw().intValue() );
498 if ( Objects.isNull( l_tower ) )
506 catch (
final IllegalStateException l_exception )
537 return MessageFormat.format(
"slice {0}", m_size );
554 private static final class CTower extends Stack<CSlice>
564 if ( ( this.size() > 0 ) && ( this.peek().size() < p_item.
size() ) )
565 throw new IllegalStateException();
567 return super.push( p_item );
573 if ( this.isEmpty() )
574 throw new IllegalStateException();
586 public final synchronized boolean empty()
588 return super.empty();
592 public final synchronized int search(
final Object p_object )
594 return super.search( p_object );
void setup(final int p_agentnumber, final long p_towernumber, final long p_slicenumber, final String p_asl, final Stream< IAction > p_action)
initialize call
final synchronized CSlice pop()
final synchronized boolean empty()
final IPath name()
returns the name with path of the action
default implementation of an action
final int size()
returns the size
base test class with helpers
void play()
test execution
final synchronized int search(final Object p_object)
CAgent(final IAgentConfiguration< CAgent > p_configuration, final int p_id)
ctor
final int minimalArgumentNumber()
minimum number of arguments
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
CSlice(final int p_size)
ctor
interface for (instantiable) plans and logical-rules
void execute()
running towers of hanoi
Long m_slicenumber
number of slices
Map< Integer, CAgent > m_agents
agent map
abstract test for playing towers of hanoi If a file agentprintin.conf exists on the main directory al...
static IPath from( @Nonnull final String p_string)
factor method to build path
class to create a path structure
final int id()
returns the id of the agent
final int minimalArgumentNumber()
minimum number of arguments
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
pops an elements from a tower
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
external action interface
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
CGenerator(final InputStream p_stream, final Stream< IAction > p_action)
ctor
final synchronized CSlice peek()
final int m_id
id of the agent
static final boolean PRINTENABLE
enable printing of test-data
final int minimalArgumentNumber()
minimum number of arguments
final CAgent generatesingle(final Object... p_data)
static Stream< IAction > actionsFromPackage( @Nullable final String... p_package)
get all classes within an Java package as action
Map< Integer, CTower > m_tower
tower map
void initialize()
test initialize
final int minimalArgumentNumber()
minimum number of arguments
final< N extends IAgent<?> > N raw()
final double m_failprobability
probability for action failing
final IPath name()
returns the name with path of the action
result for an immutable fuzzy value
static final long serialVersionUID
serial id
Long m_towernumber
number of towers
returns the number of elements of an tower
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
interface to define the agent configuration
interface for a variable builder which is called on each plan / rule execution
final int minimalArgumentNumber()
minimum number of arguments
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
class for any helper calls
final IPath name()
returns the name with path of the action
final IPath name()
returns the name with path of the 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
CTowerPush(final double p_failprobability)
ctor
AtomicBoolean m_running
running flag (agent can disable execution)
final IPath name()
returns the name with path of the action
term structure for simple datatypes
final int m_size
slice size
final synchronized CSlice push(final CSlice p_item)