LightJason - AgentSpeak(L++)
IBaseInstantiable.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;
25 
35 
36 import javax.annotation.Nonnull;
37 import java.util.Collections;
38 import java.util.HashMap;
39 import java.util.LinkedList;
40 import java.util.List;
41 import java.util.Map;
42 import java.util.Set;
43 import java.util.stream.Collectors;
44 import java.util.stream.Stream;
45 
46 
50 public abstract class IBaseInstantiable implements IInstantiable
51 {
55  private static final long serialVersionUID = 8843291880722926104L;
59  protected final List<IExecution> m_action;
60 
68  private final int m_hash;
69 
70 
78  protected IBaseInstantiable( final List<IExecution> p_action, final Set<IAnnotation<?>> p_annotation, final int p_hash )
79  {
80  m_hash = p_hash;
81  m_action = Collections.unmodifiableList( p_action );
82  m_annotation = Collections.unmodifiableMap( p_annotation.stream().collect( HashMap::new, ( m, s ) -> m.put( s.id(), s ), Map::putAll ) );
83  }
84 
85  @Override
86  public final int hashCode()
87  {
88  return m_hash;
89  }
90 
91  @Override
92  public final boolean equals( final Object p_object )
93  {
94  return ( p_object instanceof IInstantiable ) && ( this.hashCode() == p_object.hashCode() );
95  }
96 
97  @Nonnull
98  @Override
99  public final IContext instantiate( @Nonnull final IAgent<?> p_agent, @Nonnull final Stream<IVariable<?>> p_variable )
100  {
101  return CCommon.instantiate( this, p_agent, p_variable );
102  }
103 
104  @Nonnull
105  @Override
106  public Stream<IVariable<?>> variables()
107  {
108  return m_action.stream().flatMap( IExecution::variables );
109  }
110 
111  @Nonnull
112  @Override
113  public IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
114  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
115  )
116  {
117  // execution must be the first call, because all elements must be executed and iif the execution fails the @atomic flag can be checked,
118  // each item gets its own parameters, annotation and return stack, so it will be created locally, but the return list did not to be an "empty-list"
119  // because we need to allocate memory of any possible element, otherwise an unsupported operation exception is thrown
120  final List<IFuzzyValue<Boolean>> l_result = m_annotation.containsKey( IAnnotation.EType.PARALLEL )
121  ? this.executeparallel( p_context )
122  : this.executesequential( p_context );
123  // if atomic flag if exists use this for return value
124  return m_annotation.containsKey( IAnnotation.EType.ATOMIC )
125  ? CFuzzyValue.from( true )
126  : l_result.stream().collect( p_context.agent().fuzzy().getKey() );
127  }
128 
137  @SuppressWarnings( "ResultOfMethodCallIgnored" )
138  private List<IFuzzyValue<Boolean>> executesequential( final IContext p_context )
139  {
140  final List<IFuzzyValue<Boolean>> l_result = Collections.synchronizedList( new LinkedList<>() );
141 
142  m_action.stream()
143  .map( i ->
144  {
145  final IFuzzyValue<Boolean> l_return = i.execute( false, p_context, Collections.<ITerm>emptyList(), new LinkedList<>() );
146  l_result.add( l_return );
147  return p_context.agent().fuzzy().getValue().defuzzify( l_return );
148  } )
149  .filter( i -> !i )
150  .findFirst();
151 
152  return l_result;
153  }
154 
163  private List<IFuzzyValue<Boolean>> executeparallel( final IContext p_context )
164  {
165  return m_action.parallelStream()
166  .map( i -> i.execute( false, p_context, Collections.<ITerm>emptyList(), new LinkedList<>() ) )
167  .collect( Collectors.toList() );
168  }
169 
170 }
final Map< IAnnotation.EType, IAnnotation<?> > m_annotation
map with annotation (enum value for getting annotation object)
Stream< IVariable<?> > variables()
returns a stream with all used variables
List< IFuzzyValue< Boolean > > executesequential(final IContext p_context)
execute plan sequential
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
interface for (instantiable) plans and logical-rules
common structure for execution definition
Stream< IVariable<?> > variables()
returns a stream with all used variables
execution context with local data
Definition: IContext.java:42
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
List< IFuzzyValue< Boolean > > executeparallel(final IContext p_context)
execute plan parallel
final IContext instantiate( @Nonnull final IAgent<?> p_agent, @Nonnull final Stream< IVariable<?>> p_variable)
creates an individual execution context
IBaseInstantiable(final List< IExecution > p_action, final Set< IAnnotation<?>> p_annotation, final int p_hash)
ctor
static IContext instantiate( @Nonnull final IInstantiable p_instance, @Nonnull final IAgent<?> p_agent, @Nonnull final Stream< IVariable<?>> p_variable)
creates the instantiate execution context with default variables