Search in sources :

Example 76 with KnowledgeBuilderImpl

use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.

the class KnowledgeBuilderTest method testWarnOnFunctionReplacement.

@Test
public void testWarnOnFunctionReplacement() throws DroolsParserException, IOException {
    System.setProperty("drools.kbuilder.severity." + DuplicateFunction.KEY, "WARNING");
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    builder.addPackageFromDrl(new StringReader("package org.drools\n" + "function boolean testIt() {\n" + "  return true;\n" + "}\n"));
    builder.addPackageFromDrl(new StringReader("package org.drools\n" + "function boolean testIt() {\n" + "  return false;\n" + "}\n"));
    assertTrue(builder.hasWarnings());
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 77 with KnowledgeBuilderImpl

use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.

the class KnowledgeBuilderTest method testOr.

@Test
public void testOr() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final RuleImpl rule = createRule(new OrDescr(), builder, "update(stilton);");
    assertLength(0, builder.getErrors().getErrors());
    final GroupElement lhs = rule.getLhs();
    assertLength(1, lhs.getChildren());
    final GroupElement or = (GroupElement) lhs.getChildren().get(0);
    assertLength(1, or.getChildren());
    final Pattern pattern = (Pattern) or.getChildren().get(0);
}
Also used : Pattern(org.drools.core.rule.Pattern) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) GroupElement(org.drools.core.rule.GroupElement) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) OrDescr(org.drools.drl.ast.descr.OrDescr) Test(org.junit.Test)

Example 78 with KnowledgeBuilderImpl

use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.

the class KnowledgeBuilderTest method testResetProblems.

@Test
public void testResetProblems() throws DroolsParserException, IOException {
    System.setProperty("drools.kbuilder.severity." + DuplicateRule.KEY, "WARNING");
    System.setProperty("drools.kbuilder.severity." + DuplicateFunction.KEY, "ERROR");
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr1 = createBasicPackageWithOneRule(11, 1);
    final PackageDescr packageDescr2 = createBasicPackageWithOneRule(22, 2);
    builder.addPackage(packageDescr1);
    builder.addPackage(packageDescr2);
    builder.addPackageFromDrl(new StringReader("package foo \n rule ORB"));
    builder.addPackageFromDrl(new StringReader("package org.drools\n" + "function boolean testIt() {\n" + "  return true;\n" + "}\n"));
    builder.addPackageFromDrl(new StringReader("package org.drools\n" + "function boolean testIt() {\n" + "  return false;\n" + "}\n"));
    assertTrue(builder.hasWarnings());
    assertTrue(builder.hasErrors());
    builder.resetProblems();
    assertFalse(builder.hasWarnings());
    assertFalse(builder.hasErrors());
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) StringReader(java.io.StringReader) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 79 with KnowledgeBuilderImpl

use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.

the class KnowledgeBuilderTest method testReturnValueMethodCompare.

@Test
public void testReturnValueMethodCompare() {
    final KnowledgeBuilderImpl builder1 = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr1 = new PackageDescr("package1");
    createReturnValueRule(packageDescr1, " x + y ");
    builder1.addPackage(packageDescr1);
    if (builder1.hasErrors()) {
        fail(builder1.getErrors().toString());
    }
    final Pattern pattern1 = (Pattern) ((RuleImpl) builder1.getPackage("package1").getRules().iterator().next()).getLhs().getChildren().get(0);
    final Constraint returnValue1 = pattern1.getConstraints().get(0);
    final KnowledgeBuilderImpl builder2 = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr2 = new PackageDescr("package2");
    createReturnValueRule(packageDescr2, " x + y ");
    builder2.addPackage(packageDescr2);
    final Pattern pattern2 = (Pattern) ((RuleImpl) builder2.getPackage("package2").getRules().iterator().next()).getLhs().getChildren().get(0);
    final Constraint returnValue2 = pattern2.getConstraints().get(0);
    final KnowledgeBuilderImpl builder3 = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr3 = new PackageDescr("package3");
    createReturnValueRule(packageDescr3, " x - y ");
    builder3.addPackage(packageDescr3);
    final Pattern pattern3 = (Pattern) ((RuleImpl) builder3.getPackage("package3").getRules().iterator().next()).getLhs().getChildren().get(0);
    final Constraint returnValue3 = pattern3.getConstraints().get(0);
    assertEquals(returnValue1, returnValue2);
    assertFalse(returnValue1.equals(returnValue3));
    assertFalse(returnValue2.equals(returnValue3));
}
Also used : Pattern(org.drools.core.rule.Pattern) PredicateConstraint(org.drools.core.rule.PredicateConstraint) Constraint(org.drools.core.spi.Constraint) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 80 with KnowledgeBuilderImpl

use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl 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 = 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 MockBetaNode(), newRule, newRule.getLhs(), 0, new BuildContext(kBase, Collections.emptyList())));
    final Activation activation = new MockActivation(newRule, 0, newRule.getLhs(), tuple);
    final DefaultKnowledgeHelper knowledgeHelper = new DefaultKnowledgeHelper(((StatefulKnowledgeSessionImpl) workingMemory));
    knowledgeHelper.setActivation(activation);
    newRule.getConsequence().evaluate(knowledgeHelper, ((StatefulKnowledgeSessionImpl) workingMemory));
    assertEquals(new Integer(1), map.get("value"));
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) HashMap(java.util.HashMap) AndDescr(org.drools.drl.ast.descr.AndDescr) DefaultKnowledgeHelper(org.drools.kiesession.consequence.DefaultKnowledgeHelper) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Activation(org.drools.core.spi.Activation) BuildContext(org.drools.core.reteoo.builder.BuildContext) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) MockBetaNode(org.drools.mvel.MockBetaNode) StatefulKnowledgeSessionImpl(org.drools.kiesession.session.StatefulKnowledgeSessionImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) LeftTupleImpl(org.drools.core.reteoo.LeftTupleImpl) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) RuleTerminalNode(org.drools.core.reteoo.RuleTerminalNode) Test(org.junit.Test)

Aggregations

KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)178 Test (org.junit.Test)122 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)56 PackageDescr (org.drools.drl.ast.descr.PackageDescr)50 RuleDescr (org.drools.drl.ast.descr.RuleDescr)32 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)30 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)29 Pattern (org.drools.core.rule.Pattern)28 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)24 StringReader (java.io.StringReader)23 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)23 HashMap (java.util.HashMap)20 TypeDeclaration (org.drools.core.rule.TypeDeclaration)20 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)19 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)19 AndDescr (org.drools.drl.ast.descr.AndDescr)18 PatternDescr (org.drools.drl.ast.descr.PatternDescr)18 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)18 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)17 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)16