CFromList.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.List;


  37. /**
  38.  * creates a dense- or sparse-vector from a list.
  39.  * All input arguments will be converted to a
  40.  * dense or sparse vector, so the arguments must be
  41.  * lists of numbers, the last optional argument can be a string
  42.  * with "dense | sparse" to create dense or sparse structures,
  43.  * the action never fails
  44.  *
  45.  * {@code [V1|V2] = math/blas/vector( [1,2,3], [4,5,6], "dense | sparse" );}
  46.  */
  47. public final class CFromList extends IBuiltinAction
  48. {
  49.     /**
  50.      * serial id
  51.      */
  52.     private static final long serialVersionUID = -144138778430324185L;

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

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

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


  84.         // create vectors from lists
  85.         p_argument.stream()
  86.                   .limit( l_limit )
  87.                   .map( ITerm::<List<Number>>raw )
  88.                   .map( i -> i.stream().mapToDouble( Number::doubleValue ).toArray() )
  89.                   .map( i ->
  90.                   {
  91.                       switch ( l_type )
  92.                       {
  93.                           case SPARSE:
  94.                               return new SparseDoubleMatrix1D( i );
  95.                           default:
  96.                               return new DenseDoubleMatrix1D( i );
  97.                       }
  98.                   } )
  99.                   .map( CRawTerm::from )
  100.                   .forEach( p_return::add );

  101.         return CFuzzyValue.from( true );
  102.     }

  103. }