Skip to content

Commit

Permalink
[DROOLS-7270] Enhance test/grammar coverage : andRestriction, orRestr…
Browse files Browse the repository at this point in the history
…iction (#4)

- Removed some unused cast
  • Loading branch information
tkobayas authored and rgdoliveira committed Oct 24, 2024
1 parent 77e42a6 commit d637f88
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,19 @@ andExpression : left=equalityExpression (BITAND right=equalityExpression)* ;
equalityExpression : left=instanceOfExpression ( ( op=EQUAL | op=NOTEQUAL ) right=instanceOfExpression )* ;
instanceOfExpression : left=inExpression ( 'instanceof' right=type )? ;
inExpression : left=relationalExpression ( 'not'? 'in' LPAREN drlExpression (COMMA drlExpression)* RPAREN )? ;
relationalExpression : drlExpression? ;

relationalExpression : left=drlExpression (right=orRestriction)* ;
orRestriction : left=andRestriction (OR right=andRestriction)* ;
andRestriction : left=singleRestriction (AND right=singleRestriction)* ;
singleRestriction : op=relationalOperator drlExpression ;

relationalOperator
: EQUAL
| NOTEQUAL
| LE
| GE
| GT
| LT
;

/* function := FUNCTION type? ID parameters(typed) chunk_{_} */
functiondef : DRL_FUNCTION typeTypeOrVoid? IDENTIFIER formalParameters block ;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,9 @@ public PatternDescr visitLhsPattern(DRLParser.LhsPatternContext ctx) {

@Override
public List<ExprConstraintDescr> visitConstraints(DRLParser.ConstraintsContext ctx) {
if (ctx == null) {
return new ArrayList<>();
}
List<BaseDescr> descrList = visitDescrChildren(ctx);
return descrList.stream()
.filter(ExprConstraintDescr.class::isInstance)
Expand All @@ -253,57 +256,20 @@ public List<ExprConstraintDescr> visitConstraints(DRLParser.ConstraintsContext c

@Override
public ExprConstraintDescr visitConstraint(DRLParser.ConstraintContext ctx) {
Object constraint = super.visitConstraint(ctx);
if (constraint != null) {
String constraintString = constraint.toString();
DRLParser.LabelContext label = ctx.label();
if (label != null) {
constraintString = label.getText() + constraintString;
}
ExprConstraintDescr constraintDescr = new ExprConstraintDescr(constraintString);
String constraint = visitConstraintChildren(ctx);
if (!constraint.isEmpty()) {
ExprConstraintDescr constraintDescr = new ExprConstraintDescr(constraint);
constraintDescr.setType(ExprConstraintDescr.Type.NAMED);
return constraintDescr;
}
return null;
}

@Override
public String visitDrlExpression(DRLParser.DrlExpressionContext ctx) {
return ctx.children.stream()
.map(c -> c instanceof TerminalNode ? c : c.accept(this))
.filter(Objects::nonNull)
.map(Object::toString)
.collect(Collectors.joining(" "));
}

@Override
public String visitDrlPrimary(DRLParser.DrlPrimaryContext ctx) {
return ctx.children.stream()
.map(c -> c instanceof TerminalNode ? c : c.accept(this))
.filter(Objects::nonNull)
.map(Object::toString)
.collect(Collectors.joining(" "));
}

@Override
public String visitDrlIdentifier(DRLParser.DrlIdentifierContext ctx) {
return ctx.getText();
}

@Override
public String visitDrlLiteral(DRLParser.DrlLiteralContext ctx) {
ParseTree node = ctx;
while (true) {
if (node instanceof TerminalNode) {
return node.toString();
}
if (node.getChildCount() != 1) {
return super.visitDrlLiteral(ctx).toString();
}
node = node.getChild(0);
}
}

@Override
public ExistsDescr visitLhsExists(DRLParser.LhsExistsContext ctx) {
ExistsDescr existsDescr = new ExistsDescr();
Expand Down Expand Up @@ -372,6 +338,34 @@ private List<BaseDescr> visitDescrChildren(RuleNode node) {
return aggregator;
}

// leaves of constraint concatenate return Strings
private String visitConstraintChildren(RuleNode node) {
return ((ParserRuleContext) node).children.stream()
.map(c -> c instanceof TerminalNode ? c : c.accept(this))
.filter(Objects::nonNull)
.map(Object::toString)
.collect(Collectors.joining(" "));
}

@Override
public Object visitChildren(RuleNode node) {
if (hasConstraintAsAncestor(node)) {
return visitConstraintChildren(node);
}
return super.visitChildren(node);
}

private boolean hasConstraintAsAncestor(RuleNode node) {
ParseTree parent = node.getParent();
if (parent instanceof DRLParser.ConstraintContext) {
return true;
} else if (parent == null) {
return false;
} else {
return hasConstraintAsAncestor((RuleNode) parent);
}
}

private Optional<BaseDescr> visitFirstDescrChild(RuleNode node) {
int n = node.getChildCount();

Expand Down
Loading

0 comments on commit d637f88

Please sign in to comment.