CIntersect.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.list;

  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.Nonnegative;
  32. import javax.annotation.Nonnull;
  33. import java.util.Collection;
  34. import java.util.Collections;
  35. import java.util.Comparator;
  36. import java.util.List;
  37. import java.util.stream.Collectors;


  38. /**
  39.  * creates the intersection between lists.
  40.  * All arguments are lists and the action returns the
  41.  * intersection \f$ \cap M_i \forall i \in \mathbb{N} \f$, the action fails never
  42.  *
  43.  * {@code I = collection/list/intersect( [1,2,[3,4]], [3,4,[8,9]], [1,2,3,5] );}
  44.  */
  45. public final class CIntersect extends IBuiltinAction
  46. {

  47.     /**
  48.      * serial id
  49.      */
  50.     private static final long serialVersionUID = 7453409804177199062L;

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

  58.     @Nonnegative
  59.     @Override
  60.     public final int minimalArgumentNumber()
  61.     {
  62.         return 2;
  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.         // all arguments must be lists (build unique list of all elements and check all collection if an element exists in each collection)
  70.         final List<Object> l_result = CCommon.flatten( p_argument )
  71.                                         .parallel()
  72.                                         .map( ITerm::raw )
  73.                                         .distinct()
  74.                                         .filter(
  75.                                             i -> p_argument.parallelStream()
  76.                                                            .allMatch( j -> j.<Collection<ITerm>>raw()
  77.                                                                .parallelStream()
  78.                                                                .map( ITerm::raw )
  79.                                                                .collect( Collectors.toList() )
  80.                                                                .contains( i )
  81.                                                            )
  82.                                         ).collect( Collectors.toList() );
  83.         l_result.sort( Comparator.comparing( Object::hashCode ) );

  84.         p_return.add( CRawTerm.from(
  85.             p_parallel ? Collections.synchronizedList( l_result ) : l_result
  86.         ) );

  87.         return CFuzzyValue.from( true );
  88.     }

  89. }