CRawAction.java

  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. package org.lightjason.agentspeak.language.execution.action;

  24. import org.lightjason.agentspeak.language.CCommon;
  25. import org.lightjason.agentspeak.language.CRawTerm;
  26. import org.lightjason.agentspeak.language.ITerm;
  27. import org.lightjason.agentspeak.language.execution.IContext;
  28. import org.lightjason.agentspeak.language.execution.expression.IExpression;
  29. import org.lightjason.agentspeak.language.fuzzy.CFuzzyValue;
  30. import org.lightjason.agentspeak.language.fuzzy.IFuzzyValue;
  31. import org.lightjason.agentspeak.language.variable.IVariable;

  32. import javax.annotation.Nonnull;
  33. import javax.annotation.Nullable;
  34. import java.util.LinkedList;
  35. import java.util.List;
  36. import java.util.Objects;
  37. import java.util.stream.Stream;


  38. /**
  39.  * encapsulate class for any non-executable data type e.g. boolean
  40.  * and caching of execution results
  41.  */
  42. public final class CRawAction<T> extends IBaseExecution<T>
  43. {
  44.     /**
  45.      * serial id
  46.      */
  47.     private static final long serialVersionUID = 7181432659241612718L;

  48.     /**
  49.      * ctor
  50.      *
  51.      * @param p_data any object data
  52.      */
  53.     public CRawAction( @Nullable final T p_data )
  54.     {
  55.         super( p_data );
  56.     }

  57.     @Nonnull
  58.     @Override
  59.     @SuppressWarnings( "unchecked" )
  60.     public final Stream<IVariable<?>> variables()
  61.     {
  62.         if ( m_value instanceof IVariable<?> )
  63.             return Stream.of( (IVariable<?>) m_value );

  64.         if ( m_value instanceof IExpression )
  65.             return ( (IExpression) m_value ).variables();

  66.         return super.variables();
  67.     }

  68.     @Override
  69.     public final int hashCode()
  70.     {
  71.         return Objects.isNull( m_value ) ? 0 : m_value.hashCode();
  72.     }

  73.     @Override
  74.     public final boolean equals( final Object p_object )
  75.     {
  76.         return ( p_object instanceof IBaseExecution<?> ) && ( this.hashCode() == p_object.hashCode() );
  77.     }

  78.     @Override
  79.     public final String toString()
  80.     {
  81.         return Objects.isNull( m_value ) ? "" : m_value.toString();
  82.     }

  83.     @Nonnull
  84.     @Override
  85.     @SuppressWarnings( "unchecked" )
  86.     public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
  87.                                                @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
  88.     {
  89.         if ( m_value instanceof Boolean )
  90.             return this.getTypedResult( (Boolean) m_value, p_return );
  91.         if ( m_value instanceof IVariable<?> )
  92.             return this.getTypedResult( (IVariable<?>) m_value, p_context, p_return );
  93.         if ( m_value instanceof IExpression )
  94.             return this.getTypedResult( (IExpression) m_value, p_context, p_parallel, p_argument, p_return );

  95.         return this.getTypedResult( m_value, p_return );
  96.     }

  97.     /**
  98.      * fixed type result
  99.      *
  100.      * @param p_execution boolean value
  101.      * @param p_return native return
  102.      * @return fuzzy-boolean
  103.      */
  104.     @Nonnull
  105.     private IFuzzyValue<Boolean> getTypedResult( @Nonnull final Boolean p_execution, @Nonnull final List<ITerm> p_return )
  106.     {
  107.         p_return.add( CRawTerm.from( p_execution ) );
  108.         return CFuzzyValue.from( p_execution );
  109.     }

  110.     /**
  111.      * fixed type result
  112.      *
  113.      * @param p_execution variable value
  114.      * @param p_context context
  115.      * @param p_return native return
  116.      * @return fuzzy-boolean
  117.      */
  118.     @Nonnull
  119.     private IFuzzyValue<Boolean> getTypedResult( @Nonnull final IVariable<?> p_execution, @Nonnull final IContext p_context,
  120.                                                  @Nonnull final List<ITerm> p_return )
  121.     {
  122.         final IVariable<?> l_value = (IVariable<?>) CCommon.replaceFromContext( p_context, p_execution );

  123.         if ( !l_value.allocated() )
  124.             return CFuzzyValue.from( false );

  125.         if ( l_value.valueassignableto( Boolean.class ) )
  126.             return CFuzzyValue.from( l_value.raw() );

  127.         p_return.add( CRawTerm.from( l_value.raw() ) );
  128.         return CFuzzyValue.from( true );
  129.     }


  130.     /**
  131.      * fixed type result
  132.      *
  133.      * @param p_execution execution element
  134.      * @param p_context context
  135.      * @param p_parallel paralle execution
  136.      * @param p_argument arguments
  137.      * @param p_return native return
  138.      * @return fuzzy-boolean
  139.      */
  140.     @Nonnull
  141.     private IFuzzyValue<Boolean> getTypedResult( @Nonnull final IExpression p_execution, @Nonnull final IContext p_context,
  142.                                                  @Nonnull final Boolean p_parallel, @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
  143.     )
  144.     {
  145.         final List<ITerm> l_return = new LinkedList<>();
  146.         if ( ( !p_execution.execute( p_parallel, p_context, p_argument, l_return ).value() ) || ( l_return.isEmpty() ) )
  147.             return CFuzzyValue.from( false );

  148.         return CFuzzyValue.from( l_return.get( 0 ).<Boolean>raw() );
  149.     }



  150.     /**
  151.      * fixed type result
  152.      *
  153.      * @param p_execution any other value
  154.      * @param p_return native return
  155.      * @return fuzzy-boolean
  156.      */
  157.     @Nonnull
  158.     private IFuzzyValue<Boolean> getTypedResult( @Nullable final T p_execution, @Nonnull final List<ITerm> p_return )
  159.     {
  160.         p_return.add( CRawTerm.from( p_execution ) );
  161.         return CFuzzyValue.from( true );
  162.     }
  163. }