use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testResetWarnings.
@Test
public void testResetWarnings() {
System.setProperty("drools.kbuilder.severity." + DuplicateRule.KEY, "WARNING");
final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
final PackageDescr packageDescr1 = createBasicPackageWithOneRule(11, 1);
final PackageDescr packageDescr2 = createBasicPackageWithOneRule(22, 2);
builder.addPackage(packageDescr1);
builder.addPackage(packageDescr2);
assertTrue(builder.hasWarnings());
builder.resetWarnings();
assertFalse(builder.hasWarnings());
builder.addPackage(packageDescr1);
assertTrue(builder.hasWarnings());
}
use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.
the class KnowledgeBuilderTest method testTypeDeclarationNewBean.
@Test
public void testTypeDeclarationNewBean() throws Exception {
PackageDescr pkgDescr = new PackageDescr("org.drools.mvel.compiler.test");
TypeDeclarationDescr typeDescr = new TypeDeclarationDescr("NewBean");
TypeFieldDescr f1 = new TypeFieldDescr("name", new PatternDescr("String"));
TypeFieldDescr f2 = new TypeFieldDescr("age", new PatternDescr("int"));
typeDescr.addField(f1);
typeDescr.addField(f2);
pkgDescr.addTypeDeclaration(typeDescr);
KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
builder.addPackage(pkgDescr);
InternalKnowledgePackage pkg = builder.getPackage(pkgDescr.getName());
assertEquals(1, pkg.getTypeDeclarations().size());
TypeDeclaration type = pkg.getTypeDeclaration("NewBean");
assertEquals("NewBean", type.getTypeName());
assertEquals(Role.Type.FACT, type.getRole());
assertEquals("org.drools.mvel.compiler.test.NewBean", type.getTypeClass().getName());
assertFalse(builder.hasErrors());
InternalKnowledgePackage bp = builder.getPackage(pkgDescr.getName());
Class newBean = bp.getPackageClassLoader().loadClass("org.drools.mvel.compiler.test.NewBean");
assertNotNull(newBean);
}
use of org.drools.drl.ast.descr.PackageDescr 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.drl.ast.descr.PackageDescr 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.drl.ast.descr.PackageDescr 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