24 package org.lightjason.agentspeak.agent;
26 import com.codepoetics.protonpack.StreamUtils;
27 import com.google.common.collect.HashMultimap;
28 import com.google.common.collect.ImmutableMultimap;
29 import com.google.common.collect.LinkedHashMultimap;
30 import com.google.common.collect.Multimap;
31 import com.google.common.collect.Multimaps;
32 import com.google.common.collect.TreeMultimap;
33 import org.apache.commons.lang3.StringUtils;
34 import org.apache.commons.lang3.tuple.ImmutablePair;
35 import org.apache.commons.lang3.tuple.Pair;
57 import javax.annotation.Nonnegative;
58 import javax.annotation.Nonnull;
59 import javax.annotation.Nullable;
60 import java.text.MessageFormat;
61 import java.util.Arrays;
62 import java.util.Collection;
63 import java.util.Collections;
64 import java.util.Comparator;
65 import java.util.HashSet;
67 import java.util.Objects;
69 import java.util.concurrent.ConcurrentHashMap;
70 import java.util.concurrent.atomic.AtomicLong;
71 import java.util.logging.Logger;
72 import java.util.stream.Collectors;
73 import java.util.stream.Stream;
100 protected final Map<String, Object>
m_storage =
new ConcurrentHashMap<>();
104 protected final Map<Integer, ITrigger>
m_trigger =
new ConcurrentHashMap<>();
108 protected final Multimap<IPath, IRule>
m_rules = Multimaps.synchronizedMultimap( LinkedHashMultimap.create() );
112 protected final Multimap<ITrigger, IPlanStatistic>
m_plans = Multimaps.synchronizedMultimap(
128 private final Set<ITerm>
m_sleepingterm = Collections.synchronizedSet(
new HashSet<>() );
144 private final Multimap<IPath, ILiteral>
m_runningplans = Multimaps.synchronizedSetMultimap( HashMultimap.create() );
156 m_unifier = p_configuration.unifier();
158 m_variablebuilder = p_configuration.variablebuilder();
159 m_fuzzy = p_configuration.fuzzy();
162 p_configuration.plans().parallelStream().forEach( i -> m_plans.put( i.trigger(),
CPlanStatistic.
from( i ) ) );
163 p_configuration.rules().parallelStream().forEach( i -> m_rules.put( i.identifier().fqnfunctor(), i ) );
164 if ( Objects.nonNull( p_configuration.initialgoal() ) )
165 m_trigger.put( p_configuration.initialgoal().hashCode(), p_configuration.initialgoal() );
178 @SuppressWarnings(
"varargs" )
181 return Arrays.stream( p_inspector )
185 i.inspectcycletime( m_cycletime.get() );
186 i.inspectsleeping( m_sleepingcycles.get() );
187 i.inspectbelief( m_beliefbase.
stream() );
188 i.inspectplans( m_plans.values().stream() );
189 i.inspectrunningplans( m_runningplans.values().stream() );
190 i.inspectstorage( m_storage.entrySet().stream() );
191 i.inspectrules( m_rules.values().stream() );
199 return ImmutableMultimap.copyOf( m_runningplans );
205 return m_sleepingcycles.get() > 0;
214 ( Objects.isNull( p_term ) ) || ( p_term.length == 0 )
216 : Arrays.stream( p_term )
222 public final IAgent<T> sleep(
final long p_cycles, @Nonnull
final Stream<ITerm> p_literal )
224 m_sleepingcycles.set( p_cycles );
225 p_literal.filter( i -> !i.hasVariable() ).forEach( m_sleepingterm::add );
234 ( Objects.isNull( p_term ) ) || ( p_term.length == 0 )
236 : Arrays.stream( p_term )
244 p_term.forEach( m_sleepingterm::add );
267 return m_cycletime.get();
272 public final Multimap<ITrigger, IPlanStatistic>
plans()
293 public final Multimap<IPath, IRule>
rules()
300 @SuppressWarnings(
"unchecked" )
309 return MessageFormat.format(
314 Stream.of(
"Trigger",
"Running Plans",
"Beliefbase" ),
316 ( l, c ) -> MessageFormat.format(
"{0}: {1}", l, c )
327 if ( m_sleepingcycles.get() > 0 )
331 if ( p_trigger.literal().hasVariable() )
335 if ( ( Objects.nonNull( p_immediately ) ) && ( p_immediately.length > 0 ) && ( p_immediately[0] ) )
339 synchronized ( this )
341 m_trigger.putIfAbsent( p_trigger.hashCode(), p_trigger );
348 @SuppressWarnings(
"unchecked" )
349 public T
call() throws Exception
352 m_beliefbase.
update( (T)
this );
353 if ( !this.
active(
false ) )
358 m_fuzzy.getValue().update(
this );
365 m_cycletime.set( System.nanoTime() );
381 final Collection<Pair<IPlanStatistic, IContext>> l_execution = this.
generateexecution(
383 m_trigger.values().parallelStream(),
384 m_beliefbase.
trigger().parallel()
400 private Collection<Pair<IPlanStatistic, IContext>>
generateexecution( @Nonnull
final Stream<ITrigger> p_trigger )
403 .filter( Objects::nonNull )
405 .flatMap( i -> m_plans.get( i ).stream().map( j ->
new ImmutablePair<>( i, j ) ) )
408 .map( i ->
new ImmutablePair<>( i,
CCommon.unifytrigger(
m_unifier, i.getLeft(), i.getRight().plan().trigger() ) ) )
410 .filter( i -> i.getRight().getLeft() )
412 .map( i ->
CCommon.instantiateplan( i.getLeft().getRight(),
this, i.getRight().getRight() ) )
414 .filter( i ->
m_fuzzy.getValue().defuzzify( i.getLeft().plan().condition( i.getRight() ) ) )
417 .collect( Collectors.toList() );
431 i.getLeft().plan().trigger().literal().fqnfunctor(),
432 i.getLeft().plan().trigger().literal().unify( i.getRight() )
436 return p_execution.parallelStream()
441 .execute(
false, i.getRight(), Collections.emptyList(), Collections.emptyList() );
442 if (
m_fuzzy.getValue().defuzzify( l_result ) )
444 i.getLeft().incrementsuccessful();
447 i.getLeft().incrementfail();
449 } ).collect(
m_fuzzy.getKey() );
458 private boolean active(
final boolean p_immediatly )
462 if ( ( m_sleepingcycles.compareAndSet( 0, Long.MIN_VALUE ) ) || p_immediatly )
465 m_sleepingterm.isEmpty()
473 : m_sleepingterm.parallelStream()
479 ).forEach( i -> m_trigger.put( i.structurehash(), i ) );
481 m_sleepingterm.clear();
485 if ( ( m_sleepingcycles.get() > 0 ) && ( m_sleepingcycles.get() != Long.MAX_VALUE ) )
486 m_sleepingcycles.decrementAndGet();
488 return m_sleepingcycles.get() <= 0;
final Multimap< IPath, ILiteral > m_runningplans
running plans (thread-safe)
Comparator< IStructureHash > COMPARATOR
comparator
plan statistic to count execution values
static IPlanStatistic from( @Nonnull final IPlan p_plan)
factory
final Multimap< IPath, IRule > m_rules
multimap with rules
final AtomicLong m_cycletime
nano seconds at the last cycle
static final Logger LOGGER
logger
structure hash definition
final Map< String, Object > storage()
boolean active(final boolean p_immediatly)
runs the wakeup goal
Collection< Pair< IPlanStatistic, IContext > > generateexecution( @Nonnull final Stream< ITrigger > p_trigger)
create execution list with plan and context
final Set< ITerm > m_sleepingterm
set for waking-up literals
Stream< ITrigger > trigger()
retruns all trigger of the beliefbase
final IUnifier m_unifier
unifier
illegal argument exception
IAgent<?> update( @Nonnull final IAgent<?> p_agent)
updates all items
static< N > IFuzzyValue< N > from( @Nonnull final N p_value)
factory
final Map< String, Object > m_storage
storage map
synchronized Collection< Pair< IPlanStatistic, IContext > > generateexecutionlist()
create the plan executionlist with clearing internal structures
final IAgent< T > sleep(final long p_cycles, @Nonnull final Stream< ITerm > p_literal)
common structure for execution definition
final IFuzzyValue< Boolean > trigger( @Nonnull final ITrigger p_trigger, @Nullable final boolean... p_immediately)
interface of logical rule
final< N extends IInspector > Stream< N > inspect( @Nonnull final N... p_inspector)
static< T > String languagestring(final T p_source, final String p_label, final Object... p_parameter)
returns the language depend string on any object
interface of an unification algorithm
Stream< ILiteral > stream( @Nullable final IPath... p_path)
returns stream of literal
final Multimap< ITrigger, IPlanStatistic > plans()
static ITrigger from( @Nonnull final EType p_event, @Nonnull final ILiteral p_literal)
creates a trigger event^
final IVariableBuilder variablebuilder()
IBeliefbase beliefbase()
returns the beliefbase
view for a beliefbase that creates any access to the underlying data structures
execution context with local data
static Logger logger(final Class<?> p_class)
returns a logger instance
final< N extends IAgent<?> > N raw()
static final long serialVersionUID
serial id
result for an immutable fuzzy value
final IFuzzyBundle< Boolean > m_fuzzy
fuzzy result collector
default generic literal class for agent beliefs a literal consists of a functor, an optional list of ...
interface to define the agent configuration
final IAgent< T > sleep(final long p_cycles, final ITerm... p_term)
final Multimap< IPath, IRule > rules()
interface for a variable builder which is called on each plan / rule execution
final Map< Integer, ITrigger > m_trigger
execution trigger with content hash
class for any helper calls
static ILiteral from( @Nonnull final String p_functor, @Nullable final ITerm... p_values)
factory
final IAgent< T > wakeup( @Nullable final ITerm... p_term)
final Multimap< ITrigger, IPlanStatistic > m_plans
map with all existing plans and successful / fail runs
final AtomicLong m_sleepingcycles
number of sleeping cycles
final IFuzzyBundle< Boolean > fuzzy()
IBaseAgent( @Nonnull final IAgentConfiguration< T > p_configuration)
ctor
IFuzzyValue< Boolean > execute( @Nonnull final Collection< Pair< IPlanStatistic, IContext >> p_execution)
execute list of plans
final IVariableBuilder m_variablebuilder
variable builder
final IView m_beliefbase
beliefbase
final Multimap< IPath, ILiteral > runningplans()
final IAgent< T > wakeup( @Nonnull final Stream< ITerm > p_term)
inspector interface to read agent internal data