CSpanningTree.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.graph;

  24. import com.google.common.base.Function;
  25. import edu.uci.ics.jung.algorithms.shortestpath.PrimMinimumSpanningTree;
  26. import edu.uci.ics.jung.graph.DelegateTree;
  27. import edu.uci.ics.jung.graph.Graph;
  28. import org.lightjason.agentspeak.action.builtin.IBuiltinAction;
  29. import org.lightjason.agentspeak.language.CCommon;
  30. import org.lightjason.agentspeak.language.CRawTerm;
  31. import org.lightjason.agentspeak.language.ITerm;
  32. import org.lightjason.agentspeak.language.execution.IContext;
  33. import org.lightjason.agentspeak.language.fuzzy.CFuzzyValue;
  34. import org.lightjason.agentspeak.language.fuzzy.IFuzzyValue;

  35. import javax.annotation.Nonnegative;
  36. import javax.annotation.Nonnull;
  37. import java.util.Collections;
  38. import java.util.List;
  39. import java.util.Map;


  40. /**
  41.  * creates a minimal spanning tree of any graph instance.
  42.  * The action creates from each graph argument a spanning
  43.  * tree, the first map instance will be used as weight-map,
  44.  * a tuple of the string "defaultweight" and a numeric value
  45.  * defines the default weight value of the weight-map
  46.  * (the default value is zero), the action never fails
  47.  *
  48.  * {@code
  49.     [SP1|SP2] = graph/spanningtree( Graph1, Graph2 );
  50.     [SP3|SP4] = graph/spanningtree( "defaultweight", 3, WeightMap, Graph3, Graph4 );
  51.  * }
  52.  */
  53. public final class CSpanningTree extends IBuiltinAction
  54. {
  55.     /**
  56.      * serial id
  57.      */
  58.     private static final long serialVersionUID = -367284435336974616L;

  59.     @Nonnegative
  60.     @Override
  61.     public final int minimalArgumentNumber()
  62.     {
  63.         return 1;
  64.     }

  65.     @Nonnull
  66.     @Override
  67.     public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
  68.                                                @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return )
  69.     {
  70.         final double l_defaultcost = CCommon.flatten( p_argument )
  71.                                             .filter( i -> CCommon.rawvalueAssignableTo( i, Number.class ) )
  72.                                             .findFirst()
  73.                                             .map( ITerm::<Number>raw )
  74.                                             .map( Number::doubleValue )
  75.                                             .orElse( 0D );

  76.         final Map<?, Number> l_costmap = CCommon.flatten( p_argument )
  77.                                                 .filter( i -> CCommon.rawvalueAssignableTo( i, Map.class ) )
  78.                                                 .findFirst()
  79.                                                 .map( ITerm::<Map<?, Number>>raw )
  80.                                                 .orElseGet( Collections::emptyMap );

  81.         final Function<Object, Double> l_weightfunction = e -> l_costmap.getOrDefault( e, l_defaultcost ).doubleValue();
  82.         final PrimMinimumSpanningTree<Object, Object> l_treefactory = new PrimMinimumSpanningTree<>( DelegateTree.getFactory(), l_weightfunction );

  83.         // --- filter graphs ---
  84.         CCommon.flatten( p_argument )
  85.                .filter( i -> CCommon.rawvalueAssignableTo( i, Graph.class ) )
  86.                .map( ITerm::<Graph<Object, Object>>raw )
  87.                .map( l_treefactory )
  88.                .map( CRawTerm::from )
  89.                .forEach( p_return::add );

  90.         return CFuzzyValue.from( true );
  91.     }
  92. }