LightJason - AgentSpeak(L++)
CEqual.java
Go to the documentation of this file.
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 
24 package org.lightjason.agentspeak.action.builtin.bool;
25 
26 import com.google.common.collect.Multimap;
34 
35 import javax.annotation.Nonnegative;
36 import javax.annotation.Nonnull;
37 import java.util.Arrays;
38 import java.util.Collection;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.stream.Stream;
42 
43 
55 public class CEqual extends IBuiltinAction
56 {
60  private static final long serialVersionUID = -2953614515361905328L;
61 
62  @Nonnegative
63  @Override
64  public final int minimalArgumentNumber()
65  {
66  return 2;
67  }
68 
69  @Nonnull
70  @Override
71  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
72  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
73  )
74  {
75  if ( CCommon.rawvalueAssignableTo( p_argument.get( 0 ), Collection.class ) )
76  return this.pack(
77  p_return,
78  p_argument.stream()
79  .skip( 1 )
80  .map( i -> p_argument.get( 0 ).equals( i )
81  || ( CCommon.rawvalueAssignableTo( i, Collection.class )
82  && equalcollection( p_argument.get( 0 ).<Collection<?>>raw().toArray(), i.raw() ) )
83  )
84  );
85 
86  if ( CCommon.rawvalueAssignableTo( p_argument.get( 0 ), Map.class ) )
87  return this.pack(
88  p_return,
89  p_argument.stream()
90  .skip( 1 )
91  .map( i -> p_argument.get( 0 ).equals( i )
92  || ( CCommon.rawvalueAssignableTo( i, Map.class )
93  && equalmap( p_argument.get( 0 ).<Map<?, ?>>raw(), i.<Map<?, ?>>raw() ) )
94  )
95  );
96 
97  if ( CCommon.rawvalueAssignableTo( p_argument.get( 0 ), Multimap.class ) )
98  return this.pack(
99  p_return,
100  p_argument.stream()
101  .skip( 1 )
102  .map( i -> p_argument.get( 0 ).equals( i )
103  || ( CCommon.rawvalueAssignableTo( i, Multimap.class )
104  && equalmultimap( p_argument.get( 0 ).<Multimap<?, ?>>raw(), i.<Multimap<?, ?>>raw() ) )
105  )
106  );
107 
108 
109  return this.pack(
110  p_return,
111  p_argument.stream()
112  .skip( 1 )
113  .map( i -> equalobject( p_argument.get( 0 ).<Object>raw(), i.<Object>raw() ) )
114  );
115  }
116 
117 
124  protected boolean apply( final boolean p_value )
125  {
126  return p_value;
127  }
128 
129 
137  private IFuzzyValue<Boolean> pack( @Nonnull final List<ITerm> p_return, @Nonnull final Stream<Boolean> p_stream )
138  {
139  p_stream.map( this::apply )
140  .map( CRawTerm::from )
141  .forEach( p_return::add );
142 
143  return CFuzzyValue.from( true );
144  }
145 
146 
153  private static boolean equalobject( @Nonnull final Object p_source, @Nonnull final Object p_target )
154  {
155  return p_source.equals( p_target );
156  }
157 
158 
166  private static boolean equalcollection( @Nonnull final Object[] p_source, @Nonnull final Collection<?> p_target )
167  {
168  return Arrays.equals( p_source, p_target.toArray() );
169  }
170 
171 
179  private static boolean equalmap( @Nonnull final Map<?, ?> p_source, @Nonnull final Map<?, ?> p_target )
180  {
181  return Arrays.equals( p_source.keySet().toArray(), p_target.keySet().toArray() )
182  && Arrays.equals( p_source.values().toArray(), p_target.values().toArray() );
183  }
184 
185 
193  private static boolean equalmultimap( @Nonnull final Multimap<?, ?> p_source, @Nonnull final Multimap<?, ?> p_target )
194  {
195  return Arrays.equals( p_source.asMap().keySet().toArray(), p_target.asMap().keySet().toArray() )
196  && Arrays.equals( p_source.values().toArray(), p_target.values().toArray() );
197  }
198 
199 }
base class of build-in actions for setting name by package/classname (without prefix character) ...
static boolean equalobject( @Nonnull final Object p_source, @Nonnull final Object p_target)
compare any objects
Definition: CEqual.java:153
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
common structure for execution definition
static boolean equalcollection( @Nonnull final Object[] p_source, @Nonnull final Collection<?> p_target)
compares collections
Definition: CEqual.java:166
IFuzzyValue< Boolean > pack( @Nonnull final List< ITerm > p_return, @Nonnull final Stream< Boolean > p_stream)
pack the result values into term
Definition: CEqual.java:137
execution context with local data
Definition: IContext.java:42
static boolean equalmultimap( @Nonnull final Multimap<?, ?> p_source, @Nonnull final Multimap<?, ?> p_target)
compare multimap
Definition: CEqual.java:193
static boolean equalmap( @Nonnull final Map<?, ?> p_source, @Nonnull final Map<?, ?> p_target)
compare maps
Definition: CEqual.java:179
boolean apply(final boolean p_value)
apply to change boolean result
Definition: CEqual.java:124
final int minimalArgumentNumber()
minimum number of arguments
Definition: CEqual.java:64
static< T > boolean rawvalueAssignableTo( @Nonnull final T p_value, @Nonnull final Class<?>... p_class)
checks a term value for assignable class
result for an immutable fuzzy value
final IFuzzyValue< Boolean > execute(final boolean p_parallel, @Nonnull final IContext p_context, @Nonnull final List< ITerm > p_argument, @Nonnull final List< ITerm > p_return)
defines a plan-body operation
Definition: CEqual.java:71
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
static final long serialVersionUID
serial id
Definition: CEqual.java:60
term structure for simple datatypes
Definition: CRawTerm.java:45