Search in sources :

Example 61 with KiePackage

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

the class RuleRunner method runRules.

public void runRules(String[] rules, Object[] facts) {
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    for (int i = 0; i < rules.length; i++) {
        String ruleFile = rules[i];
        System.out.println("Loading file: " + ruleFile);
        kbuilder.add(ResourceFactory.newClassPathResource(ruleFile, RuleRunner.class), ResourceType.DRL);
    }
    Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
    kbase.addPackages(pkgs);
    KieSession ksession = kbase.newKieSession();
    for (int i = 0; i < facts.length; i++) {
        Object fact = facts[i];
        System.out.println("Inserting fact: " + fact);
        ksession.insert(fact);
    }
    ksession.fireAllRules();
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) KiePackage(org.kie.api.definition.KiePackage) KieSession(org.kie.api.runtime.KieSession) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 62 with KiePackage

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

the class DecisionTableTest method testDecisionTableWithDateAttributes.

private void testDecisionTableWithDateAttributes(final Resource decisionTable) {
    final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, decisionTable);
    final ArrayList<String> names = new ArrayList<>();
    final Collection<KiePackage> pkgs = kbase.getKiePackages();
    for (KiePackage kp : pkgs) {
        names.add(kp.getName());
    }
    Assertions.assertThat(names.contains(TestConstants.PACKAGE_FUNCTIONAL)).isTrue();
    Assertions.assertThat(names.contains(TestConstants.PACKAGE_TESTCOVERAGE_MODEL)).isTrue();
    final KiePackage kiePackage = (KiePackage) pkgs.toArray()[names.indexOf(TestConstants.PACKAGE_FUNCTIONAL)];
    Assertions.assertThat(kiePackage.getRules().size()).isEqualTo(3);
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieBase(org.kie.api.KieBase) ArrayList(java.util.ArrayList)

Example 63 with KiePackage

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

the class HeaderTest method testPMMLHeader.

@Test
public void testPMMLHeader() {
    String source = PMML4Helper.pmmlDefaultPackageName().replace(".", File.separator) + File.separator + "test_header.xml";
    boolean header = false;
    boolean timestamp = false;
    boolean appl = false;
    boolean descr = false;
    boolean copyright = false;
    boolean annotation = false;
    PMML4Compiler compiler = new PMML4Compiler();
    compiler.getHelper().setPack("org.kie.pmml.pmml_4_2.test");
    String theory = compiler.compile(source, null);
    BufferedReader reader = new BufferedReader(new StringReader(theory));
    try {
        String line = "";
        while ((line = reader.readLine()) != null) {
            line = line.trim();
            if (line.startsWith("// Imported PMML Model Theory"))
                header = true;
            else if (line.startsWith("// Creation timestamp :"))
                timestamp = line.contains("now");
            else if (line.startsWith("// Description :"))
                descr = line.contains("test");
            else if (line.startsWith("// Copyright :"))
                copyright = line.contains("opensource");
            else if (line.startsWith("// Annotation :"))
                annotation = line.contains("notes here");
            else if (line.startsWith("// Trained with :"))
                appl = line.contains("handmade");
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
        fail();
    }
    assertTrue(header);
    assertTrue(timestamp);
    assertTrue(descr);
    assertTrue(copyright);
    assertTrue(annotation);
    assertTrue(appl);
    KieSession ksession = getSession(theory);
    KiePackage pack = ksession.getKieBase().getKiePackage("org.kie.pmml.pmml_4_2.test");
    assertNotNull(pack);
    ksession.dispose();
}
Also used : KiePackage(org.kie.api.definition.KiePackage) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) PMML4Compiler(org.kie.pmml.pmml_4_2.PMML4Compiler) IOException(java.io.IOException) Test(org.junit.Test) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)

Example 64 with KiePackage

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

the class KieModuleMavenTest method testKieContainerBeforeAndAfterDeployOfSnapshot.

@Test
public void testKieContainerBeforeAndAfterDeployOfSnapshot() throws Exception {
    // BZ-1007977
    KieServices ks = KieServices.Factory.get();
    String group = "org.kie.test";
    String artifact = "test-module";
    String version = "1.0.0-SNAPSHOT";
    ReleaseId releaseId = ks.newReleaseId(group, artifact, version);
    String prefix = new File(".").getAbsolutePath().contains("kie-ci") ? "" : "kie-ci/";
    File kjar = new File(prefix + "src/test/resources/kjar/kjar-module-before.jar");
    File pom = new File(prefix + "src/test/resources/kjar/pom-kjar.xml");
    MavenRepository repository = getMavenRepository();
    repository.installArtifact(releaseId, kjar, pom);
    KieContainer kContainer = ks.newKieContainer(releaseId);
    KieBase kbase = kContainer.getKieBase();
    assertNotNull(kbase);
    Collection<KiePackage> packages = kbase.getKiePackages();
    assertNotNull(packages);
    assertEquals(1, packages.size());
    Collection<Rule> rules = packages.iterator().next().getRules();
    assertEquals(2, rules.size());
    ks.getRepository().removeKieModule(releaseId);
    // deploy new version
    File kjar1 = new File(prefix + "src/test/resources/kjar/kjar-module-after.jar");
    File pom1 = new File(prefix + "src/test/resources/kjar/pom-kjar.xml");
    repository.installArtifact(releaseId, kjar1, pom1);
    KieContainer kContainer2 = ks.newKieContainer(releaseId);
    KieBase kbase2 = kContainer2.getKieBase();
    assertNotNull(kbase2);
    Collection<KiePackage> packages2 = kbase2.getKiePackages();
    assertNotNull(packages2);
    assertEquals(1, packages2.size());
    Collection<Rule> rules2 = packages2.iterator().next().getRules();
    assertEquals(4, rules2.size());
    ks.getRepository().removeKieModule(releaseId);
}
Also used : MavenRepository.getMavenRepository(org.appformer.maven.integration.MavenRepository.getMavenRepository) MavenRepository(org.appformer.maven.integration.MavenRepository) KiePackage(org.kie.api.definition.KiePackage) KieBase(org.kie.api.KieBase) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) Rule(org.kie.api.definition.rule.Rule) File(java.io.File) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 65 with KiePackage

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

the class KieBaseIncludesTest method getNumberOfRules.

/**
 * Helper method which determines the number of rules in the {@link KieBase}.
 *
 * @param kieBase
 *            the {@link KieBase}
 * @return the number of rules in the {@link KieBase}
 */
private static long getNumberOfRules(KieBase kieBase) {
    long nrOfRules = 0;
    Collection<KiePackage> kiePackages = kieBase.getKiePackages();
    for (KiePackage nextKiePackage : kiePackages) {
        Collection<Rule> rules = nextKiePackage.getRules();
        System.out.println(rules);
        nrOfRules += rules.size();
    }
    return nrOfRules;
}
Also used : KiePackage(org.kie.api.definition.KiePackage) Rule(org.kie.api.definition.rule.Rule)

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