LightJason - AgentSpeak(L++)
CRemovePlan.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.action.builtin.agent;
25 
26 import com.codepoetics.protonpack.StreamUtils;
27 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
39 
40 import javax.annotation.Nonnegative;
41 import javax.annotation.Nonnull;
42 import java.util.List;
43 
44 
53 @SuppressFBWarnings( "GC_UNRELATED_TYPES" )
54 public final class CRemovePlan extends IBuiltinAction
55 {
56 
60  private static final long serialVersionUID = -2509947123811973880L;
61 
62  @Nonnegative
63  @Override
64  public final int minimalArgumentNumber()
65  {
66  return 1;
67  }
68 
69  @Nonnull
70  @Override
71  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
72  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
73  )
74  {
75  return CFuzzyValue.from(
76  StreamUtils.windowed(
77  CCommon.flatten( p_argument ),
78  2,
79  2
80  ).allMatch( i -> CRemovePlan.remove( ITrigger.EType.from( i.get( 0 ).<String>raw() ), i.get( 1 ), p_context.agent() ) )
81  );
82  }
83 
92  private static boolean remove( @Nonnull final ITrigger.EType p_trigger, @Nonnull final ITerm p_literal, @Nonnull final IAgent<?> p_agent )
93  {
94  final ILiteral l_literal;
95  try
96  {
97 
98  l_literal = CCommon.rawvalueAssignableTo( p_literal, ILiteral.class )
99  ? p_literal.<ILiteral>raw()
100  : CLiteral.parse( p_literal.<String>raw() );
101 
102  }
103  catch ( final Exception l_exception )
104  {
105  return false;
106  }
107 
108  return !p_agent.plans().removeAll( CTrigger.from( p_trigger, l_literal ) ).isEmpty();
109  }
110 }
base class of build-in actions for setting name by package/classname (without prefix character) ...
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
common structure for execution definition
final int minimalArgumentNumber()
minimum number of arguments
static ITrigger from( @Nonnull final EType p_event, @Nonnull final ILiteral p_literal)
creates a trigger event^
Definition: CTrigger.java:87
execution context with local data
Definition: IContext.java:42
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)
defines a plan-body operation
static Stream< ITerm > flatten( @Nonnull final Collection<? extends ITerm > p_terms)
flat term-in-term collection into a straight term list
static< T > boolean rawvalueAssignableTo( @Nonnull final T p_value, @Nonnull final Class<?>... p_class)
checks a term value for assignable class
result for an immutable fuzzy value
default generic literal class for agent beliefs a literal consists of a functor, an optional list of ...
Definition: CLiteral.java:64
static ILiteral parse( @Nonnull final String p_literal)
factory
Definition: CLiteral.java:259
static EType from( @Nonnull final String p_sequence)
returns a trigger type by the char sequence
Definition: ITrigger.java:197
static boolean remove( @Nonnull final ITrigger.EType p_trigger, @Nonnull final ITerm p_literal, @Nonnull final IAgent<?> p_agent)
removes the plan based on the trigger