Search in sources :

Example 36 with ClassObjectType

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

the class PatternBuilder method processClassObjectType.

private void processClassObjectType(RuleBuildContext context, ObjectType objectType, Pattern pattern) {
    if (objectType instanceof ClassObjectType) {
        // make sure the Pattern is wired up to correct ClassObjectType and set as a target for rewiring
        context.getPkg().getClassFieldAccessorStore().wireObjectType(objectType, pattern);
        Class<?> cls = ((ClassObjectType) objectType).getClassType();
        if (cls.getPackage() != null && !cls.getPackage().getName().equals("java.lang")) {
            // register the class in its own package unless it is primitive or belongs to java.lang
            TypeDeclaration typeDeclr = context.getKnowledgeBuilder().getAndRegisterTypeDeclaration(cls, cls.getPackage().getName());
            context.setTypesafe(typeDeclr == null || typeDeclr.isTypesafe());
        } else {
            context.setTypesafe(true);
        }
    }
}
Also used : AcceptsClassObjectType(org.drools.core.spi.AcceptsClassObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 37 with ClassObjectType

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

the class QueryBuilder method build.

public Pattern build(final RuleBuildContext context, final QueryDescr queryDescr) {
    ObjectType queryObjectType = ClassObjectType.DroolsQuery_ObjectType;
    final Pattern pattern = new Pattern(context.getNextPatternId(), // offset is 0 by default
    0, queryObjectType, null);
    final InternalReadAccessor extractor = PatternBuilder.getFieldReadAccessor(context, queryDescr, pattern, "name", null, true);
    final QueryNameConstraint constraint = new QueryNameConstraint(extractor, queryDescr.getName());
    PatternBuilder.registerReadAccessor(context, queryObjectType, "name", constraint);
    // adds appropriate constraint to the pattern
    pattern.addConstraint(constraint);
    ObjectType argsObjectType = ClassObjectType.DroolsQuery_ObjectType;
    InternalReadAccessor arrayExtractor = PatternBuilder.getFieldReadAccessor(context, queryDescr, null, argsObjectType, "elements", null, true);
    QueryImpl query = ((QueryImpl) context.getRule());
    String[] params;
    String[] types;
    int numParams = queryDescr.getParameters().length;
    if (query.isAbductive()) {
        params = Arrays.copyOf(queryDescr.getParameters(), queryDescr.getParameters().length + 1);
        types = Arrays.copyOf(queryDescr.getParameterTypes(), queryDescr.getParameterTypes().length + 1);
    } else {
        params = queryDescr.getParameters();
        types = queryDescr.getParameterTypes();
    }
    Declaration[] declarations = new Declaration[params.length];
    Class<?> abductionReturnKlass = null;
    if (query.isAbductive()) {
        Abductive abductive = queryDescr.getTypedAnnotation(Abductive.class);
        abductionReturnKlass = abductive.target();
        params[numParams] = "";
        types[numParams] = abductionReturnKlass.getName();
    }
    int i = 0;
    try {
        for (i = 0; i < params.length; i++) {
            Declaration declr = pattern.addDeclaration(params[i]);
            // this bit is different, notice its the ArrayElementReader that we wire up to, not the declaration.
            ArrayElementReader reader = new ArrayElementReader(arrayExtractor, i, context.getDialect().getTypeResolver().resolveType(types[i]));
            PatternBuilder.registerReadAccessor(context, argsObjectType, "elements", reader);
            declr.setReadAccessor(reader);
            declarations[i] = declr;
        }
        query.setParameters(declarations);
    } catch (ClassNotFoundException e) {
        context.addError(new DescrBuildError(context.getParentDescr(), queryDescr, e, "Unable to resolve type '" + types[i] + " for parameter" + params[i]));
    }
    context.setPrefixPattern(pattern);
    if (query.isAbductive()) {
        String returnName = "";
        try {
            AnnotationDescr ann = queryDescr.getAnnotation(Abductive.class);
            Object[] argsVal = ((Object[]) ann.getValue("args"));
            String[] args = argsVal != null ? Arrays.copyOf(argsVal, argsVal.length, String[].class) : null;
            returnName = types[numParams];
            ObjectType objectType = new ClassObjectType(abductionReturnKlass, false);
            objectType = context.getPkg().getClassFieldAccessorStore().wireObjectType(objectType, (AbductiveQuery) query);
            ((AbductiveQuery) query).setReturnType(objectType, params, args, declarations);
        } catch (NoSuchMethodException e) {
            context.addError(new DescrBuildError(context.getParentDescr(), queryDescr, e, "Unable to resolve abducible constructor for type : " + returnName + " with types " + Arrays.toString(types)));
        } catch (IllegalArgumentException e) {
            context.addError(new DescrBuildError(context.getParentDescr(), queryDescr, e, e.getMessage()));
        }
    }
    return pattern;
}
Also used : Pattern(org.drools.core.rule.Pattern) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) ClassObjectType(org.drools.core.base.ClassObjectType) Abductive(org.drools.core.beliefsystem.abductive.Abductive) AnnotationDescr(org.drools.compiler.lang.descr.AnnotationDescr) QueryNameConstraint(org.drools.core.rule.constraint.QueryNameConstraint) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) QueryImpl(org.drools.core.rule.QueryImpl) DescrBuildError(org.drools.compiler.compiler.DescrBuildError) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) ArrayElementReader(org.drools.core.base.extractors.ArrayElementReader) Declaration(org.drools.core.rule.Declaration) AbductiveQuery(org.drools.core.rule.AbductiveQuery)

Example 38 with ClassObjectType

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

the class CepEspTest method testEventExpiration2.

@Test(timeout = 10000)
public void testEventExpiration2() throws Exception {
    // read in the source
    KieBaseConfiguration kbc = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbc.setOption(EventProcessingOption.STREAM);
    KieBase kbase = loadKnowledgeBase(kbc, "test_CEP_EventExpiration2.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 39 with ClassObjectType

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

the class BaseMannersTest method getLiteralConstraint.

private AlphaNodeFieldConstraint getLiteralConstraint(final Pattern pattern, final String fieldName, final int fieldValue) {
    final Class clazz = ((ClassObjectType) pattern.getObjectType()).getClassType();
    final InternalReadAccessor extractor = store.getReader(clazz, fieldName);
    final FieldValue field = new LongFieldImpl(fieldValue);
    return new MvelConstraintTestUtil(fieldName + " == " + fieldValue, new LongFieldImpl(fieldValue), extractor);
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) LongFieldImpl(org.drools.core.base.field.LongFieldImpl) FieldValue(org.drools.core.spi.FieldValue) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil)

Example 40 with ClassObjectType

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

the class BaseMannersTest method getLiteralConstraint.

private AlphaNodeFieldConstraint getLiteralConstraint(final Pattern pattern, final String fieldName, final boolean fieldValue) {
    final Class clazz = ((ClassObjectType) pattern.getObjectType()).getClassType();
    final InternalReadAccessor extractor = store.getReader(clazz, fieldName);
    final FieldValue field = new BooleanFieldImpl(fieldValue);
    return new MvelConstraintTestUtil(fieldName + " == " + fieldValue, new BooleanFieldImpl(fieldValue), extractor);
}
Also used : BooleanFieldImpl(org.drools.core.base.field.BooleanFieldImpl) ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) FieldValue(org.drools.core.spi.FieldValue) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil)

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