LightJason - AgentSpeak(L++)
CAdditive.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.numerical;
25 
36 
37 import javax.annotation.Nonnull;
38 import java.util.LinkedList;
39 import java.util.List;
40 
41 
45 public final class CAdditive extends IBaseBinary
46 {
50  private static final long serialVersionUID = 220138218890736772L;
51 
59  public CAdditive( final EOperator p_operator, final IExpression p_lefthandside,
60  final IExpression p_righthandside
61  )
62  {
63  super( p_operator, p_lefthandside, p_righthandside );
64  if ( !m_operator.isAdditive() )
65  throw new CIllegalArgumentException( CCommon.languagestring( this, "operator", m_operator ) );
66  }
67 
68  @Nonnull
69  @Override
70  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
71  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
72  {
73  final List<ITerm> l_argument = new LinkedList<>();
74  if ( !this.executearguments( p_parallel, p_context, l_argument ) )
75  return CFuzzyValue.from( false );
76 
77 
78  switch ( m_operator )
79  {
80  case PLUS:
81  p_return.add( CRawTerm.from( this.add(
82  l_argument.get( 0 ).<Number>raw(),
83  l_argument.get( 1 ).<Number>raw()
84  ) ) );
85  return CFuzzyValue.from( true );
86 
87  case MINUS:
88  p_return.add( CRawTerm.from( this.subtract(
89  l_argument.get( 0 ).<Number>raw(),
90  l_argument.get( 1 ).<Number>raw()
91  ) ) );
92  return CFuzzyValue.from( true );
93 
94  default:
95  return CFuzzyValue.from( false );
96  }
97 
98  }
99 
110  @Nonnull
111  private <N extends Number, M extends Number> Number add( @Nonnull final N p_left, @Nonnull final M p_right )
112  {
113  return p_left.doubleValue() + p_right.doubleValue();
114  }
115 
126  @Nonnull
127  private <N extends Number, M extends Number> Number subtract( @Nonnull final N p_left, @Nonnull final M p_right )
128  {
129  return p_left.doubleValue() - p_right.doubleValue();
130  }
131 
132 }
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
Definition: CAdditive.java:70
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
static< T > String languagestring(final T p_source, final String p_label, final Object... p_parameter)
returns the language depend string on any object
final boolean isAdditive()
check of a additive operator
Definition: EOperator.java:122
execution context with local data
Definition: IContext.java:42
private< N extends Number, M extends Number > Number subtract( @Nonnull final N p_left, @Nonnull final M p_right)
runs the subtraction of number types
Definition: CAdditive.java:127
result for an immutable fuzzy value
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
final boolean executearguments(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument)
execute expression arguments
private< N extends Number, M extends Number > Number add( @Nonnull final N p_left, @Nonnull final M p_right)
runs the addition of number types
Definition: CAdditive.java:111
term structure for simple datatypes
Definition: CRawTerm.java:45
CAdditive(final EOperator p_operator, final IExpression p_lefthandside, final IExpression p_righthandside)
ctor
Definition: CAdditive.java:59