Search in sources :

Example 36 with KnowledgeBuilderImpl

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

the class PackageBuilderConfigurationTest method testMockDialect.

@Test
public void testMockDialect() {
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("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.compiler.lang.descr.FunctionImportDescr) KnowledgeBuilderConfigurationImpl(org.drools.compiler.builder.impl.KnowledgeBuilderConfigurationImpl) AndDescr(org.drools.compiler.lang.descr.AndDescr) ImportDescr(org.drools.compiler.lang.descr.ImportDescr) FunctionImportDescr(org.drools.compiler.lang.descr.FunctionImportDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) EvalDescr(org.drools.compiler.lang.descr.EvalDescr) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 37 with KnowledgeBuilderImpl

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

the class QueryBuilderTest 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.compiler.lang.descr.BindingDescr) PatternDescr(org.drools.compiler.lang.descr.PatternDescr) AndDescr(org.drools.compiler.lang.descr.AndDescr) Cheese(org.drools.compiler.Cheese) QueryDescr(org.drools.compiler.lang.descr.QueryDescr) KiePackage(org.kie.api.definition.KiePackage) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) RuleDescr(org.drools.compiler.lang.descr.RuleDescr) KieSession(org.kie.api.runtime.KieSession) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) Person(org.drools.compiler.Person) ExprConstraintDescr(org.drools.compiler.lang.descr.ExprConstraintDescr) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 38 with KnowledgeBuilderImpl

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

the class TypeDeclarationMergingTest method testOverrideFromParentInterface.

@Test
public void testOverrideFromParentInterface() {
    // inherits role but not typesafe
    String str = "" + "package org.drools.compiler.test \n" + "global java.util.List list \n" + "declare " + IB.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 39 with KnowledgeBuilderImpl

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

the class TypeDeclarationMergingTest method getPackageBuilder.

private KnowledgeBuilderImpl getPackageBuilder(String str) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    return (KnowledgeBuilderImpl) kbuilder;
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl)

Example 40 with KnowledgeBuilderImpl

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

the class BayesAssemblerService method addResource.

@Override
public void addResource(Object kbuilder, Resource resource, ResourceType type, ResourceConfiguration configuration) throws Exception {
    BayesNetwork network;
    JunctionTreeBuilder builder;
    KnowledgeBuilder kb = (KnowledgeBuilder) kbuilder;
    Bif bif = XmlBifParser.loadBif(resource, kb.getErrors());
    if (bif == null) {
        return;
    }
    try {
        network = XmlBifParser.buildBayesNetwork(bif);
    } catch (Exception e) {
        kb.getErrors().add(new BayesNetworkAssemblerError(resource, "Unable to parse opening Stream:\n" + e.toString()));
        return;
    }
    try {
        builder = new JunctionTreeBuilder(network);
    } catch (Exception e) {
        kb.getErrors().add(new BayesNetworkAssemblerError(resource, "Unable to build Junction Tree:\n" + e.toString()));
        return;
    }
    KnowledgeBuilderImpl kbuilderImpl = (KnowledgeBuilderImpl) kbuilder;
    PackageRegistry pkgReg = kbuilderImpl.getOrCreatePackageRegistry(new PackageDescr(network.getPackageName()));
    InternalKnowledgePackage kpkgs = pkgReg.getPackage();
    Map<ResourceType, ResourceTypePackage> rpkg = kpkgs.getResourceTypePackages();
    BayesPackage bpkg = (BayesPackage) rpkg.get(ResourceType.BAYES);
    if (bpkg == null) {
        bpkg = new BayesPackage();
        rpkg.put(ResourceType.BAYES, bpkg);
    }
    bpkg.addJunctionTree(network.getName(), builder.build());
}
Also used : JunctionTreeBuilder(org.drools.beliefs.bayes.JunctionTreeBuilder) ResourceType(org.kie.api.io.ResourceType) BayesNetwork(org.drools.beliefs.bayes.BayesNetwork) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) PackageRegistry(org.drools.compiler.compiler.PackageRegistry) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) PackageDescr(org.drools.compiler.lang.descr.PackageDescr) ResourceTypePackage(org.kie.api.internal.io.ResourceTypePackage) Bif(org.drools.beliefs.bayes.model.Bif) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage)

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