LightJason - AgentSpeak(L++)
IBaseBeliefbase.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 
26 import com.google.common.collect.HashMultimap;
27 import com.google.common.collect.Multimap;
28 import com.google.common.collect.Multimaps;
29 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
36 
37 import javax.annotation.Nonnull;
38 import java.lang.ref.PhantomReference;
39 import java.lang.ref.Reference;
40 import java.lang.ref.ReferenceQueue;
41 import java.util.Collections;
42 import java.util.HashSet;
43 import java.util.Objects;
44 import java.util.Set;
45 import java.util.stream.Stream;
46 
47 
57 @SuppressFBWarnings( "RI_REDUNDANT_INTERFACES" )
58 public abstract class IBaseBeliefbase implements IBeliefbase
59 {
60 
64  private final Multimap<IView, ITrigger> m_events = Multimaps.synchronizedSetMultimap( HashMultimap.create() );
68  private final Set<IView> m_views = Collections.synchronizedSet( new HashSet<>() );
72  private final ReferenceQueue<IView> m_maskreference = new ReferenceQueue<>();
73 
74 
75  @Nonnull
76  @Override
77  public final IView create( @Nonnull final String p_name )
78  {
79  return this.eventreference( new CView( p_name, this ) );
80  }
81 
82  @Nonnull
83  @Override
84  public final IView create( @Nonnull final String p_name, final IView p_parent )
85  {
86  return this.eventreference( new CView( p_name, this, p_parent ) );
87  }
88 
89  @Nonnull
90  @Override
91  public ILiteral add( @Nonnull final ILiteral p_literal )
92  {
93  return this.event( ITrigger.EType.ADDBELIEF, p_literal );
94  }
95 
96  @Nonnull
97  @Override
98  public ILiteral remove( @Nonnull final ILiteral p_literal )
99  {
100  return this.event( ITrigger.EType.DELETEBELIEF, p_literal );
101  }
102 
103  @Nonnull
104  @Override
105  public IAgent<?> update( @Nonnull final IAgent<?> p_agent )
106  {
107  // check all references of mask and remove unused references
108  for ( Reference<? extends IView> l_reference = m_maskreference.poll(); Objects.nonNull( l_reference ); l_reference = m_maskreference.poll() )
109  {
110  final IView l_view = l_reference.get();
111  if ( Objects.nonNull( l_view ) )
112  {
113  m_views.remove( l_view );
114  m_events.asMap().remove( l_view );
115  }
116  }
117 
118  return p_agent;
119  }
120 
121  @Nonnull
122  @Override
123  public Stream<ITrigger> trigger( @Nonnull final IView p_view )
124  {
125  return this.cleartrigger( p_view );
126  }
127 
128 
135  protected ILiteral event( final ITrigger.EType p_event, final ILiteral p_literal )
136  {
137  final ITrigger l_trigger = CTrigger.from( p_event, p_literal );
138  m_views.parallelStream().forEach( i -> m_events.put( i, l_trigger ) );
139  return p_literal;
140  }
141 
148  protected final IView internalremove( final IView p_view )
149  {
150  m_views.remove( p_view );
151  m_events.removeAll( p_view );
152  return p_view;
153  }
154 
161  protected IView eventreference( final IView p_view )
162  {
163  new PhantomReference<>( p_view, m_maskreference );
164  m_views.add( p_view );
165  return p_view;
166  }
167 
174  protected final Stream<ITrigger> cleartrigger( final IView p_view )
175  {
176  return m_events.removeAll( p_view ).stream();
177  }
178 
179 }
final IView internalremove(final IView p_view)
removes the interal view references
ILiteral event(final ITrigger.EType p_event, final ILiteral p_literal)
push an event and literal to the event map
IView remove( @Nonnull final Stream< ILiteral > p_literal)
removes a literal in the current structure
static ITrigger from( @Nonnull final EType p_event, @Nonnull final ILiteral p_literal)
creates a trigger event^
Definition: CTrigger.java:87
view for a beliefbase that creates any access to the underlying data structures
Definition: IView.java:44
final Stream< ITrigger > cleartrigger(final IView p_view)
copy of all trigger values
interface of beliefbase definition, that create the trigger events for the agent
Stream< ITrigger > trigger( @Nonnull final IView p_view)
returns all trigger of the beliefbase
final IView create( @Nonnull final String p_name)
returns a new view of the belief base
IView add( @Nonnull final ILiteral... p_literal)
adds a literal in the current structure
IAgent<?> update( @Nonnull final IAgent<?> p_agent)
updates all items
ILiteral add( @Nonnull final ILiteral p_literal)
adds a literal
IView eventreference(final IView p_view)
adds a view to the event referencing structure
final IView create( @Nonnull final String p_name, final IView p_parent)