Search in sources :

Example 71 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class PatternBuilder method buildQuery.

private RuleConditionElement buildQuery(RuleBuildContext context, PatternDescr descr, PatternDescr patternDescr) {
    RuleConditionElement rce = null;
    // it might be a recursive query, so check for same names
    if (context.getRule().getName().equals(patternDescr.getObjectType())) {
        // it's a query so delegate to the QueryElementBuilder
        rce = buildQueryElement(context, descr, (QueryImpl) context.getRule());
    }
    if (rce == null) {
        // look up the query in the current package
        RuleImpl rule = context.getPkg().getRule(patternDescr.getObjectType());
        if (rule instanceof QueryImpl) {
            // it's a query so delegate to the QueryElementBuilder
            rce = buildQueryElement(context, descr, (QueryImpl) rule);
        }
    }
    if (rce == null) {
        // the query may have been imported, so try package imports
        for (String importName : context.getDialect().getTypeResolver().getImports()) {
            importName = importName.trim();
            int pos = importName.indexOf('*');
            if (pos >= 0) {
                String pkgName = importName.substring(0, pos - 1);
                PackageRegistry pkgReg = context.getKnowledgeBuilder().getPackageRegistry(pkgName);
                if (pkgReg != null) {
                    RuleImpl rule = pkgReg.getPackage().getRule(patternDescr.getObjectType());
                    if (rule instanceof QueryImpl) {
                        // it's a query so delegate to the QueryElementBuilder
                        rce = buildQueryElement(context, descr, (QueryImpl) rule);
                        break;
                    }
                }
            }
        }
    }
    if (rce == null) {
        // this isn't a query either, so log an error
        registerDescrBuildError(context, patternDescr, "Unable to resolve ObjectType '" + patternDescr.getObjectType() + "'");
    }
    return rce;
}
Also used : QueryImpl(org.drools.core.rule.QueryImpl) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) RuleConditionElement(org.drools.core.rule.RuleConditionElement) 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)

Example 72 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class RuleMetadataTest method testInsert2.

@Test
public void testInsert2() {
    String rule1 = "insert( new B(1, $ab) );";
    KieBase kbase = getKnowledgeBase(rule1);
    RuleImpl rule = getRule(kbase, "R0");
    ConsequenceMetaData consequenceMetaData = rule.getConsequenceMetaData();
    assertEquals(1, consequenceMetaData.getStatements().size());
    ConsequenceMetaData.Statement statment = consequenceMetaData.getStatements().get(0);
    assertEquals(ConsequenceMetaData.Statement.Type.INSERT, statment.getType());
    assertEquals(RuleMetadataTest.B.class.getName(), statment.getFactClassName());
}
Also used : ConsequenceMetaData(org.drools.core.rule.ConsequenceMetaData) KieBase(org.kie.api.KieBase) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 73 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class RuleMetadataTest method testInsert.

@Test
public void testInsert() {
    String rule1 = "insert( new A(1, $bb, \"3\") );";
    KieBase kbase = getKnowledgeBase(rule1);
    RuleImpl rule = getRule(kbase, "R0");
    ConsequenceMetaData consequenceMetaData = rule.getConsequenceMetaData();
    assertEquals(1, consequenceMetaData.getStatements().size());
    ConsequenceMetaData.Statement statment = consequenceMetaData.getStatements().get(0);
    assertEquals(ConsequenceMetaData.Statement.Type.INSERT, statment.getType());
    assertEquals("org.drools.A", statment.getFactClassName());
    assertEquals(3, statment.getFields().size());
    ConsequenceMetaData.Field field1 = statment.getFields().get(0);
    assertEquals("a", field1.getName());
    assertEquals("1", field1.getValue());
    assertTrue(field1.isLiteral());
    ConsequenceMetaData.Field field2 = statment.getFields().get(1);
    assertEquals("b", field2.getName());
    assertEquals("$bb", field2.getValue());
    assertFalse(field2.isLiteral());
    ConsequenceMetaData.Field field3 = statment.getFields().get(2);
    assertEquals("c", field3.getName());
    assertEquals("\"3\"", field3.getValue());
    assertTrue(field3.isLiteral());
}
Also used : ConsequenceMetaData(org.drools.core.rule.ConsequenceMetaData) KieBase(org.kie.api.KieBase) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Test(org.junit.Test)

Example 74 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class KnowledgeBuilderTest method testSerializable.

@Test
public void testSerializable() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
    ruleDescr.setConsequence("map.put(\"value\", new Integer(1) );");
    // check that packageDescr is serializable
    final PackageDescr back = (PackageDescr) SerializationHelper.serializeObject(packageDescr);
    assertNotNull(back);
    assertEquals("p1", back.getName());
    builder.addPackage(packageDescr);
    InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
    final RuleImpl rule = pkg.getRule("rule-1");
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgePackage newPkg = SerializationHelper.serializeObject(pkg);
    final RuleImpl newRule = newPkg.getRule("rule-1");
    InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
    // It's been serialised so we have to simulate the re-wiring process
    newPkg.getDialectRuntimeRegistry().onAdd(kBase.getRootClassLoader());
    newPkg.getDialectRuntimeRegistry().onBeforeExecute();
    kBase.getGlobals().put("map", Map.class);
    final KieSession workingMemory = kBase.newKieSession();
    final HashMap map = new HashMap();
    workingMemory.setGlobal("map", map);
    final LeftTupleImpl tuple = new MockTuple(new HashMap());
    tuple.setLeftTupleSink(new RuleTerminalNode(1, new CompositeObjectSinkAdapterTest.MockBetaNode(), newRule, newRule.getLhs(), 0, new BuildContext(kBase)));
    final Activation activation = new MockActivation(newRule, 0, newRule.getLhs(), tuple);
    final DefaultKnowledgeHelper knowledgeHelper = new org.drools.core.base.DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
    knowledgeHelper.setActivation(activation);
    newRule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
    assertEquals(new Integer(1), map.get("value"));
}
Also used : GlobalDescr(org.drools.compiler.lang.descr.GlobalDescr) HashMap(java.util.HashMap) AndDescr(org.drools.compiler.lang.descr.AndDescr) DefaultKnowledgeHelper(org.drools.core.base.DefaultKnowledgeHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Activation(org.drools.core.spi.Activation) BuildContext(org.drools.core.reteoo.builder.BuildContext) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 75 with RuleImpl

use of org.drools.core.definitions.rule.impl.RuleImpl in project drools by kiegroup.

the class KnowledgeBuilderTest method testTimeWindowBehavior.

@Test
public void testTimeWindowBehavior() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final TypeDeclarationDescr typeDeclDescr = new TypeDeclarationDescr(StockTick.class.getName());
    typeDeclDescr.addAnnotation("role", "event");
    packageDescr.addTypeDeclaration(typeDeclDescr);
    final RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    final AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    final PatternDescr patternDescr = new PatternDescr(StockTick.class.getName(), "$tick");
    final BehaviorDescr windowDescr = new BehaviorDescr("window");
    windowDescr.setSubType("time");
    windowDescr.setParameters(Collections.singletonList("60000"));
    patternDescr.addBehavior(windowDescr);
    lhs.addDescr(patternDescr);
    ruleDescr.setConsequence("System.out.println( $tick );");
    builder.addPackage(packageDescr);
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgePackage pkg = builder.getPackageRegistry().get("p1").getPackage();
    final RuleImpl rule = pkg.getRule("rule-1");
    assertNotNull(rule);
    final Pattern pattern = (Pattern) rule.getLhs().getChildren().get(0);
    assertEquals(StockTick.class.getName(), ((ClassObjectType) pattern.getObjectType()).getClassType().getName());
    final Behavior window = pattern.getBehaviors().get(0);
    assertEquals(Behavior.BehaviorType.TIME_WINDOW, window.getType());
    assertEquals(60000, ((SlidingTimeWindow) window).getSize());
}
Also used : Pattern(org.drools.core.rule.Pattern) TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) BehaviorDescr(org.drools.compiler.lang.descr.BehaviorDescr) ClassObjectType(org.drools.core.base.ClassObjectType) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) StockTick(org.drools.compiler.StockTick) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) Behavior(org.drools.core.rule.Behavior) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Aggregations

RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)107 Test (org.junit.Test)51 Pattern (org.drools.core.rule.Pattern)30 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)28 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)21 WorkingMemory (org.drools.core.WorkingMemory)21 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)18 BuildContext (org.drools.core.reteoo.builder.BuildContext)16 KnowledgeHelper (org.drools.core.spi.KnowledgeHelper)16 Consequence (org.drools.core.spi.Consequence)15 GroupElement (org.drools.core.rule.GroupElement)14 ClassObjectType (org.drools.core.base.ClassObjectType)13 InternalWorkingMemory (org.drools.core.common.InternalWorkingMemory)13 IOException (java.io.IOException)12 ObjectInput (java.io.ObjectInput)12 ObjectOutput (java.io.ObjectOutput)12 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)12 RuleTerminalNode (org.drools.core.reteoo.RuleTerminalNode)12 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)11 KnowledgeBaseImpl (org.drools.core.impl.KnowledgeBaseImpl)11