LightJason - AgentSpeak(L++)
CVariableUnify.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.execution.action.unify;
25 
33 
34 import javax.annotation.Nonnull;
35 import java.text.MessageFormat;
36 import java.util.List;
37 import java.util.Set;
38 import java.util.stream.Stream;
39 
40 
44 public final class CVariableUnify extends CDefaultUnify
45 {
49  private static final long serialVersionUID = -8392150596878840771L;
53  private final IVariable<?> m_variable;
54 
62  public CVariableUnify( final boolean p_parallel, @Nonnull final ILiteral p_literal, @Nonnull final IVariable<?> p_variable )
63  {
64  super( p_parallel, p_literal );
65  m_variable = p_variable;
66  }
67 
68 
69  @Override
70  public final String toString()
71  {
72  return MessageFormat.format( "{0}>>({1}, {2})", m_parallel ? "@" : "", m_value, m_variable );
73  }
74 
75  @Nonnull
76  @Override
77  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
78  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
79  )
80  {
81  final Set<IVariable<?>> l_variables = p_context.agent()
82  .unifier()
83  .unify(
84  CCommon.replaceFromContext( p_context, m_variable ).raw(),
85  CCommon.replaceFromContext( p_context, m_value ).raw()
86  );
87 
88  if ( l_variables.size() != m_variablenumber )
89  return CFuzzyValue.from( false );
90 
91  CCommon.updatecontext( p_context, l_variables.stream() );
92  return CFuzzyValue.from( true );
93  }
94 
95  @Nonnull
96  @Override
97  public final Stream<IVariable<?>> variables()
98  {
99  return Stream.concat(
100  Stream.of( m_variable ),
101  CVariableUnify.super.variables()
102  );
103  }
104 }
final IVariable<?> m_variable
unification variable with literal
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
common structure for execution definition
execution context with local data
Definition: IContext.java:42
static List< ITerm > replaceFromContext( @Nonnull final IContext p_context, @Nonnull final Collection<? extends ITerm > p_terms)
replace variables with context variables
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
static IContext updatecontext( @Nonnull final IContext p_context, @Nonnull final Stream< IVariable<?>> p_unifiedvariables)
updates within an instance context all variables of the stream
result for an immutable fuzzy value
final Stream< IVariable<?> > variables()
returns a stream with all used variables
CVariableUnify(final boolean p_parallel, @Nonnull final ILiteral p_literal, @Nonnull final IVariable<?> p_variable)
ctor