Skip to content

Commit

Permalink
- move collectRules to CountBasedOrderingStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
tkobayas committed Jul 7, 2020
1 parent 38e80e7 commit 94f1493
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@

package org.drools.core.addon;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.Map;

import org.drools.core.definitions.InternalKnowledgePackage;
import org.drools.core.spi.AlphaNodeFieldConstraint;
import org.drools.core.spi.ObjectType;
import org.kie.api.conf.AlphaNodeOrderingOption;
import org.kie.api.definition.rule.Rule;

/**
*
Expand All @@ -31,7 +32,7 @@
*/
public interface AlphaNodeOrderingStrategy {

void analyzeAlphaConstraints(Set<Rule> ruleSet);
void analyzeAlphaConstraints(Map<String, InternalKnowledgePackage> pkgs, Collection<InternalKnowledgePackage> newPkgs);

void reorderAlphaConstraints(List<AlphaNodeFieldConstraint> alphaConstraints, ObjectType objectType);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
package org.drools.core.addon;

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.Objects;
import java.util.Set;

import org.drools.core.definitions.InternalKnowledgePackage;
import org.drools.core.definitions.rule.impl.RuleImpl;
import org.drools.core.rule.Accumulate;
import org.drools.core.rule.Collect;
Expand All @@ -48,7 +51,9 @@ public class CountBasedOrderingStrategy implements AlphaNodeOrderingStrategy {
private Map<ObjectType, Map<AlphaNodeFieldConstraint, Integer>> analyzedAlphaConstraints = new HashMap<>();

@Override
public void analyzeAlphaConstraints(Set<Rule> ruleSet) {
public void analyzeAlphaConstraints(Map<String, InternalKnowledgePackage> pkgs, Collection<InternalKnowledgePackage> newPkgs) {

Set<Rule> ruleSet = collectRules(pkgs, newPkgs);

List<Pattern> patternList = new ArrayList<>();
ruleSet.stream()
Expand All @@ -68,6 +73,13 @@ public void analyzeAlphaConstraints(Set<Rule> ruleSet) {
logger.trace("analyzedAlphaConstraints : {}", analyzedAlphaConstraints);
}

private Set<Rule> collectRules(Map<String, InternalKnowledgePackage> pkgs, Collection<InternalKnowledgePackage> newPkgs) {
Set<Rule> ruleSet = new HashSet<>();
pkgs.forEach((pkgName, pkg) -> ruleSet.addAll(pkg.getRules()));
newPkgs.forEach(pkg -> ruleSet.addAll(pkg.getRules())); // okay to overwrite
return ruleSet;
}

private void collectPatterns(GroupElement ge, List<Pattern> patternList) {
List<RuleConditionElement> children = ge.getChildren();
for (RuleConditionElement child : children) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,18 @@

package org.drools.core.addon;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.Map;

import org.drools.core.definitions.InternalKnowledgePackage;
import org.drools.core.spi.AlphaNodeFieldConstraint;
import org.drools.core.spi.ObjectType;
import org.kie.api.definition.rule.Rule;

public class NoopOrderingStrategy implements AlphaNodeOrderingStrategy {

@Override
public void analyzeAlphaConstraints(Set<Rule> ruleSet) {
public void analyzeAlphaConstraints(Map<String, InternalKnowledgePackage> pkgs, Collection<InternalKnowledgePackage> newPkgs) {
// no op
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import org.drools.core.common.InternalWorkingMemory;
import org.drools.core.common.RuleBasePartitionId;
import org.drools.core.definitions.InternalKnowledgePackage;
import org.drools.core.definitions.impl.KnowledgePackageImpl;
import org.drools.core.definitions.rule.impl.RuleImpl;
import org.drools.core.event.KieBaseEventSupport;
import org.drools.core.factmodel.ClassDefinition;
Expand Down Expand Up @@ -850,7 +849,7 @@ private void internalAddPackages(Collection<InternalKnowledgePackage> clonedPkgs
wm.flushPropagations();
}

alphaNodeOrderingStrategy.analyzeAlphaConstraints(collectRules(pkgs, clonedPkgs));
alphaNodeOrderingStrategy.analyzeAlphaConstraints(pkgs, clonedPkgs);

// we need to merge all byte[] first, so that the root classloader can resolve classes
for (InternalKnowledgePackage newPkg : clonedPkgs) {
Expand Down Expand Up @@ -961,13 +960,6 @@ private void internalAddPackages(Collection<InternalKnowledgePackage> clonedPkgs
}
}

private Set<Rule> collectRules(Map<String, InternalKnowledgePackage> pkgs, Collection<InternalKnowledgePackage> newPkgs) {
Set<Rule> ruleSet = new HashSet<>();
pkgs.forEach((pkgName, pkg) -> ruleSet.addAll(pkg.getRules()));
newPkgs.forEach(pkg -> ruleSet.addAll(pkg.getRules())); // okay to overwrite
return ruleSet;
}

public void processAllTypesDeclaration( Collection<InternalKnowledgePackage> pkgs ) {
List<TypeDeclaration> allTypeDeclarations = new ArrayList<>();
// Add all Type Declarations, this has to be done first incase packages cross reference each other during build process.
Expand Down

0 comments on commit 94f1493

Please sign in to comment.