24 package org.lightjason.agentspeak.language.execution.action;
26 import org.apache.commons.lang3.tuple.ImmutableTriple;
27 import org.apache.commons.lang3.tuple.Triple;
37 import javax.annotation.Nonnull;
38 import javax.annotation.Nullable;
39 import java.text.MessageFormat;
40 import java.util.Collections;
41 import java.util.HashSet;
42 import java.util.LinkedList;
43 import java.util.List;
44 import java.util.Objects;
46 import java.util.stream.Collectors;
47 import java.util.stream.Stream;
66 private final List<IExecution>
m_body;
86 @Nonnull
final IVariable<?> p_iterator, @Nonnull
final List<IExecution> p_body )
88 this( p_parallel, p_initialize, p_iterator, null, p_body );
101 @Nullable
final IVariable<?> p_return, @Nonnull
final List<IExecution> p_body )
104 m_parallel = p_parallel;
105 m_initialize = p_initialize;
107 m_body = Collections.unmodifiableList( p_body );
113 @Nonnull
final List<ITerm> p_argument, @Nonnull
final List<ITerm> p_return )
116 final List<ITerm> l_initialization =
new LinkedList<>();
117 if ( !m_initialize.
execute( p_parallel, p_context, p_argument, l_initialization ).value() )
122 if ( Objects.nonNull( m_return ) )
131 return m_initialize.hashCode() ^
m_value.hashCode() ^ m_body.hashCode() ^ ( m_parallel ? 9931 : 0 );
135 public final boolean equals(
final Object p_object )
137 return ( p_object instanceof
IExecution ) && ( this.
hashCode() == p_object.hashCode() );
144 return Objects.isNull( m_return )
147 Stream.of( m_return ),
155 return MessageFormat.format(
"{0}({1}) -> {2} | {3}", m_parallel ?
"@" :
"", m_initialize,
m_value, m_body );
170 return CCommon.flatten( p_input )
173 l_localcontext.getMiddle().set( i.raw() );
174 m_body.forEach( j -> j.execute(
176 l_localcontext.getLeft(),
177 Collections.<
ITerm>emptyList(),
180 return l_localcontext.getRight() != null ? l_localcontext.getRight().raw() : null;
182 .filter( Objects::nonNull )
183 .collect( Collectors.toList() );
196 return CCommon.flatten( p_input )
201 l_localcontext.getMiddle().set( i.raw() );
202 m_body.forEach( j -> j.execute(
204 l_localcontext.getLeft(),
205 Collections.<
ITerm>emptyList(),
208 return l_localcontext.getRight() != null ? l_localcontext.getRight().raw() : null;
210 .filter( Objects::nonNull )
211 .collect( Collectors.toList() );
225 final IVariable<?> l_return = m_return != null ? m_return.shallowcopy() : null;
227 final Set<IVariable<?>> l_variables =
new HashSet<>( p_context.instancevariables().values() );
230 l_variables.remove( l_iterator );
231 l_variables.add( l_iterator );
233 if ( Objects.nonNull( l_return ) )
235 l_variables.remove( l_return );
236 l_variables.add( l_return );
239 return new ImmutableTriple<>(
new CContext( p_context.agent(), p_context.instance(), l_variables ), l_iterator, l_return );
lambda expression definition
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
IVariable< T > set( @Nullable final T p_value)
sets the value
Stream< IVariable<?> > variables()
returns a stream with all used variables
internal execution interface
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)
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
common structure for execution definition
List<?> executeParallel( @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_input)
run parallel execution
final boolean m_parallel
flag of parallel execution
final IVariable<?> m_return
return variable
final IExecution m_initialize
initialization expression
execution context with local data
CLambdaExpression(final boolean p_parallel, @Nonnull final IExecution p_initialize, @Nonnull final IVariable<?> p_iterator, @Nullable final IVariable<?> p_return, @Nonnull final List< IExecution > p_body)
ctor
result for an immutable fuzzy value
CLambdaExpression(final boolean p_parallel, @Nonnull final IExecution p_initialize, @Nonnull final IVariable<?> p_iterator, @Nonnull final List< IExecution > p_body)
ctor
Triple< IContext, IVariable<?>, IVariable<?> > getLocalContext( @Nonnull final IContext p_context)
create the local context structure of the expression
class for any helper calls
final Stream< IVariable<?> > variables()
final boolean equals(final Object p_object)
final List< IExecution > m_body
execution body
List<?> executeSequential( @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_input)
run sequential execution
static final long serialVersionUID
serial id