CHammingDistance.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.matrix;

  24. import cern.colt.matrix.tbit.BitMatrix;
  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.List;
  35. import java.util.stream.Collectors;


  36. /**
  37.  * calculates the hamming distance.
  38.  * The action calculates between bit matrices,
  39.  * the distance will be calculated between the first
  40.  * and all other arguments, the action never fails
  41.  *
  42.  * {@code [A|B] = math/bit/matrix/hammingdistance( Matrix1, Matrix2, Matrix3 );}
  43.  * @see https://en.wikipedia.org/wiki/Hamming_distance
  44.  */
  45. public final class CHammingDistance extends IBuiltinAction
  46. {
  47.     /**
  48.      * serial id
  49.      */
  50.     private static final long serialVersionUID = 8167680292117761543L;

  51.     @Nonnegative
  52.     @Override
  53.     public final int minimalArgumentNumber()
  54.     {
  55.         return 1;
  56.     }

  57.     @Nonnull
  58.     @Override
  59.     public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
  60.                                                @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
  61.     {
  62.         final List<BitMatrix> l_arguments = CCommon.flatten( p_argument ).map( ITerm::<BitMatrix>raw ).collect( Collectors.toList() );
  63.         if ( l_arguments.size() < 2 )
  64.             return CFuzzyValue.from( false );

  65.         l_arguments.stream()
  66.                    .skip( 1 )
  67.                    .map( BitMatrix::copy )
  68.                    .peek( i -> i.xor( l_arguments.get( 0 ) ) )
  69.                    .mapToDouble( BitMatrix::cardinality )
  70.                    .boxed()
  71.                    .map( CRawTerm::from )
  72.                    .forEach( p_return::add );

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