Search in sources :

Example 66 with ClassObjectType

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

the class KiePackagesBuilder method getClassObjectType.

private ObjectType getClassObjectType(Class<?> patternClass) {
    return objectTypeCache.computeIfAbsent(patternClass.getCanonicalName(), name -> {
        boolean isEvent = false;
        if (!name.startsWith("java.") && !patternClass.isPrimitive()) {
            KnowledgePackageImpl pkg = (KnowledgePackageImpl) packages.computeIfAbsent(patternClass.getPackage().getName(), this::createKiePackage);
            TypeDeclaration typeDeclaration = pkg.getTypeDeclaration(patternClass);
            if (typeDeclaration == null) {
                typeDeclaration = createTypeDeclaration(patternClass);
                pkg.addTypeDeclaration(typeDeclaration);
            }
            isEvent = typeDeclaration.getRole() == Role.Type.EVENT;
        }
        return new ClassObjectType(patternClass, isEvent);
    });
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) TypeDeclarationUtil.createTypeDeclaration(org.drools.modelcompiler.util.TypeDeclarationUtil.createTypeDeclaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 67 with ClassObjectType

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

the class TraitTest method testAlphaNodeSharing.

@Test
public void testAlphaNodeSharing() {
    String drl = "package test; " + "import " + Entity.class.getName() + " " + "declare trait Person\n" + "    name : String\n" + "end\n" + "rule Init " + "when " + "then " + "    don( new Entity(), Person.class ); " + "end\n" + "rule One when" + "    $core: Entity( this isA Person ) " + "then " + "end " + "rule Two when" + "    $core: Entity( this isA Person ) " + "then " + "end " + "\n";
    final KieBase kbase = getKieBaseFromString(drl);
    TraitFactory.setMode(mode, kbase);
    KieSession kSession = kbase.newKieSession();
    assertEquals(3, kSession.fireAllRules());
    NamedEntryPoint nep = ((NamedEntryPoint) kSession.getEntryPoint(EntryPointId.DEFAULT.getEntryPointId()));
    ObjectTypeNode otn = nep.getEntryPointNode().getObjectTypeNodes().get(new ClassObjectType(Entity.class));
    assertNotNull(otn);
    assertEquals(1, otn.getObjectSinkPropagator().getSinks().length);
}
Also used : Entity(org.drools.core.factmodel.traits.Entity) ClassObjectType(org.drools.core.base.ClassObjectType) KieBase(org.kie.api.KieBase) NamedEntryPoint(org.drools.core.common.NamedEntryPoint) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 68 with ClassObjectType

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

the class CepEspTest method testEventExpiration3.

@Test(timeout = 10000)
public void testEventExpiration3() throws Exception {
    // read in the source
    KieBaseConfiguration conf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    conf.setOption(EventProcessingOption.STREAM);
    final KieBase kbase = loadKnowledgeBase(conf, "test_CEP_EventExpiration3.drl");
    Map<ObjectType, ObjectTypeNode> objectTypeNodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes(EntryPointId.DEFAULT);
    ObjectTypeNode node = objectTypeNodes.get(new ClassObjectType(StockTick.class));
    assertNotNull(node);
    // the expiration policy @expires(10m) should override the temporal operator usage
    assertEquals(TimeIntervalParser.parse("10m")[0] + 1, node.getExpirationOffset());
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) StockTick(org.drools.compiler.StockTick) KieBase(org.kie.api.KieBase) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) Test(org.junit.Test)

Example 69 with ClassObjectType

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

the class PatternBuilder method buildXPathDescr.

private Constraint buildXPathDescr(RuleBuildContext context, PatternDescr patternDescr, Pattern pattern, BaseDescr descr, MVELDumper.MVELDumperContext mvelCtx) {
    String expression = ((ExpressionDescr) descr).getExpression();
    XpathAnalysis xpathAnalysis = XpathAnalysis.analyze(expression);
    if (xpathAnalysis.hasError()) {
        registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expression + "': " + xpathAnalysis.getError());
        return null;
    }
    XpathConstraint xpathConstraint = new XpathConstraint();
    ObjectType objectType = pattern.getObjectType();
    Class<?> patternClass = objectType.getClassType();
    List<Class<?>> backReferenceClasses = new ArrayList<Class<?>>();
    backReferenceClasses.add(patternClass);
    XpathBackReference backRef = new XpathBackReference(pattern, backReferenceClasses);
    pattern.setBackRefDeclarations(backRef);
    ObjectType originalType = pattern.getObjectType();
    ObjectType currentObjectType = originalType;
    mvelCtx.setInXpath(true);
    try {
        for (XpathAnalysis.XpathPart part : xpathAnalysis) {
            XpathConstraint.XpathChunk xpathChunk = xpathConstraint.addChunck(patternClass, part.getField(), part.getIndex(), part.isIterate(), part.isLazy());
            // make sure the Pattern is wired up to correct ClassObjectType and set as a target for rewiring
            context.getPkg().getClassFieldAccessorStore().wireObjectType(currentObjectType, xpathChunk);
            if (xpathChunk == null) {
                registerDescrBuildError(context, patternDescr, "Invalid xpath expression '" + expression + "': cannot access " + part.getField() + " on " + patternClass);
                pattern.setObjectType(originalType);
                return null;
            }
            if (part.getInlineCast() != null) {
                try {
                    patternClass = context.getDialect().getTypeResolver().resolveType(part.getInlineCast());
                } catch (ClassNotFoundException e) {
                    registerDescrBuildError(context, patternDescr, "Unknown class " + part.getInlineCast() + " in xpath expression '" + expression + "'");
                    return null;
                }
                part.addInlineCastConstraint(patternClass);
                currentObjectType = getObjectType(context, patternDescr, patternClass.getName());
                xpathChunk.setReturnedType(currentObjectType);
            } else {
                patternClass = xpathChunk.getReturnedClass();
                currentObjectType = getObjectType(context, patternDescr, patternClass.getName());
            }
            pattern.setObjectType(currentObjectType);
            backReferenceClasses.add(0, patternClass);
            backRef.reset();
            for (String constraint : part.getConstraints()) {
                ConstraintConnectiveDescr result = parseExpression(context, patternDescr, new ExprConstraintDescr(constraint), constraint);
                if (result == null) {
                    continue;
                }
                for (Constraint c : build(context, patternDescr, pattern, result, mvelCtx)) {
                    xpathChunk.addConstraint(c);
                }
            }
        }
    } finally {
        mvelCtx.setInXpath(false);
        pattern.setBackRefDeclarations(null);
        pattern.setObjectType(originalType);
    }
    xpathConstraint.setXpathStartDeclaration(patternDescr.getXpathStartDeclaration());
    if (descr instanceof BindingDescr) {
        xpathConstraint.setDeclaration(pattern.addDeclaration(((BindingDescr) descr).getVariable()));
    }
    return xpathConstraint;
}
Also used : XpathConstraint(org.drools.core.rule.constraint.XpathConstraint) BindingDescr(org.drools.compiler.lang.descr.BindingDescr) 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) XpathBackReference(org.drools.core.rule.XpathBackReference) AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) ExpressionDescr(org.drools.compiler.lang.descr.ExpressionDescr) XpathPart(org.drools.compiler.rule.builder.XpathAnalysis.XpathPart) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr)

Example 70 with ClassObjectType

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

the class PatternBuilder method isDateType.

private boolean isDateType(RuleBuildContext context, Pattern pattern, String leftValue) {
    Declaration declaration = pattern.getDeclarations().get(leftValue);
    if (declaration != null && declaration.getExtractor() != null) {
        return declaration.getValueType() == ValueType.DATE_TYPE;
    }
    if (pattern.getObjectType() instanceof FactTemplateObjectType) {
        return ((FactTemplateObjectType) pattern.getObjectType()).getFactTemplate().getFieldTemplate(leftValue).getValueType() == ValueType.DATE_TYPE;
    }
    Class<?> clazz = ((ClassObjectType) pattern.getObjectType()).getClassType();
    Class<?> fieldType = context.getPkg().getClassFieldAccessorStore().getFieldType(clazz, leftValue);
    return fieldType != null && ValueType.isDateType(fieldType);
}
Also used : AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) FactTemplateObjectType(org.drools.core.facttemplates.FactTemplateObjectType) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

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