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());
}
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);
}
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());
}
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));
}
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"));
}
Aggregations