forked from apache/incubator-kie-drools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[incubator-kie-drools-6007] Executable model doesn't report an error …
…when duplicated (apache#6013) * removing kie-ci from dependency, because it causes a test failure in KieBaseIncludeTest * Use canonicalKieModule.getKiePackages() rather than getKieBase() * null check for kiePackage * move populateIncludedRuleNameMap out of packages loop * removed unused FileManager * performance improvement. Use getModelForKBase instead of getKiePackages * Fit into build phases * clean up
- Loading branch information
1 parent
1566e87
commit d887d5c
Showing
9 changed files
with
380 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
...degen/src/main/java/org/drools/model/codegen/execmodel/processors/ModelRuleValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* 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.model.codegen.execmodel.processors; | ||
|
||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.drools.compiler.builder.impl.processors.RuleValidator; | ||
import org.drools.compiler.compiler.PackageRegistry; | ||
import org.drools.drl.ast.descr.PackageDescr; | ||
import org.drools.drl.ast.descr.RuleDescr; | ||
import org.drools.drl.parser.ParserError; | ||
import org.kie.internal.builder.KnowledgeBuilderConfiguration; | ||
|
||
public class ModelRuleValidator extends RuleValidator { | ||
|
||
// extra rule names which need to be checked for duplicates. | ||
// Non-executable model doesn't need this because included kbase assets are added to the packageDescr | ||
// Executable model requires this because included kbase assets are modeled as separated kjar, so not added to the packageDescr | ||
private final Map<String, Set<String>> includedRuleNameMap; | ||
|
||
public ModelRuleValidator(PackageRegistry pkgRegistry, PackageDescr packageDescr, KnowledgeBuilderConfiguration configuration, Map<String, Set<String>> includedRuleNameMap) { | ||
super(pkgRegistry, packageDescr, configuration); | ||
this.includedRuleNameMap = includedRuleNameMap; | ||
} | ||
|
||
@Override | ||
public void process() { | ||
super.process(); | ||
|
||
// Check with included rule names, because exec-model doesn't add assets of included kbase to the packageDescr | ||
String packageName = packageDescr.getNamespace(); | ||
if (includedRuleNameMap.containsKey(packageName)) { | ||
Set<String> ruleNames = includedRuleNameMap.get(packageName); | ||
for (final RuleDescr rule : packageDescr.getRules()) { | ||
final String name = rule.getUnitQualifiedName(); | ||
if (ruleNames.contains(name)) { | ||
this.results.add(new ParserError(rule.getResource(), | ||
"Duplicate rule name: " + name, | ||
rule.getLine(), | ||
rule.getColumn(), | ||
packageName)); | ||
} | ||
} | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
.../java/org/drools/model/codegen/execmodel/processors/PopulateIncludedRuleNameMapPhase.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/** | ||
* 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.model.codegen.execmodel.processors; | ||
|
||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
import org.drools.compiler.builder.impl.processors.CompilationPhase; | ||
import org.drools.compiler.kie.builder.impl.InternalKieModule; | ||
import org.drools.compiler.kproject.models.KieBaseModelImpl; | ||
import org.drools.model.Model; | ||
import org.drools.modelcompiler.CanonicalKieModule; | ||
import org.kie.api.builder.model.KieBaseModel; | ||
import org.kie.internal.builder.KnowledgeBuilderResult; | ||
|
||
public class PopulateIncludedRuleNameMapPhase implements CompilationPhase { | ||
|
||
private final Map<KieBaseModel, InternalKieModule> includeModules; | ||
private final Map<String, Set<String>> includedRuleNameMap; | ||
|
||
public PopulateIncludedRuleNameMapPhase(Map<KieBaseModel, InternalKieModule> includeModules, Map<String, Set<String>> includedRuleNameMap) { | ||
this.includeModules = includeModules; | ||
this.includedRuleNameMap = includedRuleNameMap; | ||
} | ||
|
||
@Override | ||
public void process() { | ||
for (Map.Entry<KieBaseModel, InternalKieModule> entry : includeModules.entrySet()) { | ||
KieBaseModel kieBaseModel = entry.getKey(); | ||
InternalKieModule includeModule = entry.getValue(); | ||
if ((includeModule instanceof CanonicalKieModule canonicalKieModule) && canonicalKieModule.hasModelFile()) { | ||
Collection<Model> includeModels = canonicalKieModule.getModelForKBase((KieBaseModelImpl)kieBaseModel); | ||
for (Model includeModel : includeModels) { | ||
includeModel.getRules().forEach(rule -> includedRuleNameMap.computeIfAbsent(includeModel.getPackageName(), k -> new HashSet<>()).add(rule.getName())); | ||
} | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public Collection<? extends KnowledgeBuilderResult> getResults() { | ||
return Collections.emptyList(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.