Search in sources :

Example 56 with KiePackage

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

the class ResourcesTest method verifyPackageWithRules.

private void verifyPackageWithRules(final KieBase kbase, final String packageName, final int expectedRules) {
    final KiePackage pack = kbase.getKiePackage(packageName);
    Assertions.assertThat(pack).as("KiePackage with given name not found in KieBase").isNotNull();
    Assertions.assertThat(pack.getName()).as("Unexpected package name").isEqualTo(packageName);
    Assertions.assertThat((long) pack.getRules().size()).as("Unexpected number of rules").isEqualTo((long) expectedRules);
}
Also used : KiePackage(org.kie.api.definition.KiePackage)

Example 57 with KiePackage

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

the class RuleTemplateTest method testSampleCheese.

@Test
public void testSampleCheese() {
    // first we compile the decision table into a whole lot of rules.
    final ExternalSpreadsheetCompiler converter = new ExternalSpreadsheetCompiler();
    final KieServices kieServices = KieServices.Factory.get();
    final Resource table = kieServices.getResources().newClassPathResource("sample_cheese.xls", getClass());
    final Resource template = kieServices.getResources().newClassPathResource("sample_cheese.drt", getClass());
    String drl = null;
    try {
        // the data we are interested in starts at row 2, column 2 (i.e. B2)
        drl = converter.compile(table.getInputStream(), template.getInputStream(), 2, 2);
    } catch (IOException e) {
        throw new IllegalArgumentException("Could not read spreadsheet or rules stream.", e);
    }
    // compile the drl
    final Resource drlResource = kieServices.getResources().newReaderResource(new StringReader(drl));
    drlResource.setTargetPath(TestConstants.DRL_TEST_TARGET_PATH);
    final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, drlResource);
    final Collection<KiePackage> pkgs = kbase.getKiePackages();
    Assertions.assertThat(pkgs.size()).isEqualTo(2);
    final ArrayList<String> names = new ArrayList<String>();
    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(2);
    final KieSession ksession = kbase.newKieSession();
    ksession.insert(new Cheese("stilton", 42));
    ksession.insert(new Person("michael", "stilton", 42));
    final List<String> list = new ArrayList<String>();
    ksession.setGlobal("list", list);
    ksession.fireAllRules();
    LOGGER.debug(list.toString());
    ksession.dispose();
}
Also used : ExternalSpreadsheetCompiler(org.drools.decisiontable.ExternalSpreadsheetCompiler) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) Cheese(org.drools.testcoverage.common.model.Cheese) IOException(java.io.IOException) KiePackage(org.kie.api.definition.KiePackage) KieBase(org.kie.api.KieBase) StringReader(java.io.StringReader) KieSession(org.kie.api.runtime.KieSession) Person(org.drools.testcoverage.common.model.Person) Test(org.junit.Test)

Example 58 with KiePackage

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

the class TemplatesTest method testManyRows.

private void testManyRows(final String drl, final int expectedResultListSize, final int expectedRulesCount) {
    final Resource drlResource = KieServices.Factory.get().getResources().newReaderResource(new StringReader(drl));
    drlResource.setTargetPath(TestConstants.DRL_TEST_TARGET_PATH);
    final KieBase kbase = KieBaseUtil.getKieBaseFromResources(kieBaseTestConfiguration, drlResource);
    final KieSession session = kbase.newKieSession();
    final List<String> list = new ArrayList<String>();
    session.setGlobal("list", list);
    for (int i = 0; i < 500; i++) {
        session.insert(new Vegetable("tomato", 350, 8, 3, Taste.BAD));
    }
    session.fireAllRules();
    // check of size of satisfying items
    Assertions.assertThat(list.size()).isEqualTo(expectedResultListSize);
    final Collection<KiePackage> pkgs = kbase.getKiePackages();
    Assertions.assertThat(pkgs.size()).isEqualTo(1);
    final KiePackage pkg = pkgs.iterator().next();
    // check of generated rules size from template
    Assertions.assertThat(pkg.getRules().size()).isEqualTo(expectedRulesCount);
}
Also used : KiePackage(org.kie.api.definition.KiePackage) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession)

Example 59 with KiePackage

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

the class WaltzDbBenchmark method main.

public static void main(final String[] args) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("waltzdb.drl", WaltzDbBenchmark.class), ResourceType.DRL);
    Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
    KieBaseConfiguration kbaseConfiguration = KnowledgeBaseFactory.newKnowledgeBaseConfiguration();
    kbaseConfiguration.setProperty("drools.removeIdentities", "true");
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase(kbaseConfiguration);
    // final RuleBase ruleBase = RuleBaseFactory.newRuleBase( RuleBase.RETEOO,
    // conf );
    kbase.addPackages(pkgs);
    KieSession ksession = kbase.newKieSession();
    // 12,8,4
    List<Line> lines = WaltzDbBenchmark.loadLines("waltzdb16.dat");
    // 12,8,4
    List<Label> labels = WaltzDbBenchmark.loadLabels("waltzdb16.dat");
    long now = System.currentTimeMillis();
    for (Line line : lines) {
        ksession.insert(line);
        System.out.println(line.getP1() + " " + line.getP2());
    }
    for (Label label : labels) {
        ksession.insert(label);
        System.out.println(label.getId() + " " + label.getType());
    }
    Stage stage = new Stage(Stage.DUPLICATE);
    ksession.insert(stage);
    ksession.fireAllRules();
    System.out.println("Time: " + (System.currentTimeMillis() - now));
    ksession.dispose();
}
Also used : KieBaseConfiguration(org.kie.api.KieBaseConfiguration) 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 60 with KiePackage

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

the class WaltzBenchmark method main.

public static void main(final String[] args) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newClassPathResource("waltz.drl", WaltzBenchmark.class), ResourceType.DRL);
    Collection<KiePackage> pkgs = kbuilder.getKnowledgePackages();
    // add the package to a kbase
    final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(pkgs);
    long totalTime = 0;
    for (int i = 0; i < 5; i++) {
        KieSession session = kbase.newKieSession();
        String filename;
        if (args.length != 0) {
            String arg = args[0];
            filename = arg;
        } else {
            filename = "waltz50.dat";
        }
        loadLines(session, filename);
        Stage stage = new Stage(Stage.DUPLICATE);
        session.insert(stage);
        long start = System.currentTimeMillis();
        session.setGlobal("time", start);
        session.fireAllRules();
        long time = System.currentTimeMillis() - start;
        System.err.println(time);
        totalTime += time;
        session.dispose();
    }
    System.out.println("average : " + totalTime / 5);
}
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)

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