LightJason - AgentSpeak(L++)
IAchievementRule.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.achievement_test;
25 
26 import org.apache.commons.lang3.tuple.ImmutableTriple;
37 
38 import javax.annotation.Nonnull;
39 import java.util.Collection;
40 import java.util.Collections;
41 import java.util.Objects;
42 import java.util.Set;
43 
44 
48 abstract class IAchievementRule<T extends ITerm> extends IBaseExecution<T>
49 {
53  private static final long serialVersionUID = -315973892409409832L;
54 
60  protected IAchievementRule( @Nonnull final T p_type )
61  {
62  super( p_type );
63  }
64 
73  @Nonnull
74  @SuppressWarnings( "unchecked" )
75  protected static IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final ILiteral p_value
76  )
77  {
78  // read current rules, if not exists execution fails
79  final Collection<IRule> l_rules = p_context.agent().rules().get( p_value.fqnfunctor() );
80  if ( Objects.isNull( l_rules ) )
81  return CFuzzyValue.from( false );
82 
83  // first step is the unification of the caller literal, so variables will be set from the current execution context
84  final ILiteral l_unified = p_value.allocate( p_context );
85 
86  // second step execute backtracking rules sequential / parallel
87  return (
88  p_parallel
89  ? l_rules.parallelStream()
90  : l_rules.stream()
91  ).map( i ->
92  {
93 
94  // instantiate variables by unification of the rule literal
95  final Set<IVariable<?>> l_variables = p_context.agent().unifier().unify( l_unified, i.identifier() );
96 
97  // execute rule
98  final IFuzzyValue<Boolean> l_return = i.execute(
99  false, i.instantiate( p_context.agent(), l_variables.stream() ),
100  Collections.<ITerm>emptyList(),
101  Collections.<ITerm>emptyList()
102  );
103 
104  // create rule result with fuzzy- and defuzzificated value and instantiate variable set
105  return new ImmutableTriple<>( p_context.agent().fuzzy().getValue().defuzzify( l_return ), l_return, l_variables );
106 
107  } )
108 
109  // find successfully ended rule
110  .filter( ImmutableTriple::getLeft )
111  .findFirst()
112 
113  // realocate rule instantiated variables back to execution context
114  .map( i ->
115  {
116 
117  i.getRight().parallelStream()
118  .filter( j -> j instanceof IRelocateVariable )
119  .forEach( j -> ( (IRelocateVariable) j ).relocate() );
120 
121  return i.getMiddle();
122 
123  } )
124 
125  // otherwise rule fails (default behaviour)
126  .orElse( CFuzzyValue.from( false ) );
127  }
128 
129  @Override
130  public final int hashCode()
131  {
132  return Objects.isNull( m_value ) ? 0 : m_value.hashCode();
133  }
134 
135  @Override
136  public final boolean equals( final Object p_object )
137  {
138  return ( p_object instanceof IExecution ) && ( this.hashCode() == p_object.hashCode() );
139  }
140 
141 }
ILiteral allocate( @Nonnull final IContext p_context)
allocate all variables with the current context
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
interface for relocated variables (linkage between two variables for transfering the value) ...
execution context with local data
Definition: IContext.java:42
result for an immutable fuzzy value
static IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final ILiteral p_value)
execute rule from context