Search in sources :

Example 1 with ObjectType

use of org.drools.core.spi.ObjectType in project drools by kiegroup.

the class RuleContext method getDeclaration.

Declaration getDeclaration(Variable variable) {
    if (variable.isFact()) {
        Declaration declaration = innerDeclaration.get(variable);
        if (declaration == null) {
            declaration = queryDeclaration.get(variable);
        }
        if (declaration == null) {
            Pattern pattern = patterns.get(variable);
            declaration = pattern != null ? pattern.getDeclaration() : null;
        }
        return declaration;
    } else {
        Global global = ((Global) variable);
        ObjectType objectType = builder.getObjectType(global);
        InternalReadAccessor globalExtractor = new GlobalExtractor(global.getName(), objectType);
        return new Declaration(global.getName(), globalExtractor, new Pattern(0, objectType));
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) ObjectType(org.drools.core.spi.ObjectType) GlobalExtractor(org.drools.core.spi.GlobalExtractor) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Declaration(org.drools.core.rule.Declaration) Global(org.drools.model.Global)

Example 2 with ObjectType

use of org.drools.core.spi.ObjectType in project drools by kiegroup.

the class AccumulateNode method addAccFunctionDeclarationsToLeftMask.

private void addAccFunctionDeclarationsToLeftMask(InternalKnowledgeBase kbase, LeftTupleSource leftInput, Accumulate accumulate) {
    BitMask leftMask = getLeftInferredMask();
    ObjectType leftObjectType = leftInput.getObjectType();
    if (leftObjectType instanceof ClassObjectType) {
        TypeDeclaration typeDeclaration = kbase.getExactTypeDeclaration(((ClassObjectType) leftObjectType).getClassType());
        if (typeDeclaration != null && typeDeclaration.isPropertyReactive()) {
            List<String> accessibleProperties = typeDeclaration.getAccessibleProperties();
            for (Declaration decl : accumulate.getRequiredDeclarations()) {
                if (leftObjectType.equals(decl.getPattern().getObjectType())) {
                    leftMask = leftMask.setAll(calculatePositiveMask(typeDeclaration.getTypeClass(), decl.getPattern().getListenedProperties(), accessibleProperties));
                }
            }
        }
    }
    setLeftInferredMask(leftMask);
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) BitMask(org.drools.core.util.bitmask.BitMask) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Declaration(org.drools.core.rule.Declaration) TypeDeclaration(org.drools.core.rule.TypeDeclaration)

Example 3 with ObjectType

use of org.drools.core.spi.ObjectType in project drools by kiegroup.

the class LeftTupleSource method initDeclaredMask.

protected void initDeclaredMask(BuildContext context, LeftTupleSource leftInput) {
    if (context == null || context.getLastBuiltPatterns() == null) {
        // only happens during unit tests
        leftDeclaredMask = AllSetBitMask.get();
        return;
    }
    if (leftInput.getType() != NodeTypeEnums.LeftInputAdapterNode) {
        // BetaNode's not after LIANode are not relevant for left mask property specific, so don't block anything.
        leftDeclaredMask = AllSetBitMask.get();
        return;
    }
    // left input pattern
    Pattern pattern = getLeftInputPattern(context);
    ObjectType objectType = getObjectTypeForPropertyReactivity((LeftInputAdapterNode) leftInput, pattern);
    if (!(objectType instanceof ClassObjectType)) {
        // Only ClassObjectType can use property specific
        leftDeclaredMask = AllSetBitMask.get();
        return;
    }
    Class objectClass = ((ClassWireable) objectType).getClassType();
    // if pattern is null (e.g. for eval or query nodes) we cannot calculate the mask, so we set it all
    if (pattern != null && isPropertyReactive(context, objectClass)) {
        Collection<String> leftListenedProperties = pattern.getListenedProperties();
        List<String> settableProperties = getAccessibleProperties(context.getKnowledgeBase(), objectClass);
        leftDeclaredMask = calculatePositiveMask(objectClass, leftListenedProperties, settableProperties);
        leftNegativeMask = calculateNegativeMask(objectClass, leftListenedProperties, settableProperties);
        setLeftListenedProperties(leftListenedProperties);
    } else {
        // if property specific is not on, then accept all modification propagations
        leftDeclaredMask = AllSetBitMask.get();
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) ClassObjectType(org.drools.core.base.ClassObjectType) ClassWireable(org.drools.core.spi.ClassWireable)

Example 4 with ObjectType

use of org.drools.core.spi.ObjectType in project drools by kiegroup.

the class StreamsTest method testEventExpirationValue.

@Test(timeout = 10000)
public void testEventExpirationValue() throws Exception {
    String drl1 = "package org.drools.pkg1\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick\n" + "    @role(event)\n" + "end\n" + "rule X\n" + "when\n" + "    StockTick()\n" + "then\n" + "end\n";
    String drl2 = "package org.drools.pkg2\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick\n" + "    @role(event)\n" + "end\n" + "rule X\n" + "when\n" + "    StockTick()\n" + "then\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(drl1.getBytes()), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newByteArrayResource(drl2.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    KieBaseConfiguration kconf = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kconf.setOption(EventProcessingOption.STREAM);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kconf);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    List<ObjectTypeNode> otns = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
    ObjectType stot = new ClassObjectType(StockTick.class);
    for (ObjectTypeNode otn : otns) {
        if (otn.getObjectType().isAssignableFrom(stot)) {
            assertEquals(NEVER_EXPIRES, otn.getExpirationOffset());
        }
    }
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 5 with ObjectType

use of org.drools.core.spi.ObjectType in project drools by kiegroup.

the class MVELConsequenceBuilderTest method testSimpleExpression.

@Test
public void testSimpleExpression() throws Exception {
    PackageDescr pkgDescr = new PackageDescr("pkg1");
    KnowledgeBuilderImpl pkgBuilder = new KnowledgeBuilderImpl();
    pkgBuilder.addPackage(pkgDescr);
    InternalKnowledgePackage pkg = pkgBuilder.getPackageRegistry("pkg1").getPackage();
    final RuleDescr ruleDescr = new RuleDescr("rule 1");
    ruleDescr.setNamespace("pkg1");
    ruleDescr.setConsequence("modify (cheese) {price = 5 };\nretract (cheese)");
    DialectCompiletimeRegistry dialectRegistry = pkgBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    MVELDialect mvelDialect = (MVELDialect) dialectRegistry.getDialect("mvel");
    final RuleBuildContext context = new RuleBuildContext(pkgBuilder, ruleDescr, dialectRegistry, pkg, mvelDialect);
    final InstrumentedDeclarationScopeResolver declarationResolver = new InstrumentedDeclarationScopeResolver();
    final ObjectType cheeseObjeectType = new ClassObjectType(Cheese.class);
    final Pattern pattern = new Pattern(0, cheeseObjeectType, "cheese");
    final GroupElement subrule = new GroupElement(GroupElement.AND);
    subrule.addChild(pattern);
    final Map<String, Declaration> map = new HashMap<String, Declaration>();
    map.put("cheese", pattern.getDeclaration());
    declarationResolver.setDeclarations(map);
    context.setDeclarationResolver(declarationResolver);
    final MVELConsequenceBuilder builder = new MVELConsequenceBuilder();
    builder.build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
    InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
    PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
    kBase.addPackage(pkg);
    StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
    final Cheese cheddar = new Cheese("cheddar", 10);
    final InternalFactHandle f0 = (InternalFactHandle) ksession.insert(cheddar);
    final LeftTupleImpl tuple = new LeftTupleImpl(f0, null, true);
    f0.removeLeftTuple(tuple);
    final AgendaItem item = new AgendaItemImpl(0, tuple, 10, pctxFactory.createPropagationContext(1, PropagationContext.Type.DELETION, null, tuple != null ? (TerminalNode) tuple.getTupleSink() : null, null), new RuleTerminalNode(0, new CompositeObjectSinkAdapterTest.MockBetaNode(), context.getRule(), subrule, 0, new BuildContext(kBase)), null);
    final DefaultKnowledgeHelper kbHelper = new DefaultKnowledgeHelper(ksession);
    kbHelper.setActivation(item);
    ((MVELConsequence) context.getRule().getConsequence()).compile((MVELDialectRuntimeData) pkgBuilder.getPackageRegistry(pkg.getName()).getDialectRuntimeRegistry().getDialectData("mvel"));
    context.getRule().getConsequence().evaluate(kbHelper, ksession);
    assertEquals(5, cheddar.getPrice());
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) HashMap(java.util.HashMap) GroupElement(org.drools.core.rule.GroupElement) Cheese(org.drools.compiler.Cheese) MVELConsequence(org.drools.core.base.mvel.MVELConsequence) AgendaItem(org.drools.core.common.AgendaItem) ClassObjectType(org.drools.core.base.ClassObjectType) ObjectType(org.drools.core.spi.ObjectType) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) AgendaItemImpl(org.drools.core.common.AgendaItemImpl) Declaration(org.drools.core.rule.Declaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalFactHandle(org.drools.core.common.InternalFactHandle) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Pattern(org.drools.core.rule.Pattern) PropagationContextFactory(org.drools.core.common.PropagationContextFactory) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) DefaultKnowledgeHelper(org.drools.core.base.DefaultKnowledgeHelper) BuildContext(org.drools.core.reteoo.builder.BuildContext) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Aggregations

ObjectType (org.drools.core.spi.ObjectType)33 ClassObjectType (org.drools.core.base.ClassObjectType)32 Test (org.junit.Test)17 Pattern (org.drools.core.rule.Pattern)13 Declaration (org.drools.core.rule.Declaration)7 FactTemplateObjectType (org.drools.core.facttemplates.FactTemplateObjectType)5 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)5 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)5 InternalFactHandle (org.drools.core.common.InternalFactHandle)4 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)4 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)4 TypeDeclaration (org.drools.core.rule.TypeDeclaration)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)3 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)3 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)3 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)3 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)3 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)3