Search in sources :

Example 1 with RuleUnitDescr

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);
}
Also used : VariableDeclarationExpr(org.drools.javaparser.ast.expr.VariableDeclarationExpr) MethodDeclaration(org.drools.javaparser.ast.body.MethodDeclaration) RuleUnitDescr(org.drools.core.ruleunit.RuleUnitDescr) BlockStmt(org.drools.javaparser.ast.stmt.BlockStmt) StringLiteralExpr(org.drools.javaparser.ast.expr.StringLiteralExpr) AssignExpr(org.drools.javaparser.ast.expr.AssignExpr) ClassExpr(org.drools.javaparser.ast.expr.ClassExpr) ModelGeneratorVisitor(org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor) ReturnStmt(org.drools.javaparser.ast.stmt.ReturnStmt) MethodCallExpr(org.drools.javaparser.ast.expr.MethodCallExpr)

Example 2 with RuleUnitDescr

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);});
// }
}
Also used : RuleUnitRegistry(org.drools.core.ruleunit.RuleUnitRegistry) RuleUnitDescr(org.drools.core.ruleunit.RuleUnitDescr) KnowledgeBaseImpl(org.drools.core.impl.KnowledgeBaseImpl) RuleUnit(org.kie.api.runtime.rule.RuleUnit)

Example 3 with RuleUnitDescr

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;
}
Also used : Globals(org.kie.api.runtime.Globals) InternalAgendaGroup(org.drools.core.common.InternalAgendaGroup) RuleUnitDescr(org.drools.core.ruleunit.RuleUnitDescr)

Example 4 with RuleUnitDescr

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;
}
Also used : RuleUnitRegistry(org.drools.core.ruleunit.RuleUnitRegistry) RuleUnitDescr(org.drools.core.ruleunit.RuleUnitDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 5 with RuleUnitDescr

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;
}
Also used : RuleUnitRegistry(org.drools.core.ruleunit.RuleUnitRegistry) RuleUnitDescr(org.drools.core.ruleunit.RuleUnitDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Aggregations

RuleUnitDescr (org.drools.core.ruleunit.RuleUnitDescr)5 RuleUnitRegistry (org.drools.core.ruleunit.RuleUnitRegistry)3 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)2 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)2 InternalAgendaGroup (org.drools.core.common.InternalAgendaGroup)1 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)1 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)1 MethodDeclaration (org.drools.javaparser.ast.body.MethodDeclaration)1 AssignExpr (org.drools.javaparser.ast.expr.AssignExpr)1 ClassExpr (org.drools.javaparser.ast.expr.ClassExpr)1 MethodCallExpr (org.drools.javaparser.ast.expr.MethodCallExpr)1 StringLiteralExpr (org.drools.javaparser.ast.expr.StringLiteralExpr)1 VariableDeclarationExpr (org.drools.javaparser.ast.expr.VariableDeclarationExpr)1 BlockStmt (org.drools.javaparser.ast.stmt.BlockStmt)1 ReturnStmt (org.drools.javaparser.ast.stmt.ReturnStmt)1 ModelGeneratorVisitor (org.drools.modelcompiler.builder.generator.visitor.ModelGeneratorVisitor)1 Globals (org.kie.api.runtime.Globals)1 RuleUnit (org.kie.api.runtime.rule.RuleUnit)1