LightJason - AgentSpeak(L++)
CRule.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.language.instantiable.rule;
25 
26 import com.google.common.collect.Multimap;
36 
37 import javax.annotation.Nonnull;
38 import java.text.MessageFormat;
39 import java.util.Collections;
40 import java.util.List;
41 import java.util.stream.Collectors;
42 import java.util.stream.Stream;
43 
44 
48 public final class CRule extends IBaseInstantiable implements IRule
49 {
53  private static final long serialVersionUID = -1165818799700126229L;
57  private final ILiteral m_id;
58 
65  public CRule( @Nonnull final ILiteral p_id, @Nonnull final List<IExecution> p_action )
66  {
67  super(
68  p_action,
69  Collections.emptySet(),
70  Stream.concat(
71  Stream.of( p_id.hashCode() ),
72  p_action.stream().map( Object::hashCode )
73  ).reduce( 0, ( i, j ) -> i ^ j )
74  );
75  m_id = p_id;
76  }
77 
78  @Nonnull
79  @Override
80  public final ILiteral identifier()
81  {
82  return m_id;
83  }
84 
85  @Nonnull
86  @Override
87  @SuppressWarnings( "unchecked" )
88  public final IRule replaceplaceholder( @Nonnull final Multimap<IPath, IRule> p_rules )
89  {
90  return new CRule(
91  m_id,
92  m_action.stream().map( i ->
93  i instanceof CRulePlaceholder
94  // create a full deep-copy of the literal for avoid indeterminisitic behaviour on rule unification
95  ? new CAchievementRuleLiteral( (ILiteral) ( (CRulePlaceholder) i ).identifier().deepcopy() )
96  : i
97  ).collect( Collectors.toList() )
98  );
99  }
100 
101  @Nonnull
102  @Override
103  public final Stream<IVariable<?>> variables()
104  {
105  return CCommon.streamconcat(
106  super.variables(),
107  m_annotation.values().stream().flatMap( IAnnotation::variables ),
108  CCommon.flattenrecursive( m_id.orderedvalues() ).filter( i -> i instanceof IVariable<?> ).map( ITerm::term )
109  );
110  }
111 
112  @Override
113  public final String toString()
114  {
115  return MessageFormat.format(
116  "{0} ({1} ==>> {2})",
117  super.toString(),
118  m_id,
119  m_action
120  );
121  }
122 
123 }
CRule( @Nonnull final ILiteral p_id, @Nonnull final List< IExecution > p_action)
ctor
Definition: CRule.java:65
final Map< IAnnotation.EType, IAnnotation<?> > m_annotation
map with annotation (enum value for getting annotation object)
final Stream< IVariable<?> > variables()
returns a stream with all used variables
Definition: CRule.java:103
placeholder rule to define correct rule referencing
common structure for execution definition
Stream< ITerm > orderedvalues( @Nullable final IPath... p_path)
returns a stream over the ordered values in sequential ordering
final ILiteral identifier()
returns the identifier of the rule
Definition: CRule.java:80
final IRule replaceplaceholder( @Nonnull final Multimap< IPath, IRule > p_rules)
replaces all placeholder objects and reinstantiate object
Definition: CRule.java:88
static Stream< ITerm > flattenrecursive( @Nonnull final Stream< ITerm > p_input)
recursive stream of term values
final ILiteral m_id
identifier of the rule
Definition: CRule.java:57
Stream< IVariable<?> > variables()
returns a stream of variables
default< T extends ITerm > T term()
casts the object to a term-type
Definition: ITerm.java:158
static< T > Stream< T > streamconcat( @Nonnull final Stream< T >... p_streams)
concat multiple streams