LightJason - AgentSpeak(L++)
TestCMetric.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.consistency;
25 
26 import org.apache.commons.io.IOUtils;
27 import org.junit.Assume;
28 import org.junit.Before;
29 import org.junit.Test;
46 
47 import javax.annotation.Nonnull;
48 import java.io.InputStream;
49 import java.util.Collection;
50 import java.util.Collections;
51 import java.util.Set;
52 import java.util.stream.Collectors;
53 import java.util.stream.Stream;
54 
55 import static org.junit.Assert.assertEquals;
56 
57 
61 public final class TestCMetric extends IBaseTest
62 {
74  private Set<ILiteral> m_literals;
75 
81  @Before
82  public void initialize() throws Exception
83  {
84  m_generator = new CGenerator();
85  m_agentgenerator = new CAgent.CAgentGenerator( IOUtils.toInputStream( "", "UTF-8" ), Collections.emptySet() );
86 
87  m_literals = Stream.of(
88  CLiteral.from( "toplevel" ),
89  CLiteral.from( "first/sub1" ),
90  CLiteral.from( "first/sub2" ),
91  CLiteral.from( "second/sub3" ),
92  CLiteral.from( "second/sub4" ),
93  CLiteral.from( "second/sub/sub5" )
94  ).collect( Collectors.toSet() );
95  }
96 
97 
98 
102  @Test
103  public final void symmetricweightequality()
104  {
105  Assume.assumeNotNull( m_literals );
106  Assume.assumeFalse( "testing literals are empty", m_literals.isEmpty() );
107  this.check(
108  "symmetric difference equality",
109  new CAll(), new CSymmetricDifference(),
110  m_literals,
111  m_literals,
112  0, 0
113  );
114  }
115 
116 
120  @Test
121  public final void symmetricweightinequality()
122  {
123  Assume.assumeNotNull( m_literals );
124  Assume.assumeFalse( "testing literals are empty", m_literals.isEmpty() );
125  this.check(
126  "symmetric difference inequality",
127  new CAll(), new CSymmetricDifference(),
128  m_literals,
129  Stream.concat( m_literals.stream(), Stream.of( CLiteral.from( "diff" ) ) ).collect( Collectors.toSet() ),
130  1, 0
131  );
132  }
133 
134 
138  @Test
139  public final void weightequality()
140  {
141  Assume.assumeNotNull( m_literals );
142  Assume.assumeFalse( "testing literals are empty", m_literals.isEmpty() );
143 
144  this.check(
145  "weight difference equality",
146  new CAll(), new CWeightedDifference(),
147  m_literals,
148  m_literals,
149  24, 0
150  );
151  }
152 
153 
157  @Test
158  public final void weightinequality()
159  {
160  Assume.assumeNotNull( m_literals );
161  Assume.assumeFalse( "testing literals are empty", m_literals.isEmpty() );
162 
163  this.check(
164  "weight difference inequality",
165  new CAll(),
166  new CWeightedDifference(),
167  m_literals,
168  Stream.concat( m_literals.stream(), Stream.of( CLiteral.from( "diff" ) ) ).collect( Collectors.toSet() ),
169  28 + 1.0 / 6, 0
170  );
171  }
172 
173 
185  private void check( final String p_message, final IFilter p_filter, final IMetric p_metric, final Collection<ILiteral> p_belief1,
186  final Collection<ILiteral> p_belief2, final double p_excepted, final double p_delta )
187  {
188  final double l_value = p_metric.apply(
189  p_filter.apply( this.agent( p_belief1 ) ),
190  p_filter.apply( this.agent( p_belief2 ) )
191  );
192  assertEquals( p_message, p_excepted, l_value, p_delta );
193  }
194 
195 
202  private CAgent agent( final Collection<ILiteral> p_literals )
203  {
204  Assume.assumeNotNull( m_generator );
205 
206  final CAgent l_agent = m_agentgenerator.generatesingle();
207  p_literals.forEach( i -> l_agent.beliefbase().generate( m_generator, i.functorpath() ).add( i ) );
208  return l_agent;
209  }
210 
211  // ---------------------------------------------------------------------------------------------------------------------------------------------------------
212 
216  private static final class CAgent extends IBaseAgent<CAgent>
217  {
221  private static final long serialVersionUID = 4390503811927101766L;
222 
228  private CAgent( final IAgentConfiguration<CAgent> p_configuration )
229  {
230  super( p_configuration );
231  }
232 
236  private static final class CAgentGenerator extends IBaseAgentGenerator<CAgent>
237  {
243  CAgentGenerator( @Nonnull final InputStream p_stream, @Nonnull final Set<IAction> p_actions ) throws Exception
244  {
245  super( p_stream, p_actions );
246  }
247 
248  @Override
249  public CAgent generatesingle( final Object... p_data )
250  {
251  return new CAgent( m_configuration );
252  }
253  }
254 
255  }
256 
257 
261  private static final class CGenerator implements IViewGenerator
262  {
263  @Override
264  public final IView apply( final String p_name, final IView p_parent )
265  {
266  return new CBeliefbase( new CMultiStorage<>() ).create( p_name, p_parent );
267  }
268  }
269 
270 }
Set< ILiteral > m_literals
set with testing literals
final void symmetricweightinequality()
test symmetric weight metric inequality
CAgent.CAgentGenerator m_agentgenerator
agent generator
filtering interface of agent literal values for metric
Definition: IFilter.java:36
base test class with helpers
Definition: IBaseTest.java:33
void check(final String p_message, final IFilter p_filter, final IMetric p_metric, final Collection< ILiteral > p_belief1, final Collection< ILiteral > p_belief2, final double p_excepted, final double p_delta)
runs the check
metric interface of the coherency structure
Definition: IMetric.java:37
CAgentGenerator( @Nonnull final InputStream p_stream, @Nonnull final Set< IAction > p_actions)
ctor
CAgent agent(final Collection< ILiteral > p_literals)
generates an agent
filtering for all execution plans & beliefs
Definition: CAll.java:37
view for a beliefbase that creates any access to the underlying data structures
Definition: IView.java:44
IViewGenerator m_generator
literal view generator
final void symmetricweightequality()
test symmetric weight metric equality
external action interface
Definition: IAction.java:38
calculates the distance with respect to size of union and intersection of beliefbases.
thread-safe storage of the data of single- and multi-elements
default generic literal class for agent beliefs a literal consists of a functor, an optional list of ...
Definition: CLiteral.java:64
IView generate( @Nonnull final IViewGenerator p_generator, @Nonnull final IPath... p_paths)
generates path structure
interface for generating non-existing beliefbases views
final void weightequality()
test symmetric metric equality
final IView apply(final String p_name, final IView p_parent)
CAgent(final IAgentConfiguration< CAgent > p_configuration)
ctor
static ILiteral from( @Nonnull final String p_functor, @Nullable final ITerm... p_values)
factory
Definition: CLiteral.java:161
final IView create( @Nonnull final String p_name)
returns a new view of the belief base
final void weightinequality()
test symmetric metric equality
metric on collections returns the size of symmetric difference
beliefbase to generate any event-based data by reference counting
final IAgentConfiguration< T > m_configuration
configuration of an agent