LightJason - AgentSpeak(L++)
CMultiplicative.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 CMultiplicative extends IBaseBinary
46 {
50  private static final long serialVersionUID = 3046373876617720672L;
51 
59  public CMultiplicative( @Nonnull final EOperator p_operator, @Nonnull final IExpression p_lefthandside, @Nonnull final IExpression p_righthandside
60  )
61  {
62  super( p_operator, p_lefthandside, p_righthandside );
64  throw new CIllegalArgumentException( CCommon.languagestring( this, "operator", m_operator ) );
65  }
66 
67  @Nonnull
68  @Override
69  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
70  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
71  {
72  final List<ITerm> l_argument = new LinkedList<>();
73  if ( !this.executearguments( p_parallel, p_context, l_argument ) )
74  return CFuzzyValue.from( false );
75 
76  switch ( m_operator )
77  {
78  case MULTIPLY:
79  p_return.add( CRawTerm.from( this.multiply(
80  l_argument.get( 0 ).<Number>raw(),
81  l_argument.get( 1 ).<Number>raw()
82  ) ) );
83  return CFuzzyValue.from( true );
84 
85  case DIVIDE:
86  p_return.add( CRawTerm.from( this.divide(
87  l_argument.get( 0 ).<Number>raw(),
88  l_argument.get( 1 ).<Number>raw()
89  ) ) );
90  return CFuzzyValue.from( true );
91 
92  case MODULO:
93  p_return.add( CRawTerm.from( this.modulo(
94  l_argument.get( 0 ).<Number>raw(),
95  l_argument.get( 1 ).<Number>raw()
96  ) ) );
97  return CFuzzyValue.from( true );
98 
99  default:
100  return CFuzzyValue.from( false );
101  }
102 
103  }
104 
115  @Nonnull
116  private <N extends Number, M extends Number> Number multiply( @Nonnull final N p_left, @Nonnull final M p_right )
117  {
118  return p_left.doubleValue() * p_right.doubleValue();
119  }
120 
131  @Nonnull
132  private <N extends Number, M extends Number> Number divide( @Nonnull final N p_left, @Nonnull final M p_right )
133  {
134  return p_left.doubleValue() / p_right.doubleValue();
135  }
136 
147  @Nonnull
148  private <N extends Number, M extends Number> Number modulo( @Nonnull final N p_left, @Nonnull final M p_right )
149  {
150  return p_left.longValue() % p_right.longValue();
151  }
152 
153 }
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
CMultiplicative( @Nonnull final EOperator p_operator, @Nonnull final IExpression p_lefthandside, @Nonnull final IExpression p_righthandside)
ctor
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
private< N extends Number, M extends Number > Number multiply( @Nonnull final N p_left, @Nonnull final M p_right)
runs the multiply of number types
static< T > String languagestring(final T p_source, final String p_label, final Object... p_parameter)
returns the language depend string on any object
execution context with local data
Definition: IContext.java:42
final boolean isMultiplicative()
check of a multiplicative operator
Definition: EOperator.java:132
result for an immutable fuzzy value
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
private< N extends Number, M extends Number > Number divide( @Nonnull final N p_left, @Nonnull final M p_right)
runs the divide of number types
private< N extends Number, M extends Number > Number modulo( @Nonnull final N p_left, @Nonnull final M p_right)
runs the modulo of number types
final boolean executearguments(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument)
execute expression arguments
term structure for simple datatypes
Definition: CRawTerm.java:45