CSymmetricDifference.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 com.google.common.collect.ConcurrentHashMultiset;
  25. import com.google.common.collect.Multiset;
  26. import org.lightjason.agentspeak.action.builtin.IBuiltinAction;
  27. import org.lightjason.agentspeak.language.CCommon;
  28. import org.lightjason.agentspeak.language.CRawTerm;
  29. import org.lightjason.agentspeak.language.ITerm;
  30. import org.lightjason.agentspeak.language.execution.IContext;
  31. import org.lightjason.agentspeak.language.fuzzy.CFuzzyValue;
  32. import org.lightjason.agentspeak.language.fuzzy.IFuzzyValue;

  33. import javax.annotation.Nonnegative;
  34. import javax.annotation.Nonnull;
  35. import java.util.Collections;
  36. import java.util.Comparator;
  37. import java.util.List;
  38. import java.util.stream.Collectors;


  39. /**
  40.  * creates the symmetric difference between lists (difference of union and intersection).
  41.  * Creates the symmetric difference of all arguments, so all arguments are collections and the action will return
  42.  * a list with the symmetric difference \f$ (\mathbb{X} \setminus \mathbb{Y}) \cup (\mathbb{B} \setminus \mathbb{A}) \f$,
  43.  * the action fails never
  44.  *
  45.  * {@code D = collection/list/symmetricdifference( [1,2,[3,4]], [7,8,9,4], [[1,2], [3]] );}
  46.  * @see https://en.wikipedia.org/wiki/Symmetric_difference
  47.  */
  48. public final class CSymmetricDifference extends IBuiltinAction
  49. {

  50.     /**
  51.      * serial id
  52.      */
  53.     private static final long serialVersionUID = 7657032978898575726L;

  54.     /**
  55.      * ctor
  56.      */
  57.     public CSymmetricDifference()
  58.     {
  59.         super( 3 );
  60.     }

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

  67.     @Nonnull
  68.     @Override
  69.     public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
  70.                                                @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
  71.     {
  72.         // create a multiset and counts the occurence of element -> on an odd number the element will be returned
  73.         final Multiset<Object> l_count = ConcurrentHashMultiset.create();
  74.         CCommon.flatten( p_argument ).parallel().map( ITerm::raw ).forEach( l_count::add );
  75.         final List<Object> l_result = l_count.entrySet()
  76.                                              .parallelStream()
  77.                                              .filter( i -> i.getCount() % 2 == 1 )
  78.                                              .map( Multiset.Entry::getElement ).collect( Collectors.toList() );
  79.         l_result.sort( Comparator.comparing( Object::hashCode ) );

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

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

  89. }