LightJason - AgentSpeak(L++)
ISelection.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.math.statistic;
25 
33 
34 import javax.annotation.Nonnegative;
35 import javax.annotation.Nonnull;
36 import java.util.List;
37 import java.util.Random;
38 import java.util.stream.Collectors;
39 import java.util.stream.Stream;
40 
41 
46 public abstract class ISelection extends IBuiltinAction
47 {
51  private static final long serialVersionUID = -365949510289020495L;
55  private final Random m_random = new Random();
56 
60  protected ISelection()
61  {
62  super( 3 );
63  }
64 
65  @Nonnegative
66  @Override
67  public final int minimalArgumentNumber()
68  {
69  return 2 + this.additionalArgumentNumber();
70  }
71 
72  @Nonnull
73  @Override
74  @SuppressWarnings( "unchecked" )
75  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
76  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
77  {
78  // first parameter is a list with elements, which will return by the selection
79  // second parameter is a numeric value for each element
80  final List<Object> l_items = p_argument.get( 0 ).<List<Object>>raw().stream()
81  .map( i -> i instanceof ITerm ? ( CCommon.replaceFromContext( p_context, (ITerm) i ) ).raw() : i )
82  .collect( Collectors.toList() );
83  final List<Double> l_weight = this.weight(
84  l_items,
85  p_argument.get( 1 ).<List<?>>raw().stream()
86  // list can be contains default Java objects or term objects
87  .map( i -> i instanceof ITerm ? ( CCommon.replaceFromContext( p_context, (ITerm) i ) ).<Number>raw() : (Number) i )
88  .map( Number::doubleValue )
89  .map( Math::abs ),
90  p_argument.subList( 2, p_argument.size() )
91  );
92 
93  if ( ( l_items.isEmpty() ) || ( l_items.size() != l_weight.size() ) )
94  return CFuzzyValue.from( false );
95 
96  // select a random value and scale with the sum
97  double l_random = m_random.nextDouble() * l_weight.stream().mapToDouble( i -> i ).sum();
98  for ( int i = 0; i < l_weight.size(); i++ )
99  {
100  l_random -= l_weight.get( i );
101  if ( l_random <= 0 )
102  {
103  p_return.add( CRawTerm.from( l_items.get( i ) ) );
104  return CFuzzyValue.from( true );
105  }
106  }
107 
108  // on rounding error return last element
109  p_return.add( CRawTerm.from( l_items.get( l_items.size() - 1 ) ) );
110  return CFuzzyValue.from( true );
111  }
112 
121  @Nonnull
122  protected abstract List<Double> weight( @Nonnull final List<?> p_items, @Nonnull final Stream<Double> p_values, @Nonnull final List<ITerm> p_argument );
123 
129  protected int additionalArgumentNumber()
130  {
131  return 0;
132  }
133 }
base class of build-in actions for setting name by package/classname (without prefix character) ...
abstract class for creating a selection one element of a list based on a fitness weight ...
Definition: ISelection.java:46
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
common structure for execution definition
abstract List< Double > weight( @Nonnull final List<?> p_items, @Nonnull final Stream< Double > p_values, @Nonnull final List< ITerm > p_argument)
modifies the weights
final int minimalArgumentNumber()
minimum number of arguments
Definition: ISelection.java:67
execution context with local data
Definition: IContext.java:42
static List< ITerm > replaceFromContext( @Nonnull final IContext p_context, @Nonnull final Collection<? extends ITerm > p_terms)
replace variables with context variables
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: ISelection.java:75
result for an immutable fuzzy value
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
term structure for simple datatypes
Definition: CRawTerm.java:45