LightJason - AgentSpeak(L++)
CMultiAssignment.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 
33 
34 import javax.annotation.Nonnull;
35 import java.text.MessageFormat;
36 import java.util.Collections;
37 import java.util.LinkedList;
38 import java.util.List;
39 import java.util.Objects;
40 import java.util.stream.Collectors;
41 import java.util.stream.IntStream;
42 import java.util.stream.Stream;
43 
44 
48 public final class CMultiAssignment<M extends IExecution> extends IBaseExecution<List<IVariable<?>>>
49 {
53  private static final long serialVersionUID = -6123210880356077509L;
57  private final M m_righthand;
58 
65  public CMultiAssignment( @Nonnull final List<IVariable<?>> p_lefthand, @Nonnull final M p_righthand )
66  {
67  super( p_lefthand );
68  m_righthand = p_righthand;
69  }
70 
71  @Nonnull
72  @Override
73  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
74  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
75  {
76  final List<ITerm> l_result = new LinkedList<>();
77  if ( ( !m_righthand.execute( p_parallel, p_context, Collections.<ITerm>emptyList(), l_result ).value() )
78  || ( l_result.isEmpty() ) )
79  return CFuzzyValue.from( false );
80 
81 
82  // position matching on list index
83  final List<ITerm> l_flatresult = CCommon.flatten( l_result ).collect( Collectors.toList() );
84  final List<ITerm> l_assign = CCommon.replaceFromContext( p_context, m_value );
85 
86  IntStream.range( 0, Math.min( l_assign.size(), l_flatresult.size() ) )
87  .boxed()
88  .forEach( i -> l_assign.get( i ).<IVariable<Object>>term().set( l_flatresult.get( i ).raw() ) );
89 
90 
91  // tail matching
92  if ( l_assign.size() < l_flatresult.size() )
93  l_assign.get( l_assign.size() - 1 ).<IVariable<Object>>term().set( l_flatresult.subList( l_assign.size() - 1, l_flatresult.size() ) );
94 
95  return CFuzzyValue.from( true );
96  }
97 
98  @Override
99  public final int hashCode()
100  {
101  return ( Objects.isNull( m_value ) ? 0 : m_value.hashCode() ) ^ m_righthand.hashCode();
102  }
103 
104  @Override
105  public final boolean equals( final Object p_object )
106  {
107  return ( p_object instanceof IExecution ) && ( this.hashCode() == p_object.hashCode() );
108  }
109 
110  @Override
111  public final String toString()
112  {
113  return MessageFormat.format( "{0} = {1}", m_value, m_righthand );
114  }
115 
116  @Nonnull
117  @Override
118  public final Stream<IVariable<?>> variables()
119  {
120  return Stream.concat(
121  Objects.isNull( m_value ) ? Stream.empty() : m_value.stream(),
122  m_righthand.variables()
123  );
124  }
125 }
IVariable< T > set( @Nullable final T p_value)
sets the value
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
common structure for execution definition
CMultiAssignment( @Nonnull final List< IVariable<?>> p_lefthand, @Nonnull final M p_righthand)
ctor
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)
execution context with local data
Definition: IContext.java:42
result for an immutable fuzzy value