Search in sources :

Example 71 with ClassObjectType

use of org.drools.core.base.ClassObjectType in project drools by kiegroup.

the class PatternBuilder method processPositional.

protected void processPositional(final RuleBuildContext context, final PatternDescr patternDescr, final Pattern pattern, final ExprConstraintDescr descr) {
    if (descr.getType() == ExprConstraintDescr.Type.POSITIONAL && pattern.getObjectType() instanceof ClassObjectType) {
        Class<?> klazz = ((ClassObjectType) pattern.getObjectType()).getClassType();
        TypeDeclaration tDecl = context.getKnowledgeBuilder().getTypeDeclaration(klazz);
        if (tDecl == null) {
            registerDescrBuildError(context, patternDescr, "Unable to find @positional definitions for :" + klazz + "\n");
            return;
        }
        ClassDefinition clsDef = tDecl.getTypeClassDef();
        if (clsDef == null) {
            registerDescrBuildError(context, patternDescr, "Unable to find @positional field " + descr.getPosition() + " for class " + tDecl.getTypeName() + "\n");
            return;
        }
        FieldDefinition field = clsDef.getField(descr.getPosition());
        if (field == null) {
            registerDescrBuildError(context, patternDescr, "Unable to find @positional field " + descr.getPosition() + " for class " + tDecl.getTypeName() + "\n");
            return;
        }
        String expr = descr.getExpression();
        boolean isSimpleIdentifier = isIdentifier(expr);
        if (isSimpleIdentifier) {
            // create a binding
            BindingDescr binder = new BindingDescr();
            binder.setUnification(true);
            binder.setExpression(field.getName());
            binder.setVariable(descr.getExpression());
            buildRuleBindings(context, patternDescr, pattern, binder);
        } else {
            // create a constraint
            build(context, patternDescr, pattern, descr, field.getName() + " == " + descr.getExpression());
        }
    }
}
Also used : BindingDescr(org.drools.compiler.lang.descr.BindingDescr) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) FieldDefinition(org.drools.core.factmodel.FieldDefinition) ClassDefinition(org.drools.core.factmodel.ClassDefinition) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 72 with ClassObjectType

use of org.drools.core.base.ClassObjectType in project drools by kiegroup.

the class PatternBuilder method build.

protected List<Constraint> build(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern, ConstraintConnectiveDescr descr, MVELDumper.MVELDumperContext mvelCtx) {
    List<Constraint> constraints = new ArrayList<Constraint>();
    List<BaseDescr> initialDescrs = new ArrayList<BaseDescr>(descr.getDescrs());
    for (BaseDescr d : initialDescrs) {
        boolean isXPath = isXPathDescr(d);
        if (isXPath && pattern.hasXPath()) {
            registerDescrBuildError(context, patternDescr, "More than a single oopath constraint is not allowed in the same pattern");
            return constraints;
        }
        Constraint constraint = isXPath ? buildXPathDescr(context, patternDescr, pattern, d, mvelCtx) : buildCcdDescr(context, patternDescr, pattern, d, descr, mvelCtx);
        if (constraint != null) {
            Declaration declCorrXpath = getDeclarationCorrespondingToXpath(pattern, isXPath, constraint);
            if (declCorrXpath == null) {
                constraints.add(constraint);
            } else {
                // A constraint is using a declration bound to an xpath in the same pattern
                // Move the constraint inside the last chunk of the xpath defining this declaration, rewriting it as 'this'
                Pattern modifiedPattern = pattern.clone();
                modifiedPattern.setObjectType(new ClassObjectType(declCorrXpath.getDeclarationClass()));
                constraint = buildCcdDescr(context, patternDescr, modifiedPattern, d.replaceVariable(declCorrXpath.getBindingName(), "this"), descr, mvelCtx);
                if (constraint != null) {
                    pattern.getXpathConstraint().getChunks().getLast().addConstraint(constraint);
                }
            }
        }
    }
    if (descr.getDescrs().size() > initialDescrs.size()) {
        // The initial build process may have generated other constraint descrs.
        // This happens when null-safe references or inline-casts are used
        // These additional constraints must be built, and added as
        List<BaseDescr> additionalDescrs = new ArrayList<BaseDescr>(descr.getDescrs());
        additionalDescrs.removeAll(initialDescrs);
        if (!additionalDescrs.isEmpty()) {
            List<Constraint> additionalConstraints = new ArrayList<Constraint>();
            for (BaseDescr d : additionalDescrs) {
                Constraint constraint = buildCcdDescr(context, patternDescr, pattern, d, descr, mvelCtx);
                if (constraint != null) {
                    additionalConstraints.add(constraint);
                }
            }
            constraints.addAll(0, additionalConstraints);
        }
    }
    return constraints;
}
Also used : Pattern(org.drools.core.rule.Pattern) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) NegConstraint(org.drools.core.rule.constraint.NegConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) EvaluatorConstraint(org.drools.core.rule.constraint.EvaluatorConstraint) PredicateConstraint(org.drools.core.rule.PredicateConstraint) ArrayList(java.util.ArrayList) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 73 with ClassObjectType

use of org.drools.core.base.ClassObjectType in project drools by kiegroup.

the class PatternBuilder method getSettableProperties.

protected List<String> getSettableProperties(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern) {
    ObjectType patternType = pattern.getObjectType();
    if (!(patternType instanceof ClassObjectType)) {
        return null;
    }
    Class<?> patternClass = patternType.getClassType();
    TypeDeclaration typeDeclaration = getTypeDeclaration(pattern, context);
    if (!typeDeclaration.isPropertyReactive()) {
        registerDescrBuildError(context, patternDescr, "Wrong usage of @" + Watch.class.getSimpleName() + " annotation on class " + patternClass.getName() + " that is not annotated as @PropertyReactive");
    }
    typeDeclaration.setTypeClass(patternClass);
    return typeDeclaration.getAccessibleProperties();
}
Also used : AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) Watch(org.kie.api.definition.rule.Watch) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 74 with ClassObjectType

use of org.drools.core.base.ClassObjectType in project drools by kiegroup.

the class MarshallingTest method testDynamicEmptyRule.

@Test
public void testDynamicEmptyRule() throws Exception {
    String rule1 = "package org.drools.compiler.test;\n";
    rule1 += "global java.util.List list\n";
    rule1 += "rule \"Rule 1\"\n";
    rule1 += "when\n";
    rule1 += "then\n";
    rule1 += "    list.add( \"fired1\" );\n";
    rule1 += "end";
    String rule2 = "package org.drools.compiler.test;\n";
    rule2 += "global java.util.List list\n";
    rule2 += "rule \"Rule 2\"\n";
    rule2 += "when\n";
    rule2 += "then\n";
    rule2 += "    list.add( \"fired2\" );\n";
    rule2 += "end";
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) loadKnowledgeBaseFromString(rule1);
    // Make sure the rete node map is created correctly
    Map<Integer, BaseNode> nodes = RuleBaseNodes.getNodeMap((InternalKnowledgeBase) kBase);
    // Make sure the rete node map is created correctly
    assertEquals(2, nodes.size());
    assertEquals("InitialFactImpl", ((ClassObjectType) ((ObjectTypeNode) nodes.get(2)).getObjectType()).getClassType().getSimpleName());
    assertEquals("Rule 1", ((RuleTerminalNode) nodes.get(4)).getRule().getName());
    KieSession session = kBase.newKieSession();
    List list = new ArrayList();
    session.setGlobal("list", list);
    KieSession session1 = SerializationHelper.getSerialisedStatefulKnowledgeSession(session, kBase, false);
    session1.fireAllRules();
    assertEquals(1, ((List) session1.getGlobal("list")).size());
    KieSession session2 = SerializationHelper.getSerialisedStatefulKnowledgeSession(session1, kBase, false);
    session.dispose();
    session1.dispose();
    Collection<KiePackage> kpkgs = loadKnowledgePackagesFromString(rule2);
    kBase.addPackages(kpkgs);
    session2.fireAllRules();
    System.out.println(session2.getGlobal("list"));
    assertEquals(2, ((List) session2.getGlobal("list")).size());
    assertEquals("fired1", ((List) session2.getGlobal("list")).get(0));
    assertEquals("fired2", ((List) session2.getGlobal("list")).get(1));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) KiePackage(org.kie.api.definition.KiePackage) BaseNode(org.drools.core.common.BaseNode) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) List(java.util.List) ArrayList(java.util.ArrayList) IteratorToList(org.drools.compiler.integrationtests.IteratorToList) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Example 75 with ClassObjectType

use of org.drools.core.base.ClassObjectType in project drools by kiegroup.

the class ParallelEvaluationTest method test.

@Test(timeout = 10000L)
public void test() {
    StringBuilder sb = new StringBuilder(400);
    sb.append("global java.util.List list;\n");
    for (int i = 0; i < 10; i++) {
        sb.append(getRule(i, ""));
    }
    KieBase kbase = new KieHelper().addContent(sb.toString(), ResourceType.DRL).build(MultithreadEvaluationOption.YES);
    EntryPointNode epn = ((InternalKnowledgeBase) kbase).getRete().getEntryPointNode(EntryPointId.DEFAULT);
    ObjectTypeNode otn = epn.getObjectTypeNodes().get(new ClassObjectType(Integer.class));
    assertTrue(((CompositePartitionAwareObjectSinkAdapter) otn.getObjectSinkPropagator()).isHashed());
    KieSession ksession = kbase.newKieSession();
    assertTrue(((InternalWorkingMemory) ksession).getAgenda().isParallelAgenda());
    List<Integer> list = new DebugList<Integer>();
    ksession.setGlobal("list", list);
    for (int i = 0; i < 10; i++) {
        ksession.insert(i);
        ksession.insert("" + i);
    }
    ksession.fireAllRules();
    assertEquals(10, list.size());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InternalWorkingMemory(org.drools.core.common.InternalWorkingMemory) EntryPointNode(org.drools.core.reteoo.EntryPointNode) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) DebugList(org.drools.compiler.util.debug.DebugList) Test(org.junit.Test)

Aggregations

ClassObjectType (org.drools.core.base.ClassObjectType)123 Test (org.junit.Test)76 Pattern (org.drools.core.rule.Pattern)37 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)34 KieBase (org.kie.api.KieBase)32 Declaration (org.drools.core.rule.Declaration)28 ObjectType (org.drools.core.spi.ObjectType)28 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)24 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)23 BetaNode (org.drools.core.reteoo.BetaNode)17 AlphaNode (org.drools.core.reteoo.AlphaNode)15 ArrayList (java.util.ArrayList)14 List (java.util.List)13 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)13 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)13 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)13 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)13 Cheese (org.drools.core.test.model.Cheese)13 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)12 InternalFactHandle (org.drools.core.common.InternalFactHandle)12