CParse.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.blas.vector;

  24. import cern.colt.matrix.tdouble.impl.DenseDoubleMatrix1D;
  25. import cern.colt.matrix.tdouble.impl.SparseDoubleMatrix1D;
  26. import org.lightjason.agentspeak.action.builtin.IBuiltinAction;
  27. import org.lightjason.agentspeak.action.builtin.math.blas.EType;
  28. import org.lightjason.agentspeak.language.CCommon;
  29. import org.lightjason.agentspeak.language.CRawTerm;
  30. import org.lightjason.agentspeak.language.ITerm;
  31. import org.lightjason.agentspeak.language.execution.IContext;
  32. import org.lightjason.agentspeak.language.fuzzy.CFuzzyValue;
  33. import org.lightjason.agentspeak.language.fuzzy.IFuzzyValue;

  34. import javax.annotation.Nonnegative;
  35. import javax.annotation.Nonnull;
  36. import java.util.Arrays;
  37. import java.util.List;
  38. import java.util.stream.Collectors;


  39. /**
  40.  * creates a dense- or sparse-vector from as string.
  41.  * The action creates for each input argument a vector
  42.  * by parsing the string, the last string can be "dense | sparse"
  43.  * to defining a sparse / dense vector, the action never fails.
  44.  * Seperator is comma, semicolon or space
  45.  *
  46.  * {@code [V1|V2] = math/blas/vector/parse( "1,2,3", "7,8,9,10,12", "dense|dense" );}
  47.  */
  48. public final class CParse extends IBuiltinAction
  49. {
  50.     /**
  51.      * serial id
  52.      */
  53.     private static final long serialVersionUID = -6489913482373871730L;

  54.     /**
  55.      * ctor
  56.      */
  57.     public CParse()
  58.     {
  59.         super( 4 );
  60.     }

  61.     @Nonnegative
  62.     @Override
  63.     public final int minimalArgumentNumber()
  64.     {
  65.         return 1;
  66.     }

  67.     @Nonnull
  68.     @Override
  69.     public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
  70.                                                @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
  71.     {
  72.         final List<ITerm> l_arguments = CCommon.flatten( p_argument ).collect( Collectors.toList() );
  73.         final int l_limit;
  74.         final EType l_type;
  75.         if ( ( CCommon.rawvalueAssignableTo( l_arguments.get( l_arguments.size() - 1 ), String.class ) )
  76.              && ( EType.exists( l_arguments.get( l_arguments.size() - 1 ).<String>raw() ) ) )
  77.         {
  78.             l_type = EType.from( l_arguments.get( l_arguments.size() - 1 ).<String>raw() );
  79.             l_limit = l_arguments.size() - 1;
  80.         }
  81.         else
  82.         {
  83.             l_type = EType.DENSE;
  84.             l_limit = l_arguments.size();
  85.         }


  86.         // create vectors
  87.         switch ( l_type )
  88.         {
  89.             case DENSE:
  90.                 l_arguments.stream()
  91.                            .limit( l_limit )
  92.                            .map( ITerm::<String>raw )
  93.                            .map( CParse::parse )
  94.                            .map( DenseDoubleMatrix1D::new )
  95.                            .map( CRawTerm::from )
  96.                            .forEach( p_return::add );

  97.                 return CFuzzyValue.from( true );

  98.             case SPARSE:
  99.                 l_arguments.stream()
  100.                            .limit( l_limit )
  101.                            .map( ITerm::<String>raw )
  102.                            .map( CParse::parse )
  103.                            .map( SparseDoubleMatrix1D::new )
  104.                            .map( CRawTerm::from )
  105.                            .forEach( p_return::add );

  106.                 return CFuzzyValue.from( true );

  107.             default:
  108.         }

  109.         return CFuzzyValue.from( false );
  110.     }

  111.     /**
  112.      * parses the string
  113.      *
  114.      * @param p_string string
  115.      * @return double array
  116.      */
  117.     @Nonnull
  118.     private static double[] parse( @Nonnull final String p_string )
  119.     {
  120.         return Arrays.stream( p_string.split( ";|,|\\s" ) )
  121.                      .map( String::trim )
  122.                      .filter( i -> !i.isEmpty() )
  123.                      .mapToDouble( Double::parseDouble )
  124.                      .toArray();
  125.     }

  126. }