LightJason - AgentSpeak(L++)
IApplyPathAlgorithm.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.graph;
25 
26 
27 import com.codepoetics.protonpack.StreamUtils;
28 import com.google.common.base.Function;
29 import edu.uci.ics.jung.graph.Graph;
37 
38 import javax.annotation.Nonnegative;
39 import javax.annotation.Nonnull;
40 import java.util.Collections;
41 import java.util.List;
42 import java.util.Map;
43 import java.util.stream.Collectors;
44 
45 
49 public abstract class IApplyPathAlgorithm extends IBuiltinAction
50 {
54  private static final long serialVersionUID = 1424878255126290894L;
58  private static final String DEFAULTWEIGHT = "defaultweight";
59 
60  @Nonnegative
61  @Override
62  public final int minimalArgumentNumber()
63  {
64  return 1;
65  }
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  {
73  final List<ITerm> l_arguments = CCommon.flatten( p_argument ).collect( Collectors.toList() );
74  if ( l_arguments.size() < 3 )
75  return CFuzzyValue.from( false );
76 
77  final Map<Object, Number> l_weights = l_arguments.parallelStream()
78  .filter( i -> CCommon.rawvalueAssignableTo( i, Map.class ) )
79  .findFirst()
80  .map( ITerm::<Map<Object, Number>>raw )
81  .orElseGet( Collections::emptyMap );
82 
83  final Number l_defaultvalue = StreamUtils.windowed( l_arguments.stream(), 2 )
84  .filter( i -> CCommon.rawvalueAssignableTo( i.get( 0 ), String.class )
85  && DEFAULTWEIGHT.equalsIgnoreCase( i.get( 0 ).<String>raw() )
86  && CCommon.rawvalueAssignableTo( i.get( 1 ), Number.class )
87  )
88  .findFirst()
89  .map( i -> i.get( 1 ).<Number>raw() )
90  .orElse( 0D );
91 
92  final Function<Object, Number> l_weightfunction = e -> l_weights.getOrDefault( e, l_defaultvalue );
93 
94  final List<ITerm> l_vertices = StreamUtils.windowed( l_arguments.stream(), 2 )
95  .filter( i -> !( CCommon.rawvalueAssignableTo( i.get( 0 ), String.class )
96  && DEFAULTWEIGHT.equalsIgnoreCase( i.get( 0 ).<String>raw() )
97  && CCommon.rawvalueAssignableTo( i.get( 1 ), Number.class ) )
98  && !( CCommon.rawvalueAssignableTo( i.get( 0 ), Graph.class )
99  || CCommon.rawvalueAssignableTo( i.get( 1 ), Graph.class ) )
100  )
101  .findFirst()
102  .orElseGet( Collections::emptyList );
103 
104  if ( l_vertices.isEmpty() )
105  return CFuzzyValue.from( false );
106 
107 
108  l_arguments.stream()
109  .filter( i -> CCommon.rawvalueAssignableTo( i, Graph.class ) )
110  .map( ITerm::<Graph<Object, Object>>raw )
111  .map( i -> this.apply( l_vertices, i, l_weightfunction ) )
112  .map( CRawTerm::from )
113  .forEach( p_return::add );
114 
115  return CFuzzyValue.from( true );
116  }
117 
118 
127  protected abstract Object apply( @Nonnull final List<ITerm> p_vertices, @Nonnull final Graph<Object, Object> p_graph,
128  @Nonnull final Function<Object, Number> p_weightfunction );
129 
130 
131 }
base class of build-in actions for setting name by package/classname (without prefix character) ...
abstract class to define path / distance graph algorithms
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
common structure for execution definition
execution context with local data
Definition: IContext.java:42
static Stream< ITerm > flatten( @Nonnull final Collection<? extends ITerm > p_terms)
flat term-in-term collection into a straight term list
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
abstract Object apply( @Nonnull final List< ITerm > p_vertices, @Nonnull final Graph< Object, Object > p_graph, @Nonnull final Function< Object, Number > p_weightfunction)
apply function
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