CSet.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.bit.vector;

  24. import cern.colt.matrix.tbit.BitVector;
  25. import org.lightjason.agentspeak.action.builtin.IBuiltinAction;
  26. import org.lightjason.agentspeak.language.CCommon;
  27. import org.lightjason.agentspeak.language.ITerm;
  28. import org.lightjason.agentspeak.language.execution.IContext;
  29. import org.lightjason.agentspeak.language.fuzzy.CFuzzyValue;
  30. import org.lightjason.agentspeak.language.fuzzy.IFuzzyValue;

  31. import javax.annotation.Nonnegative;
  32. import javax.annotation.Nonnull;
  33. import java.util.List;
  34. import java.util.stream.Collectors;


  35. /**
  36.  * sets bit position by index and value.
  37.  * The first argument is the bit vector, the second
  38.  * argument is a boolean value or number value (0 = false),
  39.  * all other values are index positions, each index bit
  40.  * within the bit vector will be set to the given value,
  41.  * the action never fails
  42.  *
  43.  * {@code math/bit/vector/set( BitVector, true, 1, [3, 7]);}
  44.  */
  45. public final class CSet extends IBuiltinAction
  46. {
  47.     /**
  48.      * serial id
  49.      */
  50.     private static final long serialVersionUID = 6269327474104817444L;

  51.     /**
  52.      * ctor
  53.      */
  54.     public CSet()
  55.     {
  56.         super( 4 );
  57.     }

  58.     @Nonnegative
  59.     @Override
  60.     public final int minimalArgumentNumber()
  61.     {
  62.         return 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.         final List<ITerm> l_arguments = CCommon.flatten( p_argument ).collect( Collectors.toList() );
  70.         final boolean l_value = CCommon.rawvalueAssignableTo( l_arguments.get( 1 ), Number.class )
  71.                                 ? l_arguments.get( 1 ).<Number>raw().intValue() != 0
  72.                                 : l_arguments.get( 1 ).<Boolean>raw();

  73.         l_arguments.stream()
  74.                    .skip( 2 )
  75.                    .map( ITerm::<Number>raw )
  76.                    .mapToInt( Number::intValue )
  77.                    .boxed()
  78.                    .forEach( i -> l_arguments.get( 0 ).<BitVector>raw().put( i, l_value ) );

  79.         return CFuzzyValue.from( true );
  80.     }
  81. }