CContext.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;

  24. import org.lightjason.agentspeak.agent.IAgent;
  25. import org.lightjason.agentspeak.common.IPath;
  26. import org.lightjason.agentspeak.language.instantiable.IInstantiable;
  27. import org.lightjason.agentspeak.language.variable.IVariable;

  28. import javax.annotation.Nonnull;
  29. import java.text.MessageFormat;
  30. import java.util.Collection;
  31. import java.util.Collections;
  32. import java.util.Map;
  33. import java.util.stream.Collectors;


  34. /**
  35.  * execution context
  36.  *
  37.  * @tparam T instance type (plan or rule)
  38.  */
  39. public final class CContext implements IContext
  40. {
  41.     /**
  42.      * serial id
  43.      */
  44.     private static final long serialVersionUID = 2813094837634259389L;
  45.     /**
  46.      * agent of the running context
  47.      */
  48.     private final IAgent<?> m_agent;
  49.     /**
  50.      * current instance object
  51.      */
  52.     private final IInstantiable m_instance;
  53.     /**
  54.      * plan variables with their data
  55.      */
  56.     private final Map<IPath, IVariable<?>> m_variables;


  57.     /**
  58.      * ctor
  59.      *
  60.      * @param p_agent agent
  61.      * @param p_instance instance object
  62.      * @param p_variables instance variables
  63.      */
  64.     public CContext( @Nonnull final IAgent<?> p_agent, @Nonnull final IInstantiable p_instance, @Nonnull final Collection<IVariable<?>> p_variables )
  65.     {
  66.         m_agent = p_agent;
  67.         m_instance = p_instance;
  68.         m_variables = Collections.unmodifiableMap( p_variables.parallelStream().collect( Collectors.toMap( IVariable::fqnfunctor, i -> i ) ) );
  69.     }

  70.     @Nonnull
  71.     @Override
  72.     public final IContext duplicate()
  73.     {
  74.         return new CContext( m_agent, m_instance, m_variables.values().parallelStream().map( i -> i.shallowcopy() ).collect( Collectors.toSet() ) );
  75.     }

  76.     @Nonnull
  77.     @Override
  78.     public final IAgent<?> agent()
  79.     {
  80.         return m_agent;
  81.     }

  82.     @Nonnull
  83.     @Override
  84.     public final IInstantiable instance()
  85.     {
  86.         return m_instance;
  87.     }

  88.     @Nonnull
  89.     @Override
  90.     public final Map<IPath, IVariable<?>> instancevariables()
  91.     {
  92.         return m_variables;
  93.     }

  94.     @Override
  95.     public final int hashCode()
  96.     {
  97.         return m_agent.hashCode() ^ m_instance.hashCode() ^ m_variables.keySet().hashCode();
  98.     }

  99.     @Override
  100.     public final boolean equals( final Object p_object )
  101.     {
  102.         return ( p_object instanceof IContext ) && ( this.hashCode() == p_object.hashCode() );
  103.     }

  104.     @Override
  105.     public final String toString()
  106.     {
  107.         return MessageFormat.format( "{0} [{1} | {2} | {3}]", super.toString(), m_variables.values(), m_instance, m_agent );
  108.     }

  109. }