LightJason - AgentSpeak(L++)
IBaseAgentGenerator.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.generator;
25 
26 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
40 
41 import javax.annotation.Nonnull;
42 import javax.annotation.Nullable;
43 import java.io.InputStream;
44 import java.util.Collection;
45 import java.util.Collections;
46 import java.util.LinkedHashSet;
47 import java.util.Objects;
48 import java.util.Set;
49 import java.util.stream.Collectors;
50 import java.util.stream.IntStream;
51 import java.util.stream.Stream;
52 
53 
57 @SuppressFBWarnings( "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD" )
58 public abstract class IBaseAgentGenerator<T extends IAgent<?>> implements IAgentGenerator<T>
59 {
64 
65 
73  @SuppressWarnings( "unchecked" )
74  public IBaseAgentGenerator( @Nonnull final InputStream p_stream, @Nonnull final Set<IAction> p_actions ) throws Exception
75  {
76  this( p_stream, p_actions, Collections.emptySet(), IVariableBuilder.EMPTY );
77  }
78 
87  public IBaseAgentGenerator( @Nonnull final InputStream p_stream, @Nonnull final Set<IAction> p_actions, @Nonnull final IVariableBuilder p_variablebuilder )
88  throws Exception
89  {
90  this( p_stream, p_actions, Collections.emptySet(), p_variablebuilder );
91  }
92 
102  public IBaseAgentGenerator( @Nonnull final InputStream p_stream, @Nonnull final Set<IAction> p_actions,
103  @Nonnull final Set<IPlanBundle> p_planbundle, @Nonnull final IVariableBuilder p_variablebuilder ) throws Exception
104  {
105  final IASTVisitorAgent l_visitor = new CParserAgent( p_actions ).parse( p_stream );
106  m_configuration = this.configuration(
107  DEFAULTFUZZYBUNDLE,
108 
109  Stream.concat(
110  l_visitor.initialbeliefs().stream(),
111  p_planbundle.stream().flatMap( i -> i.initialbeliefs().stream() )
112  ).collect( Collectors.toCollection( LinkedHashSet::new ) ),
113 
114  Stream.concat(
115  l_visitor.plans().stream(),
116  p_planbundle.parallelStream().flatMap( i -> i.plans().stream() )
117  ).collect( Collectors.toSet() ),
118 
119  Stream.concat(
120  l_visitor.rules().stream(),
121  p_planbundle.parallelStream().flatMap( i -> i.rules().stream() )
122  ).collect( Collectors.toSet() ),
123 
124  l_visitor.initialgoal(),
125 
126  DEFAULTUNIFIER,
127 
128  p_variablebuilder
129  );
130  }
131 
144  protected IAgentConfiguration<T> configuration( @Nonnull final IFuzzyBundle<Boolean> p_fuzzy, @Nonnull final Collection<ILiteral> p_initalbeliefs,
145  @Nonnull final Set<IPlan> p_plans, @Nonnull final Set<IRule> p_rules,
146  @Nullable final ILiteral p_initialgoal, @Nonnull final IUnifier p_unifier,
147  @Nonnull final IVariableBuilder p_variablebuilder )
148  {
149  return new CDefaultAgentConfiguration<>(
150  p_fuzzy,
151  p_initalbeliefs,
152  p_plans,
153  p_rules,
154  p_initialgoal,
155  p_unifier,
156  p_variablebuilder
157  );
158  }
159 
160  @Nonnull
161  @Override
162  public final Stream<T> generatemultiple( final int p_number, @Nullable final Object... p_data )
163  {
164  return IntStream.range( 0, p_number )
165  .parallel()
166  .mapToObj( i -> this.generatesingle( p_data ) )
167  .filter( Objects::nonNull );
168  }
169 
170 }
IBaseAgentGenerator( @Nonnull final InputStream p_stream, @Nonnull final Set< IAction > p_actions, @Nonnull final IVariableBuilder p_variablebuilder)
ctor
IAgentConfiguration< T > configuration( @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, @Nonnull final IVariableBuilder p_variablebuilder)
builds the configuraion, configuration runs cloning of objects if needed
ILiteral initialgoal()
returns the initial goal
final IASTVisitorAgent parse(final InputStream p_stream)
interface of an unification algorithm
Definition: IUnifier.java:43
visitor interface of the abstract-syntax-tree (AST) for an agent
external action interface
Definition: IAction.java:38
Set< IRule > rules()
returns the rules / principles
Set< IPlan > plans()
get a multimap with event-plan matching
interface for a variable builder which is called on each plan / rule execution
IBaseAgentGenerator( @Nonnull final InputStream p_stream, @Nonnull final Set< IAction > p_actions, @Nonnull final Set< IPlanBundle > p_planbundle, @Nonnull final IVariableBuilder p_variablebuilder)
ctor
Set< ILiteral > initialbeliefs()
returns initial beliefs
final Stream< T > generatemultiple(final int p_number, @Nullable final Object... p_data)
final IAgentConfiguration< T > m_configuration
configuration of an agent