LightJason - AgentSpeak(L++)
IBaseBinary.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.expression;
25 
31 
32 import javax.annotation.Nonnull;
33 import java.text.MessageFormat;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.stream.Stream;
37 
38 
42 public abstract class IBaseBinary implements IBinaryExpression
43 {
47  private static final long serialVersionUID = 1630029294068548691L;
51  protected final EOperator m_operator;
55  protected final IExpression m_lefthandside;
59  protected final IExpression m_righthandside;
60 
61 
62 
70  protected IBaseBinary( @Nonnull final EOperator p_operator, @Nonnull final IExpression p_lefthandside, @Nonnull final IExpression p_righthandside )
71  {
72  if ( !p_operator.isBinary() )
73  throw new CIllegalArgumentException( CCommon.languagestring( IBaseBinary.class, "operator", p_operator ) );
74 
75  m_operator = p_operator;
76  m_lefthandside = p_lefthandside;
77  m_righthandside = p_righthandside;
78  }
79 
89  protected final boolean executearguments( final boolean p_parallel, @Nonnull final IContext p_context,
90  @Nonnull final List<ITerm> p_argument
91  ) throws IllegalArgumentException
92  {
93  // run left-hand- and right-hand-side argument
94  if ( ( !m_lefthandside.execute( p_parallel, p_context, Collections.<ITerm>emptyList(), p_argument ).value() )
95  || ( p_argument.isEmpty() ) )
96  return false;
97 
98  if ( ( !m_righthandside.execute( p_parallel, p_context, Collections.<ITerm>emptyList(), p_argument ).value() )
99  || ( p_argument.isEmpty() ) )
100  return false;
101 
102  if ( p_argument.size() != 2 )
103  throw new CIllegalArgumentException( CCommon.languagestring( IBaseBinary.class, "argumentnumber" ) );
104 
105  return true;
106  }
107 
108 
109  @Nonnull
110  @Override
111  public final IExpression leftHandSide()
112  {
113  return m_lefthandside;
114  }
115 
116  @Nonnull
117  @Override
118  public final IExpression rightHandSide()
119  {
120  return m_righthandside;
121  }
122 
123  @Nonnull
124  @Override
125  public final EOperator operator()
126  {
127  return m_operator;
128  }
129 
130  @Override
131  public final int hashCode()
132  {
133  return m_lefthandside.hashCode() ^ m_righthandside.hashCode() ^ m_operator.hashCode();
134  }
135 
136  @Override
137  public final boolean equals( final Object p_object )
138  {
139  return ( p_object instanceof IExpression ) && ( this.hashCode() == p_object.hashCode() );
140  }
141 
142  @Override
143  public final String toString()
144  {
145  return MessageFormat.format( "{0} {1} {2}", m_lefthandside, m_operator, m_righthandside );
146  }
147 
148  @Nonnull
149  @Override
150  public final Stream<IVariable<?>> variables()
151  {
152  return Stream.concat( m_lefthandside.variables(), m_righthandside.variables() );
153  }
154 }
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
Stream< IVariable<?> > variables()
returns a stream with all used variables
final IExpression leftHandSide()
returns the left-hand expression side
static< T > String languagestring(final T p_source, final String p_label, final Object... p_parameter)
returns the language depend string on any object
IBaseBinary( @Nonnull final EOperator p_operator, @Nonnull final IExpression p_lefthandside, @Nonnull final IExpression p_righthandside)
ctor
execution context with local data
Definition: IContext.java:42
final IExpression rightHandSide()
returns the right-hand expression side
final IExpression m_righthandside
right-hand-side argument
final EOperator operator()
return expression operator
final Stream< IVariable<?> > variables()
returns a stream with all used variables
final IExpression m_lefthandside
left-hand-side argument
final boolean executearguments(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument)
execute expression arguments