LightJason - AgentSpeak(L++)
CQueryLiteral.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.graphql;
25 
29 
30 import javax.annotation.Nonnull;
31 import java.text.MessageFormat;
32 import java.util.Collection;
33 import java.util.stream.Collectors;
34 import java.util.stream.Stream;
35 
36 
48 public final class CQueryLiteral extends IBaseGraphQL
49 {
53  private static final long serialVersionUID = -6514047475694739845L;
54 
58  public CQueryLiteral()
59  {
60  super( 3 );
61  }
62 
63  @Override
64  protected final String argumentquery( @Nonnull final ITerm p_argument )
65  {
66  return MessageFormat.format( "'{'{0}'}'", root( p_argument.raw() ) );
67  }
68 
75  @Nonnull
76  private static String root( @Nonnull final ILiteral p_literal )
77  {
78  if ( p_literal.emptyValues() )
79  return p_literal.functor();
80 
81  final String l_query = values( p_literal.values() );
82  final String l_fields = fields( p_literal.values() );
83 
84  return MessageFormat.format(
85  "{0}{1}{2}",
86  p_literal.functor(), l_query.isEmpty() ? "" : MessageFormat.format( "({0})", l_query ),
87  l_fields.isEmpty() ? "" : MessageFormat.format( "'{'{0}'}'", l_fields )
88  );
89  }
90 
97  @Nonnull
98  private static String values( @Nonnull final Stream<ITerm> p_stream )
99  {
100  return p_stream.filter( i -> i instanceof ILiteral )
101  .map( ITerm::<ILiteral>raw )
102  .collect( Collectors.toMap( ITerm::functor, i -> valueformat( i.raw() ), ( i, j ) -> i ) )
103  .values()
104  .stream()
105  .collect( Collectors.joining( " " ) )
106  .trim();
107  }
108 
115  @Nonnull
116  private static String fields( @Nonnull final Stream<ITerm> p_stream )
117  {
118  return p_stream.filter( i -> i instanceof ILiteral )
119  .map( ITerm::<ILiteral>raw )
120  .filter( i -> i.values().noneMatch( j -> j instanceof IRawTerm ) )
121  .collect( Collectors.toMap( ITerm::functor, CQueryLiteral::root, ( i, j ) -> i ) )
122  .values()
123  .stream()
124  .collect( Collectors.joining( " " ) )
125  .trim();
126  }
127 
133  @Nonnull
134  private static String valueformat( @Nonnull final ILiteral p_literal )
135  {
136  return p_literal.values()
137  .filter( i -> i instanceof IRawTerm<?> )
138  .findFirst()
139  .map( ITerm::raw )
141  .map( i -> p_literal.functor() + " : " + i )
142  .orElse( "" );
143  }
144 
151  private static String typeformat( final Object p_value )
152  {
153  if ( p_value instanceof String )
154  return MessageFormat.format( "\"{0}\"", p_value );
155 
156  if ( p_value instanceof Collection<?> )
157  return MessageFormat.format(
158  "[{0}]",
159  ( (Collection<?>) p_value ).stream().map( CQueryLiteral::typeformat ).collect( Collectors.joining( ", " ) )
160  );
161 
162  return p_value.toString();
163  }
164 
165 }
static String typeformat(final Object p_value)
type mapping
String functor()
returns the functor without path
static String fields( @Nonnull final Stream< ITerm > p_stream)
creates the field list
static String root( @Nonnull final ILiteral p_literal)
converts a literal query structure
< T > T raw()
cast to any raw value type
static String valueformat( @Nonnull final ILiteral p_literal)
format query argument
final String argumentquery( @Nonnull final ITerm p_argument)
static String values( @Nonnull final Stream< ITerm > p_stream)
argument values