CEqual.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.bool;

  24. import com.google.common.collect.Multimap;
  25. import org.lightjason.agentspeak.action.builtin.IBuiltinAction;
  26. import org.lightjason.agentspeak.language.CCommon;
  27. import org.lightjason.agentspeak.language.CRawTerm;
  28. import org.lightjason.agentspeak.language.ITerm;
  29. import org.lightjason.agentspeak.language.execution.IContext;
  30. import org.lightjason.agentspeak.language.fuzzy.CFuzzyValue;
  31. import org.lightjason.agentspeak.language.fuzzy.IFuzzyValue;

  32. import javax.annotation.Nonnegative;
  33. import javax.annotation.Nonnull;
  34. import java.util.Arrays;
  35. import java.util.Collection;
  36. import java.util.List;
  37. import java.util.Map;
  38. import java.util.stream.Stream;


  39. /**
  40.  * checks elements of equality.
  41.  * The actions checks the first argument
  42.  * to all others arguments of equality,
  43.  * list structures won't be unflaten, but
  44.  * elementwise compared, the action never fails.
  45.  * On number arguments not the value must equal, also the type (double / integral) must be equal,
  46.  * so keep in mind, that you use the correct number type on the argument input
  47.  *
  48.  * {@code [E1|E2] = bool/equal( "this is equal", "this is equal", [123, "test"] );}
  49.  */
  50. public class CEqual extends IBuiltinAction
  51. {
  52.     /**
  53.      * serial id
  54.      */
  55.     private static final long serialVersionUID = -2953614515361905328L;

  56.     @Nonnegative
  57.     @Override
  58.     public final int minimalArgumentNumber()
  59.     {
  60.         return 2;
  61.     }

  62.     @Nonnull
  63.     @Override
  64.     public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
  65.                                                @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
  66.     )
  67.     {
  68.         if ( CCommon.rawvalueAssignableTo( p_argument.get( 0 ), Collection.class ) )
  69.             return this.pack(
  70.                 p_return,
  71.                 p_argument.stream()
  72.                           .skip( 1 )
  73.                           .map( i -> p_argument.get( 0 ).equals( i )
  74.                                      || ( CCommon.rawvalueAssignableTo( i, Collection.class )
  75.                                      && equalcollection(  p_argument.get( 0 ).<Collection<?>>raw().toArray(), i.raw() ) )
  76.                           )
  77.             );

  78.         if ( CCommon.rawvalueAssignableTo( p_argument.get( 0 ), Map.class ) )
  79.             return this.pack(
  80.                 p_return,
  81.                 p_argument.stream()
  82.                           .skip( 1 )
  83.                           .map( i -> p_argument.get( 0 ).equals( i )
  84.                                      || ( CCommon.rawvalueAssignableTo( i, Map.class )
  85.                                      && equalmap(  p_argument.get( 0 ).<Map<?, ?>>raw(), i.<Map<?, ?>>raw() ) )
  86.                           )
  87.             );

  88.         if ( CCommon.rawvalueAssignableTo( p_argument.get( 0 ), Multimap.class ) )
  89.             return this.pack(
  90.                 p_return,
  91.                 p_argument.stream()
  92.                           .skip( 1 )
  93.                           .map( i -> p_argument.get( 0 ).equals( i )
  94.                                      || ( CCommon.rawvalueAssignableTo( i, Multimap.class )
  95.                                      && equalmultimap(  p_argument.get( 0 ).<Multimap<?, ?>>raw(), i.<Multimap<?, ?>>raw() ) )
  96.                           )
  97.             );


  98.         return this.pack(
  99.             p_return,
  100.             p_argument.stream()
  101.                       .skip( 1 )
  102.                       .map( i -> equalobject(  p_argument.get( 0 ).<Object>raw(), i.<Object>raw() ) )
  103.         );
  104.     }


  105.     /**
  106.      * apply to change boolean result
  107.      *
  108.      * @param p_value boolean result
  109.      * @return boolean value
  110.      */
  111.     protected boolean apply( final boolean p_value )
  112.     {
  113.         return p_value;
  114.     }


  115.     /**
  116.      * pack the result values into term
  117.      *
  118.      * @param p_return return item list
  119.      * @param p_stream boolean input stream
  120.      * @return boolean flag
  121.      */
  122.     private IFuzzyValue<Boolean> pack( @Nonnull final List<ITerm> p_return, @Nonnull final Stream<Boolean> p_stream )
  123.     {
  124.         p_stream.map( this::apply )
  125.                 .map( CRawTerm::from )
  126.                 .forEach( p_return::add );

  127.         return CFuzzyValue.from( true );
  128.     }


  129.     /**
  130.      * compare any objects
  131.      * @param p_source source object
  132.      * @param p_target object to compare
  133.      * @return equality boolean flag
  134.      */
  135.     private static boolean equalobject( @Nonnull final Object p_source, @Nonnull final Object p_target )
  136.     {
  137.         return p_source.equals( p_target );
  138.     }


  139.     /**
  140.      * compares collections
  141.      *
  142.      * @param p_source source array (converted collection to array)
  143.      * @param p_target collection to compare
  144.      * @return equality boolean flag
  145.      */
  146.     private static boolean equalcollection( @Nonnull final Object[] p_source, @Nonnull final Collection<?> p_target )
  147.     {
  148.         return Arrays.equals( p_source, p_target.toArray() );
  149.     }


  150.     /**
  151.      * compare maps
  152.      *
  153.      * @param p_source source map
  154.      * @param p_target map to compare
  155.      * @return equality boolean flag
  156.      */
  157.     private static boolean equalmap( @Nonnull final Map<?, ?> p_source, @Nonnull final Map<?, ?> p_target )
  158.     {
  159.         return Arrays.equals( p_source.keySet().toArray(), p_target.keySet().toArray() )
  160.                && Arrays.equals( p_source.values().toArray(), p_target.values().toArray() );
  161.     }


  162.     /**
  163.      * compare multimap
  164.      *
  165.      * @param p_source source multimap
  166.      * @param p_target multimap to compare
  167.      * @return equality boolean flag
  168.      */
  169.     private static boolean equalmultimap( @Nonnull final Multimap<?, ?> p_source, @Nonnull final Multimap<?, ?> p_target )
  170.     {
  171.         return Arrays.equals( p_source.asMap().keySet().toArray(), p_target.asMap().keySet().toArray() )
  172.                && Arrays.equals( p_source.values().toArray(), p_target.values().toArray() );
  173.     }

  174. }