LightJason - AgentSpeak(L++)
CView.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.beliefbase.view;
25 
34 
35 import javax.annotation.Nonnull;
36 import javax.annotation.Nullable;
37 import java.text.MessageFormat;
38 import java.util.Arrays;
39 import java.util.Objects;
40 import java.util.stream.Stream;
41 
42 
48 public final class CView implements IView
49 {
53  private final String m_name;
57  private final IBeliefbase m_beliefbase;
61  private final IView m_parent;
62 
63 
64 
71  public CView( @Nonnull final String p_name, @Nonnull final IBeliefbase p_beliefbase )
72  {
73  this( p_name, p_beliefbase, null );
74  }
75 
83  @SuppressWarnings( "unchecked" )
84  public CView( @Nonnull final String p_name, @Nonnull final IBeliefbase p_beliefbase, final IView p_parent )
85  {
86  if ( p_name.isEmpty() )
87  throw new CIllegalArgumentException( CCommon.languagestring( this, "empty" ) );
88 
89  m_name = p_name;
90  m_beliefbase = p_beliefbase;
91  m_parent = p_parent;
92  }
93 
94 
95 
96  // --- agent operations ------------------------------------------------------------------------------------------------------------------------------------
97 
98  @Nonnull
99  @Override
100  public final Stream<ITrigger> trigger()
101  {
102  // remove the root element (position 0), because the root element
103  // is not used on the agent (asl) side
104  final IPath l_path = this.path().remove( 0 );
105  return m_beliefbase.trigger( this ).map( i -> i.shallowcopy( l_path ) );
106  }
107 
108  @Nonnull
109  @Override
110  public final IAgent<?> update( @Nonnull final IAgent<?> p_agent )
111  {
112  return m_beliefbase.update( p_agent );
113  }
114 
115  // ---------------------------------------------------------------------------------------------------------------------------------------------------------
116 
117 
118 
119  // --- operations ------------------------------------------------------------------------------------------------------------------------------------------
120 
121  @Nonnull
122  @Override
123  public final IView add( @Nonnull final Stream<ILiteral> p_literal )
124  {
125  p_literal.parallel()
126  .forEach( i -> this.leafview( this.walk( i.functorpath() ) ).beliefbase().add( i.shallowcopysuffix() ) );
127  return this;
128  }
129 
130  @Nonnull
131  @Override
132  public final IView add( @Nonnull final ILiteral... p_literal )
133  {
134  return this.add( Arrays.stream( p_literal ) );
135  }
136 
137  @Nonnull
138  @Override
139  @SuppressWarnings( "varargs" )
140  public final IView add( @Nonnull final IView... p_view )
141  {
142  Arrays.stream( p_view )
143  .parallel()
144  .forEach( i ->
145  {
146  this.root()
147  .filter( j -> i.beliefbase().equals( this.beliefbase() ) )
148  .findAny()
149  .ifPresent( j ->
150  {
151  throw new CIllegalArgumentException( CCommon.languagestring( this, "equal", i.path(), j.path() ) );
152  } );
153  m_beliefbase.add( i );
154  } );
155  return this;
156  }
157 
158  @Nonnull
159  @Override
160  @SuppressWarnings( "varargs" )
161  public final IView remove( @Nonnull final IView... p_view )
162  {
163  Arrays.stream( p_view ).forEach( m_beliefbase::remove );
164  return this;
165  }
166 
167  @Nonnull
168  @Override
169  public final IView remove( @Nonnull final Stream<ILiteral> p_literal )
170  {
171  p_literal.parallel()
172  .forEach( i -> this.leafview( this.walk( i.functorpath() ) ).beliefbase().remove( i.shallowcopysuffix() ) );
173  return this;
174  }
175 
176  @Nonnull
177  @Override
178  public final IView remove( @Nonnull final ILiteral... p_literal )
179  {
180  return this.remove( Arrays.stream( p_literal ) );
181  }
182 
183  @Nonnull
184  @Override
185  public final IView clear( @Nullable final IPath... p_path )
186  {
187  if ( ( Objects.isNull( p_path ) ) || ( p_path.length == 0 ) )
189  else
190  Arrays.stream( p_path ).parallel()
191  .forEach( i -> this.leafview( this.walk( i ) ).clear() );
192 
193  return this;
194  }
195 
196  @Override
197  public final boolean containsView( @Nonnull final IPath p_path )
198  {
199  return !p_path.empty()
200  && ( p_path.size() == 1
201  ? m_beliefbase.containsView( p_path.get( 0 ) )
202  : this.leafview( this.walk( p_path.subpath( 0, p_path.size() - 1 ) ) )
203  .containsView( p_path.subpath( p_path.size() - 1, p_path.size() ) )
204  );
205  }
206 
207  @Override
208  public final boolean containsLiteral( @Nonnull final IPath p_path )
209  {
210  return !p_path.empty()
211  || ( p_path.size() == 1
212  ? m_beliefbase.containsLiteral( p_path.get( 0 ) )
213  : this.leafview( this.walk( p_path.subpath( 0, p_path.size() - 1 ) ) )
214  .containsLiteral( p_path.subpath( p_path.size() - 1, p_path.size() ) )
215  );
216  }
217 
218  // ---------------------------------------------------------------------------------------------------------------------------------------------------------
219 
220 
221 
222 
223  // --- streaming access ------------------------------------------------------------------------------------------------------------------------------------
224 
225  @Nonnull
226  @Override
227  public final Stream<ILiteral> stream( @Nullable final IPath... p_path )
228  {
229  // build path relative to this view
230  final IPath l_path = this.path();
231  return ( ( Objects.isNull( p_path ) ) || ( p_path.length == 0 )
232  ? Stream.concat( m_beliefbase.streamLiteral(), m_beliefbase.streamView().flatMap( i -> i.stream() ) )
233  : Arrays.stream( p_path )
234  .flatMap( i -> this.leafview( this.walk( i.subpath( 0, -1 ) ) ).beliefbase().literal( i.suffix() ).stream() )
235  ).map( i -> i.shallowcopy( l_path ) );
236  }
237 
238  @Nonnull
239  @Override
240  public final Stream<ILiteral> stream( final boolean p_negated, @Nullable final IPath... p_path )
241  {
242  // build path relative to this view
243  final IPath l_path = this.path();
244  return ( ( Objects.isNull( p_path ) ) || ( p_path.length == 0 )
245  ? Stream.concat(
246  m_beliefbase.streamLiteral().filter( i -> i.negated() == p_negated ),
247  m_beliefbase.streamView().flatMap( i -> i.stream( p_negated ) )
248  )
249  : Arrays.stream( p_path )
250  .flatMap( i -> this.leafview( this.walk( i.subpath( 0, -1 ) ) ).beliefbase().literal( i.suffix() ).stream() )
251  .filter( j -> j.negated() == p_negated )
252  ).map( i -> i.shallowcopy( l_path ) );
253  }
254 
255  @Nonnull
256  @Override
257  public final Stream<IView> walk( @Nonnull final IPath p_path, @Nullable final IViewGenerator... p_generator )
258  {
259  return this.walkdown( p_path, p_generator );
260  }
261 
262  @Nonnull
263  @Override
264  public final IView generate( @Nonnull final IViewGenerator p_generator, @Nonnull final IPath... p_paths )
265  {
266  Arrays.stream( p_paths )
267  .parallel()
268  .forEach( i -> this.walk( i, p_generator ) );
269  return this;
270  }
271 
279  @SuppressWarnings( "varargs" )
280  private Stream<IView> walkdown( @Nonnull final IPath p_path, @Nullable final IViewGenerator... p_generator )
281  {
282  if ( p_path.empty() )
283  return Stream.of( this );
284 
285  final IView l_view;
286  final String l_root = p_path.get( 0 );
287 
288  synchronized ( this )
289  {
290  // add is run here for avoid overwriting view with a new object reference
291  l_view = m_beliefbase.viewOrDefault(
292  l_root,
293 
294  Objects.isNull( p_generator ) || p_generator.length == 0
295  ? null
296  : p_generator[0].apply( l_root, this )
297  );
298 
299  if ( Objects.isNull( l_view ) )
300  return Stream.empty();
301  m_beliefbase.add( l_view );
302  }
303 
304  return Stream.concat(
305  Stream.of( this ),
306  l_view.walk( p_path.subpath( 1 ), p_generator )
307  );
308  }
309 
316  @Nonnull
317  private IView leafview( @Nonnull final Stream<IView> p_stream )
318  {
319  return p_stream
320  .reduce( ( i, j ) -> j )
321  .orElse( IView.EMPTY );
322  }
323 
324  // ---------------------------------------------------------------------------------------------------------------------------------------------------------
325 
326 
327 
328  // --- basic access ----------------------------------------------------------------------------------------------------------------------------------------
329 
330  @Nonnull
331  @Override
332  public final IBeliefbase beliefbase()
333  {
334  return m_beliefbase;
335  }
336 
337  @Override
338  public final boolean empty()
339  {
340  return m_beliefbase.empty();
341  }
342 
343  @Override
344  public final int size()
345  {
346  return m_beliefbase.size();
347  }
348 
349  @Nonnull
350  @Override
351  public final Stream<IView> root()
352  {
353  return this.hasParent()
354  ? Stream.concat( Stream.of( this ), Stream.of( this.parent() ).flatMap( IView::root ) )
355  : Stream.empty();
356  }
357 
358  @Nonnull
359  @Override
360  public final String name()
361  {
362  return m_name;
363  }
364 
365  @Nonnull
366  @Override
367  public final IPath path()
368  {
369  return this.root().map( IView::name ).collect( CPath.collect() ).reverse();
370  }
371 
372  @Nullable
373  @Override
374  public final IView parent()
375  {
376  return m_parent;
377  }
378 
379  @Override
380  public final boolean hasParent()
381  {
382  return m_parent != null;
383  }
384 
385  @Override
386  public final int hashCode()
387  {
388  return m_name.hashCode() ^ m_beliefbase.hashCode();
389  }
390 
391  @Override
392  public final boolean equals( final Object p_object )
393  {
394  return ( p_object instanceof IView ) && ( this.hashCode() == p_object.hashCode() );
395  }
396 
397  @Override
398  public final String toString()
399  {
400  return MessageFormat.format( "{0} ({1}): [{2}]", this.name(), super.toString(), m_beliefbase );
401  }
402 
403  // ---------------------------------------------------------------------------------------------------------------------------------------------------------
404 
405 }
Stream< IView > streamView()
returns a stream over all views
final IBeliefbase m_beliefbase
reference to the beliefbase context
Definition: CView.java:57
final Stream< ITrigger > trigger()
retruns all trigger of the beliefbase
Definition: CView.java:100
final Stream< ILiteral > stream(final boolean p_negated, @Nullable final IPath... p_path)
returns stream of literal
Definition: CView.java:240
final Stream< IView > walk( @Nonnull final IPath p_path, @Nullable final IViewGenerator... p_generator)
streams path walking
Definition: CView.java:257
IBeliefbase clear()
clears all elements
IAgent<?> update( @Nonnull final IAgent<?> p_agent)
updates all items
ILiteral add( @Nonnull final ILiteral p_literal)
adds a literal
CView( @Nonnull final String p_name, @Nonnull final IBeliefbase p_beliefbase)
ctor
Definition: CView.java:71
final boolean containsView( @Nonnull final IPath p_path)
view existing check
Definition: CView.java:197
final int size()
returns the size of literals
Definition: CView.java:344
static< T > String languagestring(final T p_source, final String p_label, final Object... p_parameter)
returns the language depend string on any object
Stream< ILiteral > stream( @Nullable final IPath... p_path)
returns stream of literal
class to create a path structure
Definition: CPath.java:53
final boolean empty()
checks if the structure empty
Definition: CView.java:338
IView leafview( @Nonnull final Stream< IView > p_stream)
returns the leaf of a view path
Definition: CView.java:317
final IView generate( @Nonnull final IViewGenerator p_generator, @Nonnull final IPath... p_paths)
generates path structure
Definition: CView.java:264
boolean empty()
checks if the structure empty
IBeliefbase beliefbase()
returns the beliefbase
view for a beliefbase that creates any access to the underlying data structures
Definition: IView.java:44
Stream< ITrigger > trigger( @Nonnull final IView p_view)
returns all trigger of the beliefbase
final IView remove( @Nonnull final IView... p_view)
removes a view in the current structure
Definition: CView.java:161
boolean containsLiteral( @Nonnull final String p_key)
contains a multi-element
final IView parent()
returns the parent of the view
Definition: CView.java:374
final IPath path()
returns the full path
Definition: CView.java:367
IView viewOrDefault( @Nonnull final String p_key, @Nullable final IView p_default)
returns a view element
Stream< IView > walkdown( @Nonnull final IPath p_path, @Nullable final IViewGenerator... p_generator)
inner walking structure of views
Definition: CView.java:280
final IAgent<?> update( @Nonnull final IAgent<?> p_agent)
updates all items
Definition: CView.java:110
interface of beliefbase definition, that create the trigger events for the agent
String name()
returns only the element name
final IBeliefbase beliefbase()
returns the beliefbase
Definition: CView.java:332
ILiteral remove( @Nonnull final ILiteral p_literal)
removes a literal
final Stream< IView > root()
returns a stream to the root node,
Definition: CView.java:351
final boolean hasParent()
check if the view has got a parent
Definition: CView.java:380
Stream< IView > root()
returns a stream to the root node,
Collection< ILiteral > literal( @Nonnull final String p_key)
returns a literal by the name
interface for generating non-existing beliefbases views
final IView add( @Nonnull final Stream< ILiteral > p_literal)
adds a literal in the current structure
Definition: CView.java:123
Stream< ILiteral > streamLiteral()
returns a stream over all literals
final IView add( @Nonnull final ILiteral... p_literal)
adds a literal in the current structure
Definition: CView.java:132
Stream< IView > walk( @Nonnull final IPath p_path, @Nullable final IViewGenerator... p_generator)
streams path walking
int size()
returns the size of literals
final boolean containsLiteral( @Nonnull final IPath p_path)
checks if a literal exists
Definition: CView.java:208
final String name()
returns only the element name
Definition: CView.java:360
IPath remove(final int p_index)
removes an element
boolean containsView( @Nonnull final String p_key)
contains a single-element
static Collector< String, IPath, IPath > collect()
returns a collector to build a path from strings
Definition: CPath.java:506
final boolean equals(final Object p_object)
Definition: CView.java:392
final IView clear( @Nullable final IPath... p_path)
clears all elements
Definition: CView.java:185
final Stream< ILiteral > stream( @Nullable final IPath... p_path)
returns stream of literal
Definition: CView.java:227