LightJason - AgentSpeak(L++)
TestCViewMap.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.beliefbase;
25 
26 import com.codepoetics.protonpack.StreamUtils;
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import org.apache.commons.io.IOUtils;
29 import org.apache.commons.lang3.tuple.ImmutablePair;
30 import org.apache.commons.lang3.tuple.Pair;
31 import org.junit.Assert;
32 import org.junit.Assume;
33 import org.junit.Before;
34 import org.junit.Test;
53 
54 import javax.annotation.Nonnegative;
55 import javax.annotation.Nonnull;
56 import java.io.IOException;
57 import java.text.MessageFormat;
58 import java.util.ArrayList;
59 import java.util.Collections;
60 import java.util.List;
61 import java.util.Map;
62 import java.util.Set;
63 import java.util.logging.LogManager;
64 import java.util.stream.Collectors;
65 import java.util.stream.Stream;
66 
67 
71 public final class TestCViewMap extends IBaseTest
72 {
76  private final Set<IAction> m_actions = Stream.concat(
78  ? Stream.of( new CTestResult() )
79  : Stream.of( new CTestResult(), new CEmptyPrint() ),
81  ).collect( Collectors.toSet() );
85  private Map<String, Object> m_data;
89  private List<Pair<Boolean, String>> m_testlog;
90 
91  static
92  {
93  // disable logger
94  LogManager.getLogManager().reset();
95  }
96 
102  @Before
103  @SuppressWarnings( "unchecked" )
104  public final void initialize() throws IOException
105  {
106  m_testlog = Collections.synchronizedList( new ArrayList<>() );
107  m_data = new ObjectMapper().readValue(
108  "{ \"val\" : 123, \"str\" : \"text value\", \"logic\" : true, \"obj\" : { \"name\" : \"abcdef\", \"val\" : 357 }, \"ar\" : [1, 3, 5] }",
109  Map.class
110  );
111  }
112 
116  @Test
117  public final void stream()
118  {
119  Assume.assumeNotNull( m_data );
120 
121  Assert.assertTrue(
122  StreamUtils.zip(
123  new CViewMap( "main", m_data ).stream().limit( m_data.size() - 2 ),
124  Stream.of(
125  CLiteral.from( "val", CRawTerm.from( 123L ) ),
126  CLiteral.from( "str", CRawTerm.from( "text value" ) ),
127  CLiteral.from( "logic", CRawTerm.from( true ) ),
128  CLiteral.from( "obj/name", CRawTerm.from( "abcdef" ) ),
129  CLiteral.from( "obj/val", CRawTerm.from( 357L ) )
130  ),
131  Object::equals
132  ).allMatch( i -> i )
133  );
134  }
135 
139  @Test
140  public final void containsliteral()
141  {
142  Assume.assumeNotNull( m_data );
143  final IView l_view = new CViewMap( "main", m_data );
144 
145  Assert.assertTrue( l_view.containsLiteral( CPath.from( "val" ) ) );
146  Assert.assertTrue( l_view.containsLiteral( CPath.from( "obj/name" ) ) );
147  Assert.assertFalse( l_view.containsLiteral( CPath.from( "not/exists" ) ) );
148  }
149 
153  @Test
154  public final void containsview()
155  {
156  Assume.assumeNotNull( m_data );
157  final IView l_view = new CViewMap( "main", m_data );
158 
159  Assert.assertFalse( l_view.containsView( CPath.from( "not/exists" ) ) );
160  Assert.assertTrue( l_view.containsView( CPath.from( "obj" ) ) );
161  }
162 
168  @Test
169  public final void inagent() throws Exception
170  {
171  Assume.assumeNotNull( m_data );
172 
173  final IAgent<?> l_agent = new CAgent.CAgentGenerator(
174  "!main. +!main <- "
175  + ">>map/str(X); "
176  + "generic/print('string-value:', X); "
177  + "test/result( bool/equal(X, 'text value'), 'unified value incorrect' ). "
178  + "-!main <- test/result( fail, 'unification wrong').",
179  m_data,
180  m_actions
181  ).generatesingle().call().call();
182  Assert.assertTrue(
183  MessageFormat.format( "{0}", m_testlog.stream().filter( i -> !i.getLeft() ).map( Pair::getRight ).collect( Collectors.toList() ) ),
184  m_testlog.stream().anyMatch( Pair::getLeft )
185  );
186  }
187 
188  // ---------------------------------------------------------------------------------------------------------------------------------------------------------
189 
193  private static final class CEmptyPrint extends IBaseAction
194  {
198  private static final long serialVersionUID = 8344720639088993942L;
199 
200  @Nonnull
201  @Override
202  public final IPath name()
203  {
204  return CPath.from( "generic/print" );
205  }
206 
207  @Nonnegative
208  @Override
209  public final int minimalArgumentNumber()
210  {
211  return 0;
212  }
213 
214  @Nonnull
215  @Override
216  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
217  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
218  )
219  {
220  return CFuzzyValue.from( true );
221  }
222  }
223 
227  private final class CTestResult extends IBaseAction
228  {
232  private static final long serialVersionUID = 9032624165822970132L;
233 
234  @Nonnull
235  @Override
236  public final IPath name()
237  {
238  return CPath.from( "test/result" );
239  }
240 
241  @Nonnegative
242  @Override
243  public final int minimalArgumentNumber()
244  {
245  return 1;
246  }
247 
248  @Nonnull
249  @Override
250  public IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
251  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
252  )
253  {
254  m_testlog.add(
255  new ImmutablePair<>(
256  p_argument.get( 0 ).<Boolean>raw(),
257  p_argument.size() > 1
258  ? p_argument.get( 1 ).<String>raw()
259  : ""
260  )
261  );
262 
263  return CFuzzyValue.from( p_argument.get( 0 ).<Boolean>raw() );
264  }
265  }
266 
270  private static final class CAgent extends IBaseAgent<IAgent<?>>
271  {
275  private static final long serialVersionUID = -2312863050588218178L;
276 
277 
284  private CAgent( final IAgentConfiguration<IAgent<?>> p_configuration, final Map<String, Object> p_map )
285  {
286  super( p_configuration );
287  m_beliefbase.add( new CViewMap( "map", p_map, m_beliefbase ) );
288  }
289 
293  private static final class CAgentGenerator extends IBaseAgentGenerator<IAgent<?>>
294  {
298  private final Map<String, Object> m_map;
299 
308  CAgentGenerator( final String p_asl, final Map<String, Object> p_map, final Set<IAction> p_actions ) throws Exception
309  {
310  super( IOUtils.toInputStream( p_asl, "UTF-8" ), p_actions );
311  m_map = p_map;
312  }
313 
314  @Override
315  public IAgent<?> generatesingle( final Object... p_data )
316  {
317  return new CAgent( m_configuration, m_map );
318  }
319  }
320 
321  }
322 }
final void inagent()
test in-agent definition
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
default implementation of an action
boolean containsView( @Nonnull final IPath p_path)
view existing check
base test class with helpers
Definition: IBaseTest.java:33
Map< String, Object > m_data
map reference
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
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 IPath name()
returns the name with path of the action
view for a beliefbase that creates any access to the underlying data structures
Definition: IView.java:44
CAgent(final IAgentConfiguration< IAgent<?>> p_configuration, final Map< String, Object > p_map)
ctor
CAgentGenerator(final String p_asl, final Map< String, Object > p_map, final Set< IAction > p_actions)
ctor
execution context with local data
Definition: IContext.java:42
external action interface
Definition: IAction.java:38
static final boolean PRINTENABLE
enable printing of test-data
Definition: IBaseTest.java:38
static Stream< IAction > actionsFromPackage( @Nullable final String... p_package)
get all classes within an Java package as action
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
result for an immutable fuzzy value
final void containsliteral()
test contains literal
default generic literal class for agent beliefs a literal consists of a functor, an optional list of ...
Definition: CLiteral.java:64
final IPath name()
returns the name with path of the action
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
List< Pair< Boolean, String > > m_testlog
list with successful plans
static ILiteral from( @Nonnull final String p_functor, @Nullable final ITerm... p_values)
factory
Definition: CLiteral.java:161
view which can use a map of maps to represent the hierarchical beliefbase structure ...
Definition: CViewMap.java:60
boolean containsLiteral( @Nonnull final IPath p_path)
checks if a literal exists
final void containsview()
test contains view
term structure for simple datatypes
Definition: CRawTerm.java:45