LightJason - AgentSpeak(L++)
CRelational.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 CRelational extends IBaseBinary
46 {
50  private static final long serialVersionUID = -5844609255203908714L;
51 
59  public CRelational( @Nonnull final EOperator p_operator, @Nonnull final IExpression p_lefthandside, @Nonnull final IExpression p_righthandside )
60  {
61  super( p_operator, p_lefthandside, p_righthandside );
62 
63  if ( !m_operator.isRelational() )
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 GREATER:
79  p_return.add( CRawTerm.from(
80  CRelational.compare( l_argument.get( 0 ), l_argument.get( 1 ) ) > 0
81  ) );
82  return CFuzzyValue.from( true );
83 
84  case GREATEREQUAL:
85  p_return.add( CRawTerm.from(
86  CRelational.compare( l_argument.get( 0 ), l_argument.get( 1 ) ) >= 0
87  ) );
88  return CFuzzyValue.from( true );
89 
90  case LESS:
91  p_return.add( CRawTerm.from(
92  CRelational.compare( l_argument.get( 0 ), l_argument.get( 1 ) ) < 0
93  ) );
94  return CFuzzyValue.from( true );
95 
96  case LESSEQUAL:
97  p_return.add( CRawTerm.from(
98  CRelational.compare( l_argument.get( 0 ), l_argument.get( 1 ) ) <= 0
99  ) );
100  return CFuzzyValue.from( true );
101 
102  default:
103  return CFuzzyValue.from( false );
104  }
105  }
106 
114  private static int compare( @Nonnull final ITerm p_left, @Nonnull final ITerm p_right )
115  {
116  return org.lightjason.agentspeak.language.CCommon.rawvalueAssignableTo( p_left.raw(), Number.class )
117  && org.lightjason.agentspeak.language.CCommon.rawvalueAssignableTo( p_right.raw(), Number.class )
118  ? comparenumber( CRelational.map( p_left.raw() ), CRelational.map( p_right.raw() ) )
119  : compareobject( CRelational.map( p_left.raw() ), CRelational.map( p_right.raw() ) );
120  }
121 
131  private static <T extends Number & Comparable<T>> int comparenumber( @Nonnull final T p_left, @Nonnull final T p_right )
132  {
133  return Double.compare( p_left.doubleValue(), p_right.doubleValue() );
134  }
135 
136 
146  private static <T extends Comparable<T>> int compareobject( @Nonnull final T p_left, @Nonnull final T p_right )
147  {
148  return p_left.compareTo( p_right );
149  }
150 
151 
152 
153 
162  @Nonnull
163  @SuppressWarnings( "unchecked" )
164  private static <N, M> N map( @Nonnull final M p_value )
165  {
166  return (N) p_value;
167  }
168 
169 }
static< N, M > N map( @Nonnull final M p_value)
type mapping method
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
CRelational( @Nonnull final EOperator p_operator, @Nonnull final IExpression p_lefthandside, @Nonnull final IExpression p_righthandside)
ctor
common structure for execution definition
final boolean isRelational()
check of a relational operator
Definition: EOperator.java:112
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 int compare( @Nonnull final ITerm p_left, @Nonnull final ITerm p_right)
compares term types
execution context with local data
Definition: IContext.java:42
static< T extends Comparable< T > int compareobject( @Nonnull final T p_left, @Nonnull final T p_right)
compare method for any object type
static< T > boolean rawvalueAssignableTo( @Nonnull final T p_value, @Nonnull final Class<?>... p_class)
checks a term value for assignable class
result for an immutable fuzzy value
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
static< T extends Number &Comparable< T > int comparenumber( @Nonnull final T p_left, @Nonnull final T p_right)
compare method for any number type
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
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