LightJason - AgentSpeak(L++)
CDefaultAgentConfiguration.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.configuration;
25 
26 import org.apache.commons.lang3.StringUtils;
40 
41 import javax.annotation.Nonnull;
42 import javax.annotation.Nullable;
43 import java.text.MessageFormat;
44 import java.util.Collection;
45 import java.util.Collections;
46 import java.util.Objects;
47 import java.util.Set;
48 import java.util.logging.Logger;
49 import java.util.stream.Stream;
50 
51 
55 public class CDefaultAgentConfiguration<T extends IAgent<?>> implements IAgentConfiguration<T>
56 {
60  protected static final Logger LOGGER = CCommon.logger( IAgentConfiguration.class );
64  protected static final String BELIEFBASEROOTNAME = "beliefbase";
68  protected final IUnifier m_unifier;
72  protected final ITrigger m_initialgoal;
76  protected final Set<IPlan> m_plans;
84  protected final Collection<ILiteral> m_initialbeliefs;
88  protected final IFuzzyBundle<Boolean> m_fuzzy;
92  protected final Set<IRule> m_rules;
93 
94 
105  public CDefaultAgentConfiguration( @Nonnull final IFuzzyBundle<Boolean> p_fuzzy, @Nonnull final Collection<ILiteral> p_initalbeliefs,
106  @Nonnull final Set<IPlan> p_plans, @Nonnull final Set<IRule> p_rules,
107  @Nullable final ILiteral p_initialgoal, @Nonnull final IUnifier p_unifier
108  )
109  {
110  this( p_fuzzy, p_initalbeliefs, p_plans, p_rules, p_initialgoal, p_unifier, IVariableBuilder.EMPTY );
111  }
112 
124  public CDefaultAgentConfiguration( @Nonnull final IFuzzyBundle<Boolean> p_fuzzy, @Nonnull final Collection<ILiteral> p_initialbeliefs,
125  @Nonnull final Set<IPlan> p_plans, @Nonnull final Set<IRule> p_rules,
126  final ILiteral p_initialgoal, @Nonnull final IUnifier p_unifier,
127  @Nonnull final IVariableBuilder p_variablebuilder
128  )
129  {
130  m_fuzzy = p_fuzzy;
131  m_unifier = p_unifier;
132  m_variablebuilder = p_variablebuilder;
133 
134  m_plans = Collections.unmodifiableSet( p_plans );
135  m_rules = Collections.unmodifiableSet( p_rules );
136  m_initialbeliefs = Collections.unmodifiableCollection( p_initialbeliefs );
137  m_initialgoal = p_initialgoal != null ? CTrigger.from( ITrigger.EType.ADDGOAL, p_initialgoal ) : null;
138 
139  LOGGER.info( MessageFormat.format( "create agent configuration: {0}", this ) );
140  }
141 
142  @Nonnull
143  @Override
144  public IView beliefbase()
145  {
146  final IView l_beliefbase = new CBeliefbase( new CMultiStorage<>() ).create( BELIEFBASEROOTNAME );
147  m_initialbeliefs.forEach( i -> l_beliefbase.add( i.shallowcopy() ) );
148 
149  // clear all events of the initial beliefs
150  l_beliefbase.trigger();
151 
152  return l_beliefbase;
153  }
154 
155  @Nullable
156  @Override
157  public final ITrigger initialgoal()
158  {
159  return m_initialgoal;
160  }
161 
162  @Nonnull
163  @Override
164  public final IUnifier unifier()
165  {
166  return m_unifier;
167  }
168 
169  @Nonnull
170  @Override
172  {
173  return m_variablebuilder;
174  }
175 
176  @Nonnull
177  @Override
179  {
180  return m_fuzzy;
181  }
182 
183  @Nonnull
184  @Override
185  public final Collection<ILiteral> initialbeliefs()
186  {
187  return m_initialbeliefs;
188  }
189 
190  @Nonnull
191  @Override
192  public final Set<IPlan> plans()
193  {
194  return m_plans;
195  }
196 
197  @Nonnull
198  @Override
199  public final Set<IRule> rules()
200  {
201  return m_rules;
202  }
203 
204  @Override
205  public final String toString()
206  {
207  final String l_elements = StringUtils.join(
208  Stream.of(
209  Objects.isNull( m_variablebuilder ) ? "" : m_variablebuilder,
210  m_initialbeliefs.isEmpty() ? "" : m_initialbeliefs,
211  Objects.isNull( m_initialgoal ) ? "" : m_initialgoal,
212  m_plans.isEmpty() ? "" : m_plans,
213  m_rules.isEmpty() ? "" : m_rules
214  ).filter( i -> !i.toString().trim().isEmpty() ).toArray(),
215  " / "
216  ).trim();
217 
218  return MessageFormat.format(
219  "{0} ( unifier: {1} / {2} {3} )",
220  super.toString(),
221  m_unifier,
222  m_fuzzy,
223  l_elements.isEmpty() ? "" : l_elements
224  ).trim();
225  }
226 }
final Collection< ILiteral > m_initialbeliefs
instance of initial beliefs
Stream< ITrigger > trigger()
retruns all trigger of the beliefbase
interface of an unification algorithm
Definition: IUnifier.java:43
static ITrigger from( @Nonnull final EType p_event, @Nonnull final ILiteral p_literal)
creates a trigger event^
Definition: CTrigger.java:87
view for a beliefbase that creates any access to the underlying data structures
Definition: IView.java:44
static Logger logger(final Class<?> p_class)
returns a logger instance
CDefaultAgentConfiguration( @Nonnull final IFuzzyBundle< Boolean > p_fuzzy, @Nonnull final Collection< ILiteral > p_initialbeliefs, @Nonnull final Set< IPlan > p_plans, @Nonnull final Set< IRule > p_rules, final ILiteral p_initialgoal, @Nonnull final IUnifier p_unifier, @Nonnull final IVariableBuilder p_variablebuilder)
ctor
thread-safe storage of the data of single- and multi-elements
final IVariableBuilder m_variablebuilder
instance of variable builder
interface for a variable builder which is called on each plan / rule execution
final IView create( @Nonnull final String p_name)
returns a new view of the belief base
IView add( @Nonnull final ILiteral... p_literal)
adds a literal in the current structure
CDefaultAgentConfiguration( @Nonnull final IFuzzyBundle< Boolean > p_fuzzy, @Nonnull final Collection< ILiteral > p_initalbeliefs, @Nonnull final Set< IPlan > p_plans, @Nonnull final Set< IRule > p_rules, @Nullable final ILiteral p_initialgoal, @Nonnull final IUnifier p_unifier)
ctor
beliefbase to generate any event-based data by reference counting