Search in sources :

Example 16 with KnowledgeBuilderImpl

use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.

the class JavaConsequenceBuilderTest method setupTest.

private void setupTest(String consequence, Map<String, Object> namedConsequences) {
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.drools");
    pkg.addImport(new ImportDeclaration("org.drools.compiler.Cheese"));
    KnowledgeBuilderConfigurationImpl conf = new KnowledgeBuilderConfigurationImpl();
    // this test was originally intended with PropertyReactive.ALLOWED:
    conf.setOption(PropertySpecificOption.ALLOWED);
    KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl(pkg, conf);
    ruleDescr = new RuleDescr("test consequence builder");
    ruleDescr.setConsequence(consequence);
    for (Entry<String, Object> entry : namedConsequences.entrySet()) {
        ruleDescr.addNamedConsequences(entry.getKey(), entry.getValue());
    }
    RuleImpl rule = ruleDescr.toRule();
    PackageRegistry pkgRegistry = kBuilder.getPackageRegistry(pkg.getName());
    DialectCompiletimeRegistry reg = kBuilder.getPackageRegistry(pkg.getName()).getDialectCompiletimeRegistry();
    context = new RuleBuildContext(kBuilder, ruleDescr, reg, pkg, reg.getDialect(pkgRegistry.getDialect()));
    rule.addPattern(new Pattern(0, new ClassObjectType(Cheese.class), "$cheese"));
    Pattern p = new Pattern(1, new ClassObjectType(Person.class), "$persone");
    Declaration declr = p.addDeclaration("age");
    final InternalReadAccessor extractor = PatternBuilder.getFieldReadAccessor(context, new BindingDescr("age", "age"), p, "age", declr, true);
    rule.addPattern(p);
    context.getDeclarationResolver().pushOnBuildStack(rule.getLhs());
    context.getDialect().getConsequenceBuilder().build(context, RuleImpl.DEFAULT_CONSEQUENCE_NAME);
    for (String name : namedConsequences.keySet()) {
        context.getDialect().getConsequenceBuilder().build(context, name);
    }
    context.getDialect().addRule(context);
    pkgRegistry.getPackage().addRule(context.getRule());
    kBuilder.compileAll();
    kBuilder.reloadAll();
}
Also used : Pattern(org.drools.core.rule.Pattern) BindingDescr(org.drools.compiler.lang.descr.BindingDescr) RuleBuildContext(org.drools.compiler.rule.builder.RuleBuildContext) ClassObjectType(org.drools.core.base.ClassObjectType) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) DialectCompiletimeRegistry(org.drools.compiler.compiler.DialectCompiletimeRegistry) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) ImportDeclaration(org.drools.core.rule.ImportDeclaration) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Declaration(org.drools.core.rule.Declaration) ImportDeclaration(org.drools.core.rule.ImportDeclaration) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) Person(org.drools.compiler.Person) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

Example 17 with KnowledgeBuilderImpl

use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.

the class DescrBuilderTest method compilePkgDescr.

private KiePackage compilePkgDescr(PackageDescr pkg, String pkgName) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newDescrResource(pkg), ResourceType.DESCR);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    if (pkgName == null) {
        Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
        assertEquals(1, kpkgs.size());
        return kpkgs.iterator().next();
    }
    return ((KnowledgeBuilderImpl) kbuilder).getPackage(pkgName);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl)

Example 18 with KnowledgeBuilderImpl

use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.

the class QueryBuilderTest method testQuery.

@Test
public void testQuery() throws Exception {
    final KnowledgeBuilderImpl builder = new KnowledgeBuilderImpl();
    final PackageDescr packageDescr = new PackageDescr("p1");
    final QueryDescr queryDescr = new QueryDescr("query1");
    queryDescr.addParameter("String", "$type");
    packageDescr.addRule(queryDescr);
    final AndDescr lhs = new AndDescr();
    queryDescr.setLhs(lhs);
    final PatternDescr pattern = new PatternDescr(Cheese.class.getName(), "stilton");
    lhs.addDescr(pattern);
    pattern.addConstraint(new ExprConstraintDescr("type == $type"));
    // Another query, no parameters
    QueryDescr queryDescr2 = new QueryDescr("query2");
    packageDescr.addRule(queryDescr2);
    AndDescr lhs2 = new AndDescr();
    queryDescr2.setLhs(lhs2);
    PatternDescr pattern2 = new PatternDescr(Cheese.class.getName());
    lhs2.addDescr(pattern2);
    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 Cheese("stilton", 15));
    QueryResults results = session.getQueryResults("query1", "stilton");
    assertEquals(1, results.size());
    Object object = results.iterator().next().get("stilton");
    assertEquals(new Cheese("stilton", 15), object);
    results = session.getQueryResults("query1", new Object[] { "cheddar" });
    assertEquals(0, results.size());
    session.insert(new Cheese("dolcelatte", 20));
    results = session.getQueryResults("query2", new Object[] {});
    assertEquals(2, results.size());
}
Also used : QueryDescr(org.drools.compiler.lang.descr.QueryDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) AndDescr(org.drools.compiler.lang.descr.AndDescr) Cheese(org.drools.compiler.Cheese) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) QueryResults(org.kie.api.runtime.rule.QueryResults) Test(org.junit.Test)

Example 19 with KnowledgeBuilderImpl

use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.

the class TypeDeclarationMergingTest method testOverrideFromParentClass.

@Test
public void testOverrideFromParentClass() {
    // inherits role, but not typesafe
    String str = "" + "package org.drools.compiler.test \n" + "global java.util.List list \n" + "declare " + CImpl.class.getCanonicalName() + "\n" + "    @typesafe(true)\n" + "    @role(event)\n" + "end\n" + "declare " + DImpl.class.getCanonicalName() + "\n" + "    @typesafe(false)\n" + "end\n";
    KnowledgeBuilderImpl builder = getPackageBuilder(str);
    TypeDeclaration tdecl = builder.getTypeDeclaration(DImpl.class);
    assertEquals(false, tdecl.isTypesafe());
    assertEquals(Role.Type.EVENT, tdecl.getRole());
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Test(org.junit.Test)

Example 20 with KnowledgeBuilderImpl

use of org.drools.compiler.builder.impl.KnowledgeBuilderImpl in project drools by kiegroup.

the class TypeDeclarationMergingTest method testInheritExitenceFromParentClass.

@Test
public void testInheritExitenceFromParentClass() {
    // inherits role and typesafe
    String str = "" + "package org.drools.compiler.test \n" + "global java.util.List list \n" + "declare " + CImpl.class.getCanonicalName() + "\n" + "    @typesafe(true)\n" + "    @role(event)\n" + "end\n" + "declare " + DImpl.class.getCanonicalName() + "\n" + "end\n";
    KnowledgeBuilderImpl builder = getPackageBuilder(str);
    TypeDeclaration tdecl = builder.getTypeDeclaration(DImpl.class);
    assertEquals(true, tdecl.isTypesafe());
    assertEquals(Role.Type.EVENT, tdecl.getRole());
}
Also used : KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Test(org.junit.Test)

Aggregations

KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)51 Test (org.junit.Test)33 RuleDescr (org.drools.compiler.lang.descr.RuleDescr)20 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)20 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)17 RuleBuildContext (org.drools.compiler.rule.builder.RuleBuildContext)12 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)11 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)11 Pattern (org.drools.core.rule.Pattern)11 TypeDeclaration (org.drools.core.rule.TypeDeclaration)11 DialectCompiletimeRegistry (org.drools.compiler.compiler.DialectCompiletimeRegistry)10 PackageRegistry (org.drools.compiler.compiler.PackageRegistry)9 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)9 KnowledgeBuilderConfigurationImpl (org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl)8 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)7 HashMap (java.util.HashMap)6 Cheese (org.drools.compiler.Cheese)6 AttributeDescr (org.drools.compiler.lang.descr.AttributeDescr)6 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)6 ClassObjectType (org.drools.core.base.ClassObjectType)6