LightJason - AgentSpeak(L++)
math/interpolate/CCreate.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.math.interpolate;
25 
26 import org.apache.commons.math3.analysis.UnivariateFunction;
27 import org.apache.commons.math3.analysis.interpolation.AkimaSplineInterpolator;
28 import org.apache.commons.math3.analysis.interpolation.DividedDifferenceInterpolator;
29 import org.apache.commons.math3.analysis.interpolation.LinearInterpolator;
30 import org.apache.commons.math3.analysis.interpolation.LoessInterpolator;
31 import org.apache.commons.math3.analysis.interpolation.NevilleInterpolator;
40 
41 import javax.annotation.Nonnegative;
42 import javax.annotation.Nonnull;
43 import java.util.List;
44 import java.util.Locale;
45 import java.util.stream.Collectors;
46 
47 
64 public final class CCreate extends IBuiltinAction
65 {
69  private static final long serialVersionUID = 8425815343500575790L;
70 
74  public CCreate()
75  {
76  super( 3 );
77  }
78 
79  @Nonnegative
80  @Override
81  public final int minimalArgumentNumber()
82  {
83  return 2;
84  }
85 
86  @Nonnull
87  @Override
88  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
89  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
90  {
91  final List<ITerm> l_arguments = CCommon.flatten( p_argument ).collect( Collectors.toList() );
92  if ( l_arguments.size() % 2 == 0 )
93  return CFuzzyValue.from( false );
94 
95  final int l_datasize = ( l_arguments.size() - 1 ) / 2;
96  p_return.add(
97  CRawTerm.from(
98  EType.from( l_arguments.get( 0 ).<String>raw() )
99  .get(
100 
101  l_arguments.stream()
102  .skip( 1 )
103  .limit( l_datasize )
104  .map( ITerm::<Number>raw )
105  .mapToDouble( Number::doubleValue )
106  .toArray(),
107 
108  l_arguments.stream()
109  .skip( l_datasize + 1 )
110  .map( ITerm::<Number>raw )
111  .mapToDouble( Number::doubleValue )
112  .toArray()
113  )
114  )
115  );
116 
117  return CFuzzyValue.from( true );
118  }
119 
120 
124  private enum EType
125  {
131 
132 
139  public static EType from( final String p_value )
140  {
141  return EType.valueOf( p_value.trim().toUpperCase( Locale.ROOT ) );
142  }
143 
151  public final UnivariateFunction get( final double[] p_xvalues, final double[] p_yvalues )
152  {
153  switch ( this )
154  {
155  case AKIMA:
156  return new AkimaSplineInterpolator().interpolate( p_xvalues, p_yvalues );
157 
158  case DIVIDEDDIFFERENCE:
159  return new DividedDifferenceInterpolator().interpolate( p_xvalues, p_yvalues );
160 
161  case LINEAR:
162  return new LinearInterpolator().interpolate( p_xvalues, p_yvalues );
163 
164  case LOESS:
165  return new LoessInterpolator().interpolate( p_xvalues, p_yvalues );
166 
167  case NEVILLE:
168  return new NevilleInterpolator().interpolate( p_xvalues, p_yvalues );
169 
170  default:
171  throw new CIllegalStateException( org.lightjason.agentspeak.common.CCommon.languagestring( this, "unknown", this ) );
172  }
173  }
174  }
175 
176 }
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< T > String languagestring(final T p_source, final String p_label, final Object... p_parameter)
returns the language depend string on any object
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
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
result for an immutable fuzzy value
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