LightJason - AgentSpeak(L++)
CContext.java
Go to the documentation of this file.
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 
24 package org.lightjason.agentspeak.language.execution;
25 
30 
31 import javax.annotation.Nonnull;
32 import java.text.MessageFormat;
33 import java.util.Collection;
34 import java.util.Collections;
35 import java.util.Map;
36 import java.util.stream.Collectors;
37 
38 
44 public final class CContext implements IContext
45 {
49  private static final long serialVersionUID = 2813094837634259389L;
53  private final IAgent<?> m_agent;
57  private final IInstantiable m_instance;
61  private final Map<IPath, IVariable<?>> m_variables;
62 
63 
71  public CContext( @Nonnull final IAgent<?> p_agent, @Nonnull final IInstantiable p_instance, @Nonnull final Collection<IVariable<?>> p_variables )
72  {
73  m_agent = p_agent;
74  m_instance = p_instance;
75  m_variables = Collections.unmodifiableMap( p_variables.parallelStream().collect( Collectors.toMap( IVariable::fqnfunctor, i -> i ) ) );
76  }
77 
78  @Nonnull
79  @Override
80  public final IContext duplicate()
81  {
82  return new CContext( m_agent, m_instance, m_variables.values().parallelStream().map( i -> i.shallowcopy() ).collect( Collectors.toSet() ) );
83  }
84 
85  @Nonnull
86  @Override
87  public final IAgent<?> agent()
88  {
89  return m_agent;
90  }
91 
92  @Nonnull
93  @Override
94  public final IInstantiable instance()
95  {
96  return m_instance;
97  }
98 
99  @Nonnull
100  @Override
101  public final Map<IPath, IVariable<?>> instancevariables()
102  {
103  return m_variables;
104  }
105 
106  @Override
107  public final int hashCode()
108  {
109  return m_agent.hashCode() ^ m_instance.hashCode() ^ m_variables.keySet().hashCode();
110  }
111 
112  @Override
113  public final boolean equals( final Object p_object )
114  {
115  return ( p_object instanceof IContext ) && ( this.hashCode() == p_object.hashCode() );
116  }
117 
118  @Override
119  public final String toString()
120  {
121  return MessageFormat.format( "{0} [{1} | {2} | {3}]", super.toString(), m_variables.values(), m_instance, m_agent );
122  }
123 
124 }
final Map< IPath, IVariable<?> > instancevariables()
returns the variables names and their current value
Definition: CContext.java:101
final Map< IPath, IVariable<?> > m_variables
plan variables with their data
Definition: CContext.java:61
final IAgent<?> agent()
returns the agent of the context
Definition: CContext.java:87
interface for (instantiable) plans and logical-rules
final IInstantiable instance()
returns the instance object
Definition: CContext.java:94
final boolean equals(final Object p_object)
Definition: CContext.java:113
IPath fqnfunctor()
returns the full-qualified functor with path and name
execution context with local data
Definition: IContext.java:42
final IContext duplicate()
duplicates the context with a shallow-copy
Definition: CContext.java:80
final IInstantiable m_instance
current instance object
Definition: CContext.java:57
CContext( @Nonnull final IAgent<?> p_agent, @Nonnull final IInstantiable p_instance, @Nonnull final Collection< IVariable<?>> p_variables)
ctor
Definition: CContext.java:71
final IAgent<?> m_agent
agent of the running context
Definition: CContext.java:53