use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class ScoringStrategiesTest method executeAndFetchScore.
/* Internal functions */
private double executeAndFetchScore(String sheetName) throws Exception {
ScorecardCompiler scorecardCompiler = new ScorecardCompiler(INTERNAL_DECLARED_TYPES);
InputStream inputStream = PMMLDocumentTest.class.getResourceAsStream("/scoremodel_scoring_strategies.xls");
boolean compileResult = scorecardCompiler.compileFromExcel(inputStream, sheetName);
if (!compileResult) {
for (ScorecardError error : scorecardCompiler.getScorecardParseErrors()) {
System.err.println("Scorecard Compiler Error :" + error.getErrorLocation() + "->" + error.getErrorMessage());
}
return -999999;
}
String drl = scorecardCompiler.getDRL();
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ks.getResources().newByteArrayResource(drl.getBytes()).setSourcePath("scoremodel_scoring_strategies.drl").setResourceType(ResourceType.DRL));
KieBuilder kieBuilder = ks.newKieBuilder(kfs);
Results res = kieBuilder.buildAll().getResults();
if (res.hasMessages(Message.Level.ERROR)) {
System.out.println(res.getMessages());
}
assertEquals(0, res.getMessages(Message.Level.ERROR).size());
KieContainer kieContainer = ks.newKieContainer(kieBuilder.getKieModule().getReleaseId());
KieBase kbase = kieContainer.getKieBase();
StatelessKieSession session = kbase.newStatelessKieSession();
FactType scorecardType = kbase.getFactType("org.drools.scorecards.example", "SampleScore");
Object scorecard = scorecardType.newInstance();
scorecardType.set(scorecard, "age", 10);
session.execute(scorecard);
return (Double) scorecardType.get(scorecard, "scorecard__calculatedScore");
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class NonStringCompareTest method testScenario.
private void testScenario(final String factFieldValueForDrl, final String factFieldValueForTest) throws IllegalAccessException, InstantiationException {
final KieBuilder kbuilder = build(factFieldValueForDrl);
Assertions.assertThat(kbuilder.getResults().getMessages(Level.ERROR)).isEmpty();
final KieBase kbase = KieBaseUtil.getDefaultKieBaseFromKieBuilder(kieBaseTestConfiguration, kbuilder);
final KieSession ksession = kbase.newKieSession();
try {
final FactType type = kbase.getFactType(TestConstants.PACKAGE_REGRESSION, "Fact");
final Object fact = type.newInstance();
type.set(fact, "field", factFieldValueForTest);
ksession.insert(fact);
final int count = ksession.fireAllRules();
Assertions.assertThat(count).isEqualTo(1);
} finally {
ksession.dispose();
}
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class NotInFusionTest method createNotEvent.
private Object createNotEvent(KieSession ksession, String property) throws Exception {
FactType type = ksession.getKieBase().getFactType("org.drools.testcoverage.regression", "NotEvent");
Object instance = type.newInstance();
type.set(instance, "property", property);
return instance;
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class QueryTest method checkRecursiveQuery.
private void checkRecursiveQuery(String query) throws InstantiationException, IllegalAccessException {
String str = "package org.test;\n" + "import " + Person.class.getCanonicalName() + ";" + query + "declare Location\n" + " thing : String\n" + " location : String\n" + "end\n" + "// rule values at A11, header at A6\n" + "rule \"testPullQueryRule\" when\n" + " String(this == \"pull\")\n" + " Person($l : likes)\n" + " ?isContainedIn($l, \"office\";)\n" + "then\n" + "end\n" + "\n" + "// rule values at A12, header at A6\n" + "rule \"testPushQueryRule\" when\n" + " String(this == \"push\")\n" + " Person($l : likes)\n" + " isContainedIn($l, \"office\";)\n" + "then\n" + "end";
KieSession ksession = getKieSession(str);
FactType locationType = ksession.getKieBase().getFactType("org.test", "Location");
final TrackingAgendaEventListener listener = new TrackingAgendaEventListener();
ksession.addEventListener(listener);
final Person peter = new Person("Peter");
peter.setLikes("steak");
final Object steakLocation = locationType.newInstance();
locationType.set(steakLocation, "thing", "steak");
locationType.set(steakLocation, "location", "table");
final Object tableLocation = locationType.newInstance();
locationType.set(tableLocation, "thing", "table");
locationType.set(tableLocation, "location", "office");
ksession.insert(peter);
final FactHandle steakHandle = ksession.insert(steakLocation);
final FactHandle tableHandle = ksession.insert(tableLocation);
ksession.insert("pull");
ksession.fireAllRules();
Assertions.assertThat(listener.isRuleFired("testPullQueryRule")).isTrue();
Assertions.assertThat(listener.isRuleFired("testPushQueryRule")).isFalse();
listener.clear();
// when location is changed of what Peter likes, pull query should
// ignore it
final Object steakLocation2 = locationType.newInstance();
locationType.set(steakLocation2, "thing", "steak");
locationType.set(steakLocation2, "location", "desk");
final Object deskLocation = locationType.newInstance();
locationType.set(deskLocation, "thing", "desk");
locationType.set(deskLocation, "location", "office");
ksession.insert(steakLocation2);
ksession.insert(deskLocation);
ksession.delete(steakHandle);
ksession.delete(tableHandle);
ksession.fireAllRules();
Assertions.assertThat(listener.isRuleFired("testPullQueryRule")).isFalse();
Assertions.assertThat(listener.isRuleFired("testPushQueryRule")).isFalse();
listener.clear();
final Person paul = new Person("Paul");
paul.setLikes("steak");
ksession.insert(paul);
ksession.fireAllRules();
Assertions.assertThat(listener.isRuleFired("testPullQueryRule")).isTrue();
Assertions.assertThat(listener.isRuleFired("testPushQueryRule")).isFalse();
}
use of org.kie.api.definition.type.FactType in project drools by kiegroup.
the class AdapterTest method testCustomInputAdapter.
@Test
public void testCustomInputAdapter() {
String source = PMML4Helper.pmmlDefaultPackageName().replace(".", "/") + "/" + "mock_cold_adapter.xml";
KieServices ks = KieServices.Factory.get();
KieFileSystem kfs = ks.newKieFileSystem();
kfs.write(ResourceFactory.newClassPathResource(source).setResourceType(ResourceType.PMML));
Results res = ks.newKieBuilder(kfs).buildAll().getResults();
if (res.hasMessages(Message.Level.ERROR)) {
System.out.println(res.getMessages(Message.Level.ERROR));
}
assertEquals(0, res.getMessages(Message.Level.ERROR).size());
KieBase kieBase = ks.newKieContainer(ks.getRepository().getDefaultReleaseId()).getKieBase();
FactType ft = kieBase.getFactType("test", "MyAdapter");
assertTrue(ft != null);
assertTrue(ft.getFactClass().isInterface());
FactType fto = kieBase.getFactType("test", "MyOutAdapter");
assertTrue(fto != null);
assertTrue(fto.getFactClass().isInterface());
}
Aggregations