Search in sources :

Example 86 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.

the class KnowledgeBuilderTest method createBasicPackageWithOneRule.

private PackageDescr createBasicPackageWithOneRule(int line, int col) {
    PackageDescr packageDescr = new PackageDescr("p1");
    RuleDescr ruleDescr = new RuleDescr("rule-1");
    ruleDescr.setLocation(line, col);
    packageDescr.addRule(ruleDescr);
    AndDescr lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    PatternDescr patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
    FieldConstraintDescr literalDescr = new FieldConstraintDescr("type");
    literalDescr.addRestriction(new LiteralRestrictionDescr("==", null));
    patternDescr.addConstraint(literalDescr);
    ruleDescr.setConsequence("");
    return packageDescr;
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) FieldConstraintDescr(org.drools.compiler.lang.descr.FieldConstraintDescr) Cheese(org.drools.compiler.Cheese) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) LiteralRestrictionDescr(org.drools.compiler.lang.descr.LiteralRestrictionDescr)

Example 87 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.

the class KnowledgeBuilderTest method testTypeDeclarationNewBean.

@Test
public void testTypeDeclarationNewBean() throws Exception {
    PackageDescr pkgDescr = new PackageDescr("org.drools.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.compiler.test.NewBean", type.getTypeClass().getName());
    assertFalse(builder.hasErrors());
    InternalKnowledgePackage bp = builder.getPackage(pkgDescr.getName());
    Class newBean = bp.getPackageClassLoader().loadClass("org.drools.compiler.test.NewBean");
    assertNotNull(newBean);
}
Also used : TypeDeclarationDescr(org.drools.compiler.lang.descr.TypeDeclarationDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) TypeFieldDescr(org.drools.compiler.lang.descr.TypeFieldDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 88 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.

the class KnowledgeBuilderTest method testDuplicateRuleNames.

@Test
public void testDuplicateRuleNames() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    RuleDescr ruleDescr;
    AndDescr lhs;
    PatternDescr patternDescr;
    FieldConstraintDescr literalDescr;
    final PackageDescr packageDescr = createBasicPackageWithOneRule(1, 1);
    ruleDescr = new RuleDescr("rule-1");
    ruleDescr.setLocation(42, 43);
    packageDescr.addRule(ruleDescr);
    lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
    literalDescr = new FieldConstraintDescr("type");
    literalDescr.addRestriction(new LiteralRestrictionDescr("!=", null));
    patternDescr.addConstraint(literalDescr);
    ruleDescr.setConsequence("");
    ruleDescr = new RuleDescr("rule-2");
    ruleDescr.setLocation(42, 43);
    packageDescr.addRule(ruleDescr);
    lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    patternDescr = new PatternDescr(Cheese.class.getName(), "stilton");
    literalDescr = new FieldConstraintDescr("type");
    literalDescr.addRestriction(new LiteralRestrictionDescr("!=", null));
    patternDescr.addConstraint(literalDescr);
    ruleDescr.setConsequence("");
    builder.addPackage(packageDescr);
    assertLength(2, builder.getErrors().getErrors());
    final ParserError err = (ParserError) builder.getErrors().getErrors()[0];
    assertEquals(42, err.getRow());
    assertEquals(43, err.getCol());
}
Also used : PatternDescr(org.drools.compiler.lang.descr.PatternDescr) ParserError(org.drools.compiler.compiler.ParserError) AndDescr(org.drools.compiler.lang.descr.AndDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) FieldConstraintDescr(org.drools.compiler.lang.descr.FieldConstraintDescr) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) LiteralRestrictionDescr(org.drools.compiler.lang.descr.LiteralRestrictionDescr) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 89 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.

the class KnowledgeBuilderTest method testWarnings.

@Test
public void testWarnings() {
    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);
    assertFalse(builder.hasErrors());
    assertTrue(builder.hasWarnings());
}
Also used : PackageDescr(org.drools.compiler.lang.descr.PackageDescr) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Example 90 with PackageDescr

use of org.drools.compiler.lang.descr.PackageDescr in project drools by kiegroup.

the class KnowledgeBuilderTest method testEvalMethodCompare.

@Test
public void testEvalMethodCompare() {
    final KnowledgeBuilderImpl builder1 = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr1 = new PackageDescr("package1");
    createEvalRule(packageDescr1, "1==1");
    builder1.addPackage(packageDescr1);
    final EvalCondition eval1 = (EvalCondition) ((RuleImpl) builder1.getPackage("package1").getRules().iterator().next()).getLhs().getChildren().get(0);
    final KnowledgeBuilderImpl builder2 = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr2 = new PackageDescr("package2");
    createEvalRule(packageDescr2, "1==1");
    builder2.addPackage(packageDescr2);
    final EvalCondition eval2 = (EvalCondition) ((RuleImpl) builder2.getPackage("package2").getRules().iterator().next()).getLhs().getChildren().get(0);
    final KnowledgeBuilderImpl builder3 = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr3 = new PackageDescr("package3");
    createEvalRule(packageDescr3, "1==3");
    builder3.addPackage(packageDescr3);
    final EvalCondition eval3 = (EvalCondition) ((RuleImpl) builder3.getPackage("package3").getRules().iterator().next()).getLhs().getChildren().get(0);
    assertEquals(eval1, eval2);
    assertFalse(eval1.equals(eval3));
    assertFalse(eval2.equals(eval3));
}
Also used : PackageDescr(org.drools.compiler.lang.descr.PackageDescr) EvalCondition(org.drools.core.rule.EvalCondition) CompositeObjectSinkAdapterTest(org.drools.core.reteoo.CompositeObjectSinkAdapterTest) Test(org.junit.Test)

Aggregations

PackageDescr (org.drools.compiler.lang.descr.PackageDescr)239 Test (org.junit.Test)192 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)103 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)85 DrlParser (org.drools.compiler.compiler.DrlParser)44 AndDescr (org.drools.compiler.lang.descr.AndDescr)39 CompositeObjectSinkAdapterTest (org.drools.core.reteoo.CompositeObjectSinkAdapterTest)27 InputStreamReader (java.io.InputStreamReader)25 ExprConstraintDescr (org.drools.compiler.lang.descr.ExprConstraintDescr)23 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)22 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)19 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)19 KiePackage (org.kie.api.definition.KiePackage)18 XmlPackageReader (org.drools.compiler.compiler.xml.XmlPackageReader)17 Cheese (org.drools.compiler.Cheese)16 KieSession (org.kie.api.runtime.KieSession)16 List (java.util.List)15 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)15 GlobalDescr (org.drools.compiler.lang.descr.GlobalDescr)15 DrlDumper (org.drools.compiler.lang.DrlDumper)14