LightJason - AgentSpeak(L++)
TestCAgent.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.agent;
25 
26 import com.tngtech.java.junit.dataprovider.DataProvider;
27 import com.tngtech.java.junit.dataprovider.DataProviderRunner;
28 import com.tngtech.java.junit.dataprovider.UseDataProvider;
29 import org.apache.commons.lang3.tuple.ImmutablePair;
30 import org.apache.commons.lang3.tuple.ImmutableTriple;
31 import org.apache.commons.lang3.tuple.Pair;
32 import org.apache.commons.lang3.tuple.Triple;
33 import org.junit.Assert;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
50 
51 import javax.annotation.Nonnegative;
52 import javax.annotation.Nonnull;
53 import java.io.FileInputStream;
54 import java.io.InputStream;
55 import java.text.MessageFormat;
56 import java.util.ArrayList;
57 import java.util.Collections;
58 import java.util.List;
59 import java.util.Set;
60 import java.util.logging.LogManager;
61 import java.util.stream.Collectors;
62 import java.util.stream.IntStream;
63 import java.util.stream.Stream;
64 
65 
70 @RunWith( DataProviderRunner.class )
71 public final class TestCAgent extends IBaseTest
72 {
76  private List<Pair<Boolean, String>> m_testlog;
77 
78  static
79  {
80  // disable logger
81  LogManager.getLogManager().reset();
82  }
83 
88  @DataProvider
89  public static Object[] generate()
90  {
91  return Stream.of(
92  new ImmutableTriple<>( "src/test/resources/agent/language/crypto.asl", 2, 9 ),
93  new ImmutableTriple<>( "src/test/resources/agent/language/math.asl", 2, 10 ),
94  new ImmutableTriple<>( "src/test/resources/agent/language/collection.asl", 2, 5 ),
95  new ImmutableTriple<>( "src/test/resources/agent/language/generic.asl", 3, 27 ),
96  new ImmutableTriple<>( "src/test/resources/agent/language/rules.asl", 2, 4 ),
97  new ImmutableTriple<>( "src/test/resources/agent/language/trigger.asl", 3, 21 ),
98  new ImmutableTriple<>( "src/test/resources/agent/language/execution.asl", 3, 4 ),
99  new ImmutableTriple<>( "src/test/resources/agent/language/webservice.asl", 4, 5 )
100  ).toArray();
101  }
102 
106  @Before
107  public final void initialize()
108  {
109  m_testlog = Collections.synchronizedList( new ArrayList<>() );
110  }
111 
118  @Test
119  @UseDataProvider( "generate" )
120  public final void testASLDefault( final Triple<String, Number, Number> p_asl ) throws Exception
121  {
122  try
123  (
124  final InputStream l_stream = new FileInputStream( p_asl.getLeft() )
125  )
126  {
127  final IAgent<?> l_agent = new CAgent.CAgentGenerator(
128  l_stream,
129  Stream.concat(
130  PRINTENABLE
131  ? Stream.of( new CTestResult() )
132  : Stream.of( new CTestResult(), new CEmptyPrint() ),
134  ).collect( Collectors.toSet() )
135  ).generatesingle();
136 
137  IntStream.range( 0, p_asl.getMiddle().intValue() )
138  .forEach( i ->
139  {
140  try
141  {
142  l_agent.call();
143  }
144  catch ( final Exception l_exception )
145  {
146  Assert.assertTrue( MessageFormat.format( "{0}: {1}", p_asl.getLeft(), l_exception.getMessage() ), false );
147  }
148  } );
149  }
150  catch ( final Exception l_exception )
151  {
152  Assert.assertTrue( MessageFormat.format( "{0}: {1}", p_asl.getLeft(), l_exception.getMessage() ), false );
153  }
154 
155  Assert.assertEquals( MessageFormat.format( "{0} {1}", "number of tests", p_asl.getLeft() ), p_asl.getRight().longValue(), m_testlog.size() );
156  Assert.assertTrue(
157  MessageFormat.format( "{0}", m_testlog.stream().filter( i -> !i.getLeft() ).map( Pair::getRight ).collect( Collectors.toList() ) ),
158  m_testlog.stream().anyMatch( Pair::getLeft )
159  );
160  }
161 
162  // ---------------------------------------------------------------------------------------------------------------------------------------------------------
163 
167  private static final class CEmptyPrint extends IBaseAction
168  {
172  private static final long serialVersionUID = 8344720639088993942L;
173 
174  @Nonnull
175  @Override
176  public final IPath name()
177  {
178  return CPath.from( "generic/print" );
179  }
180 
181  @Nonnegative
182  @Override
183  public final int minimalArgumentNumber()
184  {
185  return 0;
186  }
187 
188  @Nonnull
189  @Override
190  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
191  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
192  )
193  {
194  return CFuzzyValue.from( true );
195  }
196  }
197 
201  private final class CTestResult extends IBaseAction
202  {
206  private static final long serialVersionUID = 9032624165822970132L;
207 
208  @Nonnull
209  @Override
210  public final IPath name()
211  {
212  return CPath.from( "test/result" );
213  }
214 
215  @Nonnegative
216  @Override
217  public final int minimalArgumentNumber()
218  {
219  return 1;
220  }
221 
222  @Nonnull
223  @Override
224  public IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
225  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
226  )
227  {
228  m_testlog.add(
229  new ImmutablePair<>(
230  p_argument.get( 0 ).<Boolean>raw(),
231  p_argument.size() > 1
232  ? p_argument.get( 1 ).<String>raw()
233  : ""
234  )
235  );
236 
237  return CFuzzyValue.from( p_argument.get( 0 ).<Boolean>raw() );
238  }
239  }
240 
244  private static final class CAgent extends IBaseAgent<IAgent<?>>
245  {
249  private static final long serialVersionUID = 7077303993134371057L;
250 
256  private CAgent( final IAgentConfiguration<IAgent<?>> p_configuration )
257  {
258  super( p_configuration );
259  }
260 
264  private static final class CAgentGenerator extends IBaseAgentGenerator<IAgent<?>>
265  {
266 
274  CAgentGenerator( final InputStream p_stream, final Set<IAction> p_actions ) throws Exception
275  {
276  super( p_stream, p_actions, ( p_agent, p_runningcontext ) -> Stream.of(
277  new CConstant<>( "MyConstInt", 123 ),
278  new CConstant<>( "MyConstString", "here is a test string" )
279  ) );
280  }
281 
282  @Override
283  public IAgent<?> generatesingle( final Object... p_data )
284  {
285  return new CAgent( m_configuration );
286  }
287  }
288 
289  }
290 
291 }
CAgent(final IAgentConfiguration< IAgent<?>> p_configuration)
ctor
default implementation of an action
base test class with helpers
Definition: IBaseTest.java:33
List< Pair< Boolean, String > > m_testlog
list with successful plans
Definition: TestCAgent.java:76
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
final IPath name()
returns the name with path of the action
final IPath name()
returns the name with path of the action
static IPath from( @Nonnull final String p_string)
factor method to build path
Definition: CPath.java:166
final int minimalArgumentNumber()
minimum number of arguments
class to create a path structure
Definition: CPath.java:53
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 Object [] generate()
data provider for defining asl files
Definition: TestCAgent.java:89
execution context with local data
Definition: IContext.java:42
external action interface
Definition: IAction.java:38
static Stream< IAction > actionsFromPackage( @Nullable final String... p_package)
get all classes within an Java package as action
final void initialize()
return list initialize
result for an immutable fuzzy value
final int minimalArgumentNumber()
minimum number of arguments
CAgentGenerator(final InputStream p_stream, final Set< IAction > p_actions)
ctor
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