Search in sources :

Example 26 with KiePackage

use of org.kie.api.definition.KiePackage 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 27 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class KnowledgeBuilderTest method testResourceMapping.

@Ignore
@Test
public void testResourceMapping() throws Exception {
    ServiceRegistryImpl serviceRegistry = (ServiceRegistryImpl) ServiceRegistry.getInstance();
    serviceRegistry.reset();
    ServiceDiscoveryImpl.getInstance().addService(PMMLCompiler.class.getCanonicalName(), new PMMLCompiler() {

        public String compile(InputStream stream, ClassLoader cl) {
            return "rule R2 when then end";
        }

        @Override
        public List<KnowledgeBuilderResult> getResults() {
            return Collections.emptyList();
        }

        @Override
        public void clearResults() {
        // To change body of implemented methods use File | Settings | File Templates.
        }

        public Resource[] transform(Resource input, ClassLoader classLoader) {
            return new Resource[0];
        }

        public String getCompilerVersion() {
            return "KnowledgeBuilderTest";
        }

        @Override
        public List<PMMLResource> precompile(InputStream stream, ClassLoader classLoader, KieBaseModel rootModel) {
            return Collections.emptyList();
        }

        @Override
        public List<PMMLResource> precompile(String fileName, ClassLoader classLoader, KieBaseModel rootModel) {
            return Collections.emptyList();
        }

        @Override
        public Map<String, String> getJavaClasses(InputStream stream) {
            return Collections.emptyMap();
        }

        @Override
        public Map<String, String> getJavaClasses(String fileName) {
            return Collections.emptyMap();
        }
    });
    serviceRegistry.reload();
    String rule = "package org.drools.compiler.test\n" + "rule R1 when\n" + " \n" + "then\n" + "end\n";
    Resource res1 = ResourceFactory.newByteArrayResource(rule.getBytes());
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(res1, ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    KiePackage kp1 = kbuilder.getKnowledgePackages().iterator().next();
    assertEquals(1, kp1.getRules().size());
    Rule r = kp1.getRules().iterator().next();
    assertEquals(res1, ((RuleImpl) r).getResource());
    String pmml = "<PMML version=\"4.0\"><Header/></PMML>";
    Resource res2 = ResourceFactory.newByteArrayResource(pmml.getBytes());
    KnowledgeBuilder kbuilder2 = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder2.add(res2, ResourceType.PMML);
    assertFalse(kbuilder2.getErrors().toString(), kbuilder2.hasErrors());
    KiePackage kp2 = kbuilder2.getKnowledgePackages().iterator().next();
    assertEquals(1, kp2.getRules().size());
    Rule r2 = kp2.getRules().iterator().next();
    assertEquals(res2, ((RuleImpl) r2).getResource());
}
Also used : ServiceRegistryImpl(org.kie.api.internal.utils.ServiceRegistryImpl) InputStream(java.io.InputStream) PMMLCompiler(org.drools.compiler.compiler.PMMLCompiler) PMMLResource(org.drools.compiler.compiler.PMMLResource) Resource(org.kie.api.io.Resource) KieBaseModel(org.kie.api.builder.model.KieBaseModel) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) ArrayList(java.util.ArrayList) List(java.util.List) Rule(org.kie.api.definition.rule.Rule) Map(java.util.Map) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 28 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class KnowledgeBuilderTest method testAddKPackageSingle.

@Test
public void testAddKPackageSingle() throws Exception {
    String rule = "package org.drools.compiler.test\n" + "import org.drools.compiler.StockTick\n" + "rule R1 when\n" + "   StockTick()\n" + "then\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(rule.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
    assertEquals(2, kpkgs.size());
    KiePackage kpkg = kpkgs.iterator().next();
    byte[] skpkg = DroolsStreamUtils.streamOut(kpkg);
    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(skpkg), ResourceType.PKG);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    kpkgs = kbuilder.getKnowledgePackages();
    assertEquals(1, kpkgs.size());
    kpkg = kpkgs.iterator().next();
    assertEquals(1, kpkg.getRules().size());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) Test(org.junit.Test)

Example 29 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class KnowledgeBuilderTest method testAddKPackageCollection.

@Test
public void testAddKPackageCollection() throws Exception {
    String rule = "package org.drools.compiler.test\n" + "import org.drools.compiler.StockTick\n" + "declare StockTick @role(event) end\n" + "rule R1 when\n" + "   StockTick()\n" + "then\n" + "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(rule.getBytes()), ResourceType.DRL);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    Collection<KiePackage> kpkgs = kbuilder.getKnowledgePackages();
    assertEquals(2, kpkgs.size());
    byte[] skpkg = DroolsStreamUtils.streamOut(kpkgs);
    kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(skpkg), ResourceType.PKG);
    assertFalse(kbuilder.getErrors().toString(), kbuilder.hasErrors());
    kpkgs = kbuilder.getKnowledgePackages();
    assertEquals(2, kpkgs.size());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) Test(org.junit.Test)

Example 30 with KiePackage

use of org.kie.api.definition.KiePackage in project drools by kiegroup.

the class KnowledgeBuilderTest method testRepeatedDeclarationInMultiplePackages.

@Test
public void testRepeatedDeclarationInMultiplePackages() {
    String str = "package org.drools.test1;\n" + "import org.drools.compiler.Cheese;\n" + "" + "rule R\n" + "when Cheese() then end \n" + "";
    String str2 = "package org.drools.test2;\n" + "import org.drools.compiler.Cheese;\n" + "" + "rule S\n" + "when Cheese() then end \n" + "";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
    kbuilder.add(ResourceFactory.newByteArrayResource(str2.getBytes()), ResourceType.DRL);
    assertEquals(3, kbuilder.getKnowledgePackages().size());
    for (KiePackage kp : kbuilder.getKnowledgePackages()) {
        KnowledgePackageImpl kpi = (KnowledgePackageImpl) kp;
        TypeDeclaration cheez = kpi.getTypeDeclaration("Cheese");
        if ("org.drools.compiler".equals(kpi.getName())) {
            assertNotNull(cheez);
        } else {
            assertNull(cheez);
        }
    }
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) TypeDeclaration(org.drools.core.rule.TypeDeclaration) Test(org.junit.Test)

Aggregations

KiePackage (org.kie.api.definition.KiePackage)124 Test (org.junit.Test)93 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)80 KieSession (org.kie.api.runtime.KieSession)70 ArrayList (java.util.ArrayList)44 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)39 List (java.util.List)25 Cheese (org.drools.compiler.Cheese)22 Person (org.drools.compiler.Person)18 PackageDescr (org.drools.compiler.lang.descr.PackageDescr)17 InternalFactHandle (org.drools.core.common.InternalFactHandle)15 IteratorToList (org.drools.compiler.integrationtests.IteratorToList)11 EntryPoint (org.kie.api.runtime.rule.EntryPoint)10 Resource (org.kie.api.io.Resource)9 StringReader (java.io.StringReader)8 HashMap (java.util.HashMap)8 FactHandle (org.kie.api.runtime.rule.FactHandle)8 Map (java.util.Map)7 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)7 KieBase (org.kie.api.KieBase)7