LightJason - AgentSpeak(L++)
CDefaultUnify.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 
35 
36 import javax.annotation.Nonnull;
37 import java.text.MessageFormat;
38 import java.util.List;
39 import java.util.Map;
40 import java.util.stream.Stream;
41 
42 
48 public class CDefaultUnify extends IBaseExecution<ILiteral>
49 {
53  private static final long serialVersionUID = -2858968840990978860L;
57  protected final boolean m_parallel;
61  protected final long m_variablenumber;
62 
69  public CDefaultUnify( final boolean p_parallel, @Nonnull final ILiteral p_literal )
70  {
71  super( p_literal );
72  m_parallel = p_parallel;
73 
74  // check unique variables - get variable frequency especially any variable "_"
75  final Map<IVariable<?>, Integer> l_frequency = CCommon.variablefrequency( p_literal );
76  if ( l_frequency.isEmpty() )
78 
79  if ( l_frequency.entrySet().stream().filter( i -> !i.getKey().any() ).anyMatch( i -> i.getValue() > 1 ) )
80  throw new CIllegalArgumentException( org.lightjason.agentspeak.common.CCommon.languagestring( this, "uniquevariable" ) );
81 
82  // count variables
83  m_variablenumber = l_frequency.size();
84  }
85 
86  @Override
87  public String toString()
88  {
89  return MessageFormat.format( "{0}>>{1}", m_parallel ? "@" : "", m_value );
90  }
91 
92  @Nonnull
93  @Override
94  public IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
95  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
96  )
97  {
98  return p_context.agent().unifier().unify(
99  p_context,
100  m_value,
101  m_variablenumber,
103  m_parallel
104  );
105  }
106 
107  @Nonnull
108  @Override
109  public Stream<IVariable<?>> variables()
110  {
111  return CCommon.flattenrecursive( m_value.values() ).filter( i -> i instanceof IVariable<?> ).map( ITerm::term );
112  }
113 
114 }
IExpression EMPTY
empty expression, is always true
common structure for execution definition
static< T > String languagestring(final T p_source, final String p_label, final Object... p_parameter)
returns the language depend string on any object
static Stream< ITerm > flattenrecursive( @Nonnull final Stream< ITerm > p_input)
recursive stream of term values
Stream< IVariable<?> > variables()
returns a stream with all used variables
execution context with local data
Definition: IContext.java:42
static Map< IVariable<?>, Integer > variablefrequency( @Nonnull final ILiteral p_literal)
consts the variables within a literal
default< T extends ITerm > T term()
casts the object to a term-type
Definition: ITerm.java:158
CDefaultUnify(final boolean p_parallel, @Nonnull final ILiteral p_literal)
ctor
result for an immutable fuzzy value
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