CDeconstruct.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.ILiteral;
  26. import org.lightjason.agentspeak.language.ITerm;
  27. import org.lightjason.agentspeak.language.execution.IContext;
  28. import org.lightjason.agentspeak.language.execution.IExecution;
  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 java.text.MessageFormat;
  34. import java.util.List;
  35. import java.util.Objects;
  36. import java.util.stream.Collectors;
  37. import java.util.stream.Stream;


  38. /**
  39.  * deconstruct assignment
  40.  */
  41. public final class CDeconstruct<M extends ITerm> extends IBaseExecution<List<IVariable<?>>>
  42. {
  43.     /**
  44.      * serial id
  45.      */
  46.     private static final long serialVersionUID = 6234582899813921890L;
  47.     /**
  48.      * right-hand argument (literal)
  49.      */
  50.     private final M m_righthand;

  51.     /**
  52.      * ctor
  53.      *
  54.      * @param p_lefthand left-hand variable list
  55.      * @param p_righthand right-hand argument
  56.      */
  57.     @Nonnull
  58.     public CDeconstruct( @Nonnull final List<IVariable<?>> p_lefthand, @Nonnull final M p_righthand )
  59.     {
  60.         super( p_lefthand );
  61.         m_righthand = p_righthand;
  62.     }

  63.     @Nonnull
  64.     @Override
  65.     public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
  66.                                                @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
  67.     {
  68.         this.set( CCommon.replaceFromContext( p_context, m_value ), CCommon.replaceFromContext( p_context, m_righthand ).raw() );
  69.         return CFuzzyValue.from( true );
  70.     }

  71.     @Override
  72.     public final int hashCode()
  73.     {
  74.         return Objects.isNull( m_value ) ? 0 : m_value.hashCode() ^ m_righthand.hashCode();
  75.     }

  76.     @Override
  77.     public final boolean equals( final Object p_object )
  78.     {
  79.         return ( p_object instanceof IExecution ) && ( this.hashCode() == p_object.hashCode() );
  80.     }

  81.     @Override
  82.     public final String toString()
  83.     {
  84.         return MessageFormat.format( "{0} =.. {1}", m_value, m_righthand );
  85.     }

  86.     @Nonnull
  87.     @Override
  88.     public final Stream<IVariable<?>> variables()
  89.     {
  90.         return Objects.isNull( m_value ) ? Stream.empty() : m_value.stream();
  91.     }

  92.     /**
  93.      * sets the values
  94.      *
  95.      * @param p_assignment variable list
  96.      * @param p_term term
  97.      */
  98.     private void set( @Nonnull final List<ITerm> p_assignment, @Nonnull final ILiteral p_term )
  99.     {
  100.         if ( p_assignment.size() >= 1 )
  101.             p_assignment.get( 0 ).<IVariable<Object>>term().set( p_term.fqnfunctor().toString() );
  102.         if ( p_assignment.size() >= 2 )
  103.             p_assignment.get( 1 ).<IVariable<Object>>term().set( p_term.values().collect( Collectors.toList() ) );
  104.     }
  105. }