LightJason - AgentSpeak(L++)
TestCTermVariablesConstant.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.language;
25 
26 import com.codepoetics.protonpack.StreamUtils;
27 import org.junit.Assert;
28 import org.junit.Test;
37 
38 import java.util.stream.Stream;
39 
40 
44 public final class TestCTermVariablesConstant extends IBaseTest
45 {
46 
50  @Test
51  public final void literal()
52  {
53  final ILiteral l_emptyliteral = CLiteral.from( "foo/bar" );
54 
55  Assert.assertEquals( l_emptyliteral.functor(), "bar" );
56  Assert.assertEquals( l_emptyliteral.fqnfunctor(), CPath.from( "foo/bar" ) );
57  Assert.assertFalse( l_emptyliteral.hasAt() );
58  Assert.assertTrue( l_emptyliteral.emptyValues() );
59 
60 
61 
62  final ILiteral l_valueliteral = CLiteral.from( "foo/value", CRawTerm.from( 5 ), CRawTerm.from( "hello" ) );
63 
64  Assert.assertEquals( l_valueliteral.functor(), "value" );
65  Assert.assertEquals( l_valueliteral.fqnfunctor(), CPath.from( "foo/value" ) );
66  Assert.assertFalse( l_valueliteral.hasAt() );
67  Assert.assertFalse( l_valueliteral.emptyValues() );
68 
69  Assert.assertTrue(
70  StreamUtils.zip(
71  Stream.of( 5, "hello" ),
72  l_valueliteral.values(),
73  ( i, j ) -> i.equals( j.raw() )
74  ).allMatch( i -> i )
75  );
76  }
77 
78 
82  @Test
83  public final void rawterm()
84  {
85  final ITerm l_stringterm = CRawTerm.from( "hello" );
86 
87  Assert.assertTrue( CCommon.rawvalueAssignableTo( l_stringterm, String.class ) );
88  Assert.assertEquals( l_stringterm.raw(), "hello" );
89 
90 
91  final double l_value = Math.random();
92  final ITerm l_numberterm = CRawTerm.from( l_value );
93 
94  Assert.assertTrue( CCommon.rawvalueAssignableTo( l_numberterm, Number.class ) );
95  Assert.assertTrue( CCommon.rawvalueAssignableTo( l_numberterm, Double.class ) );
96  Assert.assertEquals( "number value", l_numberterm.raw(), l_value, 0 );
97  }
98 
99 
103  @Test( expected = RuntimeException.class )
104  public final void constantaccess()
105  {
106  new CConstant<>( "CA", 5 ).set( 10 );
107  }
108 
109 
113  @Test
114  public final void constant()
115  {
116  final double l_value = Math.random();
117  final ITerm l_constant = new CConstant<>( "C", l_value );
118 
119  Assert.assertEquals( l_constant.functor(), "C" );
120  Assert.assertTrue( l_constant.hasVariable() );
121  Assert.assertTrue( CCommon.rawvalueAssignableTo( l_constant, Number.class ) );
122  Assert.assertTrue( CCommon.rawvalueAssignableTo( l_constant, Double.class ) );
123  Assert.assertEquals( "constant number value", l_constant.raw(), l_value, 0 );
124  }
125 
129  @Test
130  public final void constantcopy()
131  {
132  Assert.assertEquals(
133  new CConstant<>( "const/value", "test" ).shallowcopysuffix().functor(),
134  "value"
135  );
136 
137  Assert.assertEquals(
138  new CConstant<>( "const/value", "test" ).shallowcopy( CPath.from( "xxx" ) ).fqnfunctor().toString(),
139  "xxx/const/value"
140  );
141  }
142 
146  @Test( expected = IllegalStateException.class )
147  public final void variableany()
148  {
149  final IVariable<?> l_variable = new CVariable<Object>( "_" );
150 
151  Assert.assertTrue( l_variable.any() );
152  Assert.assertFalse( l_variable.allocated() );
153 
154  l_variable.thrownotallocated();
155  }
156 
157 
161  @Test( expected = IllegalArgumentException.class )
162  public final void variablevalueassignable()
163  {
164  new CVariable<Object>( "num", 123 ).throwvaluenotassignableto( String.class );
165  }
166 
170  @Test
171  public final void variableequals()
172  {
173  Assert.assertEquals(
174  new CVariable<Object>( "foo", "str" ),
175  new CVariable<Object>( "foo", 123 )
176  );
177 
178  Assert.assertEquals(
179  new CVariable<>( "eq", 56 ),
180  new CVariable<>( "eq", 56 )
181  );
182  }
183 
187  @Test
188  public final void variabletostring()
189  {
190  Assert.assertEquals( new CVariable<>( "data" ).toString(), "data()" );
191  Assert.assertEquals( new CVariable<>( "data", "value" ).toString(), "data(value)" );
192  }
193 
194 
198  @Test
199  public final void variable()
200  {
201  Assert.assertTrue( new CVariable<>( "_" ).any() );
202 
203 
204  double l_value = Math.random();
205  final IVariable<Number> l_variable = new CVariable<>( "V", l_value );
206 
207  Assert.assertEquals( l_variable.functor(), "V" );
208  Assert.assertTrue( l_variable.allocated() );
209  Assert.assertFalse( l_variable.mutex() );
210  Assert.assertTrue( l_variable.hasVariable() );
211  Assert.assertTrue( CCommon.rawvalueAssignableTo( l_variable, Number.class ) );
212  Assert.assertTrue( CCommon.rawvalueAssignableTo( l_variable, Double.class ) );
213 
214 
215  Assert.assertEquals( "get variable number value", l_variable.raw(), l_value, 0 );
216 
217  l_value = Math.random();
218  l_variable.set( l_value );
219 
220  Assert.assertEquals( "set variable number value", l_variable.raw(), l_value, 0 );
221 
222  l_variable.set( null );
223  Assert.assertFalse( l_variable.allocated() );
224  }
225 
229  @Test
230  public final void variablefunctor()
231  {
232  Assert.assertEquals(
233  new CVariable<>( "prefix/name" ).functorpath(),
234  CPath.from( "prefix" )
235  );
236  }
237 
241  @Test
242  public final void variablemutex()
243  {
244  double l_value = Math.random();
245  final IVariable<Number> l_variable = new CMutexVariable<>( "V", l_value );
246 
247  Assert.assertEquals( l_variable.functor(), "V" );
248  Assert.assertTrue( l_variable.allocated() );
249  Assert.assertTrue( l_variable.mutex() );
250  Assert.assertTrue( l_variable.hasVariable() );
251  Assert.assertTrue( CCommon.rawvalueAssignableTo( l_variable, Number.class ) );
252  Assert.assertTrue( CCommon.rawvalueAssignableTo( l_variable, Double.class ) );
253 
254 
255  Assert.assertEquals( "get mutex variable number value", l_variable.raw(), l_value, 0 );
256 
257  l_value = Math.random();
258  l_variable.set( l_value );
259 
260  Assert.assertEquals( "set mutex variable number value", l_variable.raw(), l_value, 0 );
261 
262  l_variable.set( null );
263  Assert.assertFalse( l_variable.allocated() );
264  }
265 
269  @Test
270  public final void variablecopy()
271  {
272  final IVariable<?> l_variable = new CVariable<>( "prefix/copy", new Object() );
273 
274  Assert.assertEquals( l_variable.shallowcopysuffix().fqnfunctor(), CPath.from( "copy" ) );
275  Assert.assertEquals( l_variable.shallowcopy( CPath.from( "xxx" ) ).fqnfunctor(), CPath.from( "xxx/prefix/copy" ) );
276  Assert.assertEquals( l_variable.shallowcopysuffix().<Object>raw(), l_variable.<Object>raw() );
277 
278  final ITerm l_deep = l_variable.deepcopy( CPath.from( "foo" ) );
279  Assert.assertEquals( l_deep.fqnfunctor(), CPath.from( "prefix/copy/foo" ) );
280  Assert.assertNotEquals( l_deep.<Object>raw(), l_variable.<Object>raw() );
281 
282  final ITerm l_deepsuffix = l_variable.deepcopysuffix();
283  Assert.assertEquals( l_deepsuffix.fqnfunctor(), CPath.from( "copy" ) );
284  Assert.assertNotEquals( l_deepsuffix.<Object>raw(), l_variable.<Object>raw() );
285  }
286 
287 
291  @Test
292  public final void relocatevariablerelocate()
293  {
294  final IVariable<String> l_variable = new CVariable<>( "RA" );
295  final CRelocateVariable<String> l_relocate = new CRelocateVariable<>( l_variable );
296 
297  Assert.assertFalse( l_relocate.mutex() );
298 
299  Assert.assertEquals( l_variable.functor(), "RA" );
300  Assert.assertEquals( l_relocate.functor(), "RA" );
301 
302  Assert.assertFalse( l_variable.allocated() );
303  Assert.assertFalse( l_relocate.allocated() );
304 
305  Assert.assertTrue( l_variable.hasVariable() );
306  Assert.assertTrue( l_relocate.hasVariable() );
307 
308 
309  l_relocate.set( "relocated foo" );
310  Assert.assertTrue( l_relocate.allocated() );
311  Assert.assertEquals( l_relocate.raw(), "relocated foo" );
312 
313 
314  l_relocate.relocate();
315  Assert.assertTrue( l_variable.allocated() );
316  Assert.assertEquals( l_variable.raw(), "relocated foo" );
317  }
318 
319 
323  @Test
324  public final void relocatevariablemutex()
325  {
326  final IVariable<String> l_variable = new CVariable<>( "RAM" );
327  final CRelocateMutexVariable<String> l_relocate = new CRelocateMutexVariable<>( l_variable );
328 
329  Assert.assertTrue( l_relocate.mutex() );
330 
331  Assert.assertEquals( l_variable.functor(), "RAM" );
332  Assert.assertEquals( l_relocate.functor(), "RAM" );
333 
334  Assert.assertFalse( l_variable.allocated() );
335  Assert.assertFalse( l_relocate.allocated() );
336 
337  Assert.assertTrue( l_variable.hasVariable() );
338  Assert.assertTrue( l_relocate.hasVariable() );
339 
340 
341  l_relocate.set( "relocated mutex foo" );
342  Assert.assertTrue( l_relocate.allocated() );
343  Assert.assertEquals( l_relocate.raw(), "relocated mutex foo" );
344 
345 
346  l_relocate.relocate();
347  Assert.assertTrue( l_variable.allocated() );
348  Assert.assertEquals( l_variable.raw(), "relocated mutex foo" );
349  }
350 
351 }
IVariable< T > set( @Nullable final T p_value)
sets the value
Definition: CVariable.java:112
final String functor()
returns the functor without path
Definition: CVariable.java:181
boolean allocated()
returns allocated state
final boolean hasVariable()
checks if the literal has variables
Definition: CVariable.java:201
base test class with helpers
Definition: IBaseTest.java:33
IVariable< T > set( @Nullable final T p_value)
sets the value
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
Stream< ITerm > values(final IPath... p_path)
returns a stream over value items
final void variablevalueassignable()
test exception on value asiable
IPath fqnfunctor()
returns the full-qualified functor with path and name
boolean any()
flag to define a "any variable"
boolean emptyValues()
check for empty values
boolean mutex()
flag to check if variable has is concurrency- / thread-safe
String functor()
returns the functor without path
default generic literal class for agent beliefs a literal consists of a functor, an optional list of ...
Definition: CLiteral.java:64
< T > T raw()
cast to any raw value type
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
static ILiteral from( @Nonnull final String p_functor, @Nullable final ITerm... p_values)
factory
Definition: CLiteral.java:161
boolean hasVariable()
checks if the literal has variables
boolean mutex()
flag to check if variable has is concurrency- / thread-safe
Definition: CVariable.java:130
boolean allocated()
returns allocated state
Definition: CVariable.java:118
term structure for simple datatypes
Definition: CRawTerm.java:45
boolean hasAt()
returns if the literal has an @ prefix
public< N > N raw()
cast to any raw value type
Definition: CVariable.java:208