LightJason - AgentSpeak(L++)
language/instantiable/plan/CPlan.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.instantiable.plan;
25 
26 import org.apache.commons.lang3.StringUtils;
39 
40 import javax.annotation.Nonnull;
41 import java.text.MessageFormat;
42 import java.util.Collections;
43 import java.util.LinkedList;
44 import java.util.List;
45 import java.util.Objects;
46 import java.util.Set;
47 import java.util.stream.Stream;
48 
49 
53 public final class CPlan extends IBaseInstantiable implements IPlan
54 {
58  private static final long serialVersionUID = -8130277494195919583L;
62  private final ITrigger m_triggerevent;
66  private final IExpression m_condition;
67 
68 
76  public CPlan( @Nonnull final ITrigger p_event, @Nonnull final List<IExecution> p_body, @Nonnull final Set<IAnnotation<?>> p_annotation )
77  {
78  this( p_event, IExpression.EMPTY, p_body, p_annotation );
79  }
80 
89  public CPlan( @Nonnull final ITrigger p_event, @Nonnull final IExpression p_condition,
90  @Nonnull final List<IExecution> p_body, @Nonnull final Set<IAnnotation<?>> p_annotation )
91  {
92  super(
93  p_body,
94  p_annotation,
95 
97  Stream.of(
98  p_event.hashCode(),
99  p_condition.hashCode()
100  ),
101  p_body.stream().map( Object::hashCode ),
102  p_annotation.stream().map( Object::hashCode )
103  ).reduce( 0, ( i, j ) -> i ^ j )
104  );
105 
106  m_triggerevent = p_event;
107  m_condition = p_condition;
108  }
109 
110  @Nonnull
111  @Override
112  public final ITrigger trigger()
113  {
114  return m_triggerevent;
115  }
116 
117  @Nonnull
118  @Override
119  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
120  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
121  )
122  {
123  final IFuzzyValue<Boolean> l_result = super.execute( p_parallel, p_context, p_argument, p_return );
124 
125  // create delete-goal trigger
126  if ( !p_context.agent().fuzzy().getValue().defuzzify( l_result ) )
127  p_context.agent().trigger( CTrigger.from( ITrigger.EType.DELETEGOAL, m_triggerevent.literal().unify( p_context ) ) );
128 
129  return l_result;
130  }
131 
132  @Nonnull
133  @Override
134  public final IFuzzyValue<Boolean> condition( final IContext p_context )
135  {
136  /*
137  // check condition for bounded variables
138  if ( m_condition.variables()
139  .parallel()
140  .map( i -> CCommon.replaceFromContext( p_context, i ) )
141  .filter( ITerm::hasVariable )
142  .map( i -> (IVariable) i )
143  .anyMatch( i -> !i.allocated() )
144  )
145  return CFuzzyValue.from( false );
146 
147  */
148 
149  final List<ITerm> l_return = new LinkedList<>();
150  return CFuzzyValue.from(
151  m_condition.execute( false, p_context, Collections.emptyList(), l_return ).value()
152  && ( l_return.size() == 1 )
153  ? l_return.get( 0 ).<Boolean>raw()
154  : false
155  );
156  }
157 
158  @Override
159  public final String toString()
160  {
161  return MessageFormat.format(
162  "{0} ({1} | {2}{3} ==>> {4})",
163  super.toString(),
164  m_annotation.values(),
166  Objects.isNull( m_condition ) ? "" : MessageFormat.format( " |- {0}", m_condition ),
167  StringUtils.join( m_action, "; " )
168  );
169  }
170 
171  @Nonnull
172  @Override
173  public final Stream<IVariable<?>> variables()
174  {
175  return CCommon.streamconcat(
176  m_condition != null ? m_condition.variables() : Stream.empty(),
177  super.variables(),
178  m_annotation.values().stream().flatMap( IAnnotation::variables ),
179  CCommon.flattenrecursive( m_triggerevent.literal().orderedvalues() ).filter( i -> i instanceof IVariable<?> ).map( i -> (IVariable<?>) i )
180  );
181  }
182 
183 }
final Map< IAnnotation.EType, IAnnotation<?> > m_annotation
map with annotation (enum value for getting annotation object)
IExpression EMPTY
empty expression, is always true
IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
Stream< IVariable<?> > variables()
returns a stream with all used variables
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
common structure for execution definition
Stream< ITerm > orderedvalues( @Nullable final IPath... p_path)
returns a stream over the ordered values in sequential ordering
final IExpression m_condition
execution condition / expression
static Stream< ITerm > flattenrecursive( @Nonnull final Stream< ITerm > p_input)
recursive stream of term values
static ITrigger from( @Nonnull final EType p_event, @Nonnull final ILiteral p_literal)
creates a trigger event^
Definition: CTrigger.java:87
ILiteral unify( @Nonnull final IContext p_context)
unifies variables if exists
execution context with local data
Definition: IContext.java:42
Stream< IVariable<?> > variables()
returns a stream of variables
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
result for an immutable fuzzy value
CPlan( @Nonnull final ITrigger p_event, @Nonnull final List< IExecution > p_body, @Nonnull final Set< IAnnotation<?>> p_annotation)
ctor
final Stream< IVariable<?> > variables()
returns a stream with all used variables
static< T > Stream< T > streamconcat( @Nonnull final Stream< T >... p_streams)
concat multiple streams
final IFuzzyValue< Boolean > condition(final IContext p_context)
execute the plan condition
CPlan( @Nonnull final ITrigger p_event, @Nonnull final IExpression p_condition, @Nonnull final List< IExecution > p_body, @Nonnull final Set< IAnnotation<?>> p_annotation)
ctor