From 8658f7a5b655652dfd1ab1aec6d557700a1c6d80 Mon Sep 17 00:00:00 2001 From: Toshiya Kobayashi Date: Fri, 7 Jun 2024 15:23:45 +0900 Subject: [PATCH] [incubator-kie-drools-5933] [new-parser] PackageDescr.resource not set (#5984) --- .../antlr4/DescrCommonPropertyTest.java | 17 +++- .../java/org/drools/drl/parser/DrlParser.java | 2 +- .../drl/parser/antlr4/BaseDescrFactory.java | 6 ++ .../drl/parser/antlr4/DRLParserHelper.java | 7 +- .../drl/parser/antlr4/DRLParserWrapper.java | 9 ++- .../drl/parser/antlr4/DRLVisitorImpl.java | 77 ++++++++++++++++--- 6 files changed, 100 insertions(+), 18 deletions(-) diff --git a/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/DescrCommonPropertyTest.java b/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/DescrCommonPropertyTest.java index 25c51ca9b7f..0582c9faf5e 100644 --- a/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/DescrCommonPropertyTest.java +++ b/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/DescrCommonPropertyTest.java @@ -18,6 +18,11 @@ */ package org.drools.drl.parser.antlr4; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + import org.drools.drl.ast.descr.AccumulateDescr; import org.drools.drl.ast.descr.AccumulateImportDescr; import org.drools.drl.ast.descr.AndDescr; @@ -49,8 +54,10 @@ import org.drools.drl.ast.descr.WindowReferenceDescr; import org.drools.drl.parser.DrlParser; import org.drools.drl.parser.DroolsParserException; +import org.drools.io.InputStreamResource; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.kie.api.io.Resource; import static org.assertj.core.api.Assertions.assertThat; @@ -61,17 +68,20 @@ class DescrCommonPropertyTest { private DrlParser parser; + private Resource resource; + @BeforeEach public void setUp() { parser = ParserTestUtils.getParser(); } private PackageDescr parseAndGetPackageDescr(String drl) { - try { - PackageDescr pkg = parser.parse(null, drl); + try (InputStream inputStream = new ByteArrayInputStream(drl.getBytes(StandardCharsets.UTF_8))) { + resource = new InputStreamResource(inputStream); + PackageDescr pkg = parser.parse(resource); assertThat(parser.hasErrors()).as(parser.getErrors().toString()).isFalse(); return pkg; - } catch (DroolsParserException e) { + } catch (DroolsParserException | IOException e) { throw new RuntimeException(e); } } @@ -83,6 +93,7 @@ private void assertProperties(BaseDescr descr, int startCharacter, int endCharac assertThat(descr.getColumn()).isEqualTo(column); // first column of the start token. column is 0-based assertThat(descr.getEndLine()).isEqualTo(endLine); // line of the end token. line is 1-based assertThat(descr.getEndColumn()).isEqualTo(endColumn); // last column of the end token. column is 0-based + assertThat(descr.getResource()).isEqualTo(resource); } @Test diff --git a/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/DrlParser.java b/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/DrlParser.java index 26ee6dffb7b..398e5f49923 100644 --- a/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/DrlParser.java +++ b/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/DrlParser.java @@ -200,7 +200,7 @@ public PackageDescr parse(final boolean isEditor, private PackageDescr compileWithAntlr4Parser(Function packageDescrFunction) throws DroolsParserException { try { // we don't use languageLevel here, assuming DRL6 compatible - DRLParserWrapper parser = new DRLParserWrapper(); + DRLParserWrapper parser = new DRLParserWrapper(resource); PackageDescr packageDescr = packageDescrFunction.apply(parser); for (final DRLParserError drlParserError : parser.getErrors()) { final ParserError err = new ParserError(resource, diff --git a/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/BaseDescrFactory.java b/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/BaseDescrFactory.java index 31fe45747e3..f48db5926b8 100644 --- a/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/BaseDescrFactory.java +++ b/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/BaseDescrFactory.java @@ -20,6 +20,7 @@ import org.antlr.v4.runtime.ParserRuleContext; import org.drools.drl.ast.descr.BaseDescr; +import org.kie.api.io.Resource; import static org.drools.drl.parser.antlr4.DescrHelper.populateCommonProperties; @@ -53,6 +54,11 @@ public Builder withParserRuleContext(ParserRuleContext ctx) { return this; } + public Builder withResource(Resource resource) { + toReturn.setResource(resource); + return this; + } + public T build() { return toReturn; } diff --git a/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLParserHelper.java b/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLParserHelper.java index 7d945f6bb8b..131145b2d75 100644 --- a/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLParserHelper.java +++ b/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLParserHelper.java @@ -33,6 +33,7 @@ import org.antlr.v4.runtime.tree.ParseTree; import org.antlr.v4.runtime.tree.TerminalNode; import org.drools.drl.ast.descr.PackageDescr; +import org.kie.api.io.Resource; import org.kie.internal.builder.conf.LanguageLevelOption; /** @@ -49,7 +50,7 @@ private DRLParserHelper() { */ public static PackageDescr parse(String drl) { DRLParser drlParser = createDrlParser(drl); - return compilationUnitContext2PackageDescr(drlParser.compilationUnit(), drlParser.getTokenStream()); + return compilationUnitContext2PackageDescr(drlParser.compilationUnit(), drlParser.getTokenStream(), null); } public static DRLParser createDrlParser(String drl) { @@ -89,8 +90,8 @@ private static DRLParser createDrlParser(CharStream charStream) { /** * DRLVisitorImpl visits a parse tree and creates a PackageDescr */ - public static PackageDescr compilationUnitContext2PackageDescr(DRLParser.CompilationUnitContext ctx, TokenStream tokenStream) { - DRLVisitorImpl visitor = new DRLVisitorImpl(tokenStream); + public static PackageDescr compilationUnitContext2PackageDescr(DRLParser.CompilationUnitContext ctx, TokenStream tokenStream, Resource resource) { + DRLVisitorImpl visitor = new DRLVisitorImpl(tokenStream, resource); Object descr = visitor.visit(ctx); if (descr instanceof PackageDescr) { return (PackageDescr) descr; diff --git a/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLParserWrapper.java b/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLParserWrapper.java index e2f8ee99604..e89ad669197 100644 --- a/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLParserWrapper.java +++ b/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLParserWrapper.java @@ -25,6 +25,7 @@ import java.util.stream.Collectors; import org.drools.drl.ast.descr.PackageDescr; +import org.kie.api.io.Resource; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,6 +40,12 @@ public class DRLParserWrapper { private final List errors = new ArrayList<>(); + private final Resource resource; + + public DRLParserWrapper(Resource resource) { + this.resource = resource; + } + /** * Main entry point for parsing DRL */ @@ -72,7 +79,7 @@ private PackageDescr parse(DRLParser drlParser) { errors.addAll(errorListener.getErrors()); try { - return compilationUnitContext2PackageDescr(cxt, drlParser.getTokenStream()); + return compilationUnitContext2PackageDescr(cxt, drlParser.getTokenStream(), resource); } catch (Exception e) { LOGGER.error("Exception while creating PackageDescr", e); errors.add(new DRLParserError(e)); diff --git a/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLVisitorImpl.java b/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLVisitorImpl.java index cefce9921c1..47d1b40bf9b 100644 --- a/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLVisitorImpl.java +++ b/drools-drl/drools-drl-parser/src/main/java/org/drools/drl/parser/antlr4/DRLVisitorImpl.java @@ -65,6 +65,7 @@ import org.drools.drl.ast.descr.UnitDescr; import org.drools.drl.ast.descr.WindowDeclarationDescr; import org.drools.drl.ast.descr.WindowReferenceDescr; +import org.kie.api.io.Resource; import static org.drools.drl.parser.antlr4.Antlr4ParserStringUtils.extractNamedConsequenceName; import static org.drools.drl.parser.antlr4.Antlr4ParserStringUtils.getTextPreservingWhitespace; @@ -83,9 +84,11 @@ public class DRLVisitorImpl extends DRLParserBaseVisitor { private final TokenStream tokenStream; + private final Resource resource; - public DRLVisitorImpl(TokenStream tokenStream) { + public DRLVisitorImpl(TokenStream tokenStream, Resource resource) { this.tokenStream = tokenStream; + this.resource = resource; } /** @@ -95,6 +98,7 @@ public DRLVisitorImpl(TokenStream tokenStream) { public PackageDescr visitCompilationUnit(DRLParser.CompilationUnitContext ctx) { PackageDescr packageDescr = BaseDescrFactory.builder(new PackageDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); if (ctx.packagedef() != null) { packageDescr.setName(getTextWithoutErrorNode(ctx.packagedef().name)); @@ -158,6 +162,7 @@ private void applyChildrenDescrs(PackageDescr packageDescr, List desc public UnitDescr visitUnitdef(DRLParser.UnitdefContext ctx) { return BaseDescrFactory.builder(new UnitDescr(ctx.name.getText())) .withParserRuleContext(ctx) + .withResource(resource) .build(); } @@ -170,6 +175,7 @@ public BaseDescr visitDrlStatementdef(DRLParser.DrlStatementdefContext ctx) { public GlobalDescr visitGlobaldef(DRLParser.GlobaldefContext ctx) { return BaseDescrFactory.builder(new GlobalDescr(ctx.drlIdentifier().getText(), ctx.type().getText())) .withParserRuleContext(ctx) + .withResource(resource) .build(); } @@ -179,12 +185,14 @@ public ImportDescr visitImportStandardDef(DRLParser.ImportStandardDefContext ctx if (ctx.DRL_FUNCTION() != null || ctx.STATIC() != null) { FunctionImportDescr functionImportDescr = BaseDescrFactory.builder(new FunctionImportDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); functionImportDescr.setTarget(target); return functionImportDescr; } else { ImportDescr importDescr = BaseDescrFactory.builder(new ImportDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); importDescr.setTarget(target); return importDescr; @@ -195,6 +203,7 @@ public ImportDescr visitImportStandardDef(DRLParser.ImportStandardDefContext ctx public AccumulateImportDescr visitImportAccumulateDef(DRLParser.ImportAccumulateDefContext ctx) { AccumulateImportDescr accumulateImportDescr = BaseDescrFactory.builder(new AccumulateImportDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); accumulateImportDescr.setTarget(ctx.drlQualifiedName().getText()); accumulateImportDescr.setFunctionName(ctx.drlIdentifier().getText()); @@ -205,6 +214,7 @@ public AccumulateImportDescr visitImportAccumulateDef(DRLParser.ImportAccumulate public FunctionDescr visitFunctiondef(DRLParser.FunctiondefContext ctx) { FunctionDescr functionDescr = BaseDescrFactory.builder(new FunctionDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); if (ctx.typeTypeOrVoid() != null) { functionDescr.setReturnType(getTextPreservingWhitespace(ctx.typeTypeOrVoid())); @@ -237,6 +247,7 @@ public BaseDescr visitDeclaredef(DRLParser.DeclaredefContext ctx) { public TypeDeclarationDescr visitTypeDeclaration(DRLParser.TypeDeclarationContext ctx) { TypeDeclarationDescr typeDeclarationDescr = BaseDescrFactory.builder(new TypeDeclarationDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); typeDeclarationDescr.setTypeName(ctx.name.getText()); @@ -262,6 +273,7 @@ public TypeDeclarationDescr visitTypeDeclaration(DRLParser.TypeDeclarationContex public EnumDeclarationDescr visitEnumDeclaration(DRLParser.EnumDeclarationContext ctx) { EnumDeclarationDescr enumDeclarationDescr = BaseDescrFactory.builder(new EnumDeclarationDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); enumDeclarationDescr.setTypeName(ctx.name.getText()); @@ -286,6 +298,7 @@ public EnumDeclarationDescr visitEnumDeclaration(DRLParser.EnumDeclarationContex public EnumLiteralDescr visitEnumerative(DRLParser.EnumerativeContext ctx) { EnumLiteralDescr enumLiteralDescr = BaseDescrFactory.builder(new EnumLiteralDescr(ctx.drlIdentifier().getText())) .withParserRuleContext(ctx) + .withResource(resource) .build(); ctx.expression().stream() .map(Antlr4ParserStringUtils::getTextPreservingWhitespace) @@ -297,6 +310,7 @@ public EnumLiteralDescr visitEnumerative(DRLParser.EnumerativeContext ctx) { public EntryPointDeclarationDescr visitEntryPointDeclaration(DRLParser.EntryPointDeclarationContext ctx) { EntryPointDeclarationDescr entryPointDeclarationDescr = BaseDescrFactory.builder(new EntryPointDeclarationDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); entryPointDeclarationDescr.setEntryPointId(safeStripStringDelimiters(ctx.name.getText())); ctx.drlAnnotation().stream() @@ -309,6 +323,7 @@ public EntryPointDeclarationDescr visitEntryPointDeclaration(DRLParser.EntryPoin public WindowDeclarationDescr visitWindowDeclaration(DRLParser.WindowDeclarationContext ctx) { WindowDeclarationDescr windowDeclarationDescr = BaseDescrFactory.builder(new WindowDeclarationDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); windowDeclarationDescr.setName(ctx.name.getText()); ctx.drlAnnotation().stream() @@ -325,6 +340,7 @@ public WindowDeclarationDescr visitWindowDeclaration(DRLParser.WindowDeclaration public RuleDescr visitRuledef(DRLParser.RuledefContext ctx) { RuleDescr ruleDescr = BaseDescrFactory.builder(new RuleDescr(safeStripStringDelimiters(ctx.name.getText()))) .withParserRuleContext(ctx) + .withResource(resource) .build(); if (ctx.EXTENDS() != null) { @@ -359,7 +375,9 @@ public RuleDescr visitRuledef(DRLParser.RuledefContext ctx) { lhsDescrList.forEach(rootDescr::addOrMerge); DescrHelper.populateCommonProperties(rootDescr, ctx.lhs().lhsExpression()); } else { - ruleDescr.setLhs(new AndDescr()); + ruleDescr.setLhs(BaseDescrFactory.builder(new AndDescr()) + .withResource(resource) + .build()); } if (ctx.rhs() != null) { @@ -383,6 +401,7 @@ public RuleDescr visitRuledef(DRLParser.RuledefContext ctx) { public QueryDescr visitQuerydef(DRLParser.QuerydefContext ctx) { QueryDescr queryDescr = BaseDescrFactory.builder(new QueryDescr(safeStripStringDelimiters(ctx.name.getText()))) .withParserRuleContext(ctx) + .withResource(resource) .build(); DRLParser.ParametersContext parametersContext = ctx.parameters(); @@ -414,12 +433,14 @@ public AnnotationDescr visitDrlAnnotation(DRLParser.DrlAnnotationContext ctx) { } return BaseDescrFactory.builder(ctx.anno.result) .withParserRuleContext(ctx) + .withResource(resource) .build(); } // A chunk that is neither a single value nor a list of key-value pairs. For example `!*, age` in `@watch(!*, age)`. AnnotationDescr annotationDescr = BaseDescrFactory.builder(new AnnotationDescr(ctx.name.getText())) .withParserRuleContext(ctx) + .withResource(resource) .build(); if (ctx.chunk() != null) { annotationDescr.setValue(getTextPreservingWhitespace(ctx.chunk())); @@ -431,6 +452,7 @@ public AnnotationDescr visitDrlAnnotation(DRLParser.DrlAnnotationContext ctx) { public TypeFieldDescr visitField(DRLParser.FieldContext ctx) { TypeFieldDescr typeFieldDescr = BaseDescrFactory.builder(new TypeFieldDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); typeFieldDescr.setFieldName(ctx.label().drlIdentifier().getText()); typeFieldDescr.setPattern(new PatternDescr(ctx.type().getText())); @@ -447,6 +469,7 @@ public TypeFieldDescr visitField(DRLParser.FieldContext ctx) { public AttributeDescr visitExpressionAttribute(DRLParser.ExpressionAttributeContext ctx) { AttributeDescr attributeDescr = BaseDescrFactory.builder(new AttributeDescr(ctx.name.getText())) .withParserRuleContext(ctx) + .withResource(resource) .build(); attributeDescr.setValue(getTextPreservingWhitespace(ctx.conditionalAttributeValue())); attributeDescr.setType(AttributeDescr.Type.EXPRESSION); @@ -457,6 +480,7 @@ public AttributeDescr visitExpressionAttribute(DRLParser.ExpressionAttributeCont public AttributeDescr visitBooleanAttribute(DRLParser.BooleanAttributeContext ctx) { AttributeDescr attributeDescr = BaseDescrFactory.builder(new AttributeDescr(ctx.name.getText())) .withParserRuleContext(ctx) + .withResource(resource) .build(); attributeDescr.setValue(ctx.BOOL_LITERAL() != null ? ctx.BOOL_LITERAL().getText() : "true"); attributeDescr.setType(AttributeDescr.Type.BOOLEAN); @@ -467,6 +491,7 @@ public AttributeDescr visitBooleanAttribute(DRLParser.BooleanAttributeContext ct public AttributeDescr visitStringAttribute(DRLParser.StringAttributeContext ctx) { AttributeDescr attributeDescr = BaseDescrFactory.builder(new AttributeDescr(ctx.name.getText())) .withParserRuleContext(ctx) + .withResource(resource) .build(); attributeDescr.setValue(unescapeJava(safeStripStringDelimiters(ctx.DRL_STRING_LITERAL().getText()))); attributeDescr.setType(AttributeDescr.Type.STRING); @@ -477,6 +502,7 @@ public AttributeDescr visitStringAttribute(DRLParser.StringAttributeContext ctx) public AttributeDescr visitStringListAttribute(DRLParser.StringListAttributeContext ctx) { AttributeDescr attributeDescr = BaseDescrFactory.builder(new AttributeDescr(ctx.name.getText())) .withParserRuleContext(ctx) + .withResource(resource) .build(); List valueList = ctx.DRL_STRING_LITERAL().stream() .map(ParseTree::getText) @@ -503,6 +529,7 @@ private static String createStringList(List valueList) { public AttributeDescr visitIntOrChunkAttribute(DRLParser.IntOrChunkAttributeContext ctx) { AttributeDescr attributeDescr = BaseDescrFactory.builder(new AttributeDescr(ctx.name.getText())) .withParserRuleContext(ctx) + .withResource(resource) .build(); if (ctx.DECIMAL_LITERAL() != null) { attributeDescr.setValue(ctx.DECIMAL_LITERAL().getText()); @@ -557,6 +584,7 @@ private PatternDescr getSinglePatternDescr(DRLParser.LhsPatternBindContext ctx) private OrDescr getOrDescrWithMultiplePatternDescr(DRLParser.LhsPatternBindContext ctx) { OrDescr orDescr = BaseDescrFactory.builder(new OrDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); List descrList = visitDescrChildren(ctx); descrList.stream() @@ -581,11 +609,13 @@ public PatternDescr visitLhsPattern(DRLParser.LhsPatternContext ctx) { String constraint = visitConstraintChildren(ctx); ExprConstraintDescr constraintDescr = BaseDescrFactory.builder(new ExprConstraintDescr(constraint)) .withParserRuleContext(ctx) + .withResource(resource) .build(); constraintDescr.setType(ExprConstraintDescr.Type.NAMED); constraintDescr.setPosition(0); PatternDescr patternDescr = BaseDescrFactory.builder(new PatternDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); patternDescr.addConstraint(constraintDescr); return patternDescr; @@ -593,6 +623,7 @@ public PatternDescr visitLhsPattern(DRLParser.LhsPatternContext ctx) { PatternDescr patternDescr = BaseDescrFactory.builder(new PatternDescr(ctx.objectType.getText())) .withParserRuleContext(ctx) + .withResource(resource) .build(); if (ctx.QUESTION() != null) { patternDescr.setQuery(true); @@ -621,6 +652,7 @@ private void addToPatternDescr(PatternDescr patternDescr, ExprConstraintDescr ex public NamedConsequenceDescr visitNamedConsequenceInvocation(DRLParser.NamedConsequenceInvocationContext ctx) { NamedConsequenceDescr namedConsequenceDescr = BaseDescrFactory.builder(new NamedConsequenceDescr(ctx.drlIdentifier().getText())) .withParserRuleContext(ctx) + .withResource(resource) .build(); return namedConsequenceDescr; } @@ -629,6 +661,7 @@ public NamedConsequenceDescr visitNamedConsequenceInvocation(DRLParser.NamedCons public NamedConsequenceDescr visitBreakingNamedConsequenceInvocation(DRLParser.BreakingNamedConsequenceInvocationContext ctx) { NamedConsequenceDescr namedConsequenceDescr = BaseDescrFactory.builder(new NamedConsequenceDescr(ctx.drlIdentifier().getText())) .withParserRuleContext(ctx) + .withResource(resource) .build(); namedConsequenceDescr.setBreaking(true); return namedConsequenceDescr; @@ -645,9 +678,11 @@ public NamedConsequenceDescr visitBreakingNamedConsequenceInvocation(DRLParser.B public ConditionalBranchDescr visitConditionalBranch(DRLParser.ConditionalBranchContext ctx) { ConditionalBranchDescr conditionalBranchDescr = BaseDescrFactory.builder(new ConditionalBranchDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); EvalDescr evalDescr = BaseDescrFactory.builder(new EvalDescr()) .withParserRuleContext(ctx.conditionalOrExpression()) + .withResource(resource) .build(); evalDescr.setContent(getTextPreservingWhitespace(ctx.conditionalOrExpression())); conditionalBranchDescr.setCondition(evalDescr); @@ -665,6 +700,7 @@ public ConditionalBranchDescr visitConditionalBranch(DRLParser.ConditionalBranch if (ctx.do2 != null) { ConditionalBranchDescr elseBranchDescr = BaseDescrFactory.builder(new ConditionalBranchDescr()) .withParserRuleContext(ctx.do2) + .withResource(resource) .build(); conditionalBranchDescr.setElseBranch(elseBranchDescr); NamedConsequenceDescr namedConsequenceDescr = visitNamedConsequenceInvocation(ctx.do2); @@ -672,6 +708,7 @@ public ConditionalBranchDescr visitConditionalBranch(DRLParser.ConditionalBranch } else if (ctx.break2 != null) { ConditionalBranchDescr elseBranchDescr = BaseDescrFactory.builder(new ConditionalBranchDescr()) .withParserRuleContext(ctx.break2) + .withResource(resource) .build(); conditionalBranchDescr.setElseBranch(elseBranchDescr); NamedConsequenceDescr namedConsequenceDescr = visitBreakingNamedConsequenceInvocation(ctx.break2); @@ -689,6 +726,7 @@ public ConditionalBranchDescr visitConditionalBranch(DRLParser.ConditionalBranch public ForallDescr visitLhsForall(DRLParser.LhsForallContext ctx) { ForallDescr forallDescr = BaseDescrFactory.builder(new ForallDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); visitDescrChildren(ctx).forEach(forallDescr::addDescr); return forallDescr; @@ -698,6 +736,7 @@ public ForallDescr visitLhsForall(DRLParser.LhsForallContext ctx) { public PatternDescr visitLhsAccumulate(DRLParser.LhsAccumulateContext ctx) { AccumulateDescr accumulateDescr = BaseDescrFactory.builder(new AccumulateDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); // accumulateDescr.input is always AndDescr accumulateDescr.setInput(wrapWithAndDescr(visitLhsAndDef(ctx.lhsAndDef()), ctx.lhsAndDef())); @@ -707,7 +746,9 @@ public PatternDescr visitLhsAccumulate(DRLParser.LhsAccumulateContext ctx) { accumulateDescr.addFunction(visitAccumulateFunction(accumulateFunctionContext)); } - PatternDescr patternDescr = new PatternDescr("Object"); + PatternDescr patternDescr = BaseDescrFactory.builder(new PatternDescr("Object")) + .withResource(resource) + .build(); patternDescr.setSource(accumulateDescr); List constraintDescrList = visitConstraints(ctx.constraints()); constraintDescrList.forEach(patternDescr::addConstraint); @@ -731,6 +772,7 @@ private AndDescr wrapWithAndDescr(BaseDescr baseDescr, ParserRuleContext ctx) { public Object visitLhsGroupBy(DRLParser.LhsGroupByContext ctx) { GroupByDescr groupByDescr = BaseDescrFactory.builder(new GroupByDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); groupByDescr.setInput(visitLhsAndDef(ctx.lhsAndDef())); @@ -744,7 +786,9 @@ public Object visitLhsGroupBy(DRLParser.LhsGroupByContext ctx) { groupByDescr.addFunction(visitAccumulateFunction(accumulateFunctionContext)); } - PatternDescr patternDescr = new PatternDescr("Object"); + PatternDescr patternDescr = BaseDescrFactory.builder(new PatternDescr("Object")) + .withResource(resource) + .build(); patternDescr.setSource(groupByDescr); List constraintDescrList = visitConstraints(ctx.constraints()); constraintDescrList.forEach(patternDescr::addConstraint); @@ -756,6 +800,7 @@ public Object visitLhsGroupBy(DRLParser.LhsGroupByContext ctx) { public BehaviorDescr visitPatternFilter(DRLParser.PatternFilterContext ctx) { BehaviorDescr behaviorDescr = BaseDescrFactory.builder(new BehaviorDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); behaviorDescr.setType(ctx.DRL_WINDOW().getText()); behaviorDescr.setSubType(ctx.drlIdentifier().getText()); @@ -769,6 +814,7 @@ public BehaviorDescr visitPatternFilter(DRLParser.PatternFilterContext ctx) { public FromDescr visitFromExpression(DRLParser.FromExpressionContext ctx) { FromDescr fromDescr = BaseDescrFactory.builder(new FromDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); fromDescr.setDataSource(new MVELExprDescr(getTextPreservingWhitespace(ctx))); return fromDescr; @@ -778,6 +824,7 @@ public FromDescr visitFromExpression(DRLParser.FromExpressionContext ctx) { public CollectDescr visitFromCollect(DRLParser.FromCollectContext ctx) { CollectDescr collectDescr = BaseDescrFactory.builder(new CollectDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); collectDescr.setInputPattern((PatternDescr) visitLhsPatternBind(ctx.lhsPatternBind())); return collectDescr; @@ -787,6 +834,7 @@ public CollectDescr visitFromCollect(DRLParser.FromCollectContext ctx) { public AccumulateDescr visitFromAccumulate(DRLParser.FromAccumulateContext ctx) { AccumulateDescr accumulateDescr = BaseDescrFactory.builder(new AccumulateDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); // accumulateDescr.input is always AndDescr accumulateDescr.setInput(wrapWithAndDescr(visitLhsAndDef(ctx.lhsAndDef()), ctx.lhsAndDef())); @@ -827,6 +875,7 @@ public AccumulateDescr.AccumulateFunctionCallDescr visitAccumulateFunction(DRLPa public EntryPointDescr visitFromEntryPoint(DRLParser.FromEntryPointContext ctx) { return BaseDescrFactory.builder(new EntryPointDescr(safeStripStringDelimiters(ctx.stringId().getText()))) .withParserRuleContext(ctx) + .withResource(resource) .build(); } @@ -834,6 +883,7 @@ public EntryPointDescr visitFromEntryPoint(DRLParser.FromEntryPointContext ctx) public WindowReferenceDescr visitFromWindow(DRLParser.FromWindowContext ctx) { return BaseDescrFactory.builder(new WindowReferenceDescr(ctx.drlIdentifier().getText())) .withParserRuleContext(ctx) + .withResource(resource) .build(); } @@ -887,8 +937,9 @@ public List visitConstraint(DRLParser.ConstraintContext ctx String constraint = visitConstraintChildren(ctx); if (!constraint.isEmpty()) { ExprConstraintDescr constraintDescr = BaseDescrFactory.builder(new ExprConstraintDescr(constraint)) - .withParserRuleContext(ctx) - .build(); + .withParserRuleContext(ctx) + .withResource(resource) + .build(); constraintDescr.setType(ExprConstraintDescr.Type.NAMED); descrList.add(constraintDescr); return descrList; @@ -922,6 +973,7 @@ public String visitDrlIdentifier(DRLParser.DrlIdentifierContext ctx) { public ExistsDescr visitLhsExists(DRLParser.LhsExistsContext ctx) { ExistsDescr existsDescr = BaseDescrFactory.builder(new ExistsDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); if (ctx.lhsExpression() != null) { // exists( A() or B() ) @@ -943,6 +995,7 @@ public ExistsDescr visitLhsExists(DRLParser.LhsExistsContext ctx) { public NotDescr visitLhsNot(DRLParser.LhsNotContext ctx) { NotDescr notDescr = BaseDescrFactory.builder(new NotDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); if (ctx.lhsExpression() != null) { // not ( A() or B() ) @@ -964,6 +1017,7 @@ public NotDescr visitLhsNot(DRLParser.LhsNotContext ctx) { public EvalDescr visitLhsEval(DRLParser.LhsEvalContext ctx) { return BaseDescrFactory.builder(new EvalDescr(getTextPreservingWhitespace(ctx.conditionalOrExpression()))) .withParserRuleContext(ctx) + .withResource(resource) .build(); } @@ -982,8 +1036,9 @@ public BaseDescr visitLhsOr(DRLParser.LhsOrContext ctx) { return descrList.get(0).getDescr(); } else { OrDescr orDescr = BaseDescrFactory.builder(new OrDescr()) - .withParserRuleContext(ctx) - .build(); + .withParserRuleContext(ctx) + .withResource(resource) + .build(); // For example, in case of A() or B() or C(), // Parser creates AST like this: // lhsOr @@ -1033,8 +1088,9 @@ private BaseDescr createAndDescr(ParserRuleContext ctx) { return descrList.get(0).getDescr(); } else { AndDescr andDescr = BaseDescrFactory.builder(new AndDescr()) - .withParserRuleContext(ctx) - .build(); + .withParserRuleContext(ctx) + .withResource(resource) + .build(); // For example, in case of A() and B() and C(), // Parser creates AST like this: // lhsAnd @@ -1083,6 +1139,7 @@ public BaseDescr visitLhsUnary(DRLParser.LhsUnaryContext ctx) { // lhsUnary may have multiple children e.g. consequenceInvocation, connect with AND AndDescr andDescr = BaseDescrFactory.builder(new AndDescr()) .withParserRuleContext(ctx) + .withResource(resource) .build(); children.forEach(andDescr::addDescr); return andDescr;