CBeliefbase.java

  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. package org.lightjason.agentspeak.beliefbase;

  24. import org.lightjason.agentspeak.agent.IAgent;
  25. import org.lightjason.agentspeak.beliefbase.storage.IStorage;
  26. import org.lightjason.agentspeak.beliefbase.view.IView;
  27. import org.lightjason.agentspeak.language.ILiteral;
  28. import org.lightjason.agentspeak.language.instantiable.plan.trigger.ITrigger;

  29. import javax.annotation.Nonnull;
  30. import javax.annotation.Nullable;
  31. import java.util.Collection;
  32. import java.util.stream.Stream;


  33. /**
  34.  * beliefbase to generate any event-based data by reference counting
  35.  */
  36. public final class CBeliefbase extends IBaseBeliefbase
  37. {
  38.     /**
  39.      * storage with data
  40.      */
  41.     private final IStorage<ILiteral, IView> m_storage;

  42.     /**
  43.      * ctor
  44.      *
  45.      * @param p_storage storage
  46.      */
  47.     public CBeliefbase( @Nonnull final IStorage<ILiteral, IView> p_storage )
  48.     {
  49.         m_storage = p_storage;
  50.     }

  51.     @Override
  52.     public final int hashCode()
  53.     {
  54.         return m_storage.hashCode();
  55.     }

  56.     @Override
  57.     public final boolean equals( final Object p_object )
  58.     {
  59.         return ( p_object instanceof IBeliefbase ) && ( this.hashCode() == p_object.hashCode() );
  60.     }

  61.     @Nonnull
  62.     @Override
  63.     public final ILiteral add( @Nonnull final ILiteral p_literal )
  64.     {
  65.         return m_storage.putMultiElement( p_literal.functor(), p_literal )
  66.                ? super.add( p_literal )
  67.                : p_literal;
  68.     }

  69.     @Nonnull
  70.     @Override
  71.     public final IView add( @Nonnull final IView p_view )
  72.     {
  73.         m_storage.putSingleElement( p_view.name(), p_view );
  74.         return p_view;
  75.     }

  76.     @Nonnull
  77.     @Override
  78.     public final IView remove( @Nonnull final IView p_view )
  79.     {
  80.         m_storage.removeSingleElement( this.internalremove( p_view ).name() );
  81.         return p_view;
  82.     }

  83.     @Nonnull
  84.     @Override
  85.     public final ILiteral remove( @Nonnull final ILiteral p_literal )
  86.     {
  87.         return m_storage.removeMultiElement( p_literal.functor(), p_literal )
  88.                ? super.remove( p_literal )
  89.                : p_literal;
  90.     }

  91.     @Override
  92.     public final boolean containsLiteral( @Nonnull final String p_key )
  93.     {
  94.         return m_storage.containsMultiElement( p_key );
  95.     }

  96.     @Override
  97.     public final boolean containsView( @Nonnull final String p_key )
  98.     {
  99.         return m_storage.containsSingleElement( p_key );
  100.     }

  101.     @Nonnull
  102.     @Override
  103.     public final IView view( @Nonnull final String p_key )
  104.     {
  105.         return m_storage.getSingleElement( p_key );
  106.     }

  107.     @Nonnull
  108.     @Override
  109.     public final IView viewOrDefault( @Nonnull final String p_key, @Nullable final IView p_default )
  110.     {
  111.         return m_storage.getSingleElementOrDefault( p_key, p_default );
  112.     }

  113.     @Nonnull
  114.     @Override
  115.     public final Collection<ILiteral> literal( @Nonnull final String p_key )
  116.     {
  117.         return m_storage.getMultiElement( p_key );
  118.     }

  119.     @Nonnull
  120.     @Override
  121.     public final IAgent<?> update( @Nonnull final IAgent<?> p_agent )
  122.     {
  123.         super.update( p_agent );
  124.         m_storage.streamSingleElements().parallel().forEach( i -> i.update( p_agent ) );
  125.         return m_storage.update( p_agent );
  126.     }

  127.     @Nonnull
  128.     @Override
  129.     public final IBeliefbase clear()
  130.     {
  131.         // create delete-event for all literals
  132.         m_storage
  133.             .streamMultiElements()
  134.             .parallel()
  135.             .forEach( i -> this.event( ITrigger.EType.DELETEBELIEF, i ) );

  136.         m_storage.streamSingleElements().parallel().forEach( i -> i.clear() );
  137.         m_storage.clear();

  138.         return this;
  139.     }

  140.     @Override
  141.     public final boolean empty()
  142.     {
  143.         return m_storage.empty();
  144.     }

  145.     @Override
  146.     public final int size()
  147.     {
  148.         return m_storage.size() + m_storage.streamSingleElements().parallel().mapToInt( IStructure::size ).sum();
  149.     }

  150.     @Nonnull
  151.     @Override
  152.     public final Stream<ITrigger> trigger( @Nonnull final IView p_view )
  153.     {
  154.         return Stream.concat(
  155.             super.trigger( p_view ).parallel(),
  156.             m_storage.streamSingleElements().parallel().flatMap( IView::trigger )
  157.         );
  158.     }

  159.     @Nonnull
  160.     @Override
  161.     public final Stream<ILiteral> streamLiteral()
  162.     {
  163.         return m_storage.streamMultiElements();
  164.     }

  165.     @Nonnull
  166.     @Override
  167.     public final Stream<IView> streamView()
  168.     {
  169.         return m_storage.streamSingleElements();
  170.     }

  171.     @Override
  172.     public final String toString()
  173.     {
  174.         return m_storage.toString();
  175.     }



  176. }