IAchievementRule.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.achievement_test;

  24. import org.apache.commons.lang3.tuple.ImmutableTriple;
  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.execution.action.IBaseExecution;
  30. import org.lightjason.agentspeak.language.fuzzy.CFuzzyValue;
  31. import org.lightjason.agentspeak.language.fuzzy.IFuzzyValue;
  32. import org.lightjason.agentspeak.language.instantiable.rule.IRule;
  33. import org.lightjason.agentspeak.language.variable.IRelocateVariable;
  34. import org.lightjason.agentspeak.language.variable.IVariable;

  35. import javax.annotation.Nonnull;
  36. import java.util.Collection;
  37. import java.util.Collections;
  38. import java.util.Objects;
  39. import java.util.Set;


  40. /**
  41.  * abstract class for execute a logical-rule
  42.  */
  43. abstract class IAchievementRule<T extends ITerm> extends IBaseExecution<T>
  44. {
  45.     /**
  46.      * serial id
  47.      */
  48.     private static final long serialVersionUID = -315973892409409832L;

  49.     /**
  50.      * ctor
  51.      *
  52.      * @param p_type value of the achievment-goal
  53.      */
  54.     protected IAchievementRule( @Nonnull final T p_type )
  55.     {
  56.         super( p_type );
  57.     }

  58.     /**
  59.      * execute rule from context
  60.      *
  61.      * @param p_parallel parallel execution
  62.      * @param p_context execution context
  63.      * @param p_value execution literal
  64.      * @return boolean result
  65.      */
  66.     @Nonnull
  67.     @SuppressWarnings( "unchecked" )
  68.     protected static IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final ILiteral p_value
  69.     )
  70.     {
  71.         // read current rules, if not exists execution fails
  72.         final Collection<IRule> l_rules = p_context.agent().rules().get( p_value.fqnfunctor() );
  73.         if ( Objects.isNull( l_rules ) )
  74.             return CFuzzyValue.from( false );

  75.         // first step is the unification of the caller literal, so variables will be set from the current execution context
  76.         final ILiteral l_unified = p_value.allocate( p_context );

  77.         // second step execute backtracking rules sequential / parallel
  78.         return (
  79.             p_parallel
  80.             ? l_rules.parallelStream()
  81.             : l_rules.stream()
  82.         ).map( i ->
  83.         {

  84.             // instantiate variables by unification of the rule literal
  85.             final Set<IVariable<?>> l_variables = p_context.agent().unifier().unify( l_unified, i.identifier() );

  86.             // execute rule
  87.             final IFuzzyValue<Boolean> l_return = i.execute(
  88.                 false, i.instantiate( p_context.agent(), l_variables.stream() ),
  89.                 Collections.<ITerm>emptyList(),
  90.                 Collections.<ITerm>emptyList()
  91.             );

  92.             // create rule result with fuzzy- and defuzzificated value and instantiate variable set
  93.             return new ImmutableTriple<>( p_context.agent().fuzzy().getValue().defuzzify( l_return ), l_return, l_variables );

  94.         } )

  95.          // find successfully ended rule
  96.          .filter( ImmutableTriple::getLeft )
  97.          .findFirst()

  98.          // realocate rule instantiated variables back to execution context
  99.          .map( i ->
  100.          {

  101.              i.getRight().parallelStream()
  102.               .filter( j -> j instanceof IRelocateVariable )
  103.               .forEach( j -> ( (IRelocateVariable) j ).relocate() );

  104.              return i.getMiddle();

  105.          } )

  106.          // otherwise rule fails (default behaviour)
  107.          .orElse( CFuzzyValue.from( false ) );
  108.     }

  109.     @Override
  110.     public final int hashCode()
  111.     {
  112.         return Objects.isNull( m_value ) ? 0 : m_value.hashCode();
  113.     }

  114.     @Override
  115.     public final boolean equals( final Object p_object )
  116.     {
  117.         return ( p_object instanceof IExecution ) && ( this.hashCode() == p_object.hashCode() );
  118.     }

  119. }