LightJason - AgentSpeak(L++)
CTernaryOperation.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;
25 
33 
34 import javax.annotation.Nonnull;
35 import java.text.MessageFormat;
36 import java.util.Collections;
37 import java.util.LinkedList;
38 import java.util.List;
39 import java.util.Objects;
40 import java.util.stream.Stream;
41 
42 
46 public final class CTernaryOperation extends IBaseExecution<IExpression>
47 {
51  private static final long serialVersionUID = 5518298371186501138L;
55  private final IExecution m_true;
59  private final IExecution m_false;
60 
61 
69  public CTernaryOperation( @Nonnull final IExpression p_expression, @Nonnull final IExecution p_true, @Nonnull final IExecution p_false )
70  {
71  super( p_expression );
72  m_true = p_true;
73  m_false = p_false;
74  }
75 
76  @Nonnull
77  @Override
78  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
79  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
80  {
81  final List<ITerm> l_argument = new LinkedList<>();
82  if ( ( !m_value.execute( p_parallel, p_context, Collections.<ITerm>emptyList(), l_argument ).value() )
83  || ( l_argument.size() != 1 ) )
84  return CFuzzyValue.from( false );
85 
86  return l_argument.get( 0 ).raw()
87  ? m_true.execute( p_parallel, p_context, Collections.<ITerm>emptyList(), p_return )
88  : m_false.execute( p_parallel, p_context, Collections.<ITerm>emptyList(), p_return );
89  }
90 
91  @Override
92  public final int hashCode()
93  {
94  return ( Objects.isNull( m_value ) ? 0 : m_value.hashCode() ) ^ m_true.hashCode() ^ m_false.hashCode();
95  }
96 
97  @Override
98  public final boolean equals( final Object p_object )
99  {
100  return ( p_object instanceof IExecution ) && ( this.hashCode() == p_object.hashCode() );
101  }
102 
103  @Override
104  public final String toString()
105  {
106  return MessageFormat.format( "[ {0} ] ? [ {1} ] : [ {2} ]", m_value, m_true, m_false );
107  }
108 
109  @Nonnull
110  @Override
111  public final Stream<IVariable<?>> variables()
112  {
113  return Stream.concat( m_true.variables(), m_false.variables() );
114  }
115 }
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
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
execution context with local data
Definition: IContext.java:42
result for an immutable fuzzy value
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)
CTernaryOperation( @Nonnull final IExpression p_expression, @Nonnull final IExecution p_true, @Nonnull final IExecution p_false)
ctor