24 package org.lightjason.agentspeak.beliefbase.view;
26 import org.apache.commons.lang3.tuple.ImmutablePair;
27 import org.apache.commons.lang3.tuple.Pair;
38 import javax.annotation.Nonnull;
39 import javax.annotation.Nullable;
40 import java.util.Arrays;
41 import java.util.Collection;
42 import java.util.Collections;
43 import java.util.Locale;
45 import java.util.Objects;
46 import java.util.concurrent.ConcurrentHashMap;
47 import java.util.function.BiConsumer;
48 import java.util.function.Consumer;
49 import java.util.function.Function;
50 import java.util.stream.Collectors;
51 import java.util.stream.Stream;
77 private final Map<String, Object>
m_data;
113 public CViewMap( @Nonnull
final String p_name, @Nonnull
final Map<String, Object> p_map )
115 this( p_name, p_map, null );
125 public CViewMap( @Nonnull
final String p_name, @Nonnull
final Map<String, Object> p_map, @Nullable
final IView p_parent )
128 p_name, p_map, p_parent,
130 ( i, j ) -> j.putIfAbsent( i,
new ConcurrentHashMap<>() ),
132 ( i, j ) -> i.getValue().limit( 1 ).filter(
ITerm::raw ).forEach( n -> j.put( i.getKey(), n.raw() ) ),
136 final Object l_data = j.get( i );
137 if ( l_data instanceof Map<?, ?> )
138 j.remove( i, l_data );
143 final Object l_data = j.get( i );
144 if ( !( l_data instanceof Map<?, ?> ) )
145 j.remove( i, l_data );
148 i -> i.toLowerCase( Locale.ROOT ),
149 i -> i.toLowerCase( Locale.ROOT )
167 public CViewMap( @Nonnull
final String p_name, @Nonnull
final Map<String, Object> p_map, @Nullable
final IView p_parent,
168 @Nonnull
final BiConsumer<String, Map<String, Object>> p_addviewconsumer,
169 @Nonnull
final BiConsumer<Pair<String, Stream<ITerm>>, Map<String, Object>> p_addliteralconsumer,
170 @Nonnull
final BiConsumer<String, Map<String, Object>> p_removeviewconsumer,
171 @Nonnull
final BiConsumer<String, Map<String, Object>> p_removeliteralconsumer,
172 @Nonnull
final Consumer<Map<String, Object>> p_clearconsumer,
173 @Nonnull
final Function<String, String> p_literaltokey, @Nonnull
final Function<String, String> p_keytoliteral
179 m_literaltokey = p_literaltokey;
180 m_keytoliteral = p_keytoliteral;
181 m_clearconsumer = p_clearconsumer;
182 m_addviewconsumer = p_addviewconsumer;
183 m_addliteralconsumer = p_addliteralconsumer;
184 m_removeviewconsumer = p_removeviewconsumer;
185 m_removeliteralconsumer = p_removeliteralconsumer;
192 return this.
walkdown( p_path, p_generator );
204 public final Stream<IView>
root()
207 ? Stream.concat( Stream.of(
this ), Stream.of( this.
parent() ).flatMap(
IView::root ) )
242 return m_parent != null;
249 return m_beliefbase.
trigger(
this );
254 @SuppressWarnings(
"unchecked" )
259 return ( ( Objects.isNull( p_path ) ) || ( p_path.length == 0 )
261 : Arrays.stream( p_path )
263 ).map( i -> i.shallowcopy( l_path ) );
268 public final Stream<ILiteral>
stream(
final boolean p_negated, @Nullable
final IPath... p_path )
270 return p_negated ? Stream.empty() : this.
stream( p_path );
277 if ( ( Objects.isNull( p_path ) ) || ( p_path.length == 0 ) )
280 Arrays.stream( p_path ).flatMap( i -> this.
walkdown( i ) ).forEach( i -> i.clear() );
287 public final IView add( @Nonnull
final Stream<ILiteral> p_literal )
289 p_literal.forEach( m_beliefbase::add );
297 return this.
add( Arrays.stream( p_literal ) );
302 @SuppressWarnings(
"varargs" )
305 Arrays.
stream( p_view ).forEach( m_beliefbase::remove );
311 public IView remove( @Nonnull
final Stream<ILiteral> p_literal )
313 p_literal.forEach( m_beliefbase::remove );
319 @SuppressWarnings(
"varargs" )
322 return this.
remove( Arrays.stream( p_literal ) );
327 @SuppressWarnings(
"varargs" )
330 Arrays.
stream( p_view ).forEach( m_beliefbase::remove );
335 @SuppressWarnings(
"unchecked" )
338 return !p_path.empty()
339 && ( p_path.size() == 1
341 : this.
leafview( this.
walk( p_path.subpath( 0, p_path.size() - 1 ) ) )
347 @SuppressWarnings(
"unchecked" )
350 return !p_path.empty()
351 && ( p_path.size() == 1
353 : this.
leafview( this.
walk( p_path.subpath( 0, p_path.size() - 1 ) ) )
354 .
containsView( p_path.subpath( p_path.size() - 1, p_path.size() ) )
387 .reduce( ( i, j ) -> j )
391 @SuppressWarnings(
"unchecked" )
394 if ( p_path.empty() )
395 return Stream.of(
this );
398 final Object l_data =
m_data.get( l_key );
399 return l_data instanceof Map<?, ?>
403 l_key, (Map<String, Object>) l_data,
this,
405 ).walk( p_path.subpath( 1 ), p_generator ) )
423 @SuppressWarnings(
"unchecked" )
426 return (
int)
m_data.values()
428 .filter( i -> !( i instanceof Map<?, ?> ) )
432 .filter( i -> i instanceof Map<?, ?> )
433 .mapToInt( i ->
new CViewMap( i.getKey(), (Map<String, Object>) i.getValue() ).
size() )
448 return Stream.empty();
453 @SuppressWarnings(
"unchecked" )
458 .filter( i -> !( i.getValue() instanceof Map<?, ?> ) )
464 @SuppressWarnings(
"unchecked" )
469 .filter( i -> i.getValue() instanceof Map<?, ?> )
517 return m_data.containsKey( l_key ) && ( !(
m_data.get( l_key ) instanceof Map<?, ?> ) );
524 return m_data.containsKey( l_key ) && (
m_data.get( l_key ) instanceof Map<?, ?> );
536 public final Collection<ILiteral>
literal( @Nonnull
final String p_key )
539 if ( !
m_data.containsKey( l_key ) )
540 return Collections.emptySet();
542 final Object l_data =
m_data.get( l_key );
543 if (
m_data.get( l_key ) instanceof Map<?, ?> )
544 return Collections.emptySet();
546 return Stream.of(
CLiteral.
from( p_key,
this.toterm( l_data ) ) ).collect( Collectors.toSet() );
570 @SuppressWarnings(
"unchecked" )
573 if ( p_value instanceof Collection<?> )
574 return ( (Collection<Object>) p_value ).stream().flatMap( this::toterm );
576 if ( p_value instanceof Map<?, ?> )
577 return ( (Map<String, Object>) p_value ).entrySet().stream()
580 if ( p_value instanceof Integer )
581 return Stream.of(
CRawTerm.
from( ( (Number) p_value ).longValue() ) );
final IView create( @Nonnull final String p_name)
returns a new view of the belief base
Stream< IView > streamView()
returns a stream over all views
CViewMap( @Nonnull final String p_name, @Nonnull final Map< String, Object > p_map)
ctor
final BiConsumer< Pair< String, Stream< ITerm > >, Map< String, Object > > m_addliteralconsumer
add-literal consumer
Stream< ITerm > toterm(final Object p_value)
final Function< String, String > m_keytoliteral
key to literal converting
final int size()
returns the size of literals
CViewMap( @Nonnull final String p_name, @Nonnull final Map< String, Object > p_map, @Nullable final IView p_parent, @Nonnull final BiConsumer< String, Map< String, Object >> p_addviewconsumer, @Nonnull final BiConsumer< Pair< String, Stream< ITerm >>, Map< String, Object >> p_addliteralconsumer, @Nonnull final BiConsumer< String, Map< String, Object >> p_removeviewconsumer, @Nonnull final BiConsumer< String, Map< String, Object >> p_removeliteralconsumer, @Nonnull final Consumer< Map< String, Object >> p_clearconsumer, @Nonnull final Function< String, String > p_literaltokey, @Nonnull final Function< String, String > p_keytoliteral)
ctor
final IView view( @Nonnull final String p_key)
returns a view element
final Stream< ITrigger > trigger()
retruns all trigger of the beliefbase
boolean containsView( @Nonnull final String p_key)
contains a single-element
IAgent<?> update( @Nonnull final IAgent<?> p_agent)
updates all items
final int size()
returns the size of literals
final IView parent()
returns the parent of the view
IView leafview( @Nonnull final Stream< IView > p_stream)
returns the leaf of a view path
final IView create( @Nonnull final String p_name, @Nullable final IView p_parent)
returns a new view of the belief base
final IView m_parent
parent name
final IView add( @Nonnull final Stream< ILiteral > p_literal)
adds a literal in the current structure
Stream< ILiteral > stream( @Nullable final IPath... p_path)
returns stream of literal
class to create a path structure
final boolean empty()
checks if the structure empty
final IAgent<?> update( @Nonnull final IAgent<?> p_agent)
updates all items
IView generate( @Nonnull final IViewGenerator p_generator, @Nonnull final IPath... p_paths)
generates path structure
final Stream< ITrigger > trigger( @Nonnull final IView p_view)
returns all trigger of the beliefbase
boolean empty()
checks if the structure empty
final IView clear( @Nullable final IPath... p_path)
clears all elements
IBeliefbase beliefbase()
returns the beliefbase
view for a beliefbase that creates any access to the underlying data structures
final IBeliefbase beliefbase()
returns the beliefbase
Stream< ITrigger > trigger( @Nonnull final IView p_view)
returns all trigger of the beliefbase
boolean containsLiteral( @Nonnull final String p_key)
contains a multi-element
final String name()
returns only the element name
final IBeliefbase clear()
clears all elements
final boolean hasParent()
check if the view has got a parent
final Stream< IView > root()
returns a stream to the root node,
final Stream< IView > streamView()
returns a stream over all views
final BiConsumer< String, Map< String, Object > > m_addviewconsumer
add-view consumer
interface of beliefbase definition, that create the trigger events for the agent
String name()
returns only the element name
final String m_name
view name
final IView viewOrDefault( @Nonnull final String p_key, @Nullable final IView p_default)
returns a view element
final Map< String, Object > m_data
root map
final ILiteral add( @Nonnull final ILiteral p_literal)
adds a literal
final Stream< ILiteral > streamLiteral()
returns a stream over all literals
final Collection< ILiteral > literal( @Nonnull final String p_key)
returns a literal by the name
final IPath path()
returns the full path
final BiConsumer< String, Map< String, Object > > m_removeliteralconsumer
remove-literal consumer
IView remove( @Nonnull final Stream< ILiteral > p_literal)
removes a literal in the current structure
default generic literal class for agent beliefs a literal consists of a functor, an optional list of ...
Stream< IView > root()
returns a stream to the root node,
final boolean containsLiteral( @Nonnull final String p_key)
contains a multi-element
< T > T raw()
cast to any raw value type
Collection< ILiteral > literal( @Nonnull final String p_key)
returns a literal by the name
interface for generating non-existing beliefbases views
final Stream< IView > walk( @Nonnull final IPath p_path, @Nullable final IViewGenerator... p_generator)
streams path walking
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
final BiConsumer< String, Map< String, Object > > m_removeviewconsumer
remove-view consumer
final boolean empty()
checks if the structure empty
Stream< ILiteral > streamLiteral()
returns a stream over all literals
final boolean containsView( @Nonnull final IPath p_path)
view existing check
final Stream< ILiteral > stream( @Nullable final IPath... p_path)
returns stream of literal
static ILiteral from( @Nonnull final String p_functor, @Nullable final ITerm... p_values)
factory
int size()
returns the size of literals
final IView add( @Nonnull final IView p_view)
adds a view
final IAgent<?> update( @Nonnull final IAgent<?> p_agent)
updates all items
view which can use a map of maps to represent the hierarchical beliefbase structure ...
final IView add( @Nonnull final ILiteral... p_literal)
adds a literal in the current structure
CViewMap( @Nonnull final String p_name, @Nonnull final Map< String, Object > p_map, @Nullable final IView p_parent)
ctor
boolean containsView( @Nonnull final String p_key)
contains a single-element
final IBeliefbase m_beliefbase
beliefbase
static Collector< String, IPath, IPath > collect()
returns a collector to build a path from strings
final Consumer< Map< String, Object > > m_clearconsumer
clear consumer
Stream< IView > walkdown(final IPath p_path, @Nullable final IViewGenerator... p_generator)
final Function< String, String > m_literaltokey
path to key converting
final Stream< ILiteral > stream(final boolean p_negated, @Nullable final IPath... p_path)
returns stream of literal
boolean containsLiteral( @Nonnull final IPath p_path)
checks if a literal exists
term structure for simple datatypes