CCreateStatistic.java

  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. package org.lightjason.agentspeak.action.builtin.math.statistic;

  24. import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
  25. import org.apache.commons.math3.stat.descriptive.StatisticalSummary;
  26. import org.apache.commons.math3.stat.descriptive.SummaryStatistics;
  27. import org.apache.commons.math3.stat.descriptive.SynchronizedDescriptiveStatistics;
  28. import org.apache.commons.math3.stat.descriptive.SynchronizedSummaryStatistics;
  29. import org.lightjason.agentspeak.action.builtin.IBuiltinAction;
  30. import org.lightjason.agentspeak.error.CIllegalStateException;
  31. import org.lightjason.agentspeak.language.CCommon;
  32. import org.lightjason.agentspeak.language.CRawTerm;
  33. import org.lightjason.agentspeak.language.ITerm;
  34. import org.lightjason.agentspeak.language.execution.IContext;
  35. import org.lightjason.agentspeak.language.fuzzy.CFuzzyValue;
  36. import org.lightjason.agentspeak.language.fuzzy.IFuzzyValue;

  37. import javax.annotation.Nonnull;
  38. import java.util.List;
  39. import java.util.Locale;
  40. import java.util.stream.Stream;


  41. /**
  42.  * action to create a statistic.
  43.  * The action creates statistic objects to collect data,
  44.  * each argument must be a string with "summary" or "descriptive"
  45.  * for a summary or descriptive statistic object, on no arguments
  46.  * a summary statistic object is created, the action never fails
  47.  *
  48.  * {@code [S1|S2] = math/statistic/createstaistic("summary", ["descriptive"]);}
  49.  * @see http://commons.apache.org/proper/commons-math/userguide/stat.html
  50.  */
  51. public final class CCreateStatistic extends IBuiltinAction
  52. {
  53.     /**
  54.      * serial id
  55.      */
  56.     private static final long serialVersionUID = -8275199738531829131L;

  57.     /**
  58.      * ctor
  59.      */
  60.     public CCreateStatistic()
  61.     {
  62.         super( 3 );
  63.     }

  64.     @Nonnull
  65.     @Override
  66.     public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
  67.                                                @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
  68.     {

  69.         (
  70.             p_argument.size() == 0
  71.             ? Stream.of( EType.SUMMARY )
  72.             : CCommon.flatten( p_argument ).map( ITerm::<String>raw ).map( EType::from )
  73.         ).map( i -> i.generate( p_parallel ) )
  74.          .map( CRawTerm::from ).forEach( p_return::add );

  75.         return CFuzzyValue.from( true );
  76.     }


  77.     /**
  78.      * enume statistic type
  79.      */
  80.     private enum EType
  81.     {
  82.         SUMMARY,
  83.         DESCRIPTIVE;

  84.         /**
  85.          * additional factory
  86.          *
  87.          * @param p_value string
  88.          * @return enum
  89.          */
  90.         @Nonnull
  91.         public static EType from( @Nonnull final String p_value )
  92.         {
  93.             return EType.valueOf( p_value.trim().toUpperCase( Locale.ROOT ) );
  94.         }

  95.         /**
  96.          * returns the statistic object
  97.          *
  98.          * @param p_parallel parallel-safe
  99.          * @return statistic object
  100.          */
  101.         @Nonnull
  102.         public final StatisticalSummary generate( @Nonnull final Boolean p_parallel )
  103.         {
  104.             switch ( this )
  105.             {
  106.                 case SUMMARY:
  107.                     return p_parallel
  108.                            ? new SynchronizedSummaryStatistics()
  109.                            : new SummaryStatistics();

  110.                 case DESCRIPTIVE:
  111.                     return p_parallel
  112.                            ? new SynchronizedDescriptiveStatistics()
  113.                            : new DescriptiveStatistics();

  114.                 default:
  115.                     throw new CIllegalStateException( org.lightjason.agentspeak.common.CCommon.languagestring( this, "unknown", this ) );
  116.             }
  117.         }
  118.     }
  119. }