LightJason - AgentSpeak(L++)
TestCUnifier.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 org.junit.Assert;
27 import org.junit.Test;
33 
34 import java.text.MessageFormat;
35 import java.util.Arrays;
36 import java.util.List;
37 import java.util.Set;
38 import java.util.stream.Collectors;
39 import java.util.stream.Stream;
40 
41 import static org.junit.Assert.assertEquals;
42 import static org.junit.Assert.assertNotEquals;
43 
44 
48 public final class TestCUnifier extends IBaseTest
49 {
50 
56  @Test
57  public final void literalvaluetraversing() throws Exception
58  {
59  final Set<ILiteral> l_test = Stream.of(
60  CLiteral.parse( "first('Hello')" ),
61  CLiteral.parse( "first('Foo')" )
62  ).collect( Collectors.toSet() );
63 
64  final ILiteral l_literal = CLiteral.from( "toplevel", Stream.concat( l_test.stream(), Stream.of(
65  CLiteral.parse( "second/sub(1)" ),
66  CLiteral.parse( "second/sub(2)" ),
67  CLiteral.parse( "second/sub(3)" )
68  ) ).collect( Collectors.toSet() ) );
69 
70  final List<ITerm> l_result = l_literal.values( CPath.from( "first" ) ).collect( Collectors.toList() );
71  assertEquals( MessageFormat.format( "literal traversing in {0} is wrong", l_literal ), l_result.size(), l_test.size() );
72  }
73 
74 
80  @Test
81  public final void literalvaluesequentialtraversing() throws Exception
82  {
83  final ILiteral[] l_test = Stream.of(
84  CLiteral.parse( "first('Hello')" ),
85  CLiteral.parse( "first('Foo')" )
86  ).toArray( ILiteral[]::new );
87 
88  final ILiteral l_literal = CLiteral.from( "toplevel", Stream.concat(
89  Arrays.stream( l_test ),
90  Stream.of(
91  CLiteral.parse( "second/sub(1)" ),
92  CLiteral.parse( "second/sub(2)" ),
93  CLiteral.parse( "second/sub(3)" )
94  )
95  ).collect( Collectors.toList() ) );
96 
97  Assert.assertArrayEquals(
98  MessageFormat.format( "literal sequential traversing in {0} is wrong for", l_literal ),
99  l_literal.orderedvalues( CPath.from( "first" ) ).toArray(),
100  l_test
101  );
102  }
103 
109  @Test
110  public final void structurehash() throws Exception
111  {
112  final ILiteral l_first = CLiteral.parse( "foo(sub(3),sub(X),test(1235),data(value('data string')))[ann(1),value('test')]" );
113  final ILiteral l_second = CLiteral.parse( "foo(sub(3),sub(X),test(123),data(value('data string another value')))[ann(13),value('test2')]" );
114 
115  assertEquals(
116  MessageFormat.format( "literal value hash of [{0}] and [{1}] is [{2} / {3}] inequal", l_first, l_second, l_first.structurehash(),
117  l_second.structurehash()
118  ),
119  l_first.structurehash(),
120  l_second.structurehash()
121  );
122 
123  final ILiteral l_third = CLiteral.parse( "foo()" );
124  final ILiteral l_fourth = CLiteral.parse( "hallo()" );
125  assertNotEquals(
126  MessageFormat.format( "literal value hash of [{0}] and [{1}] are equal [{2}]", l_third, l_fourth, l_third.structurehash() ),
127  l_third.structurehash(),
128  l_fourth.structurehash()
129  );
130  }
131 
132 }
final void structurehash()
literal value hashing
base test class with helpers
Definition: IBaseTest.java:33
Stream< ITerm > orderedvalues( @Nullable final IPath... p_path)
returns a stream over the ordered values in sequential ordering
static IPath from( @Nonnull final String p_string)
factor method to build path
Definition: CPath.java:166
class to create a path structure
Definition: CPath.java:53
final void literalvaluesequentialtraversing()
traversion of literal value content
Stream< ITerm > values(final IPath... p_path)
returns a stream over value items
final void literalvaluetraversing()
traversion of literal value content
int structurehash()
returns a hash value which defines a hash ove rthe structure
default generic literal class for agent beliefs a literal consists of a functor, an optional list of ...
Definition: CLiteral.java:64
static ILiteral parse( @Nonnull final String p_literal)
factory
Definition: CLiteral.java:259
static ILiteral from( @Nonnull final String p_functor, @Nullable final ITerm... p_values)
factory
Definition: CLiteral.java:161