CCreate.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.collection.set;

  24. import org.lightjason.agentspeak.action.builtin.IBuiltinAction;
  25. import org.lightjason.agentspeak.language.CCommon;
  26. import org.lightjason.agentspeak.language.CRawTerm;
  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.Nonnull;
  32. import java.util.Collections;
  33. import java.util.List;
  34. import java.util.Set;
  35. import java.util.stream.Collectors;


  36. /**
  37.  * action to create a set.
  38.  * The action creates a set and put all given arguments
  39.  * inside the set, the action never fails
  40.  *
  41.  * {@code
  42.     S = collection/set/create( "1", [ 1, 2, 3] );
  43.     S = collection/set/create();
  44.  * }
  45.  */
  46. public final class CCreate extends IBuiltinAction
  47. {
  48.     /**
  49.      * serial id
  50.      */
  51.     private static final long serialVersionUID = 6965219782366810100L;

  52.     /**
  53.      * ctor
  54.      */
  55.     public CCreate()
  56.     {
  57.         super( 3 );
  58.     }

  59.     @Nonnull
  60.     @Override
  61.     public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
  62.                                                @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
  63.     )
  64.     {
  65.         final Set<?> l_return = CCommon.flatten( p_argument ).map( ITerm::raw ).collect( Collectors.toSet() );

  66.         p_return.add(
  67.             CRawTerm.from(
  68.                 p_parallel
  69.                 ? Collections.synchronizedSet( l_return )
  70.                 : l_return
  71.             )
  72.         );

  73.         return CFuzzyValue.from( true );
  74.     }
  75. }