Search in sources :

Example 46 with PackageDescr

use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.

the class KnowledgeBuilderTest method testEval.

@Test
public void testEval() 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);
    final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
    lhs.addDescr(pattern);
    BindingDescr fieldBindingDescr = new BindingDescr("x", "price");
    pattern.addConstraint(fieldBindingDescr);
    fieldBindingDescr = new BindingDescr("y", "price");
    pattern.addConstraint(fieldBindingDescr);
    packageDescr.addGlobal(new GlobalDescr("map", "java.util.Map"));
    final EvalDescr evalDescr = new EvalDescr("( ( Integer )map.get( new Integer(x) ) ).intValue() == y");
    lhs.addDescr(evalDescr);
    ruleDescr.setConsequence("update(stilton);");
    builder.addPackage(packageDescr);
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgePackage pkg = builder.getPackage(packageDescr.getName());
    final RuleImpl rule = pkg.getRule("rule-1");
    final EvalCondition eval = (EvalCondition) rule.getLhs().getChildren().get(1);
    final CompiledInvoker invoker = (CompiledInvoker) eval.getEvalExpression();
    String s = invoker.getMethodBytecode();
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) BindingDescr(org.drools.drl.ast.descr.BindingDescr) PatternDescr(org.drools.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) Cheese(org.drools.mvel.compiler.Cheese) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) EvalCondition(org.drools.core.rule.EvalCondition) CompiledInvoker(org.drools.core.spi.CompiledInvoker) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) EvalDescr(org.drools.drl.ast.descr.EvalDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) PackageDescr(org.drools.drl.ast.descr.PackageDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 47 with PackageDescr

use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.

the class KnowledgeBuilderTest method testWarningsReportAsErrors.

@Test
public void testWarningsReportAsErrors() {
    System.setProperty("drools.kbuilder.severity." + DuplicateRule.KEY, "ERROR");
    KnowledgeBuilderConfigurationImpl cfg = new KnowledgeBuilderConfigurationImpl();
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl(cfg);
    final PackageDescr packageDescr1 = createBasicPackageWithOneRule(11, 1);
    final PackageDescr packageDescr2 = createBasicPackageWithOneRule(22, 2);
    builder.addPackage(packageDescr1);
    builder.addPackage(packageDescr2);
    assertTrue(builder.hasErrors());
    assertFalse(builder.hasWarnings());
}
Also used : KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Test(org.junit.Test)

Example 48 with PackageDescr

use of org.drools.drl.ast.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.drl.ast.descr.PatternDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) FieldConstraintDescr(org.drools.drl.ast.descr.FieldConstraintDescr) Cheese(org.drools.mvel.compiler.Cheese) PackageDescr(org.drools.drl.ast.descr.PackageDescr) LiteralRestrictionDescr(org.drools.drl.ast.descr.LiteralRestrictionDescr)

Example 49 with PackageDescr

use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.

the class KnowledgeBuilderTest method testTypeDeclaration.

@Test
public void testTypeDeclaration() throws Exception {
    PackageDescr pkgDescr = new PackageDescr("org.drools.mvel.compiler");
    TypeDeclarationDescr typeDescr = new TypeDeclarationDescr("StockTick");
    typeDescr.addAnnotation(Role.class.getCanonicalName(), "Event");
    typeDescr.addAnnotation(TypeSafe.class.getCanonicalName(), "true");
    pkgDescr.addTypeDeclaration(typeDescr);
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    builder.addPackage(pkgDescr);
    if (builder.hasErrors()) {
        fail(builder.getErrors().toString());
    }
    InternalKnowledgePackage pkg = builder.getPackage(pkgDescr.getName());
    assertEquals(1, pkg.getTypeDeclarations().size());
    TypeDeclaration type = pkg.getTypeDeclaration("StockTick");
    assertTrue(type.isTypesafe());
    assertEquals(Role.Type.EVENT, type.getRole());
    assertEquals(StockTick.class, type.getTypeClass());
}
Also used : Role(org.kie.api.definition.type.Role) TypeDeclarationDescr(org.drools.drl.ast.descr.TypeDeclarationDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) PackageDescr(org.drools.drl.ast.descr.PackageDescr) TypeDeclaration(org.drools.core.rule.TypeDeclaration) TypeSafe(org.kie.api.definition.type.TypeSafe) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 50 with PackageDescr

use of org.drools.drl.ast.descr.PackageDescr in project drools by kiegroup.

the class KnowledgeBuilderTest method testLiteral.

@Test
public void testLiteral() 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);
    final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
    lhs.addDescr(pattern);
    pattern.addConstraint(new ExprConstraintDescr("type == 'stilton'"));
    ruleDescr.setConsequence("update(stilton);");
    builder.addPackage(packageDescr);
    assertLength(0, builder.getErrors().getErrors());
}
Also used : PatternDescr(org.drools.drl.ast.descr.PatternDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) AndDescr(org.drools.drl.ast.descr.AndDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr) Cheese(org.drools.mvel.compiler.Cheese) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) 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