use of org.drools.core.ruleunit.RuleUnitDescr in project drools by kiegroup.
the class ModelGenerator method processRule.
private static void processRule(KnowledgeBuilderImpl kbuilder, TypeResolver typeResolver, PackageModel packageModel, PackageDescr packageDescr, RuleDescr ruleDescr, boolean isPattern) {
RuleContext context = new RuleContext(kbuilder, packageModel, ruleDescr, typeResolver, isPattern);
for (Entry<String, Object> kv : ruleDescr.getNamedConsequences().entrySet()) {
context.addNamedConsequence(kv.getKey(), kv.getValue().toString());
}
new ModelGeneratorVisitor(context, packageModel).visit(getExtendedLhs(packageDescr, ruleDescr));
final String ruleMethodName = "rule_" + toId(ruleDescr.getName());
MethodDeclaration ruleMethod = new MethodDeclaration(EnumSet.of(Modifier.PUBLIC, Modifier.STATIC), RULE_TYPE, ruleMethodName);
ruleMethod.setJavadocComment(" Rule name: " + ruleDescr.getName() + " ");
VariableDeclarationExpr ruleVar = new VariableDeclarationExpr(RULE_TYPE, RULE_CALL);
MethodCallExpr ruleCall = new MethodCallExpr(null, RULE_CALL);
if (!ruleDescr.getNamespace().isEmpty()) {
ruleCall.addArgument(new StringLiteralExpr(ruleDescr.getNamespace()));
}
ruleCall.addArgument(new StringLiteralExpr(ruleDescr.getName()));
RuleUnitDescr ruleUnitDescr = context.getRuleUnitDescr();
MethodCallExpr buildCallScope = ruleUnitDescr != null ? new MethodCallExpr(ruleCall, UNIT_CALL).addArgument(new ClassExpr(classToReferenceType(ruleUnitDescr.getRuleUnitClass()))) : ruleCall;
for (MethodCallExpr attributeExpr : ruleAttributes(context, ruleDescr)) {
attributeExpr.setScope(buildCallScope);
buildCallScope = attributeExpr;
}
for (MethodCallExpr metaAttributeExpr : ruleMetaAttributes(context, ruleDescr)) {
metaAttributeExpr.setScope(buildCallScope);
buildCallScope = metaAttributeExpr;
}
MethodCallExpr buildCall = new MethodCallExpr(buildCallScope, BUILD_CALL, NodeList.nodeList(context.getExpressions()));
BlockStmt ruleVariablesBlock = new BlockStmt();
createUnitData(ruleUnitDescr, ruleVariablesBlock);
createVariables(kbuilder, ruleVariablesBlock, packageModel, context);
ruleMethod.setBody(ruleVariablesBlock);
MethodCallExpr executeCall = new Consequence(context).createCall(ruleDescr, ruleDescr.getConsequence().toString(), ruleVariablesBlock, false);
buildCall.addArgument(executeCall);
ruleVariablesBlock.addStatement(new AssignExpr(ruleVar, buildCall, AssignExpr.Operator.ASSIGN));
ruleVariablesBlock.addStatement(new ReturnStmt(RULE_CALL));
packageModel.putRuleMethod(ruleMethodName, ruleMethod);
}
use of org.drools.core.ruleunit.RuleUnitDescr in project drools by kiegroup.
the class SegmentExecution method applySegmentModel.
public void applySegmentModel(PMMLRequestData requestData, DataSource<PMMLRequestData> data, DataSource<PMML4Result> results, DataSource<SegmentExecution> segmentExecutions, KnowledgeHelper helper) throws IllegalStateException {
if (ruleUnitClassName == null || ruleUnitClassName.trim().isEmpty()) {
throw new IllegalStateException("Unable to apply segment model: No rule unit class name is available");
}
Class<? extends RuleUnit> ruleUnitClass = null;
RuleUnitRegistry rur = ((KnowledgeBaseImpl) helper.getKieRuntime().getKieBase()).getRuleUnitRegistry();
RuleUnit ruOld = ((WrappedStatefulKnowledgeSessionForRHS) helper.getKieRuntime()).getRuleUnitExecutor().getCurrentRuleUnit();
RuleUnitDescr rud = rur.getNamedRuleUnit(ruleUnitClassName).orElse(null);
if (rud != null) {
ruleUnitClass = rud.getRuleUnitClass();
} else {
throw new IllegalStateException("Unable to apply segment model: Unable to find rule unit");
}
// if (ruleUnitClass != null) {
// helper.run(ruleUnitClass);
// PMML4Result result = new PMML4Result(this);
// FactHandle requestFH = data.insert(this.requestData);
// FactHandle resultsFH = results.insert(result);
//
// helper.run(ruleUnitClass);
// RuleUnit ruNew = ((WrappedStatefulKnowledgeSessionForRHS)helper.getKieRuntime()).getRuleUnitExecutor().getCurrentRuleUnit();
// System.out.println("** Result **");
// System.out.println(result);
//
// // Update the state and let the Mining session know
// this.state = SegmentExecutionState.EXECUTING;
// FactHandle handle = ((InternalDataSource)segmentExecutions).getFactHandleForObject(this);
// segmentExecutions.update(handle, this);
//
// helper.run(ruleUnitClass);
// System.out.println("Sub-Model Results");
// results.forEach(r -> {System.out.println(r);});
// }
}
use of org.drools.core.ruleunit.RuleUnitDescr in project drools by kiegroup.
the class RuleUnitExecutorSession method bindRuleUnit.
private RuleUnitDescr bindRuleUnit(RuleUnit ruleUnit) {
suspended.set(false);
currentRuleUnit = ruleUnit;
currentRuleUnit.onStart();
factHandlesMap.computeIfAbsent(ruleUnit.getClass(), x -> session.getEntryPoint(RULE_UNIT_ENTRY_POINT).insert(ruleUnit));
RuleUnitDescr ruDescr = session.kBase.getRuleUnitRegistry().getRuleUnitDescr(ruleUnit);
ruDescr.bindDataSources(session, ruleUnit);
((Globals) session.getGlobalResolver()).setDelegate(new RuleUnitGlobals(ruDescr, ruleUnit));
InternalAgendaGroup unitGroup = (InternalAgendaGroup) session.getAgenda().getAgendaGroup(ruleUnit.getClass().getName());
unitGroup.setAutoDeactivate(false);
unitGroup.setFocus();
return ruDescr;
}
use of org.drools.core.ruleunit.RuleUnitDescr in project drools by kiegroup.
the class DroolsAbstractPMMLTest method getStartingRuleUnit.
protected Class<? extends RuleUnit> getStartingRuleUnit(String startingRule, InternalKnowledgeBase ikb, List<String> possiblePackages) {
RuleUnitRegistry unitRegistry = ikb.getRuleUnitRegistry();
Map<String, InternalKnowledgePackage> pkgs = ikb.getPackagesMap();
RuleImpl ruleImpl = null;
for (String pkgName : possiblePackages) {
if (pkgs.containsKey(pkgName)) {
InternalKnowledgePackage pkg = pkgs.get(pkgName);
ruleImpl = pkg.getRule(startingRule);
if (ruleImpl != null) {
RuleUnitDescr descr = unitRegistry.getRuleUnitFor(ruleImpl).orElse(null);
if (descr != null) {
return descr.getRuleUnitClass();
}
}
}
}
return null;
}
use of org.drools.core.ruleunit.RuleUnitDescr in project drools by kiegroup.
the class PMMLExecutor method startingRuleUnit.
protected Class<? extends RuleUnit> startingRuleUnit(String startingRule, String modelId, String... knownPackageNames) {
List<String> possiblePackages = calculatePossiblePackageNames(modelId, knownPackageNames);
InternalKnowledgeBase internalKnowledgeBase = (InternalKnowledgeBase) kieBase;
RuleUnitRegistry unitRegistry = internalKnowledgeBase.getRuleUnitRegistry();
Map<String, InternalKnowledgePackage> pkgs = internalKnowledgeBase.getPackagesMap();
RuleImpl ruleImpl = null;
for (String pkgName : possiblePackages) {
if (pkgs.containsKey(pkgName)) {
InternalKnowledgePackage pkg = pkgs.get(pkgName);
ruleImpl = pkg.getRule(startingRule);
if (ruleImpl != null) {
RuleUnitDescr descr = unitRegistry.getRuleUnitFor(ruleImpl).orElse(null);
if (descr != null) {
return descr.getRuleUnitClass();
}
}
}
}
return null;
}
Aggregations