LightJason - AgentSpeak(L++)
grammar/CCommon.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.grammar;
25 
26 import com.codepoetics.protonpack.StreamUtils;
31 
32 import javax.annotation.Nonnull;
33 import java.util.AbstractMap;
34 import java.util.Collection;
35 import java.util.Collections;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.stream.Collectors;
39 import java.util.stream.Stream;
40 
41 
45 public final class CCommon
46 {
47 
51  static final Map<String, Double> NUMERICCONSTANT = Collections.unmodifiableMap(
52  StreamUtils.zip(
53  Stream.of(
54  "pi",
55  "euler",
56  "lightspeed",
57  "avogadro",
58  "boltzmann",
59  "gravity",
60  "electron",
61  "neutron",
62  "proton",
63  "positiveinfinity",
64  "negativeinfinity",
65  "maximumvalue",
66  "minimumvalue",
67  "nan"
68  ),
69 
70  Stream.of(
71  Math.PI,
72  Math.E,
73  299792458.0,
74  6.0221412927e23,
75  8.617330350e-15,
76  6.67408e-11,
77  9.10938356e-31,
78  1674927471214e-27,
79  1.6726219e-27,
80  Double.POSITIVE_INFINITY,
81  Double.NEGATIVE_INFINITY,
82  Double.MAX_VALUE,
83  Double.MIN_VALUE,
84  Double.NaN
85  ),
86 
87  AbstractMap.SimpleImmutableEntry::new
88  ).collect( Collectors.toMap( Map.Entry::getKey, Map.Entry::getValue ) ) );
89 
90 
94  private CCommon()
95  {}
96 
105  @Nonnull
106  static IExpression createLogicalBinaryExpression( @Nonnull final EOperator p_operator, @Nonnull final IExpression p_lefthandside,
107  @Nonnull final Collection<IExpression> p_righthandside
108  )
109  {
110  if ( ( !p_operator.isBinary() ) || ( !p_operator.isLogical() ) )
111  throw new CSyntaxErrorException( org.lightjason.agentspeak.common.CCommon.languagestring( CCommon.class, "notbinarylogicoperator", p_operator ) );
112 
113  final List<IExpression> l_expression = Stream.concat( Stream.of( p_lefthandside ), p_righthandside.stream() ).collect( Collectors.toList() );
114 
115  // only left-hand-side is existing
116  if ( l_expression.size() == 1 )
117  return l_expression.get( 0 );
118 
119 
120  // otherwise creare concat expression
121  while ( l_expression.size() > 1 )
122  l_expression.add( 0, new CBinary( p_operator, l_expression.remove( 0 ), l_expression.remove( 0 ) ) );
123 
124  return l_expression.get( 0 );
125  }
126 }
static< T > String languagestring(final T p_source, final String p_label, final Object... p_parameter)
returns the language depend string on any object
static IExpression createLogicalBinaryExpression( @Nonnull final EOperator p_operator, @Nonnull final IExpression p_lefthandside, @Nonnull final Collection< IExpression > p_righthandside)
creates a logical expression concationation with single operator
static final Map< String, Double > NUMERICCONSTANT
numeric constant values - infinity is defined manually