LightJason - AgentSpeak(L++)
CASTVisitorPlanBundle.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.grammar;
25 
26 import com.google.common.collect.LinkedHashMultimap;
27 import com.google.common.collect.Multimap;
28 import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
29 import org.apache.commons.lang3.tuple.ImmutablePair;
30 import org.apache.commons.lang3.tuple.Pair;
88 
89 import javax.annotation.Nonnull;
90 import java.text.MessageFormat;
91 import java.util.Collection;
92 import java.util.Collections;
93 import java.util.HashSet;
94 import java.util.LinkedHashSet;
95 import java.util.LinkedList;
96 import java.util.List;
97 import java.util.Map;
98 import java.util.Objects;
99 import java.util.Set;
100 import java.util.logging.Logger;
101 import java.util.stream.Collectors;
102 
103 
110 @SuppressWarnings( {"all", "warnings", "unchecked", "unused", "cast"} )
111 public final class CASTVisitorPlanBundle extends AbstractParseTreeVisitor<Object> implements IASTVisitorPlanBundle
112 {
116  private static final Logger LOGGER = CCommon.logger( IASTVisitorAgent.class );
120  private final Set<ILiteral> m_initialbeliefs = new LinkedHashSet<>();
124  private final Set<IPlan> m_plans = new HashSet<>();
128  private final Multimap<IPath, IRule> m_rules = LinkedHashMultimap.create();
132  private final Map<IPath, IAction> m_actions;
133 
139  public CASTVisitorPlanBundle( @Nonnull final Set<IAction> p_actions )
140  {
141  m_actions = p_actions.stream().collect( Collectors.toMap( i -> i.name(), i -> i ) );
142  LOGGER.info( MessageFormat.format( "create parser with actions & rules : {0} / {1}", m_actions.keySet(), m_rules.keySet() ) );
143  }
144 
145 
146  // --- plan bundle rules -----------------------------------------------------------------------------------------------------------------------------------
147 
148  @Override
149  public final Object visitPlanbundle( final PlanBundleParser.PlanbundleContext p_context )
150  {
151  return this.visitChildren( p_context );
152  }
153 
154  // ---------------------------------------------------------------------------------------------------------------------------------------------------------
155 
156  // --- AgentSpeak(L) rules ---------------------------------------------------------------------------------------------------------------------------------
157 
158  @Override
159  public final Object visitBelief( final PlanBundleParser.BeliefContext p_context )
160  {
161  if ( Objects.isNull( p_context.literal() ) )
162  return null;
163 
164  m_initialbeliefs.add( (ILiteral) this.visitLiteral( p_context.literal() ) );
165  return null;
166  }
167 
168  @Override
169  public final Object visitPlans( final PlanBundleParser.PlansContext p_context )
170  {
171  if ( Objects.isNull( p_context.plan() ) )
172  return null;
173 
174  p_context.plan().stream().forEach( i -> ( (List<IPlan>) this.visitPlan( i ) ).stream().forEach( j -> m_plans.add( j ) ) );
175  LOGGER.info( MessageFormat.format( "parsed plans: {0}", m_plans ) );
176  return null;
177  }
178 
179  @Override
180  public final Object visitLogicrules( final PlanBundleParser.LogicrulesContext p_context )
181  {
182  // create placeholder objects first and run parsing again to build full-qualified rule objects
183  p_context.logicrule().stream()
184  .map( i -> (IRule) this.visitLogicrulePlaceHolder( i ) )
185  .forEach( i -> m_rules.put( i.identifier().fqnfunctor(), i ) );
186 
187  final Multimap<IPath, IRule> l_rules = LinkedHashMultimap.create();
188  p_context.logicrule().stream()
189  .flatMap( i -> ( (List<IRule>) this.visitLogicrule( i ) ).stream() )
190  .forEach( i -> l_rules.put( i.identifier().fqnfunctor(), i ) );
191 
192  // clear rule list and replace placeholder objects
193  m_rules.clear();
194  l_rules.values().stream()
195  .map( i -> i.replaceplaceholder( l_rules ) )
196  .forEach( i -> m_rules.put( i.identifier().fqnfunctor(), i ) );
197 
198  LOGGER.info( MessageFormat.format( "parsed rules: {0}", m_rules.values() ) );
199  return null;
200  }
201 
202  @Override
203  public final Object visitLogicrule( final PlanBundleParser.LogicruleContext p_context )
204  {
205  final ILiteral l_literal = (ILiteral) this.visitLiteral( p_context.literal() );
206  return p_context.logicalruledefinition().stream()
207  .map( i -> new CRule( (ILiteral) l_literal.deepcopy(), (List<IExecution>) this.visitLogicalruledefinition( i ) ) )
208  .collect( Collectors.toList() );
209  }
210 
211  @Override
212  public final Object visitLogicalruledefinition( final PlanBundleParser.LogicalruledefinitionContext p_context )
213  {
214  return this.visitBody( p_context.body() );
215  }
216 
217  @Override
218  public final Object visitPlan( final PlanBundleParser.PlanContext p_context )
219  {
220  final Set<IAnnotation<?>> l_annotation = (Set<IAnnotation<?>>) this.visitAnnotations( p_context.annotations() );
221  final CTrigger l_trigger = new CTrigger(
222  (ITrigger.EType) this.visitPlan_trigger( p_context.plan_trigger() ),
223  (ILiteral) this.visitLiteral( p_context.literal() )
224  );
225 
226  return p_context.plandefinition()
227  .stream()
228  .map( i ->
229  {
230  final Pair<IExpression, List<IExecution>> l_content = (Pair<IExpression, List<IExecution>>) this.visitPlandefinition( i );
231  return new CPlan( l_trigger, l_content.getLeft(), l_content.getRight(), l_annotation );
232  } )
233  .collect( Collectors.toList() );
234  }
235 
236  @Override
237  public final Object visitPlandefinition( final PlanBundleParser.PlandefinitionContext p_context )
238  {
239  return new ImmutablePair<IExpression, List<IExecution>>(
240  Objects.isNull( p_context.expression() ) ? IExpression.EMPTY
241  : (IExpression) this.visitExpression( p_context.expression() ),
242  (List<IExecution>) this.visitBody( p_context.body() )
243  );
244  }
245 
246  @Override
247  public final Object visitAnnotations( final PlanBundleParser.AnnotationsContext p_context )
248  {
249  if ( ( Objects.isNull( p_context ) ) || ( p_context.isEmpty() ) )
250  return Collections.emptySet();
251 
252 
253  final Set<IAnnotation<?>> l_annotation = new HashSet<>();
254 
255  if ( Objects.nonNull( p_context.annotation_atom() ) )
256  p_context.annotation_atom().stream().map( i -> (IAnnotation<?>) this.visitAnnotation_atom( i ) ).forEach( l_annotation::add );
257 
258  if ( Objects.nonNull( p_context.annotation_literal() ) )
259  p_context.annotation_literal().stream().map( i -> (IAnnotation<?>) this.visitAnnotation_literal( i ) ).forEach( l_annotation::add );
260 
261  return l_annotation.isEmpty() ? Collections.emptySet() : l_annotation;
262  }
263 
264  @Override
265  public final Object visitAnnotation_atom( final PlanBundleParser.Annotation_atomContext p_context )
266  {
267  if ( Objects.nonNull( p_context.ATOMIC() ) )
269 
270  if ( Objects.nonNull( p_context.PARALLEL() ) )
272 
273  throw new CIllegalArgumentException( CCommon.languagestring( this, "atomannotation", p_context.getText() ) );
274  }
275 
276  @Override
277  public final Object visitAnnotation_literal( final PlanBundleParser.Annotation_literalContext p_context )
278  {
279  return this.visitChildren( p_context );
280  }
281 
282  @Override
283  public Object visitAnnotation_value_literal( final PlanBundleParser.Annotation_value_literalContext p_context )
284  {
285  if ( Objects.nonNull( p_context.number() ) )
286  return new CValueAnnotation<>(
288  (String) this.visitVariableatom( p_context.variableatom() ),
289  ( (Number) this.visitNumber( p_context.number() ) ).doubleValue()
290  );
291 
292  if ( Objects.nonNull( p_context.STRING() ) )
293  return new CValueAnnotation<>(
295  (String) this.visitVariableatom( p_context.variableatom() ),
296  stringvalue( p_context.STRING().getText() )
297  );
298 
299  throw new CIllegalArgumentException( CCommon.languagestring( this, "valueannotation", p_context.getText() ) );
300  }
301 
302  @Override
303  public final Object visitPlan_trigger( final PlanBundleParser.Plan_triggerContext p_context )
304  {
305  return this.visitChildren( p_context );
306  }
307 
308  @Override
309  public final Object visitPlan_goal_trigger( final PlanBundleParser.Plan_goal_triggerContext p_context )
310  {
311  if ( ITrigger.EType.ADDGOAL.sequence().equals( p_context.getText() ) )
312  return ITrigger.EType.ADDGOAL;
313 
314  if ( ITrigger.EType.DELETEGOAL.sequence().equals( p_context.getText() ) )
315  return ITrigger.EType.DELETEGOAL;
316 
317  throw new CIllegalArgumentException( CCommon.languagestring( this, "goaltrigger", p_context.getText() ) );
318  }
319 
320  @Override
321  public final Object visitPlan_belief_trigger( final PlanBundleParser.Plan_belief_triggerContext p_context )
322  {
323  if ( ITrigger.EType.ADDBELIEF.sequence().equals( p_context.getText() ) )
324  return ITrigger.EType.ADDBELIEF;
325 
326  if ( ITrigger.EType.DELETEBELIEF.sequence().equals( p_context.getText() ) )
327  return ITrigger.EType.DELETEBELIEF;
328 
329  throw new CIllegalArgumentException( CCommon.languagestring( this, "belieftrigger", p_context.getText() ) );
330  }
331 
332  @Override
333  public final Object visitBody( final PlanBundleParser.BodyContext p_context )
334  {
335  // filter null values of the body formular, because blank lines adds a null value, body-formula rule return an executable call everytime
336  return p_context.body_formula().stream()
337  .filter( i -> Objects.nonNull( i ) )
338  .map( i -> this.visitBody_formula( i ) )
339  .filter( i -> i instanceof IExecution )
340  // expression are encapsulate to get result
341  .map( i -> i instanceof IExpression ? new CRawAction<>( i ) : i )
342  .collect( Collectors.toList() );
343  }
344 
345  @Override
346  public final Object visitBody_formula( final PlanBundleParser.Body_formulaContext p_context )
347  {
348  return this.visitChildren( p_context );
349  }
350 
351  @Override
352  public final Object visitRepair_formula( final PlanBundleParser.Repair_formulaContext p_context )
353  {
354  // a non-existing repair formula can return any object-item, so convert it
355  // to executable structure, because the grammar rule must return an executable item
356  if ( Objects.isNull( p_context.repair_formula() ) )
357  return this.visitChildren( p_context );
358 
359 
360  // if there exists any repair element, build a sequential hierarchie of repair calls
361  if ( Objects.nonNull( p_context.executable_term() ) )
362  return new CRepair(
363  (IExecution) this.visitExecutable_term( p_context.executable_term() ),
364  (IExecution) this.visitRepair_formula( p_context.repair_formula() )
365  );
366 
367  if ( Objects.nonNull( p_context.test_action() ) )
368  return new CRepair(
369  (IExecution) this.visitTest_action( p_context.test_action() ),
370  (IExecution) this.visitRepair_formula( p_context.repair_formula() )
371  );
372 
373  if ( Objects.nonNull( p_context.achievement_goal_action() ) )
374  return new CRepair(
375  (IExecution) this.visitAchievement_goal_action( p_context.achievement_goal_action() ),
376  (IExecution) this.visitRepair_formula( p_context.repair_formula() )
377  );
378 
379  throw new CSyntaxErrorException( CCommon.languagestring( this, "repairelement", p_context.getText() ) );
380  }
381 
382  @Override
383  public final Object visitUnification( final PlanBundleParser.UnificationContext p_context )
384  {
385  final Object l_constraint = this.visitUnification_constraint( p_context.unification_constraint() );
386 
387  if ( l_constraint instanceof IExpression )
388  return new CExpressionUnify(
389  p_context.AT() != null,
390  (ILiteral) this.visitLiteral( p_context.literal() ),
391  (IExpression) l_constraint
392  );
393 
394  if ( l_constraint instanceof IVariable<?> )
395  return new CVariableUnify(
396  p_context.AT() != null,
397  (ILiteral) this.visitLiteral( p_context.literal() ),
398  (IVariable<?>) l_constraint
399  );
400 
401  return new CDefaultUnify( p_context.AT() != null, (ILiteral) this.visitLiteral( p_context.literal() ) );
402  }
403 
404  @Override
405  public final Object visitUnification_constraint( final PlanBundleParser.Unification_constraintContext p_context )
406  {
407  if ( Objects.isNull( p_context ) )
408  return null;
409 
410  if ( Objects.nonNull( p_context.expression() ) )
411  return this.visitExpression( p_context.expression() );
412 
413  if ( Objects.nonNull( p_context.variable() ) )
414  return this.visitVariable( p_context.variable() );
415 
416  return null;
417  }
418 
419  @Override
420  public final Object visitBlock_formula( final PlanBundleParser.Block_formulaContext p_context )
421  {
422  if ( Objects.nonNull( p_context.body_formula() ) )
423  {
424  final LinkedList<IExecution> l_statement = new LinkedList<>();
425  l_statement.add( (IExecution) this.visitBody_formula( p_context.body_formula() ) );
426  return l_statement;
427  }
428 
429  return this.visitBody( p_context.body() );
430  }
431 
432  @Override
433  public final Object visitLambda( final PlanBundleParser.LambdaContext p_context )
434  {
435  if ( Objects.nonNull( p_context.lambda_return() ) )
436  return new CLambdaExpression(
437  Objects.nonNull( p_context.AT() ),
438  (IExecution) this.visitLambda_initialization( p_context.lambda_initialization() ),
439  (IVariable<?>) this.visitVariable( p_context.variable() ),
440  (IVariable<?>) this.visitLambda_return( p_context.lambda_return() ),
441  (List<IExecution>) this.visitBlock_formula( p_context.block_formula() )
442  );
443 
444  return new CLambdaExpression(
445  Objects.nonNull( p_context.AT() ),
446  (IExecution) this.visitLambda_initialization( p_context.lambda_initialization() ),
447  (IVariable<?>) this.visitVariable( p_context.variable() ),
448  (List<IExecution>) this.visitBlock_formula( p_context.block_formula() )
449  );
450  }
451 
452  @Override
453  public final Object visitLambda_initialization( final PlanBundleParser.Lambda_initializationContext p_context )
454  {
455  if ( Objects.nonNull( p_context.variable() ) )
456  return new CRawAction<>( this.visitVariable( p_context.variable() ) );
457 
458  if ( Objects.nonNull( p_context.literal() ) )
459  return new CProxyAction( m_actions, (ILiteral) this.visitLiteral( p_context.literal() ) );
460 
461  throw new CSyntaxErrorException( CCommon.languagestring( this, "lambdainitialization", p_context.getText() ) );
462  }
463 
464  @Override
465  public final Object visitLambda_return( final PlanBundleParser.Lambda_returnContext p_context )
466  {
467  return this.visitVariable( p_context.variable() );
468  }
469 
470  @Override
471  public final Object visitExecutable_term( final PlanBundleParser.Executable_termContext p_context )
472  {
473  if ( Objects.nonNull( p_context.STRING() ) )
474  return new CRawAction<>( stringvalue( p_context.STRING().getText() ) );
475  if ( Objects.nonNull( p_context.number() ) )
476  return new CRawAction<>( this.visitNumber( p_context.number() ) );
477  if ( Objects.nonNull( p_context.LOGICALVALUE() ) )
478  return new CRawAction<>( logicalvalue( p_context.LOGICALVALUE().getText() ) );
479 
480  if ( Objects.nonNull( p_context.executable_action() ) )
481  return this.visitExecutable_action( p_context.executable_action() );
482  if ( Objects.nonNull( p_context.executable_rule() ) )
483  return this.visitExecutable_rule( p_context.executable_rule() );
484 
485  if ( Objects.nonNull( p_context.expression() ) )
486  return this.visitExpression( p_context.expression() );
487  if ( Objects.nonNull( p_context.ternary_operation() ) )
488  return this.visitTernary_operation( p_context.ternary_operation() );
489 
490  throw new CIllegalArgumentException( CCommon.languagestring( this, "termunknown", p_context.getText() ) );
491  }
492 
493  @Override
494  public final Object visitAssignment_expression( final PlanBundleParser.Assignment_expressionContext p_context )
495  {
496  return this.visitChildren( p_context );
497  }
498 
499  @Override
500  public final Object visitAssignment_expression_singlevariable( final PlanBundleParser.Assignment_expression_singlevariableContext p_context )
501  {
502  return new CSingleAssignment<>(
503  (IVariable<?>) this.visitVariable( p_context.variable() ),
504  (IExecution) this.visitExecutable_term( p_context.executable_term() )
505  );
506  }
507 
508  @Override
509  public final Object visitAssignment_expression_multivariable( final PlanBundleParser.Assignment_expression_multivariableContext p_context )
510  {
511  return new CMultiAssignment<>(
512  p_context.variablelist().variable().stream().map( i -> (IVariable<?>) this.visitVariable( i ) )
513  .collect( Collectors.toList() ),
514  (IExecution) this.visitExecutable_term( p_context.executable_term() )
515  );
516  }
517 
518  @Override
519  public final Object visitUnary_expression( final PlanBundleParser.Unary_expressionContext p_context )
520  {
521  switch ( p_context.UNARYOPERATOR().getText() )
522  {
523  case "++":
524  return new CIncrement<>( (IVariable<Number>) this.visitVariable( p_context.variable() ) );
525 
526  case "--":
527  return new CDecrement<>( (IVariable<Number>) this.visitVariable( p_context.variable() ) );
528 
529  default:
530  throw new CIllegalArgumentException( CCommon.languagestring( this, "unaryoperator", p_context.getText() ) );
531  }
532  }
533 
534  @Override
535  public final Object visitBinary_expression( final PlanBundleParser.Binary_expressionContext p_context )
536  {
537  final IVariable<Number> l_lhs = (IVariable<Number>) this.visitVariable( p_context.variable( 0 ) );
538  final ITerm l_rhs = p_context.variable().size() == 2
539  ? (IVariable<Number>) this.visitVariable( p_context.variable( 1 ) )
540  : CRawTerm.from( this.visitNumber( p_context.number() ) );
541 
542  return new COperatorAssign(
543  l_lhs, l_rhs, org.lightjason.agentspeak.language.execution.expressionbinary.EOperator.from( p_context.BINARYOPERATOR().getText() )
544  );
545  }
546 
547  @Override
548  public final Object visitAchievement_goal_action( final PlanBundleParser.Achievement_goal_actionContext p_context )
549  {
550  if ( Objects.nonNull( p_context.literal() ) )
551  return new CAchievementGoalLiteral( (ILiteral) this.visitLiteral( p_context.literal() ), Objects.nonNull( p_context.DOUBLEEXCLAMATIONMARK() ) );
552 
553  if ( Objects.nonNull( p_context.variable_evaluate() ) )
554  return new CAchievementGoalVariable(
555  (IVariableEvaluate) this.visitVariable_evaluate( p_context.variable_evaluate() ),
556  p_context.DOUBLEEXCLAMATIONMARK() != null
557  );
558 
559  throw new CIllegalArgumentException( CCommon.languagestring( this, "achievmentgoal", p_context.getText() ) );
560  }
561 
562  @Override
563  public final Object visitTernary_operation( final PlanBundleParser.Ternary_operationContext p_context )
564  {
565  return new CTernaryOperation(
566  (IExpression) this.visitExpression( p_context.expression() ),
567  (IExecution) this.visitTernary_operation_true( p_context.ternary_operation_true() ),
568  (IExecution) this.visitTernary_operation_false( p_context.ternary_operation_false() )
569  );
570  }
571 
572  @Override
573  public final Object visitTernary_operation_true( final PlanBundleParser.Ternary_operation_trueContext p_context )
574  {
575  return this.visitExecutable_term( p_context.executable_term() );
576  }
577 
578  @Override
579  public final Object visitTernary_operation_false( final PlanBundleParser.Ternary_operation_falseContext p_context )
580  {
581  return this.visitExecutable_term( p_context.executable_term() );
582  }
583 
584  @Override
585  public final Object visitTest_action( final PlanBundleParser.Test_actionContext p_context )
586  {
587  // dollar sign is used to recognize a rule
588  return p_context.DOLLAR() != null
589  ? new CTestRule( CPath.from( (String) this.visitAtom( p_context.atom() ) ) )
590  : new CTestGoal( CPath.from( (String) this.visitAtom( p_context.atom() ) ) );
591  }
592 
593  @Override
594  public final Object visitBelief_action( final PlanBundleParser.Belief_actionContext p_context )
595  {
596  if ( Objects.nonNull( p_context.PLUS() ) )
597  return new CBeliefAction( (ILiteral) this.visitLiteral( p_context.literal() ), CBeliefAction.EAction.ADD );
598 
599  if ( Objects.nonNull( p_context.MINUS() ) )
600  return new CBeliefAction( (ILiteral) this.visitLiteral( p_context.literal() ), CBeliefAction.EAction.DELETE );
601 
602  throw new CIllegalArgumentException( CCommon.languagestring( this, "beliefaction", p_context.getText() ) );
603  }
604 
605  @Override
606  public final Object visitDeconstruct_expression( final PlanBundleParser.Deconstruct_expressionContext p_context )
607  {
608  return new CDeconstruct<>(
609  p_context.variablelist().variable().stream().map( i -> (IVariable<?>) this.visitVariable( i ) ).collect( Collectors.toList() ),
610  (ITerm) ( p_context.literal() != null ? this.visitLiteral( p_context.literal() ) : this.visitVariable( p_context.variable() ) )
611  );
612  }
613 
614  // ---------------------------------------------------------------------------------------------------------------------------------------------------------
615 
616 
617  // --- simple datatypes ------------------------------------------------------------------------------------------------------------------------------------
618 
619  @Override
620  public final Object visitLiteral( final PlanBundleParser.LiteralContext p_context )
621  {
622  return new CLiteral(
623  p_context.AT() != null,
624  p_context.STRONGNEGATION() != null,
625  CPath.from( this.visitAtom( p_context.atom() ).toString() ),
626  (Collection<ITerm>) this.visitTermlist( p_context.termlist() )
627  );
628  }
629 
630  @Override
631  public final Object visitTerm( final PlanBundleParser.TermContext p_context )
632  {
633  if ( Objects.nonNull( p_context.STRING() ) )
634  return stringvalue( p_context.STRING().getText() );
635  if ( Objects.nonNull( p_context.number() ) )
636  return this.visitNumber( p_context.number() );
637  if ( Objects.nonNull( p_context.LOGICALVALUE() ) )
638  return logicalvalue( p_context.LOGICALVALUE().getText() );
639 
640  if ( Objects.nonNull( p_context.literal() ) )
641  return this.visitLiteral( p_context.literal() );
642  if ( Objects.nonNull( p_context.variable() ) )
643  return this.visitVariable( p_context.variable() );
644 
645  if ( Objects.nonNull( p_context.termlist() ) )
646  return this.visitTermlist( p_context.termlist() );
647  if ( Objects.nonNull( p_context.expression() ) )
648  return this.visitExpression( p_context.expression() );
649  if ( Objects.nonNull( p_context.ternary_operation() ) )
650  return this.visitTernary_operation( p_context.ternary_operation() );
651 
652  throw new CIllegalArgumentException( CCommon.languagestring( this, "termunknown", p_context.getText() ) );
653  }
654 
655  @Override
656  public final Object visitTermlist( final PlanBundleParser.TermlistContext p_context )
657  {
658  if ( ( Objects.isNull( p_context ) ) || ( p_context.isEmpty() ) )
659  return Collections.<ITerm>emptyList();
660 
661  return p_context.term().stream()
662  .map( i -> this.visitTerm( i ) )
663  .filter( i -> Objects.nonNull( i ) )
664  .map( i -> i instanceof ITerm ? (ITerm) i : CRawTerm.from( i ) )
665  .collect( Collectors.toList() );
666  }
667 
668  @Override
669  public final Object visitVariablelist( final PlanBundleParser.VariablelistContext p_context )
670  {
671  return this.visitChildren( p_context );
672  }
673 
674  // ---------------------------------------------------------------------------------------------------------------------------------------------------------
675 
676 
677  // --- raw rules -------------------------------------------------------------------------------------------------------------------------------------------
678 
679  @Override
680  public final Object visitNumber( final PlanBundleParser.NumberContext p_context )
681  {
682  if ( Objects.nonNull( p_context.CONSTANTNUMBER() ) )
683  return numericonstant( p_context.CONSTANTNUMBER().getText() );
684 
685  final Number l_value = (Number) this.visitChildren( p_context );
686  return p_context.MINUS() != null
687  ? -1 * l_value.doubleValue()
688  : l_value.doubleValue();
689  }
690 
691  @Override
692  public final Object visitDigitsequence( final PlanBundleParser.DigitsequenceContext p_context )
693  {
694  return Double.valueOf( p_context.getText() );
695  }
696 
697  @Override
698  public final Object visitAtom( final PlanBundleParser.AtomContext p_context )
699  {
700  return p_context.getText();
701  }
702 
703  @Override
704  public final Object visitVariable( final PlanBundleParser.VariableContext p_context )
705  {
706  return Objects.isNull( p_context.AT() ) ? new CVariable<>( p_context.getText() ) : new CMutexVariable<>( p_context.getText() );
707  }
708 
709  @Override
710  public final Object visitVariableatom( final PlanBundleParser.VariableatomContext p_context )
711  {
712  return p_context.getText();
713  }
714 
715  @Override
716  public final Object visitExpression( final PlanBundleParser.ExpressionContext p_context )
717  {
718  // bracket expression
719  if ( Objects.nonNull( p_context.expression_bracket() ) )
720  return this.visitExpression_bracket( p_context.expression_bracket() );
721 
722  // or-expression
724  EOperator.OR,
725  (IExpression) this.visitExpression_logical_and( p_context.expression_logical_and() ),
726  p_context.expression() != null
727  ? p_context.expression().stream().map( i -> (IExpression) this.visitExpression( i ) ).collect( Collectors.toList() )
728  : Collections.emptyList()
729  );
730  }
731 
732  @Override
733  public final Object visitExpression_bracket( final PlanBundleParser.Expression_bracketContext p_context )
734  {
735  return this.visitExpression( p_context.expression() );
736  }
737 
738  @Override
739  public final Object visitExpression_logical_and( final PlanBundleParser.Expression_logical_andContext p_context )
740  {
742  EOperator.AND,
743  (IExpression) this.visitExpression_logical_xor( p_context.expression_logical_xor() ),
744  p_context.expression() != null
745  ? p_context.expression().stream().map( i -> (IExpression) this.visitExpression( i ) ).collect( Collectors.toList() )
746  : Collections.emptyList()
747  );
748  }
749 
750  @Override
751  public final Object visitExpression_logical_xor( final PlanBundleParser.Expression_logical_xorContext p_context )
752  {
753  if ( Objects.nonNull( p_context.expression_logical_element() ) )
755  EOperator.XOR,
756  (IExpression) this.visitExpression_logical_element( p_context.expression_logical_element() ),
757  p_context.expression() != null
758  ? p_context.expression().stream().map( i -> (IExpression) this.visitExpression( i ) ).collect( Collectors.toList() )
759  : Collections.emptyList()
760  );
761 
762  if ( Objects.nonNull( p_context.expression_logical_negation() ) )
763  return this.visitExpression_logical_negation( p_context.expression_logical_negation() );
764 
765  if ( Objects.nonNull( p_context.expression_numeric() ) )
766  return this.visitExpression_numeric( p_context.expression_numeric() );
767 
768  throw new CSyntaxErrorException( CCommon.languagestring( this, "logicallefthandside", p_context.getText() ) );
769  }
770 
771  @Override
772  public final Object visitExpression_logical_negation( final PlanBundleParser.Expression_logical_negationContext p_context )
773  {
774  return new CUnary( EOperator.NEGATION, (IExpression) this.visitExpression( p_context.expression() ) );
775  }
776 
777  @Override
778  public final Object visitExpression_logical_element( final PlanBundleParser.Expression_logical_elementContext p_context )
779  {
780  if ( Objects.nonNull( p_context.LOGICALVALUE().getSymbol() ) )
781  return new CAtom( logicalvalue( p_context.LOGICALVALUE().getText() ) );
782 
783  if ( Objects.nonNull( p_context.variable() ) )
784  return new CAtom( this.visitVariable( p_context.variable() ) );
785 
786  if ( Objects.nonNull( p_context.unification() ) )
787  return new CProxyReturnExpression<>( (IExecution) this.visitUnification( p_context.unification() ) );
788 
789  if ( Objects.nonNull( p_context.executable_action() ) )
790  return new CProxyReturnExpression<>( (IExecution) this.visitExecutable_action( p_context.executable_action() ) );
791 
792  if ( Objects.nonNull( p_context.executable_rule() ) )
793  return new CProxyReturnExpression<>( (IExecution) this.visitExecutable_rule( p_context.executable_rule() ) );
794 
795  throw new CSyntaxErrorException( CCommon.languagestring( this, "logicalelement", p_context.getText() ) );
796  }
797 
798  @Override
799  public final Object visitExpression_numeric( final PlanBundleParser.Expression_numericContext p_context )
800  {
801  if ( Objects.isNull( p_context.expression_numeric() ) )
802  return this.visitExpression_numeric_relation( p_context.expression_numeric_relation() );
803 
804  if ( Objects.nonNull( p_context.EQUAL() ) )
805  return new CComparable(
807  (IExpression) this.visitExpression_numeric_relation( p_context.expression_numeric_relation() ),
808  (IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
809  );
810 
811  if ( Objects.nonNull( p_context.NOTEQUAL() ) )
812  return new CComparable(
814  (IExpression) this.visitExpression_numeric_relation( p_context.expression_numeric_relation() ),
815  (IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
816  );
817 
818  throw new CSyntaxErrorException( CCommon.languagestring( this, "compareoperator", p_context.getText() ) );
819  }
820 
821  @Override
822  public final Object visitExpression_numeric_relation( final PlanBundleParser.Expression_numeric_relationContext p_context )
823  {
824  if ( Objects.isNull( p_context.expression_numeric() ) )
825  return this.visitExpression_numeric_additive( p_context.expression_numeric_additive() );
826 
827  if ( Objects.nonNull( p_context.GREATER() ) )
828  return new CRelational(
830  (IExpression) this.visitExpression_numeric_additive( p_context.expression_numeric_additive() ),
831  (IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
832  );
833 
834  if ( Objects.nonNull( p_context.GREATEREQUAL() ) )
835  return new CRelational(
837  (IExpression) this.visitExpression_numeric_additive( p_context.expression_numeric_additive() ),
838  (IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
839  );
840 
841  if ( Objects.nonNull( p_context.LESS() ) )
842  return new CRelational(
843  EOperator.LESS,
844  (IExpression) this.visitExpression_numeric_additive( p_context.expression_numeric_additive() ),
845  (IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
846  );
847 
848  if ( Objects.nonNull( p_context.LESSEQUAL() ) )
849  return new CRelational(
851  (IExpression) this.visitExpression_numeric_additive( p_context.expression_numeric_additive() ),
852  (IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
853  );
854 
855  throw new CSyntaxErrorException( CCommon.languagestring( this, "relationaloperator", p_context.getText() ) );
856  }
857 
858  @Override
859  public final Object visitExpression_numeric_additive( final PlanBundleParser.Expression_numeric_additiveContext p_context )
860  {
861  if ( Objects.isNull( p_context.expression_numeric() ) )
862  return this.visitExpression_numeric_multiplicative( p_context.expression_numeric_multiplicative() );
863 
864  if ( Objects.nonNull( p_context.PLUS() ) )
865  return new CAdditive(
866  EOperator.PLUS,
867  (IExpression) this.visitExpression_numeric_multiplicative( p_context.expression_numeric_multiplicative() ),
868  (IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
869  );
870 
871  if ( Objects.nonNull( p_context.MINUS() ) )
872  return new CAdditive(
874  (IExpression) this.visitExpression_numeric_multiplicative( p_context.expression_numeric_multiplicative() ),
875  (IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
876  );
877 
878  throw new CSyntaxErrorException( CCommon.languagestring( this, "additiveoperator", p_context.getText() ) );
879  }
880 
881  @Override
882  public final Object visitExpression_numeric_multiplicative( final PlanBundleParser.Expression_numeric_multiplicativeContext p_context )
883  {
884  if ( Objects.isNull( p_context.expression_numeric() ) )
885  return this.visitExpression_numeric_power( p_context.expression_numeric_power() );
886 
887  if ( Objects.nonNull( p_context.MULTIPLY() ) )
888  return new CMultiplicative(
890  (IExpression) this.visitExpression_numeric_power( p_context.expression_numeric_power() ),
891  (IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
892  );
893 
894  if ( Objects.nonNull( p_context.SLASH() ) )
895  return new CMultiplicative(
897  (IExpression) this.visitExpression_numeric_power( p_context.expression_numeric_power() ),
898  (IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
899  );
900 
901  if ( Objects.nonNull( p_context.MODULO() ) )
902  return new CMultiplicative(
904  (IExpression) this.visitExpression_numeric_power( p_context.expression_numeric_power() ),
905  (IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
906  );
907 
908  throw new CSyntaxErrorException( CCommon.languagestring( this, "multiplicativeoperator", p_context.getText() ) );
909  }
910 
911  @Override
912  public final Object visitExpression_numeric_power( final PlanBundleParser.Expression_numeric_powerContext p_context )
913  {
914  if ( Objects.isNull( p_context.expression_numeric() ) )
915  return this.visitExpression_numeric_element( p_context.expression_numeric_element() );
916 
917  return new CPower(
919  (IExpression) this.visitExpression_numeric_element( p_context.expression_numeric_element() ),
920  (IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
921  );
922  }
923 
924  @Override
925  public final Object visitExpression_numeric_element( final PlanBundleParser.Expression_numeric_elementContext p_context )
926  {
927  if ( Objects.nonNull( p_context.number() ) )
928  return new CAtom( this.visitNumber( p_context.number() ) );
929 
930  if ( Objects.nonNull( p_context.variable() ) )
931  return new CAtom( this.visitVariable( p_context.variable() ) );
932 
933  if ( Objects.nonNull( p_context.executable_action() ) )
934  return new CProxyReturnExpression<>( (IExecution) this.visitExecutable_action( p_context.executable_action() ) );
935 
936  if ( Objects.nonNull( p_context.executable_rule() ) )
937  return new CProxyReturnExpression<>( (IExecution) this.visitExecutable_rule( p_context.executable_rule() ) );
938 
939  throw new CSyntaxErrorException( CCommon.languagestring( this, "numericelement", p_context.getText() ) );
940  }
941 
942  @Override
943  public final Object visitExecutable_action( final PlanBundleParser.Executable_actionContext p_context )
944  {
945  return new CProxyAction( m_actions, (ILiteral) this.visitLiteral( p_context.literal() ) );
946  }
947 
948  @Override
949  public final Object visitExecutable_rule( final PlanBundleParser.Executable_ruleContext p_context )
950  {
951  if ( Objects.nonNull( p_context.literal() ) )
952  return new CAchievementRuleLiteral( (ILiteral) this.visitLiteral( p_context.literal() ) );
953 
954  if ( Objects.nonNull( p_context.variable_evaluate() ) )
955  return new CAchievementRuleVariable( (IVariableEvaluate) this.visitVariable_evaluate( p_context.variable_evaluate() ) );
956 
957  throw new CSyntaxErrorException( CCommon.languagestring( this, "executablerule", p_context.getText() ) );
958  }
959 
960  @Override
961  public final Object visitVariable_evaluate( final PlanBundleParser.Variable_evaluateContext p_context )
962  {
963  return new CVariableEvaluate(
964  (IVariable<?>) this.visitVariable( p_context.variable() ),
965  (List<ITerm>) this.visitTermlist( p_context.termlist() )
966  );
967  }
968 
969  // ---------------------------------------------------------------------------------------------------------------------------------------------------------
970 
971 
972  // --- helper ----------------------------------------------------------------------------------------------------------------------------------------------
973 
980  protected Object visitLogicrulePlaceHolder( final PlanBundleParser.LogicruleContext p_context )
981  {
982  return new CRulePlaceholder( (ILiteral) this.visitLiteral( p_context.literal() ) );
983  }
984 
991  private static Number numericonstant( @Nonnull final String p_value )
992  {
993  final Double l_constant = org.lightjason.agentspeak.grammar.CCommon.NUMERICCONSTANT.get( p_value );
994  if ( Objects.nonNull( l_constant ) )
995  return l_constant;
996 
997  throw new CSyntaxErrorException( CCommon.languagestring( CASTVisitorPlanBundle.class, "constantunknown", p_value ) );
998  }
999 
1006  private static boolean logicalvalue( @Nonnull final String p_value )
1007  {
1008  return ( !p_value.isEmpty() ) && ( ( "true".equals( p_value ) ) || ( "success".equals( p_value ) ) );
1009  }
1010 
1017  private static String stringvalue( @Nonnull final String p_value )
1018  {
1019  return p_value.length() < 3 ? "" : p_value.substring( 1, p_value.length() - 1 );
1020  }
1021 
1022  // ---------------------------------------------------------------------------------------------------------------------------------------------------------
1023 
1024 
1025  // --- getter structure ------------------------------------------------------------------------------------------------------------------------------------
1026 
1027  @Nonnull
1028  @Override
1029  public final Set<ILiteral> initialbeliefs()
1030  {
1031  return m_initialbeliefs;
1032  }
1033 
1034  @Nonnull
1035  @Override
1036  public final Set<IPlan> plans()
1037  {
1038  return m_plans;
1039  }
1040 
1041  @Nonnull
1042  @Override
1043  public final Set<IRule> rules()
1044  {
1045  return new HashSet<>( m_rules.values() );
1046  }
1047 
1048  // ---------------------------------------------------------------------------------------------------------------------------------------------------------
1049 }
final Object visitLiteral(final PlanBundleParser.LiteralContext p_context)
final Object visitExpression_numeric_multiplicative(final PlanBundleParser.Expression_numeric_multiplicativeContext p_context)
default abstract-syntax-tree (AST) visitor for plan-bundles scripts
final Object visitVariable_evaluate(final PlanBundleParser.Variable_evaluateContext p_context)
final Set< IPlan > plans()
get a multimap with event-plan matching
final Set< IRule > rules()
returns the rules / principles
final Object visitAssignment_expression_singlevariable(final PlanBundleParser.Assignment_expression_singlevariableContext p_context)
final Object visitExpression_logical_xor(final PlanBundleParser.Expression_logical_xorContext p_context)
IExpression EMPTY
empty expression, is always true
static String stringvalue( @Nonnull final String p_value)
create a string value without quotes
final Object visitPlandefinition(final PlanBundleParser.PlandefinitionContext p_context)
final Object visitExpression_numeric_relation(final PlanBundleParser.Expression_numeric_relationContext p_context)
placeholder rule to define correct rule referencing
final Object visitExecutable_rule(final PlanBundleParser.Executable_ruleContext p_context)
final Object visitDeconstruct_expression(final PlanBundleParser.Deconstruct_expressionContext p_context)
final Object visitLogicrules(final PlanBundleParser.LogicrulesContext p_context)
final Object visitAnnotations(final PlanBundleParser.AnnotationsContext p_context)
final Object visitLambda(final PlanBundleParser.LambdaContext p_context)
final Object visitPlans(final PlanBundleParser.PlansContext p_context)
final Object visitExpression_numeric_element(final PlanBundleParser.Expression_numeric_elementContext p_context)
final Object visitLambda_return(final PlanBundleParser.Lambda_returnContext p_context)
static IPath from( @Nonnull final String p_string)
factor method to build path
Definition: CPath.java:166
static< T > String languagestring(final T p_source, final String p_label, final Object... p_parameter)
returns the language depend string on any object
final Object visitTernary_operation(final PlanBundleParser.Ternary_operationContext p_context)
class to create a path structure
Definition: CPath.java:53
encapsulate class for any non-executable data type e.g.
Definition: CRawAction.java:47
final Object visitTest_action(final PlanBundleParser.Test_actionContext p_context)
final Object visitExpression_numeric_power(final PlanBundleParser.Expression_numeric_powerContext p_context)
final Object visitExpression_numeric_additive(final PlanBundleParser.Expression_numeric_additiveContext p_context)
final Object visitLambda_initialization(final PlanBundleParser.Lambda_initializationContext p_context)
final Object visitPlan(final PlanBundleParser.PlanContext p_context)
visitor interface of the abstract-syntax-tree (AST) for an agent
final Object visitExpression_logical_and(final PlanBundleParser.Expression_logical_andContext p_context)
final Object visitLogicrule(final PlanBundleParser.LogicruleContext p_context)
final Object visitExpression_logical_element(final PlanBundleParser.Expression_logical_elementContext p_context)
Object visitLogicrulePlaceHolder(final PlanBundleParser.LogicruleContext p_context)
create a rule placeholder object
external action interface
Definition: IAction.java:38
static Logger logger(final Class<?> p_class)
returns a logger instance
final Object visitPlan_belief_trigger(final PlanBundleParser.Plan_belief_triggerContext p_context)
final Object visitBelief(final PlanBundleParser.BeliefContext p_context)
final Object visitTernary_operation_false(final PlanBundleParser.Ternary_operation_falseContext p_context)
final Object visitNumber(final PlanBundleParser.NumberContext p_context)
final Object visitTernary_operation_true(final PlanBundleParser.Ternary_operation_trueContext p_context)
static EOperator from( @Nonnull final String p_value)
returns operator from string
final Object visitLogicalruledefinition(final PlanBundleParser.LogicalruledefinitionContext p_context)
final Object visitUnification(final PlanBundleParser.UnificationContext p_context)
final Object visitAnnotation_atom(final PlanBundleParser.Annotation_atomContext p_context)
final Object visitAtom(final PlanBundleParser.AtomContext p_context)
final Object visitBody_formula(final PlanBundleParser.Body_formulaContext p_context)
final Object visitExpression_bracket(final PlanBundleParser.Expression_bracketContext p_context)
final Object visitRepair_formula(final PlanBundleParser.Repair_formulaContext p_context)
defines an execution element with a repair call
Definition: CRepair.java:42
default generic literal class for agent beliefs a literal consists of a functor, an optional list of ...
Definition: CLiteral.java:64
final Object visitExecutable_action(final PlanBundleParser.Executable_actionContext p_context)
final Object visitAnnotation_literal(final PlanBundleParser.Annotation_literalContext p_context)
final Object visitVariable(final PlanBundleParser.VariableContext p_context)
static Number numericonstant( @Nonnull final String p_value)
returns the value of a numeric constant
final Object visitVariableatom(final PlanBundleParser.VariableatomContext p_context)
final Object visitPlan_trigger(final PlanBundleParser.Plan_triggerContext p_context)
static IExpression createLogicalBinaryExpression( @Nonnull final EOperator p_operator, @Nonnull final IExpression p_lefthandside, @Nonnull final Collection< IExpression > p_righthandside)
creates a logical expression concationation with single operator
final Object visitAchievement_goal_action(final PlanBundleParser.Achievement_goal_actionContext p_context)
T deepcopy( @Nullable final IPath... p_prefix)
clones the object (shallow-copy)
static boolean logicalvalue( @Nonnull final String p_value)
converts a string token to the type
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
Definition: CRawTerm.java:104
final Object visitExpression(final PlanBundleParser.ExpressionContext p_context)
final Object visitPlan_goal_trigger(final PlanBundleParser.Plan_goal_triggerContext p_context)
final Object visitAssignment_expression_multivariable(final PlanBundleParser.Assignment_expression_multivariableContext p_context)
static final Map< String, Double > NUMERICCONSTANT
numeric constant values - infinity is defined manually
final Object visitVariablelist(final PlanBundleParser.VariablelistContext p_context)
final Object visitUnification_constraint(final PlanBundleParser.Unification_constraintContext p_context)
visitor interface of the abstract-syntax-tree (AST) for a plan bundle
final Object visitTerm(final PlanBundleParser.TermContext p_context)
final Object visitBinary_expression(final PlanBundleParser.Binary_expressionContext p_context)
final Object visitTermlist(final PlanBundleParser.TermlistContext p_context)
final Object visitExecutable_term(final PlanBundleParser.Executable_termContext p_context)
final Map< IPath, IAction > m_actions
map with action definition
final Object visitAssignment_expression(final PlanBundleParser.Assignment_expressionContext p_context)
final Object visitBody(final PlanBundleParser.BodyContext p_context)
final Object visitDigitsequence(final PlanBundleParser.DigitsequenceContext p_context)
final Object visitBelief_action(final PlanBundleParser.Belief_actionContext p_context)
final Set< ILiteral > initialbeliefs()
returns initial beliefs
final Object visitBlock_formula(final PlanBundleParser.Block_formulaContext p_context)
final Object visitExpression_numeric(final PlanBundleParser.Expression_numericContext p_context)
final Object visitPlanbundle(final PlanBundleParser.PlanbundleContext p_context)
Object visitAnnotation_value_literal(final PlanBundleParser.Annotation_value_literalContext p_context)
final Object visitExpression_logical_negation(final PlanBundleParser.Expression_logical_negationContext p_context)
CASTVisitorPlanBundle( @Nonnull final Set< IAction > p_actions)
ctor
term structure for simple datatypes
Definition: CRawTerm.java:45
final Object visitUnary_expression(final PlanBundleParser.Unary_expressionContext p_context)