LightJason - AgentSpeak(L++)
CVariableEvaluate.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.variable;
25 
33 
34 import javax.annotation.Nonnull;
35 import java.text.MessageFormat;
36 import java.util.Collections;
37 import java.util.List;
38 import java.util.stream.Stream;
39 
40 
44 public final class CVariableEvaluate implements IVariableEvaluate
45 {
49  private static final long serialVersionUID = 7310663182659231951L;
53  private final IVariable<?> m_variable;
57  private final List<ITerm> m_parameter;
58 
64  public CVariableEvaluate( @Nonnull final IVariable<?> p_variable )
65  {
66  this( p_variable, Collections.<ITerm>emptyList() );
67  }
68 
75  public CVariableEvaluate( @Nonnull final IVariable<?> p_variable, @Nonnull final List<ITerm> p_parameter )
76  {
77  m_variable = p_variable;
78  m_parameter = Collections.unmodifiableList( p_parameter );
79  }
80 
81 
82  @Override
83  public final boolean mutex()
84  {
85  return m_variable.mutex();
86  }
87 
88  @Nonnull
89  @Override
90  public final ILiteral evaluate( final IContext p_context )
91  {
92  final IVariable<?> l_variable = CCommon.replaceFromContext( p_context, m_variable ).term();
93  if ( !l_variable.allocated() )
94  throw new CIllegalStateException();
95 
96  // if variable is a string
97  if ( l_variable.valueassignableto( String.class ) )
98  return this.fromString( l_variable.raw(), p_context );
99 
100  if ( m_variable.valueassignableto( ILiteral.class ) )
101  return this.fromLiteral( l_variable.raw(), p_context );
102 
103  throw new CIllegalStateException();
104  }
105 
106  @Nonnull
107  @Override
108  public Stream<IVariable<?>> variables()
109  {
110  return Stream.concat(
111  Stream.of( m_variable ),
112  m_parameter.parallelStream()
113  .filter( i -> i instanceof IVariable<?> )
114  .map( ITerm::term )
115  );
116  }
117 
118 
119  @Override
120  public final int hashCode()
121  {
122  return m_variable.hashCode();
123  }
124 
125  @Override
126  public final boolean equals( final Object p_object )
127  {
128  return ( p_object instanceof IVariableEvaluate ) && ( this.hashCode() == p_object.hashCode() );
129  }
130 
131  @Override
132  public final String toString()
133  {
134  return MessageFormat.format( "{0}{1}", m_variable, m_parameter.isEmpty() ? "" : m_parameter );
135  }
136 
137  @Nonnull
138  @Override
139  public final String functor()
140  {
141  return m_variable.functor();
142  }
143 
144  @Nonnull
145  @Override
146  public final IPath functorpath()
147  {
148  return m_variable.functorpath();
149  }
150 
151  @Nonnull
152  @Override
153  public final IPath fqnfunctor()
154  {
155  return m_variable.fqnfunctor();
156  }
157 
158  @Override
159  public final boolean hasVariable()
160  {
161  return true;
162  }
163 
164  @Override
165  public final <T> T raw()
166  {
167  return m_variable.raw();
168  }
169 
170  @Nonnull
171  @Override
172  public final ITerm deepcopy( final IPath... p_prefix )
173  {
174  return m_variable.deepcopy( p_prefix );
175  }
176 
177  @Nonnull
178  @Override
179  public final ITerm deepcopysuffix()
180  {
181  return m_variable.deepcopysuffix();
182  }
183 
184  @Override
185  public final int structurehash()
186  {
187  return 0;
188  }
189 
197  private ILiteral fromString( final String p_value, final IContext p_context )
198  {
199  return CLiteral.from( p_value, m_parameter ).unify( p_context );
200  }
201 
209  private ILiteral fromLiteral( final ILiteral p_literal, final IContext p_context )
210  {
211  return m_parameter.isEmpty()
212  ? p_literal.unify( p_context )
213  : new CLiteral(
214  p_literal.hasAt(),
215  p_literal.negated(),
216  p_literal.fqnfunctor(),
218  ).unify( p_context );
219  }
220 
221 }
Stream< IVariable<?> > variables()
returns a stream with all used variables
final List< ITerm > m_parameter
optional parameter list
CVariableEvaluate( @Nonnull final IVariable<?> p_variable, @Nonnull final List< ITerm > p_parameter)
ctor
ILiteral fromLiteral(final ILiteral p_literal, final IContext p_context)
creates the result literal from an input literal
ILiteral fromString(final String p_value, final IContext p_context)
creates the result literal from an input string
common structure for execution definition
ILiteral unify( @Nonnull final IContext p_context)
unifies variables if exists
IPath fqnfunctor()
returns the full-qualified functor with path and name
final String functor()
returns the functor without path
execution context with local data
Definition: IContext.java:42
final boolean hasVariable()
checks if the literal has variables
final int structurehash()
returns a hash value which defines a hash ove rthe structure
final ILiteral evaluate(final IContext p_context)
evaluates the content of the variable
final IPath functorpath()
returns the path of the functor
boolean mutex()
flag to check if variable has is concurrency- / thread-safe
default< T extends ITerm > T term()
casts the object to a term-type
Definition: ITerm.java:158
boolean negated()
getter of the literal for the negation
default generic literal class for agent beliefs a literal consists of a functor, an optional list of ...
Definition: CLiteral.java:64
final IVariable<?> m_variable
content variable with a string or literal
final IPath fqnfunctor()
returns the full-qualified functor with path and name
CVariableEvaluate( @Nonnull final IVariable<?> p_variable)
ctor
static ILiteral from( @Nonnull final String p_functor, @Nullable final ITerm... p_values)
factory
Definition: CLiteral.java:161
boolean hasAt()
returns if the literal has an @ prefix