24 package org.lightjason.agentspeak.grammar;
26 import com.google.common.collect.LinkedHashMultimap;
27 import com.google.common.collect.Multimap;
28 import org.antlr.v4.runtime.tree.AbstractParseTreeVisitor;
61 import javax.annotation.Nonnull;
62 import java.util.Collection;
63 import java.util.Collections;
65 import java.util.Objects;
67 import java.util.stream.Collectors;
73 @SuppressWarnings( {
"all",
"warnings",
"unchecked",
"unused",
"cast"} )
83 private final Multimap<IPath, IRule> m_rules = LinkedHashMultimap.create();
103 this( Collections.<
IAction>emptySet(), Collections.<
IRule>emptySet() );
112 public CASTVisitorType( @Nonnull
final Set<IAction> p_actions, @Nonnull
final Set<IRule> p_rules )
114 m_actions = p_actions.stream().collect( Collectors.toMap( i -> i.name(), i -> i ) );
115 p_rules.stream().forEach( i -> m_rules.put( i.identifier().fqnfunctor(), i ) );
124 m_literal = (
ILiteral) this.visitChildren( p_context );
131 m_expression = (
IExpression) this.visitChildren( p_context );
138 m_term = (
ITerm) this.visitChildren( p_context );
145 if ( Objects.nonNull( p_context.STRING() ) )
147 if ( Objects.nonNull( p_context.number() ) )
148 return new CRawAction<>( this.visitNumber( p_context.number() ) );
149 if ( Objects.nonNull( p_context.LOGICALVALUE() ) )
150 return new CRawAction<>( logicalvalue( p_context.LOGICALVALUE().getText() ) );
152 if ( Objects.nonNull( p_context.executable_action() ) )
153 return this.visitExecutable_action( p_context.executable_action() );
154 if ( Objects.nonNull( p_context.executable_rule() ) )
155 return this.visitExecutable_rule( p_context.executable_rule() );
157 if ( Objects.nonNull( p_context.expression() ) )
158 return this.visitExpression( p_context.expression() );
159 if ( Objects.nonNull( p_context.ternary_operation() ) )
160 return this.visitTernary_operation( p_context.ternary_operation() );
173 final Object l_constraint = this.visitUnification_constraint( p_context.unification_constraint() );
177 Objects.nonNull( p_context.AT() ),
178 (
ILiteral) this.visitLiteral( p_context.literal() ),
179 (IExpression) l_constraint
184 Objects.nonNull( p_context.AT() ),
185 (
ILiteral) this.visitLiteral( p_context.literal() ),
186 (IVariable<?>) l_constraint
189 return new CDefaultUnify( Objects.nonNull( p_context.AT() ), (
ILiteral) this.visitLiteral( p_context.literal() ) );
195 if ( Objects.isNull( p_context ) )
198 if ( Objects.nonNull( p_context.expression() ) )
199 return this.visitExpression( p_context.expression() );
201 if ( Objects.nonNull( p_context.variable() ) )
202 return this.visitVariable( p_context.variable() );
211 (
IExpression) this.visitExpression( p_context.expression() ),
212 (
IExecution) this.visitTernary_operation_true( p_context.ternary_operation_true() ),
213 (
IExecution) this.visitTernary_operation_false( p_context.ternary_operation_false() )
220 return this.visitExecutable_term( p_context.executable_term() );
226 return this.visitExecutable_term( p_context.executable_term() );
235 public final Object
visitLiteral(
final TypeParser.LiteralContext p_context )
238 Objects.nonNull( p_context.AT() ),
239 Objects.nonNull( p_context.STRONGNEGATION() ),
240 CPath.
from(
this.visitAtom( p_context.atom() ).toString() ),
241 (Collection<ITerm>) this.visitTermlist( p_context.termlist() )
246 public final Object
visitTerm(
final TypeParser.TermContext p_context )
248 if ( Objects.nonNull( p_context.STRING() ) )
249 return stringvalue( p_context.STRING().getText() );
250 if ( Objects.nonNull( p_context.number() ) )
251 return this.visitNumber( p_context.number() );
252 if ( Objects.nonNull( p_context.LOGICALVALUE() ) )
253 return logicalvalue( p_context.LOGICALVALUE().getText() );
255 if ( Objects.nonNull( p_context.literal() ) )
256 return this.visitLiteral( p_context.literal() );
257 if ( Objects.nonNull( p_context.variable() ) )
258 return this.visitVariable( p_context.variable() );
260 if ( Objects.nonNull( p_context.termlist() ) )
261 return this.visitTermlist( p_context.termlist() );
262 if ( Objects.nonNull( p_context.expression() ) )
263 return this.visitExpression( p_context.expression() );
264 if ( Objects.nonNull( p_context.ternary_operation() ) )
265 return this.visitTernary_operation( p_context.ternary_operation() );
271 public final Object
visitTermlist(
final TypeParser.TermlistContext p_context )
273 if ( ( Objects.isNull( p_context ) ) || ( p_context.isEmpty() ) )
274 return Collections.<
ITerm>emptyList();
276 return p_context.term().stream()
277 .map( i -> this.visitTerm( i ) )
278 .filter( i -> Objects.nonNull( i ) )
280 .collect( Collectors.toList() );
286 return this.visitChildren( p_context );
295 public final Object
visitNumber(
final TypeParser.NumberContext p_context )
297 if ( Objects.nonNull( p_context.CONSTANTNUMBER() ) )
298 return numericonstant( p_context.CONSTANTNUMBER().getText() );
300 final Number l_value = (Number) this.visitChildren( p_context );
301 return Objects.nonNull( p_context.MINUS() )
302 ? -1 * l_value.doubleValue()
303 : l_value.doubleValue();
309 return Double.valueOf( p_context.getText() );
313 public final Object
visitAtom(
final TypeParser.AtomContext p_context )
315 return p_context.getText();
319 public final Object
visitVariable(
final TypeParser.VariableContext p_context )
321 return Objects.isNull( p_context.AT() )
322 ?
new CVariable<>( (String) this.visitVariableatom( p_context.variableatom() ) )
323 :
new CMutexVariable<>( (String) this.visitVariableatom( p_context.variableatom() ) );
329 return p_context.getText();
336 if ( Objects.nonNull( p_context.expression_bracket() ) )
337 return this.visitExpression_bracket( p_context.expression_bracket() );
340 return CCommon.createLogicalBinaryExpression(
342 (
IExpression)
this.visitExpression_logical_and( p_context.expression_logical_and() ),
343 Objects.nonNull( p_context.expression() )
344 ? p_context.expression().stream().map( i -> (
IExpression) this.visitExpression( i ) ).collect(
345 Collectors.toList() )
353 return this.visitExpression( p_context.expression() );
359 return CCommon.createLogicalBinaryExpression(
361 (
IExpression)
this.visitExpression_logical_xor( p_context.expression_logical_xor() ),
362 Objects.nonNull( p_context.expression() )
363 ? p_context.expression().stream().map( i -> (
IExpression) this.visitExpression( i ) ).collect(
364 Collectors.toList() )
372 if ( Objects.nonNull( p_context.expression_logical_element() ) )
373 return CCommon.createLogicalBinaryExpression(
375 (
IExpression)
this.visitExpression_logical_element( p_context.expression_logical_element() ),
376 Objects.nonNull( p_context.expression() )
377 ? p_context.expression().stream().map( i -> (
IExpression) this.visitExpression( i ) ).collect(
378 Collectors.toList() )
382 if ( Objects.nonNull( p_context.expression_logical_negation() ) )
383 return this.visitExpression_logical_negation( p_context.expression_logical_negation() );
385 if ( Objects.nonNull( p_context.expression_numeric() ) )
386 return this.visitExpression_numeric( p_context.expression_numeric() );
400 if ( Objects.nonNull( p_context.LOGICALVALUE() ) )
401 return new CAtom( logicalvalue( p_context.LOGICALVALUE().getText() ) );
403 if ( Objects.nonNull( p_context.variable() ) )
404 return new CAtom( this.visitVariable( p_context.variable() ) );
406 if ( Objects.nonNull( p_context.unification() ) )
409 if ( Objects.nonNull( p_context.executable_action() ) )
412 if ( Objects.nonNull( p_context.executable_rule() ) )
421 if ( Objects.isNull( p_context.expression_numeric() ) )
422 return this.visitExpression_numeric_relation( p_context.expression_numeric_relation() );
424 if ( Objects.nonNull( p_context.EQUAL() ) )
427 (
IExpression)
this.visitExpression_numeric_relation( p_context.expression_numeric_relation() ),
428 (
IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
431 if ( Objects.nonNull( p_context.NOTEQUAL() ) )
434 (
IExpression)
this.visitExpression_numeric_relation( p_context.expression_numeric_relation() ),
435 (
IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
444 if ( Objects.isNull( p_context.expression_numeric() ) )
445 return this.visitExpression_numeric_additive( p_context.expression_numeric_additive() );
447 if ( Objects.nonNull( p_context.GREATER() ) )
450 (
IExpression)
this.visitExpression_numeric_additive( p_context.expression_numeric_additive() ),
451 (
IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
454 if ( Objects.nonNull( p_context.GREATEREQUAL() ) )
457 (
IExpression)
this.visitExpression_numeric_additive( p_context.expression_numeric_additive() ),
458 (
IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
461 if ( Objects.nonNull( p_context.LESS() ) )
464 (
IExpression)
this.visitExpression_numeric_additive( p_context.expression_numeric_additive() ),
465 (
IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
468 if ( Objects.nonNull( p_context.LESSEQUAL() ) )
471 (
IExpression)
this.visitExpression_numeric_additive( p_context.expression_numeric_additive() ),
472 (
IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
481 if ( Objects.isNull( p_context.expression_numeric() ) )
482 return this.visitExpression_numeric_multiplicative( p_context.expression_numeric_multiplicative() );
484 if ( Objects.nonNull( p_context.PLUS() ) )
487 (
IExpression)
this.visitExpression_numeric_multiplicative( p_context.expression_numeric_multiplicative() ),
488 (
IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
491 if ( Objects.nonNull( p_context.MINUS() ) )
494 (
IExpression)
this.visitExpression_numeric_multiplicative( p_context.expression_numeric_multiplicative() ),
495 (
IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
504 if ( Objects.isNull( p_context.expression_numeric() ) )
505 return this.visitExpression_numeric_power( p_context.expression_numeric_power() );
507 if ( Objects.nonNull( p_context.MULTIPLY() ) )
510 (
IExpression)
this.visitExpression_numeric_power( p_context.expression_numeric_power() ),
511 (
IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
514 if ( Objects.nonNull( p_context.SLASH() ) )
517 (
IExpression)
this.visitExpression_numeric_power( p_context.expression_numeric_power() ),
518 (
IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
521 if ( Objects.nonNull( p_context.MODULO() ) )
524 (
IExpression)
this.visitExpression_numeric_power( p_context.expression_numeric_power() ),
525 (
IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
534 if ( Objects.isNull( p_context.expression_numeric() ) )
535 return this.visitExpression_numeric_element( p_context.expression_numeric_element() );
539 (
IExpression)
this.visitExpression_numeric_element( p_context.expression_numeric_element() ),
540 (
IExpression) this.visitExpression_numeric( p_context.expression_numeric() )
547 if ( Objects.nonNull( p_context.number() ) )
548 return new CAtom( this.visitNumber( p_context.number() ) );
550 if ( Objects.nonNull( p_context.variable() ) )
551 return new CAtom( this.visitVariable( p_context.variable() ) );
553 if ( Objects.nonNull( p_context.executable_action() ) )
556 if ( Objects.nonNull( p_context.executable_rule() ) )
571 if ( Objects.nonNull( p_context.literal() ) )
597 if ( Objects.nonNull( l_constant ) )
611 return ( !p_value.isEmpty() ) && ( (
"true".equals( p_value ) ) || (
"success".equals( p_value ) ) );
620 private static String
stringvalue( @Nonnull
final String p_value )
622 return p_value.length() < 3 ?
"" : p_value.substring( 1, p_value.length() - 1 );
final Object visitExpression_numeric_relation(final TypeParser.Expression_numeric_relationContext p_context)
static boolean logicalvalue( @Nonnull final String p_value)
converts a string token to the type
final Object visitTernary_operation_false(final TypeParser.Ternary_operation_falseContext p_context)
IExpression EMPTY
empty expression, is always true
proxy action to encapsulate all actions
final Object visitVariable_evaluate(final TypeParser.Variable_evaluateContext p_context)
illegal argument exception
final Object visitUnification(final TypeParser.UnificationContext p_context)
internal execution interface
final Object visitAtom(final TypeParser.AtomContext p_context)
multiplicative binary expression
interface of logical rule
final Object visitUnification_constraint(final TypeParser.Unification_constraintContext p_context)
static IPath from( @Nonnull final String p_string)
factor method to build path
exception for parsing error
final Object visitExpression_logical_xor(final TypeParser.Expression_logical_xorContext p_context)
static< T > String languagestring(final T p_source, final String p_label, final Object... p_parameter)
returns the language depend string on any object
parser for complex-datatypes
class to create a path structure
encapsulate class for any non-executable data type e.g.
final Object visitExpression(final TypeParser.ExpressionContext p_context)
final Object visitExpression_numeric_additive(final TypeParser.Expression_numeric_additiveContext p_context)
final Object visitVariable(final TypeParser.VariableContext p_context)
numerical relation expression
comparable binary expression
final Object visitExpression_numeric_multiplicative(final TypeParser.Expression_numeric_multiplicativeContext p_context)
final Object visitExecutable_action(final TypeParser.Executable_actionContext p_context)
static String stringvalue( @Nonnull final String p_value)
create a string value without quotes
final Object visitTernary_operation(final TypeParser.Ternary_operationContext p_context)
external action interface
final Object visitExpression_logical_negation(final TypeParser.Expression_logical_negationContext p_context)
final Object visitExpression_term(final TypeParser.Expression_termContext p_context)
final Object visitTernary_operation_true(final TypeParser.Ternary_operation_trueContext p_context)
static Number numericonstant( @Nonnull final String p_value)
returns the value of a numeric constant
final Object visitVariableatom(final TypeParser.VariableatomContext p_context)
encpasulating any execution context
final Object visitExpression_numeric_power(final TypeParser.Expression_numeric_powerContext p_context)
final Map< IPath, IAction > m_actions
map with action definition
achievement for rule-literal execution
default generic literal class for agent beliefs a literal consists of a functor, an optional list of ...
final IExpression expression()
get the parsed expression
final Object visitLiteral(final TypeParser.LiteralContext p_context)
final ILiteral literal()
get the parsed literal
final Object visitLiteral_type(final TypeParser.Literal_typeContext p_context)
final Object visitExecutable_rule(final TypeParser.Executable_ruleContext p_context)
final ITerm term()
get the parsed term
final Object visitExpression_numeric_element(final TypeParser.Expression_numeric_elementContext p_context)
static< N > CRawTerm< N > from(final N p_value)
factory for a raw term
class for any helper calls
final Object visitExpression_type(final TypeParser.Expression_typeContext p_context)
default variable definition
final Object visitVariablelist(final TypeParser.VariablelistContext p_context)
final Object visitExpression_logical_element(final TypeParser.Expression_logical_elementContext p_context)
static final Map< String, Double > NUMERICCONSTANT
numeric constant values - infinity is defined manually
final Object visitExpression_logical_and(final TypeParser.Expression_logical_andContext p_context)
final Object visitTermlist(final TypeParser.TermlistContext p_context)
final Object visitExpression_numeric(final TypeParser.Expression_numericContext p_context)
final Object visitDigitsequence(final TypeParser.DigitsequenceContext p_context)
final Object visitExpression_bracket(final TypeParser.Expression_bracketContext p_context)
final Object visitTerm(final TypeParser.TermContext p_context)
additve binary expression
CASTVisitorType( @Nonnull final Set< IAction > p_actions, @Nonnull final Set< IRule > p_rules)
ctor
final Object visitNumber(final TypeParser.NumberContext p_context)
interface of a type parser
term structure for simple datatypes
ILiteral EMPTY
empty literal
final Object visitExecutable_term(final TypeParser.Executable_termContext p_context)