Search in sources :

Example 16 with AndDescr

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

the class PackageBuilderConfigurationTest method testMockDialect.

@Test
public void testMockDialect() {
    InternalKnowledgePackage pkg = CoreComponentFactory.get().createKnowledgePackage("org.pkg1");
    KnowledgeBuilderConfigurationImpl cfg1 = new KnowledgeBuilderConfigurationImpl();
    MockDialectConfiguration mockConf = new MockDialectConfiguration();
    // cfg1.buildDialectRegistry().addDialect( "mock",
    // mockConf.getDialect() );
    cfg1.addDialect("mock", mockConf);
    cfg1.setDefaultDialect("mock");
    KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl(pkg, cfg1);
    PackageRegistry pkgRegistry = builder.getPackageRegistry(pkg.getName());
    DialectCompiletimeRegistry dialectRegistry = pkgRegistry.getDialectCompiletimeRegistry();
    MockDialect mockDialect2 = (MockDialect) dialectRegistry.getDialect(cfg1.getDefaultDialect());
    assertSame(pkg, mockDialect2.getPkg());
    assertNull(mockDialect2.getRuleDescr());
    RuleDescr ruleDescr = new RuleDescr("test rule");
    ruleDescr.addAttribute(new AttributeDescr("dialect", "mock"));
    ruleDescr.setLhs(new AndDescr());
    EvalDescr evalDescr = new EvalDescr();
    ruleDescr.getLhs().addDescr(evalDescr);
    PackageDescr pkgDescr = new PackageDescr("org.pkg1");
    pkgDescr.addImport(new ImportDescr("java.util.HashMap"));
    FunctionImportDescr functionImportDescr = new FunctionImportDescr();
    functionImportDescr.setTarget("java.lang.System.currentTimeMillis");
    pkgDescr.addFunctionImport(functionImportDescr);
    pkgDescr.addRule(ruleDescr);
    builder.addPackage(pkgDescr);
    assertSame(ruleDescr, mockDialect2.getRuleDescr());
    assertTrue(mockDialect2.getImport().contains("java.util.HashMap"));
    assertTrue(mockDialect2.getStaticImport().contains("java.lang.System.currentTimeMillis"));
    assertEquals("eval was built", evalDescr.getContent());
    assertEquals("consequence was built", ruleDescr.getConsequence());
    assertTrue(mockDialect2.isCompileAll());
    assertNotNull(pkg.getRule("test rule"));
    // make sure there were no other general errors.
    assertFalse(builder.hasErrors());
}
Also used : FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) AndDescr(org.drools.drl.ast.descr.AndDescr) ImportDescr(org.drools.drl.ast.descr.ImportDescr) FunctionImportDescr(org.drools.drl.ast.descr.FunctionImportDescr) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) 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) AttributeDescr(org.drools.drl.ast.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 17 with AndDescr

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

the class KnowledgeBuilderTest method createEvalRule.

private void createEvalRule(final PackageDescr packageDescr, final String expression) {
    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"));
    final EvalDescr evalDescr = new EvalDescr(expression);
    lhs.addDescr(evalDescr);
    ruleDescr.setConsequence("");
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) AndDescr(org.drools.drl.ast.descr.AndDescr) EvalDescr(org.drools.drl.ast.descr.EvalDescr) RuleDescr(org.drools.drl.ast.descr.RuleDescr)

Example 18 with AndDescr

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

the class KnowledgeBuilderTest method createLiteralRule.

private void createLiteralRule(final BaseDescr descr) {
    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(Primitives.class.getName());
    lhs.addDescr(pattern);
    pattern.addConstraint(descr);
    ruleDescr.setConsequence("");
    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) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Primitives(org.drools.mvel.compiler.Primitives)

Example 19 with AndDescr

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

the class KnowledgeBuilderTest method testReturnValue.

@Test
public void testReturnValue() 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"));
    pattern.addConstraint(new ExprConstraintDescr("price == (( (Integer) map.get( new Integer( x )) ).intValue() * y)"));
    ruleDescr.setConsequence("update(stilton);");
    builder.addPackage(packageDescr);
    assertEquals("Should not have any errors", 0, builder.getErrors().getErrors().length);
}
Also used : GlobalDescr(org.drools.drl.ast.descr.GlobalDescr) BindingDescr(org.drools.drl.ast.descr.BindingDescr) 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)

Example 20 with AndDescr

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

the class PatternBuilderForQueryTest method testRuleWithQuery.

@Test
public void testRuleWithQuery() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    QueryDescr queryDescr = new QueryDescr("query1");
    queryDescr.addParameter("String", "$name");
    queryDescr.addParameter("int", "$age");
    queryDescr.addParameter("String", "$likes");
    packageDescr.addRule(queryDescr);
    AndDescr lhs = new AndDescr();
    queryDescr.setLhs(lhs);
    PatternDescr pattern = new PatternDescr(Person.class.getName());
    lhs.addDescr(pattern);
    pattern.addConstraint(new BindingDescr("$name", "name", true));
    pattern.addConstraint(new BindingDescr("$age", "age", true));
    pattern.addConstraint(new BindingDescr("$likes", "likes", true));
    RuleDescr ruleDescr = new RuleDescr("rule-1");
    packageDescr.addRule(ruleDescr);
    lhs = new AndDescr();
    ruleDescr.setLhs(lhs);
    pattern = new PatternDescr(Cheese.class.getName());
    lhs.addDescr(pattern);
    pattern.addConstraint(new BindingDescr("$type", "type"));
    pattern = new PatternDescr("query1");
    pattern.setQuery(true);
    lhs.addDescr(pattern);
    ExprConstraintDescr expr = new ExprConstraintDescr("'bobba'");
    expr.setPosition(0);
    expr.setType(Type.POSITIONAL);
    pattern.addConstraint(expr);
    expr = new ExprConstraintDescr("$age");
    expr.setPosition(1);
    expr.setType(Type.POSITIONAL);
    pattern.addConstraint(expr);
    expr = new ExprConstraintDescr("$type");
    expr.setPosition(2);
    expr.setType(Type.POSITIONAL);
    pattern.addConstraint(expr);
    ruleDescr.setConsequence("System.out.println(\"age: \" + $age);");
    builder.addPackage(packageDescr);
    assertLength(0, builder.getErrors().getErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(Arrays.asList(new KiePackage[] { builder.getPackage(packageDescr.getName()) }));
    final KieSession session = kbase.newKieSession();
    session.insert(new Person("bobba", "stilton", 90));
    session.insert(new Person("bobba", "brie", 80));
    session.insert(new Person("bobba", "brie", 75));
    session.insert(new Person("darth", "brie", 100));
    session.insert(new Person("luke", "brie", 25));
    session.insert(new Cheese("brie", 25));
    session.fireAllRules();
}
Also used : 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) QueryDescr(org.drools.drl.ast.descr.QueryDescr) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) RuleDescr(org.drools.drl.ast.descr.RuleDescr) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.drl.ast.descr.PackageDescr) Person(org.drools.mvel.compiler.Person) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase) Test(org.junit.Test)

Aggregations

AndDescr (org.drools.drl.ast.descr.AndDescr)88 Test (org.junit.Test)61 PatternDescr (org.drools.drl.ast.descr.PatternDescr)60 RuleDescr (org.drools.drl.ast.descr.RuleDescr)54 PackageDescr (org.drools.drl.ast.descr.PackageDescr)39 ExprConstraintDescr (org.drools.drl.ast.descr.ExprConstraintDescr)34 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)18 BaseDescr (org.drools.drl.ast.descr.BaseDescr)18 Cheese (org.drools.mvel.compiler.Cheese)13 ConditionalElementDescr (org.drools.drl.ast.descr.ConditionalElementDescr)11 GlobalDescr (org.drools.drl.ast.descr.GlobalDescr)10 OrDescr (org.drools.drl.ast.descr.OrDescr)10 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)9 BindingDescr (org.drools.drl.ast.descr.BindingDescr)8 NotDescr (org.drools.drl.ast.descr.NotDescr)8 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)7 QueryDescr (org.drools.drl.ast.descr.QueryDescr)6 RuleDescrBuilder (org.drools.drl.ast.dsl.RuleDescrBuilder)5 FieldConstraintDescr (org.drools.drl.ast.descr.FieldConstraintDescr)4 LiteralRestrictionDescr (org.drools.drl.ast.descr.LiteralRestrictionDescr)4