CRulePlaceholder.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.instantiable.rule;

  24. import com.google.common.collect.Multimap;
  25. import org.lightjason.agentspeak.agent.IAgent;
  26. import org.lightjason.agentspeak.common.IPath;
  27. import org.lightjason.agentspeak.language.CCommon;
  28. import org.lightjason.agentspeak.language.ILiteral;
  29. import org.lightjason.agentspeak.language.ITerm;
  30. import org.lightjason.agentspeak.language.execution.IContext;
  31. import org.lightjason.agentspeak.language.fuzzy.CFuzzyValue;
  32. import org.lightjason.agentspeak.language.fuzzy.IFuzzyValue;
  33. import org.lightjason.agentspeak.language.variable.IVariable;

  34. import javax.annotation.Nonnull;
  35. import java.text.MessageFormat;
  36. import java.util.List;
  37. import java.util.stream.Stream;


  38. /**
  39.  * placeholder rule to define correct rule referencing
  40.  *
  41.  * @note rules are the first executable elements which are parsed,
  42.  * so if a rule calls itself (recursive) the reference does not exists,
  43.  * with this class a placeholder is used first and after that we replace
  44.  * the placeholder with the correct rule object
  45.  */
  46. public final class CRulePlaceholder implements IRule
  47. {
  48.     /**
  49.      * serial id
  50.      */
  51.     private static final long serialVersionUID = 6857304030640970668L;
  52.     /**
  53.      * identifier of the rule
  54.      */
  55.     private final ILiteral m_id;

  56.     /**
  57.      * ctor
  58.      *
  59.      * @param p_id rule literal
  60.      */
  61.     public CRulePlaceholder( final ILiteral p_id )
  62.     {
  63.         m_id = p_id;
  64.     }


  65.     @Nonnull
  66.     @Override
  67.     public final ILiteral identifier()
  68.     {
  69.         return m_id;
  70.     }

  71.     @Nonnull
  72.     @Override
  73.     public final IRule replaceplaceholder( @Nonnull final Multimap<IPath, IRule> p_rules )
  74.     {
  75.         return this;
  76.     }

  77.     @Nonnull
  78.     @Override
  79.     public final IContext instantiate( @Nonnull final IAgent<?> p_agent, @Nonnull final Stream<IVariable<?>> p_variable )
  80.     {
  81.         return CCommon.instantiate( this, p_agent, p_variable );
  82.     }

  83.     @Nonnull
  84.     @Override
  85.     public IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
  86.                                          @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
  87.     )
  88.     {
  89.         return CFuzzyValue.from( false );
  90.     }

  91.     @Nonnull
  92.     @Override
  93.     public final Stream<IVariable<?>> variables()
  94.     {
  95.         return Stream.empty();
  96.     }

  97.     @Override
  98.     public final int hashCode()
  99.     {
  100.         return m_id.hashCode();
  101.     }

  102.     @Override
  103.     public final boolean equals( final Object p_object )
  104.     {
  105.         return ( p_object instanceof IRule ) && ( this.hashCode() == p_object.hashCode() );
  106.     }

  107.     @Override
  108.     public final String toString()
  109.     {
  110.         return MessageFormat.format(
  111.             "{0} ({1} ==>> ?)",
  112.             super.toString(),
  113.             m_id
  114.         );
  115.     }

  116. }