LightJason - AgentSpeak(L++)
CRawAction.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 
34 
35 import javax.annotation.Nonnull;
36 import javax.annotation.Nullable;
37 import java.util.LinkedList;
38 import java.util.List;
39 import java.util.Objects;
40 import java.util.stream.Stream;
41 
42 
47 public final class CRawAction<T> extends IBaseExecution<T>
48 {
52  private static final long serialVersionUID = 7181432659241612718L;
53 
59  public CRawAction( @Nullable final T p_data )
60  {
61  super( p_data );
62  }
63 
64  @Nonnull
65  @Override
66  @SuppressWarnings( "unchecked" )
67  public final Stream<IVariable<?>> variables()
68  {
69  if ( m_value instanceof IVariable<?> )
70  return Stream.of( (IVariable<?>) m_value );
71 
72  if ( m_value instanceof IExpression )
73  return ( (IExpression) m_value ).variables();
74 
75  return super.variables();
76  }
77 
78  @Override
79  public final int hashCode()
80  {
81  return Objects.isNull( m_value ) ? 0 : m_value.hashCode();
82  }
83 
84  @Override
85  public final boolean equals( final Object p_object )
86  {
87  return ( p_object instanceof IBaseExecution<?> ) && ( this.hashCode() == p_object.hashCode() );
88  }
89 
90  @Override
91  public final String toString()
92  {
93  return Objects.isNull( m_value ) ? "" : m_value.toString();
94  }
95 
96  @Nonnull
97  @Override
98  @SuppressWarnings( "unchecked" )
99  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
100  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
101  {
102  if ( m_value instanceof Boolean )
103  return this.getTypedResult( (Boolean) m_value, p_return );
104  if ( m_value instanceof IVariable<?> )
105  return this.getTypedResult( (IVariable<?>) m_value, p_context, p_return );
106  if ( m_value instanceof IExpression )
107  return this.getTypedResult( (IExpression) m_value, p_context, p_parallel, p_argument, p_return );
108 
109  return this.getTypedResult( m_value, p_return );
110  }
111 
119  @Nonnull
120  private IFuzzyValue<Boolean> getTypedResult( @Nonnull final Boolean p_execution, @Nonnull final List<ITerm> p_return )
121  {
122  p_return.add( CRawTerm.from( p_execution ) );
123  return CFuzzyValue.from( p_execution );
124  }
125 
134  @Nonnull
135  private IFuzzyValue<Boolean> getTypedResult( @Nonnull final IVariable<?> p_execution, @Nonnull final IContext p_context,
136  @Nonnull final List<ITerm> p_return )
137  {
138  final IVariable<?> l_value = (IVariable<?>) CCommon.replaceFromContext( p_context, p_execution );
139 
140  if ( !l_value.allocated() )
141  return CFuzzyValue.from( false );
142 
143  if ( l_value.valueassignableto( Boolean.class ) )
144  return CFuzzyValue.from( l_value.raw() );
145 
146  p_return.add( CRawTerm.from( l_value.raw() ) );
147  return CFuzzyValue.from( true );
148  }
149 
150 
161  @Nonnull
162  private IFuzzyValue<Boolean> getTypedResult( @Nonnull final IExpression p_execution, @Nonnull final IContext p_context,
163  @Nonnull final Boolean p_parallel, @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
164  )
165  {
166  final List<ITerm> l_return = new LinkedList<>();
167  if ( ( !p_execution.execute( p_parallel, p_context, p_argument, l_return ).value() ) || ( l_return.isEmpty() ) )
168  return CFuzzyValue.from( false );
169 
170  return CFuzzyValue.from( l_return.get( 0 ).<Boolean>raw() );
171  }
172 
173 
174 
182  @Nonnull
183  private IFuzzyValue<Boolean> getTypedResult( @Nullable final T p_execution, @Nonnull final List<ITerm> p_return )
184  {
185  p_return.add( CRawTerm.from( p_execution ) );
186  return CFuzzyValue.from( true );
187  }
188 }
final Stream< IVariable<?> > variables()
returns a stream with all used variables
Definition: CRawAction.java:67
IFuzzyValue< Boolean > getTypedResult( @Nullable final T p_execution, @Nonnull final List< ITerm > p_return)
fixed type result
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
IFuzzyValue< Boolean > getTypedResult( @Nonnull final Boolean p_execution, @Nonnull final List< ITerm > p_return)
fixed type result
common structure for execution definition
encapsulate class for any non-executable data type e.g.
Definition: CRawAction.java:47
IFuzzyValue< Boolean > getTypedResult( @Nonnull final IVariable<?> p_execution, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_return)
fixed type result
execution context with local data
Definition: IContext.java:42
IFuzzyValue< Boolean > getTypedResult( @Nonnull final IExpression p_execution, @Nonnull final IContext p_context, @Nonnull final Boolean p_parallel, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
fixed type result
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)
defines a plan-body operation
Definition: CRawAction.java:99
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
term structure for simple datatypes
Definition: CRawTerm.java:45