LightJason - AgentSpeak(L++)
CGetPlan.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;
41 
42 import javax.annotation.Nonnegative;
43 import javax.annotation.Nonnull;
44 import java.util.Collection;
45 import java.util.List;
46 import java.util.stream.Collectors;
47 
48 
59 @SuppressFBWarnings( "GC_UNRELATED_TYPES" )
60 public final class CGetPlan extends IBuiltinAction
61 {
65  private static final long serialVersionUID = 2674034436717184541L;
66 
67  @Nonnegative
68  @Override
69  public final int minimalArgumentNumber()
70  {
71  return 1;
72  }
73 
74  @Nonnull
75  @Override
76  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
77  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
78  )
79  {
80  return CFuzzyValue.from(
81  StreamUtils.windowed(
82  CCommon.flatten( p_argument ),
83  2,
84  2
85  ).allMatch( i -> CGetPlan.query( ITrigger.EType.from( i.get( 0 ).<String>raw() ), i.get( 1 ), p_context.agent(), p_return ) )
86  );
87  }
88 
98  private static boolean query( @Nonnull final ITrigger.EType p_trigger, @Nonnull final ITerm p_literal,
99  @Nonnull final IAgent<?> p_agent, @Nonnull final List<ITerm> p_return )
100  {
101  final ILiteral l_literal;
102  try
103  {
104 
105  l_literal = CCommon.rawvalueAssignableTo( p_literal, ILiteral.class )
106  ? p_literal.<ILiteral>raw()
107  : CLiteral.parse( p_literal.<String>raw() );
108 
109  }
110  catch ( final Exception l_exception )
111  {
112  return false;
113  }
114 
115  final Collection<IPlanStatistic> l_plans = p_agent.plans().get( CTrigger.from( p_trigger, l_literal ) );
116  if ( l_plans.isEmpty() )
117  return false;
118 
119  p_return.add(
120  CRawTerm.from(
121  l_plans.stream().map( IPlanStatistic::plan ).collect( Collectors.toList() )
122  )
123  );
124 
125  return true;
126  }
127 }
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
static ITrigger from( @Nonnull final EType p_event, @Nonnull final ILiteral p_literal)
creates a trigger event^
Definition: CTrigger.java:87
final int minimalArgumentNumber()
minimum number of arguments
Definition: CGetPlan.java:69
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
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
static boolean query( @Nonnull final ITrigger.EType p_trigger, @Nonnull final ITerm p_literal, @Nonnull final IAgent<?> p_agent, @Nonnull final List< ITerm > p_return)
query tha plan from an agent
Definition: CGetPlan.java:98
default generic literal class for agent beliefs a literal consists of a functor, an optional list of ...
Definition: CLiteral.java:64
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
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
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
Definition: CGetPlan.java:76
term structure for simple datatypes
Definition: CRawTerm.java:45