LightJason - AgentSpeak(L++)
TestCActionAgent.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.Before;
29 import org.junit.Test;
56 
57 import javax.annotation.Nonnull;
58 import java.io.ByteArrayInputStream;
59 import java.io.InputStream;
60 import java.nio.charset.StandardCharsets;
61 import java.util.AbstractMap;
62 import java.util.ArrayList;
63 import java.util.Collections;
64 import java.util.List;
65 import java.util.Set;
66 import java.util.logging.LogManager;
67 import java.util.stream.Collectors;
68 import java.util.stream.IntStream;
69 import java.util.stream.Stream;
70 
71 
75 public final class TestCActionAgent extends IBaseTest
76 {
81 
82 
83 
84  static
85  {
86  LogManager.getLogManager().reset();
87  }
88 
89 
95  @Before
96  public void initialize() throws Exception
97  {
98  m_context = new CContext(
99  new CAgent.CGenerator( new ByteArrayInputStream( "".getBytes( StandardCharsets.UTF_8 ) ), Collections.emptySet() ).generatesingle(),
100  new CEmptyPlan( CTrigger.from( ITrigger.EType.ADDGOAL, CLiteral.from( "contextplan" ) ) ),
101  Collections.emptyList()
102  );
103  }
104 
105 
109  @Test
110  public final void planlist()
111  {
112  final ITrigger l_trigger = CTrigger.from( ITrigger.EType.ADDGOAL, CLiteral.from( "testplanlist" ) );
113  final IPlan l_plan = new CEmptyPlan( l_trigger );
114  final List<ITerm> l_return = new ArrayList<>();
115 
116  new CPlanList().execute(
117  false, m_context,
118  Collections.emptyList(),
119  l_return
120  );
121 
122  Assert.assertEquals( l_return.size(), 1 );
123  Assert.assertTrue( l_return.get( 0 ).raw() instanceof List<?> );
124  Assert.assertEquals( l_return.get( 0 ).<List<?>>raw().size(), 0 );
125 
126 
127  m_context.agent().plans().put( l_plan.trigger(), CPlanStatistic.from( l_plan ) );
128 
129  new CPlanList().execute(
130  false, m_context,
131  Collections.emptyList(),
132  l_return
133  );
134 
135  Assert.assertEquals( l_return.size(), 2 );
136  Assert.assertTrue( l_return.get( 1 ).raw() instanceof List<?> );
137  Assert.assertEquals( l_return.get( 1 ).<List<?>>raw().size(), 1 );
138  Assert.assertTrue( l_return.get( 1 ).<List<?>>raw().get( 0 ) instanceof AbstractMap.Entry<?, ?> );
139  Assert.assertEquals( l_return.get( 1 ).<List<AbstractMap.Entry<String, ILiteral>>>raw().get( 0 ).getKey(), l_trigger.type().sequence() );
140  Assert.assertEquals( l_return.get( 1 ).<List<AbstractMap.Entry<String, ILiteral>>>raw().get( 0 ).getValue(), l_trigger.literal() );
141  }
142 
143 
147  @Test
148  public final void addplan()
149  {
150  final IPlan l_plan = new CEmptyPlan( CTrigger.from( ITrigger.EType.ADDGOAL, CLiteral.from( "testaddplan" ) ) );
151 
152  new CAddPlan().execute(
153  false, m_context,
154  Stream.of( l_plan ).map( CRawTerm::from ).collect( Collectors.toList() ),
155  Collections.emptyList()
156  );
157 
158  Assert.assertEquals( m_context.agent().plans().size(), 1 );
159  Assert.assertArrayEquals(
160  m_context.agent().plans().values().stream().map( IPlanStatistic::plan ).toArray(),
161  Stream.of( l_plan ).toArray()
162  );
163  }
164 
165 
169  @Test
170  public final void cycletime()
171  {
172  this.next();
173 
174  final List<ITerm> l_return = new ArrayList<>();
175  new CCycleTime().execute(
176  false, m_context,
177  Collections.emptyList(),
178  l_return
179  );
180 
181  Assert.assertEquals( l_return.size(), 1 );
182  Assert.assertTrue( l_return.get( 0 ).<Number>raw().longValue() > 0 );
183  }
184 
185 
189  @Test
190  public final void getplan()
191  {
192  final IPlan l_plan = new CEmptyPlan( CTrigger.from( ITrigger.EType.ADDGOAL, CLiteral.from( "testgetplan" ) ) );
193  final List<ITerm> l_return = new ArrayList<>();
194 
195 
196  new CGetPlan().execute(
197  false, m_context,
198  Collections.emptyList(),
199  l_return
200  );
201 
202  Assert.assertTrue( l_return.isEmpty() );
203 
204 
205  m_context.agent().plans().put( l_plan.trigger(), CPlanStatistic.from( l_plan ) );
206 
207  new CGetPlan().execute(
208  false, m_context,
209  Stream.of( "+!", "testgetplan" ).map( CRawTerm::from ).collect( Collectors.toList() ),
210  l_return
211  );
212 
213  Assert.assertEquals( l_return.size(), 1 );
214  Assert.assertTrue( l_return.get( 0 ).raw() instanceof List<?> );
215  Assert.assertEquals( l_return.get( 0 ).<List<?>>raw().size(), 1 );
216  Assert.assertArrayEquals( l_return.get( 0 ).<List<?>>raw().toArray(), Stream.of( l_plan ).toArray() );
217  }
218 
219 
223  @Test
224  public final void removeplan()
225  {
226  final IPlan l_plan = new CEmptyPlan( CTrigger.from( ITrigger.EType.ADDGOAL, CLiteral.from( "testremoveplan" ) ) );
227  m_context.agent().plans().put( l_plan.trigger(), CPlanStatistic.from( l_plan ) );
228 
229  Assert.assertTrue(
230  new CRemovePlan().execute(
231  false, m_context,
232  Stream.of( "+!", "testremoveplan" ).map( CRawTerm::from ).collect( Collectors.toList() ),
233  Collections.emptyList()
234  ).value()
235  );
236  }
237 
238 
242  @Test
243  public final void removeplanerror()
244  {
245  Assert.assertFalse(
246  new CRemovePlan().execute(
247  false, m_context,
248  Stream.of( "+!", "testremoveerrorplan" ).map( CRawTerm::from ).collect( Collectors.toList() ),
249  Collections.emptyList()
250  ).value()
251  );
252  }
253 
254 
258  @Test
259  public final void clearbeliefbase()
260  {
261  IntStream.range( 0, 100 )
262  .mapToObj( i -> RandomStringUtils.random( 12, "abcdefghijklmnop" ) )
263  .map( i -> CLiteral.from( i ) )
264  .forEach( i -> m_context.agent().beliefbase().add( i ) );
265 
266  Assert.assertEquals( m_context.agent().beliefbase().size(), 100 );
267 
268 
270  false, m_context,
271  Collections.emptyList(),
272  Collections.emptyList()
273  );
274 
275  Assert.assertEquals( m_context.agent().beliefbase().size(), 0 );
276  }
277 
278 
282  @Test
283  public final void belieflist()
284  {
285  final List<ITerm> l_return = new ArrayList<>();
286  final Set<String> l_list = IntStream.range( 0, 100 )
287  .mapToObj( i -> RandomStringUtils.random( 12, "abcdefghijklmnop" ) )
288  .peek( i -> m_context.agent().beliefbase().add( CLiteral.from( i ) ) )
289  .collect( Collectors.toSet() );
290 
291  Assert.assertEquals( m_context.agent().beliefbase().size(), 100 );
292 
293  new CBeliefList().execute(
294  false, m_context,
295  Collections.emptyList(),
296  l_return
297  );
298 
299 
300  Assert.assertEquals( l_return.size(), 1 );
301  Assert.assertTrue( l_return.get( 0 ).raw() instanceof List<?> );
302 
303  Assert.assertTrue(
304  l_return.get( 0 )
305  .<List<ILiteral>>raw()
306  .stream()
307  .map( i -> i.fqnfunctor().toString() )
308  .allMatch( l_list::contains )
309  );
310  }
311 
312 
318  private IContext next()
319  {
320  try
321  {
322  m_context.agent().call();
323  }
324  catch ( final Exception l_exception )
325  {
326  Assert.assertTrue( l_exception.getMessage(), false );
327  }
328 
329  return m_context;
330  }
331 
332  // ---------------------------------------------------------------------------------------------------------------------------------------------------------
333 
337  private static final class CAgent extends IBaseAgent<CAgent>
338  {
342  private static final long serialVersionUID = 8036930915838541805L;
343 
349  private CAgent( final IAgentConfiguration<CAgent> p_configuration )
350  {
351  super( p_configuration );
352  }
353 
354 
355 
359  private static final class CGenerator extends IBaseAgentGenerator<CAgent>
360  {
361 
362  CGenerator( final InputStream p_stream, final Set<IAction> p_actions ) throws Exception
363  {
364  super( p_stream, p_actions );
365  }
366 
367  @Override
368  public final CAgent generatesingle( final Object... p_data )
369  {
370  return new CAgent( m_configuration );
371  }
372  }
373 
374  }
375 
379  private static class CEmptyPlan extends IBaseInstantiable implements IPlan
380  {
384  private static final long serialVersionUID = 6885053756134284862L;
388  private final ITrigger m_trigger;
389 
395  CEmptyPlan( final ITrigger p_trigger )
396  {
397  super( Collections.emptyList(), Collections.emptySet(), 0 );
398  m_trigger = p_trigger;
399  }
400 
401  @Nonnull
402  @Override
403  public final ITrigger trigger()
404  {
405  return m_trigger;
406  }
407 
408  @Nonnull
409  @Override
410  public final IFuzzyValue<Boolean> condition( final IContext p_context )
411  {
412  return CFuzzyValue.from( true );
413  }
414  }
415 
416 }
CGenerator(final InputStream p_stream, final Set< IAction > p_actions)
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: CCycleTime.java:54
final IFuzzyValue< Boolean > condition(final IContext p_context)
execute the plan condition
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
base test class with helpers
Definition: IBaseTest.java:33
Multimap< ITrigger, IPlanStatistic > plans()
returns the internal map of plans
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
static ITrigger from( @Nonnull final EType p_event, @Nonnull final ILiteral p_literal)
creates a trigger event^
Definition: CTrigger.java:87
execution context with local data
Definition: IContext.java:42
external action interface
Definition: IAction.java:38
action to get plan-information as list.
Definition: CPlanList.java:48
CAgent(final IAgentConfiguration< CAgent > p_configuration)
ctor
result for an immutable fuzzy value
default generic literal class for agent beliefs a literal consists of a functor, an optional list of ...
Definition: CLiteral.java:64
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: CPlanList.java:57
final Map< Integer, ITrigger > m_trigger
execution trigger with content hash
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
ITrigger trigger()
returns the trigger event
static ILiteral from( @Nonnull final String p_functor, @Nullable final ITerm... p_values)
factory
Definition: CLiteral.java:161
int size()
returns the size of literals
IView add( @Nonnull final ILiteral... p_literal)
adds a literal in the current structure
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: CGetPlan.java:76
IView beliefbase()
returns the beliefbase
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: CAddPlan.java:64
term structure for simple datatypes
Definition: CRawTerm.java:45
IAgent<?> agent()
returns the agent of the context
final IAgentConfiguration< T > m_configuration
configuration of an agent