LightJason - AgentSpeak(L++)
CJsonList.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.web.rest;
25 
31 
32 import javax.annotation.Nonnull;
33 import java.io.IOException;
34 import java.util.List;
35 import java.util.Map;
36 
37 
49 public final class CJsonList extends IBaseRest
50 {
54  private static final long serialVersionUID = -5630882266708277403L;
55 
59  public CJsonList()
60  {
61  super( 3 );
62  }
63 
64  @Nonnull
65  @Override
66  @SuppressWarnings( "unchecked" )
67  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
68  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
69  )
70  {
71  try
72  {
73  final List<?> l_data = IBaseRest.json(
74  p_argument.get( 0 ).<String>raw(),
75  List.class
76  );
77 
78  if ( p_argument.size() == 2 )
79  l_data.stream()
80  .map( i -> CLiteral.from( p_argument.get( p_argument.size() - 1 ).<String>raw(), flatterm( (Map<String, ?>) i ) ) )
81  .forEach( p_return::add );
82  else
83  p_return.add(
85  p_argument.stream().skip( 1 ).map( ITerm::<String>raw ),
86  l_data.stream()
87  .map( i -> CLiteral.from( p_argument.get( p_argument.size() - 1 ).<String>raw(), flatterm( (Map<String, ?>) i ) ) )
88  )
89  );
90 
91  return CFuzzyValue.from( true );
92  }
93  catch ( final IOException l_exception )
94  {
95  return CFuzzyValue.from( false );
96  }
97  }
98 
99 }
static Stream< ITerm > flatterm( @Nullable final Object p_object)
converts an object into a term stream
Definition: IBaseWeb.java:178
action for calling a restful webservice with a JSON object list.
Definition: CJsonList.java:49
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: CJsonList.java:67
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
static ITerm baseliteral( @Nonnull final Stream< String > p_functor, @Nonnull final Stream< ITerm > p_values)
creates a literal structure from a stream of string elements, the string stream will be build in a tr...
Definition: IBaseWeb.java:159
execution context with local data
Definition: IContext.java:42
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 from( @Nonnull final String p_functor, @Nullable final ITerm... p_values)
factory
Definition: CLiteral.java:161
base class to read data from the restful service
Definition: IBaseRest.java:42
static< T > T json( @Nonnull final String p_url, @Nonnull final Class< T > p_class)
reads a json structure from an url
Definition: IBaseRest.java:83