Search in sources :

Example 51 with PackageDescr

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());
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 52 with PackageDescr

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);
}
Also used : TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) TypeFieldDescr(org.drools.drl.ast.descr.TypeFieldDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) PackageDescr(org.drools.drl.ast.descr.PackageDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 53 with PackageDescr

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());
}
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 54 with PackageDescr

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));
}
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 55 with PackageDescr

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

PackageDescr (org.drools.drl.ast.descr.PackageDescr)262 Test (org.junit.Test)210 RuleDescr (org.drools.drl.ast.descr.RuleDescr)104 PatternDescr (org.drools.drl.ast.descr.PatternDescr)85 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)52 AndDescr (org.drools.drl.ast.descr.AndDescr)39 DrlParser (org.drools.drl.parser.DrlParser)39 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)25 InputStreamReader (java.io.InputStreamReader)23 CompositePackageDescr (org.drools.compiler.lang.descr.CompositePackageDescr)23 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)23 KieSession (org.kie.api.runtime.KieSession)19 List (java.util.List)18 InternalKnowledgeBase (org.drools.kiesession.rulebase.InternalKnowledgeBase)18 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)17 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)17 KiePackage (org.kie.api.definition.KiePackage)17 DrlDumper (org.drools.mvel.DrlDumper)16 Cheese (org.drools.mvel.compiler.Cheese)16 AccumulateDescr (org.drools.drl.ast.descr.AccumulateDescr)15