LightJason - AgentSpeak(L++)
CLambdaExpression.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.action;
25 
26 import org.apache.commons.lang3.tuple.ImmutableTriple;
27 import org.apache.commons.lang3.tuple.Triple;
36 
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;
45 import java.util.Set;
46 import java.util.stream.Collectors;
47 import java.util.stream.Stream;
48 
49 
53 public final class CLambdaExpression extends IBaseExecution<IVariable<?>>
54 {
58  private static final long serialVersionUID = 5128636076731831236L;
62  private final IExecution m_initialize;
66  private final List<IExecution> m_body;
70  private final boolean m_parallel;
74  private final IVariable<?> m_return;
75 
76 
85  public CLambdaExpression( final boolean p_parallel, @Nonnull final IExecution p_initialize,
86  @Nonnull final IVariable<?> p_iterator, @Nonnull final List<IExecution> p_body )
87  {
88  this( p_parallel, p_initialize, p_iterator, null, p_body );
89  }
90 
100  public CLambdaExpression( final boolean p_parallel, @Nonnull final IExecution p_initialize, @Nonnull final IVariable<?> p_iterator,
101  @Nullable final IVariable<?> p_return, @Nonnull final List<IExecution> p_body )
102  {
103  super( p_iterator );
104  m_parallel = p_parallel;
105  m_initialize = p_initialize;
106  m_return = p_return;
107  m_body = Collections.unmodifiableList( p_body );
108  }
109 
110  @Nonnull
111  @Override
112  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
113  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
114  {
115  // run initialization
116  final List<ITerm> l_initialization = new LinkedList<>();
117  if ( !m_initialize.execute( p_parallel, p_context, p_argument, l_initialization ).value() )
118  return CFuzzyValue.from( false );
119 
120  // run lambda expression
121  final List<?> l_return = m_parallel ? this.executeParallel( p_context, l_initialization ) : this.executeSequential( p_context, l_initialization );
122  if ( Objects.nonNull( m_return ) )
123  CCommon.replaceFromContext( p_context, m_return ).<IVariable<List<?>>>term().set( l_return );
124 
125  return CFuzzyValue.from( true );
126  }
127 
128  @Override
129  public final int hashCode()
130  {
131  return m_initialize.hashCode() ^ m_value.hashCode() ^ m_body.hashCode() ^ ( m_parallel ? 9931 : 0 );
132  }
133 
134  @Override
135  public final boolean equals( final Object p_object )
136  {
137  return ( p_object instanceof IExecution ) && ( this.hashCode() == p_object.hashCode() );
138  }
139 
140  @Nonnull
141  @Override
142  public final Stream<IVariable<?>> variables()
143  {
144  return Objects.isNull( m_return )
145  ? m_initialize.variables()
146  : Stream.concat(
147  Stream.of( m_return ),
148  m_initialize.variables()
149  );
150  }
151 
152  @Override
153  public final String toString()
154  {
155  return MessageFormat.format( "{0}({1}) -> {2} | {3}", m_parallel ? "@" : "", m_initialize, m_value, m_body );
156  }
157 
165  @Nonnull
166  private List<?> executeSequential( @Nonnull final IContext p_context, @Nonnull final List<ITerm> p_input )
167  {
168  final Triple<IContext, IVariable<?>, IVariable<?>> l_localcontext = this.getLocalContext( p_context );
169 
170  return CCommon.flatten( p_input )
171  .map( i ->
172  {
173  l_localcontext.getMiddle().set( i.raw() );
174  m_body.forEach( j -> j.execute(
175  m_parallel,
176  l_localcontext.getLeft(),
177  Collections.<ITerm>emptyList(),
178  new LinkedList<>()
179  ) );
180  return l_localcontext.getRight() != null ? l_localcontext.getRight().raw() : null;
181  } )
182  .filter( Objects::nonNull )
183  .collect( Collectors.toList() );
184  }
185 
193  @Nonnull
194  private List<?> executeParallel( @Nonnull final IContext p_context, @Nonnull final List<ITerm> p_input )
195  {
196  return CCommon.flatten( p_input )
197  .parallel()
198  .map( i ->
199  {
200  final Triple<IContext, IVariable<?>, IVariable<?>> l_localcontext = this.getLocalContext( p_context );
201  l_localcontext.getMiddle().set( i.raw() );
202  m_body.forEach( j -> j.execute(
203  m_parallel,
204  l_localcontext.getLeft(),
205  Collections.<ITerm>emptyList(),
206  new LinkedList<>()
207  ) );
208  return l_localcontext.getRight() != null ? l_localcontext.getRight().raw() : null;
209  } )
210  .filter( Objects::nonNull )
211  .collect( Collectors.toList() );
212  }
213 
214 
221  @Nonnull
222  private Triple<IContext, IVariable<?>, IVariable<?>> getLocalContext( @Nonnull final IContext p_context )
223  {
224  final IVariable<?> l_iterator = m_value.shallowcopy();
225  final IVariable<?> l_return = m_return != null ? m_return.shallowcopy() : null;
226 
227  final Set<IVariable<?>> l_variables = new HashSet<>( p_context.instancevariables().values() );
228 
229  m_body.stream().flatMap( IExecution::variables ).forEach( l_variables::add );
230  l_variables.remove( l_iterator );
231  l_variables.add( l_iterator );
232 
233  if ( Objects.nonNull( l_return ) )
234  {
235  l_variables.remove( l_return );
236  l_variables.add( l_return );
237  }
238 
239  return new ImmutableTriple<>( new CContext( p_context.agent(), p_context.instance(), l_variables ), l_iterator, l_return );
240  }
241 }
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
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
execution context with local data
Definition: IContext.java:42
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
List<?> executeSequential( @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_input)
run sequential execution