Skip to content

Commit

Permalink
performance improvement. Use getModelForKBase instead of getKiePackages
Browse files Browse the repository at this point in the history
  • Loading branch information
tkobayas committed Jun 20, 2024
1 parent ac095cc commit 7003a80
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -455,7 +455,7 @@ private Collection<String> getRuleClassNames() {
return ruleClassesNames;
}

private Collection<Model> getModelForKBase(KieBaseModelImpl kBaseModel) {
public Collection<Model> getModelForKBase(KieBaseModelImpl kBaseModel) {
Map<String, Model> modelsMap = getModels();
if (kBaseModel.getPackages().isEmpty()) {
return modelsMap.values();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@

package org.drools.modelcompiler.builder;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

Expand All @@ -31,7 +31,6 @@
import org.drools.compiler.builder.impl.TypeDeclarationFactory;
import org.drools.compiler.compiler.PackageRegistry;
import org.drools.compiler.compiler.ParserError;
import org.drools.compiler.kie.builder.impl.AbstractKieModule;
import org.drools.compiler.kie.builder.impl.BuildContext;
import org.drools.compiler.kie.builder.impl.InternalKieModule;
import org.drools.compiler.kproject.models.KieBaseModelImpl;
Expand All @@ -48,21 +47,14 @@
import org.drools.core.rule.ImportDeclaration;
import org.drools.core.rule.TypeDeclaration;
import org.drools.core.util.StringUtils;
import org.drools.model.Model;
import org.drools.modelcompiler.CanonicalKieModule;
import org.drools.modelcompiler.CanonicalKiePackages;
import org.drools.modelcompiler.builder.errors.UnsupportedFeatureError;
import org.drools.modelcompiler.builder.generator.DRLIdGenerator;
import org.drools.modelcompiler.builder.generator.DrlxParseUtil;
import org.drools.modelcompiler.builder.generator.declaredtype.POJOGenerator;
import org.kie.api.KieBase;
import org.kie.api.KieServices;
import org.kie.api.builder.KieModule;
import org.kie.api.builder.KieRepository;
import org.kie.api.builder.ReleaseId;
import org.kie.api.builder.model.KieBaseModel;
import org.kie.api.definition.KiePackage;
import org.kie.api.runtime.KieContainer;
import org.kie.internal.builder.KnowledgeBuilder;
import org.kie.internal.builder.ResultSeverity;

import static com.github.javaparser.StaticJavaParser.parseImport;
Expand All @@ -84,7 +76,7 @@ public class ModelBuilderImpl<T extends PackageSources> extends KnowledgeBuilder

private Map<String, CompositePackageDescr> compositePackagesMap;

private Map<String, List<String>> includedRuleNameMap = new HashMap<>();
private Map<String, Set<String>> includedRuleNameMap = new HashMap<>();

public ModelBuilderImpl(Function<PackageModel, T> sourcesGenerator, KnowledgeBuilderConfigurationImpl configuration, ReleaseId releaseId, boolean oneClassPerRule) {
super(configuration);
Expand Down Expand Up @@ -259,12 +251,9 @@ private void populateIncludedRuleNameMap() {
continue;
}
CanonicalKieModule canonicalKieModule = (CanonicalKieModule) includeModule;
CanonicalKiePackages canonicalKiePackages = canonicalKieModule.getKiePackages((KieBaseModelImpl) kieBaseModel);
Collection<InternalKnowledgePackage> kiePackages = canonicalKiePackages.getKiePackages();
for (InternalKnowledgePackage kiePackage : kiePackages) {
kiePackage.getRules().forEach(rule -> {
includedRuleNameMap.computeIfAbsent(kiePackage.getName(), k -> new ArrayList<>()).add(rule.getName());
});
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()));
}
}
}
Expand Down Expand Up @@ -349,15 +338,18 @@ protected void validateUniqueRuleNames(final PackageDescr packageDescr) {
super.validateUniqueRuleNames(packageDescr);

// check for duplicated rule names in included kbase
List<String> ruleNames = includedRuleNameMap.computeIfAbsent(packageDescr.getNamespace(), k -> new ArrayList<>());
for (final RuleDescr ruleDescr : packageDescr.getRules()) {
if (ruleNames.contains(ruleDescr.getName())) {
addBuilderResult(new ParserError(ruleDescr.getResource(),
"Duplicate rule name: " + ruleDescr.getName(),
ruleDescr.getLine(),
ruleDescr.getColumn(),
packageDescr.getNamespace()));
if (includedRuleNameMap.containsKey(packageDescr.getNamespace())) {
Set<String> ruleNames = includedRuleNameMap.get(packageDescr.getNamespace());
for (final RuleDescr ruleDescr : packageDescr.getRules()) {
if (ruleNames.contains(ruleDescr.getName())) {
addBuilderResult(new ParserError(ruleDescr.getResource(),
"Duplicate rule name: " + ruleDescr.getName(),
ruleDescr.getLine(),
ruleDescr.getColumn(),
packageDescr.getNamespace()));
}
}
}

}
}

0 comments on commit 7003a80

Please sign in to comment.