From 8104f7fed55f4ca63afef6270cf4c29ae205a717 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Locker?= Date: Wed, 15 May 2024 00:25:47 +0200 Subject: [PATCH] Set namespace of qualified declared types --- .../drl/parser/antlr4/MiscDRLParserTest.java | 14 ++++++++++ .../antlr4/qualified_type_declaration.drl | 27 +++++++++++++++++++ .../drl/parser/antlr4/DRLVisitorImpl.java | 10 +++++-- 3 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 drools-drl/drools-drl-parser-tests/src/test/resources/org/drools/drl/parser/antlr4/qualified_type_declaration.drl diff --git a/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/MiscDRLParserTest.java b/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/MiscDRLParserTest.java index 8b220177dfd..8075c89aed6 100644 --- a/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/MiscDRLParserTest.java +++ b/drools-drl/drools-drl-parser-tests/src/test/java/org/drools/drl/parser/antlr4/MiscDRLParserTest.java @@ -3071,6 +3071,20 @@ public void parse_TypeDeclarationWithFields() throws Exception { } + @Test + public void parse_QualifiedTypeDeclaration() throws Exception { + final PackageDescr pkg = parseAndGetPackageDescrFromFile( + "qualified_type_declaration.drl" ); + + TypeDeclarationDescr someFact = pkg.getTypeDeclarations().get( 0 ); + assertThat(someFact.getTypeName()).isEqualTo("SomeFact"); + assertThat(someFact.getNamespace()).isEqualTo("com.sample1"); + + EnumDeclarationDescr color = pkg.getEnumDeclarations().get( 0 ); + assertThat(color.getTypeName()).isEqualTo("Color"); + assertThat(color.getNamespace()).isEqualTo("com.sample2"); + } + @Test public void parenthesesOneLevelNestWithThreeSiblings() throws Exception { final PackageDescr pkg = parseAndGetPackageDescrFromFile( "Rule_with_nested_LHS.drl" ); diff --git a/drools-drl/drools-drl-parser-tests/src/test/resources/org/drools/drl/parser/antlr4/qualified_type_declaration.drl b/drools-drl/drools-drl-parser-tests/src/test/resources/org/drools/drl/parser/antlr4/qualified_type_declaration.drl new file mode 100644 index 00000000000..0eaa7067117 --- /dev/null +++ b/drools-drl/drools-drl-parser-tests/src/test/resources/org/drools/drl/parser/antlr4/qualified_type_declaration.drl @@ -0,0 +1,27 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations + * under the License. + */ + +package org.drools.compiler.test; + +declare com.sample1.SomeFact + name : String + age: Integer +end + +declare enum com.sample2.Color + WHITE; +end 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 4b0036e669b..49a527612ab 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 @@ -235,9 +235,12 @@ public BaseDescr visitDeclaredef(DRLParser.DeclaredefContext ctx) { @Override public TypeDeclarationDescr visitTypeDeclaration(DRLParser.TypeDeclarationContext ctx) { - TypeDeclarationDescr typeDeclarationDescr = BaseDescrFactory.builder(new TypeDeclarationDescr(ctx.name.getText())) + TypeDeclarationDescr typeDeclarationDescr = BaseDescrFactory.builder(new TypeDeclarationDescr()) .withParserRuleContext(ctx) .build(); + + typeDeclarationDescr.setTypeName(ctx.name.getText()); + if (ctx.DRL_TRAIT() != null) { typeDeclarationDescr.setTrait(true); } @@ -257,9 +260,12 @@ public TypeDeclarationDescr visitTypeDeclaration(DRLParser.TypeDeclarationContex @Override public EnumDeclarationDescr visitEnumDeclaration(DRLParser.EnumDeclarationContext ctx) { - EnumDeclarationDescr enumDeclarationDescr = BaseDescrFactory.builder(new EnumDeclarationDescr(ctx.name.getText())) + EnumDeclarationDescr enumDeclarationDescr = BaseDescrFactory.builder(new EnumDeclarationDescr()) .withParserRuleContext(ctx) .build(); + + enumDeclarationDescr.setTypeName(ctx.name.getText()); + ctx.drlAnnotation().stream() .map(this::visitDrlAnnotation) .forEach(enumDeclarationDescr::addAnnotation);