LightJason - AgentSpeak(L++)
graph/CCreate.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 import edu.uci.ics.jung.graph.DirectedSparseGraph;
27 import edu.uci.ics.jung.graph.DirectedSparseMultigraph;
28 import edu.uci.ics.jung.graph.Graph;
29 import edu.uci.ics.jung.graph.SparseGraph;
30 import edu.uci.ics.jung.graph.SparseMultigraph;
31 import edu.uci.ics.jung.graph.UndirectedSparseGraph;
32 import edu.uci.ics.jung.graph.UndirectedSparseMultigraph;
41 
42 import javax.annotation.Nonnegative;
43 import javax.annotation.Nonnull;
44 import java.util.Arrays;
45 import java.util.Collections;
46 import java.util.List;
47 import java.util.Locale;
48 import java.util.Set;
49 import java.util.stream.Collectors;
50 
51 
64 public class CCreate extends IBuiltinAction
65 {
69  private static final long serialVersionUID = -8220165218772387059L;
70 
71  @Nonnegative
72  @Override
73  public final int minimalArgumentNumber()
74  {
75  return 1;
76  }
77 
78  @Nonnull
79  @Override
80  public final IFuzzyValue<Boolean> execute( final boolean p_parallel, @Nonnull final IContext p_context,
81  @Nonnull final List<ITerm> p_argument, @Nonnull final List<ITerm> p_return
82  )
83  {
84  CCommon.flatten( p_argument )
85  .map( ITerm::<String>raw )
86  .map( i -> EGraphTypes.exist( i ) ? EGraphTypes.from( i ) : EGraphTypes.SPARSE )
87  .map( EGraphTypes::get )
88  .map( CRawTerm::from )
89  .forEach( p_return::add );
90 
91  return CFuzzyValue.from( true );
92  }
93 
94 
98  private enum EGraphTypes
99  {
106 
110  private static final Set<String> TYPES = Collections.unmodifiableSet(
111  Arrays.stream( EGraphTypes.values() )
112  .map( i -> i.name().toUpperCase( Locale.ROOT ) )
113  .collect( Collectors.toSet() )
114  );
115 
121  @Nonnull
122  public final Graph<?, ?> get()
123  {
124  switch ( this )
125  {
126  case SPARSE:
127  return new SparseGraph<>();
128 
129  case SPARSEMULTI:
130  return new SparseMultigraph<>();
131 
132  case DIRECTEDSPARSE:
133  return new DirectedSparseGraph<>();
134 
135  case DIRECTEDSPARSEMULTI:
136  return new DirectedSparseMultigraph<>();
137 
138  case UNDIRECTEDSPARSE:
139  return new UndirectedSparseGraph<>();
140 
141  case UNDIRECTEDSPARSEMULTI:
142  return new UndirectedSparseMultigraph<>();
143 
144  default:
145  throw new CIllegalStateException( org.lightjason.agentspeak.common.CCommon.languagestring( this, "unknown", this ) );
146  }
147  }
148 
155  public static boolean exist( @Nonnull final String p_value )
156  {
157  return TYPES.contains( p_value.toUpperCase( Locale.ROOT ) );
158  }
159 
166  @Nonnull
167  public static EGraphTypes from( @Nonnull final String p_value )
168  {
169  return EGraphTypes.valueOf( p_value.toUpperCase( Locale.ROOT ) );
170  }
171 
172  }
173 }
base class of build-in actions for setting name by package/classname (without prefix character) ...
final int minimalArgumentNumber()
minimum number of arguments
static boolean exist( @Nonnull final String p_value)
checks if a key exists
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
common structure for execution definition
static EGraphTypes from( @Nonnull final String p_value)
returns graph type
static< T > String languagestring(final T p_source, final String p_label, final Object... p_parameter)
returns the language depend string on any object
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
final Graph<?, ?> get()
returns a new graph instance
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
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