LightJason - AgentSpeak(L++)
CBeliefbase.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;
25 
31 
32 import javax.annotation.Nonnull;
33 import javax.annotation.Nullable;
34 import java.util.Collection;
35 import java.util.stream.Stream;
36 
37 
41 public final class CBeliefbase extends IBaseBeliefbase
42 {
47 
53  public CBeliefbase( @Nonnull final IStorage<ILiteral, IView> p_storage )
54  {
55  m_storage = p_storage;
56  }
57 
58  @Override
59  public final int hashCode()
60  {
61  return m_storage.hashCode();
62  }
63 
64  @Override
65  public final boolean equals( final Object p_object )
66  {
67  return ( p_object instanceof IBeliefbase ) && ( this.hashCode() == p_object.hashCode() );
68  }
69 
70  @Nonnull
71  @Override
72  public final ILiteral add( @Nonnull final ILiteral p_literal )
73  {
74  return m_storage.putMultiElement( p_literal.functor(), p_literal )
75  ? super.add( p_literal )
76  : p_literal;
77  }
78 
79  @Nonnull
80  @Override
81  public final IView add( @Nonnull final IView p_view )
82  {
83  m_storage.putSingleElement( p_view.name(), p_view );
84  return p_view;
85  }
86 
87  @Nonnull
88  @Override
89  public final IView remove( @Nonnull final IView p_view )
90  {
91  m_storage.removeSingleElement( this.internalremove( p_view ).name() );
92  return p_view;
93  }
94 
95  @Nonnull
96  @Override
97  public final ILiteral remove( @Nonnull final ILiteral p_literal )
98  {
99  return m_storage.removeMultiElement( p_literal.functor(), p_literal )
100  ? super.remove( p_literal )
101  : p_literal;
102  }
103 
104  @Override
105  public final boolean containsLiteral( @Nonnull final String p_key )
106  {
107  return m_storage.containsMultiElement( p_key );
108  }
109 
110  @Override
111  public final boolean containsView( @Nonnull final String p_key )
112  {
113  return m_storage.containsSingleElement( p_key );
114  }
115 
116  @Nonnull
117  @Override
118  public final IView view( @Nonnull final String p_key )
119  {
120  return m_storage.getSingleElement( p_key );
121  }
122 
123  @Nonnull
124  @Override
125  public final IView viewOrDefault( @Nonnull final String p_key, @Nullable final IView p_default )
126  {
127  return m_storage.getSingleElementOrDefault( p_key, p_default );
128  }
129 
130  @Nonnull
131  @Override
132  public final Collection<ILiteral> literal( @Nonnull final String p_key )
133  {
134  return m_storage.getMultiElement( p_key );
135  }
136 
137  @Nonnull
138  @Override
139  public final IAgent<?> update( @Nonnull final IAgent<?> p_agent )
140  {
141  super.update( p_agent );
142  m_storage.streamSingleElements().parallel().forEach( i -> i.update( p_agent ) );
143  return m_storage.update( p_agent );
144  }
145 
146  @Nonnull
147  @Override
148  public final IBeliefbase clear()
149  {
150  // create delete-event for all literals
151  m_storage
153  .parallel()
154  .forEach( i -> this.event( ITrigger.EType.DELETEBELIEF, i ) );
155 
156  m_storage.streamSingleElements().parallel().forEach( i -> i.clear() );
157  m_storage.clear();
158 
159  return this;
160  }
161 
162  @Override
163  public final boolean empty()
164  {
165  return m_storage.empty();
166  }
167 
168  @Override
169  public final int size()
170  {
171  return m_storage.size() + m_storage.streamSingleElements().parallel().mapToInt( IStructure::size ).sum();
172  }
173 
174  @Nonnull
175  @Override
176  public final Stream<ITrigger> trigger( @Nonnull final IView p_view )
177  {
178  return Stream.concat(
179  super.trigger( p_view ).parallel(),
180  m_storage.streamSingleElements().parallel().flatMap( IView::trigger )
181  );
182  }
183 
184  @Nonnull
185  @Override
186  public final Stream<ILiteral> streamLiteral()
187  {
188  return m_storage.streamMultiElements();
189  }
190 
191  @Nonnull
192  @Override
193  public final Stream<IView> streamView()
194  {
195  return m_storage.streamSingleElements();
196  }
197 
198  @Override
199  public final String toString()
200  {
201  return m_storage.toString();
202  }
203 
204 
205 
206 }
Stream< N > streamMultiElements()
returns a stream over all multi-elements
boolean containsSingleElement( @Nonnull final String p_key)
contains a single-element
Stream< M > streamSingleElements()
returns a stream over all single-elements
final IView internalremove(final IView p_view)
removes the interal view references
boolean putMultiElement( @Nonnull final String p_key, final N p_value)
puts a multi-element into the storage
final IView viewOrDefault( @Nonnull final String p_key, @Nullable final IView p_default)
returns a view element
boolean removeMultiElement( @Nonnull final String p_key, final N p_value)
removes a multi-element from the storage
Stream< ITrigger > trigger()
retruns all trigger of the beliefbase
final IView add( @Nonnull final IView p_view)
adds a view
ILiteral event(final ITrigger.EType p_event, final ILiteral p_literal)
push an event and literal to the event map
CBeliefbase( @Nonnull final IStorage< ILiteral, IView > p_storage)
ctor
Collection< N > getMultiElement( @Nonnull final String p_key)
returns a collection of multi-elementy by name
final int size()
returns the size of literals
boolean empty()
checks if a storage is empty
final Stream< ITrigger > trigger( @Nonnull final IView p_view)
returns all trigger of the beliefbase
final IView view( @Nonnull final String p_key)
returns a view element
boolean containsMultiElement( @Nonnull final String p_key)
contains a multi-element
view for a beliefbase that creates any access to the underlying data structures
Definition: IView.java:44
final boolean containsLiteral( @Nonnull final String p_key)
contains a multi-element
final boolean empty()
checks if the structure empty
interface of beliefbase definition, that create the trigger events for the agent
final ILiteral add( @Nonnull final ILiteral p_literal)
adds a literal
final IStorage< ILiteral, IView > m_storage
storage with data
M getSingleElementOrDefault( @Nonnull final String p_key, final M p_default)
returns a single-element by the name
final boolean equals(final Object p_object)
final Collection< ILiteral > literal( @Nonnull final String p_key)
returns a literal by the name
final Stream< ILiteral > streamLiteral()
returns a stream over all literals
IAgent<?> update( @Nonnull final IAgent<?> p_agent)
updates all items
int size()
returns the size of literals
final boolean containsView( @Nonnull final String p_key)
contains a single-element
final IAgent<?> update( @Nonnull final IAgent<?> p_agent)
updates all items
M getSingleElement( @Nonnull final String p_key)
returns a single-element by the name
final Stream< IView > streamView()
returns a stream over all views
boolean putSingleElement( @Nonnull final String p_key, final M p_value)
puts a single-element into the storage
final IBeliefbase clear()
clears all elements
boolean removeSingleElement( @Nonnull final String p_key)
removes a single-element from the storage
beliefbase to generate any event-based data by reference counting