LightJason - AgentSpeak(L++)
matrix/CToBlas.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.bit.matrix;
25 
26 import cern.colt.matrix.tbit.BitMatrix;
27 import cern.colt.matrix.tdouble.DoubleMatrix2D;
28 import cern.colt.matrix.tdouble.impl.DenseDoubleMatrix2D;
29 import cern.colt.matrix.tdouble.impl.SparseDoubleMatrix2D;
38 
39 import javax.annotation.Nonnegative;
40 import javax.annotation.Nonnull;
41 import java.util.List;
42 import java.util.stream.Collectors;
43 import java.util.stream.IntStream;
44 
45 
55 public final class CToBlas extends IBuiltinAction
56 {
60  private static final long serialVersionUID = -5682613760524318512L;
61 
65  public CToBlas()
66  {
67  super( 4 );
68  }
69 
70  @Nonnegative
71  @Override
72  public final int minimalArgumentNumber()
73  {
74  return 1;
75  }
76 
77  @Nonnull
78  @Override
79  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
80  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
81  {
82  final List<ITerm> l_arguments = CCommon.flatten( p_argument ).collect( Collectors.toList() );
83  final EType l_type = l_arguments.parallelStream()
84  .filter( i -> CCommon.rawvalueAssignableTo( i, String.class ) )
85  .findFirst().map( i -> EType.from( i.<String>raw() ) )
86  .orElse( EType.SPARSE );
87 
88  switch ( l_type )
89  {
90  case DENSE:
91  l_arguments.stream()
92  .filter( i -> CCommon.rawvalueAssignableTo( i, BitMatrix.class ) )
93  .map( ITerm::<BitMatrix>raw )
94  .map( i -> CToBlas.tomatrix( i, new DenseDoubleMatrix2D( i.rows(), i.columns() ) ) )
95  .map( CRawTerm::from )
96  .forEach( p_return::add );
97 
98  return CFuzzyValue.from( true );
99 
100 
101  case SPARSE:
102  l_arguments.stream()
103  .filter( i -> CCommon.rawvalueAssignableTo( i, BitMatrix.class ) )
104  .map( ITerm::<BitMatrix>raw )
105  .map( i -> CToBlas.tomatrix( i, new SparseDoubleMatrix2D( i.rows(), i.columns() ) ) )
106  .map( CRawTerm::from )
107  .forEach( p_return::add );
108 
109  return CFuzzyValue.from( true );
110 
111  default:
112 
113  return CFuzzyValue.from( false );
114  }
115  }
116 
117 
125  @Nonnull
126  private static DoubleMatrix2D tomatrix( @Nonnull final BitMatrix p_source, @Nonnull final DoubleMatrix2D p_target )
127  {
128  IntStream.range( 0, p_source.rows() )
129  .forEach( r -> IntStream.range( 0, p_source.columns() ).forEach( c -> p_target.setQuick( r, c, p_source.getQuick( r, c ) ? 1 : 0 ) ) );
130 
131  return p_target;
132  }
133 }
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
final int minimalArgumentNumber()
minimum number of arguments
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
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
static< T > boolean rawvalueAssignableTo( @Nonnull final T p_value, @Nonnull final Class<?>... p_class)
checks a term value for assignable class
result for an immutable fuzzy value
static DoubleMatrix2D tomatrix( @Nonnull final BitMatrix p_source, @Nonnull final DoubleMatrix2D p_target)
converts the bit matrix to the blas matrix
static EType from(final String p_name)
additional factory
Definition: EType.java:53
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