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);
}
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();
}
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);
}
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();
}
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);
}
Aggregations