LightJason - AgentSpeak(L++)
storage/CRemove.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.storage;
25 
33 
34 import javax.annotation.Nonnegative;
35 import javax.annotation.Nonnull;
36 import javax.annotation.Nullable;
37 import java.util.List;
38 import java.util.function.Function;
39 import java.util.stream.Stream;
40 
41 
50 public final class CRemove extends IStorage
51 {
55  private static final long serialVersionUID = 7237340367513736766L;
56 
60  public CRemove()
61  {
62  super();
63  }
64 
70  public CRemove( @Nonnull final Function<String, Boolean> p_resolver )
71  {
72  super( p_resolver );
73  }
74 
80  public CRemove( @Nullable final String... p_forbidden )
81  {
82  super( p_forbidden );
83  }
84 
90  public CRemove( @Nonnull final Stream<String> p_fordbidden )
91  {
92  super( p_fordbidden );
93  }
94 
95  @Nonnegative
96  @Override
97  public final int minimalArgumentNumber()
98  {
99  return 1;
100  }
101 
102  @Nonnull
103  @Override
104  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
105  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
106  {
107  CCommon.flatten( p_argument )
108  .map( ITerm::<String>raw )
109  .forEach( i -> this.remove( p_context.agent(), i, p_return ) );
110 
111  return CFuzzyValue.from( true );
112  }
113 
114 
122  private void remove( @Nonnull final IAgent<?> p_agent, @Nonnull final String p_key, @Nonnull final List<ITerm> p_return )
123  {
124  if ( ( m_resolver.apply( p_key ) ) || ( !p_agent.storage().containsKey( p_key ) ) )
125  return;
126 
127  p_return.add(
128  CRawTerm.from(
129  p_agent.storage().remove( p_key )
130  )
131  );
132  }
133 
134 }
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
common structure for execution definition
final Function< String, Boolean > m_resolver
set with forbidden keys
removes an element by name from the storage.
CRemove( @Nonnull final Stream< String > p_fordbidden)
ctor
final int minimalArgumentNumber()
minimum number of arguments
execution context with local data
Definition: IContext.java:42
static Stream< ITerm > flatten( @Nonnull final Collection<? extends ITerm > p_terms)
flat term-in-term collection into a straight term list
CRemove( @Nonnull final Function< String, Boolean > p_resolver)
ctor
result for an immutable fuzzy value
CRemove( @Nullable final String... p_forbidden)
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)
defines a plan-body operation
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
term structure for simple datatypes
Definition: CRawTerm.java:45