use of org.kie.internal.utils.KieHelper in project opennms by OpenNMS.
the class CorrelationExample method main.
/**
* <p>main</p>
*
* @param args an array of {@link java.lang.String} objects.
* @throws java.lang.Exception if any.
*/
public static void main(final String[] args) throws Exception {
final KieSession session = new KieHelper().addResource(new ClassPathResource("CorrelationExample.drl")).build().newKieSession();
KieRuntimeLogger logger = KieServices.Factory.get().getLoggers().newFileLogger(session, "log/correlation");
try (InputStream in = CorrelationExample.class.getResourceAsStream("simulation")) {
final Simulation simulation = new Simulation();
System.out.println("Loading Simulation");
simulation.load(in);
System.out.println("Executing Simulation");
simulation.simulate(session);
}
logger.close();
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class TemplateIntegrationTest method test.
@Test
public void test() {
String drl = "global java.util.List list\n" + "rule \"String detector\"\n" + " when\n" + " $s : String( )\n" + " then\n" + " list.add($s);\n" + "end";
final KieSession ksession = new KieHelper().addContent(template, ResourceType.TEMPLATE).build().newKieSession();
Company myCompany = new Company(123, "myCompany");
Company yourCompany = new Company(456, "yourCompany");
ksession.insert(myCompany);
ksession.insert(yourCompany);
ksession.fireAllRules();
assertTrue(myCompany.isFound());
assertFalse(yourCompany.isFound());
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class TemplateWithDSLIntegrationTest method testCompileTemplateWithDSL.
@Test
public void testCompileTemplateWithDSL() {
String template = "<rule>\n" + " <name>guided-template-with-dsl</name>\n" + " <modelVersion>1.0</modelVersion>\n" + " <attributes/>\n" + " <metadataList/>\n" + " <lhs>\n" + " <dslSentence>\n" + " <drl>applicant:" + Applicant.class.getCanonicalName() + "(approved=={bool})</drl>\n" + " <definition>When the applicant approval is {bool:BOOLEAN:checked}</definition>\n" + " <values>\n" + " <org.drools.workbench.models.datamodel.rule.DSLVariableValue>\n" + " <value>false</value>\n" + " </org.drools.workbench.models.datamodel.rule.DSLVariableValue>\n" + " <org.drools.workbench.models.datamodel.rule.DSLComplexVariableValue>\n" + " <value>bool</value>\n" + " <id>BOOLEAN:checked</id>\n" + " </org.drools.workbench.models.datamodel.rule.DSLComplexVariableValue>\n" + " </values>\n" + " </dslSentence>\n" + " </lhs>\n" + " <rhs>\n" + " <dslSentence>\n" + " <drl>applicant.setApproved(true)</drl>\n" + " <definition>Approve the loan</definition>\n" + " <values/>\n" + " </dslSentence>\n" + " </rhs>\n" + " <imports>\n" + " <imports/>\n" + " </imports>\n" + " <packageName>org.mortgages</packageName>\n" + " <isNegated>false</isNegated>\n" + " <table>\n" + " <entry>\n" + " <string>__ID_KOL_NAME__</string>\n" + " <list>\n" + " <string>1</string>\n" + " </list>\n" + " </entry>\n" + " </table>\n" + " <idCol>1</idCol>\n" + " <rowsCount>1</rowsCount>\n" + "</rule>";
String dsl = "[when]When the applicant approval is {bool:BOOLEAN:checked} = applicant:" + Applicant.class.getCanonicalName() + "(approved=={bool})\n" + "[then]Approve the loan = applicant.setApproved(true)";
KieHelper kieHelper = new KieHelper();
KieSession kieSession = kieHelper.addContent(template, ResourceType.TEMPLATE).addContent(dsl, ResourceType.DSL).build().newKieSession();
Applicant applicant = new Applicant();
applicant.setApproved(false);
kieSession.insert(applicant);
int rulesFired = kieSession.fireAllRules();
Assert.assertEquals("Incorrect number of rules fired!", 1, rulesFired);
Assert.assertEquals("Rule RHS wasn't triggered!", true, applicant.isApproved());
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class TruthMaintenanceTest method testDeleteLogicalAssertionFromRule.
@Test
public void testDeleteLogicalAssertionFromRule() {
// BZ-1317026
String drl = "global java.util.List list;\n" + "rule R1 when\n" + "then\n" + " insertLogical( \"test\" ); \n" + "end\n" + "rule R2 when\n" + " $s : String()\n" + "then\n" + " list.add( $s ); \n" + " delete( $s ); \n" + "end\n";
KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
ksession.fireAllRules();
assertEquals(1, list.size());
assertEquals("test", list.get(0));
Collection<FactHandle> fhs = ksession.getFactHandles(new ClassObjectFilter(String.class));
assertEquals(0, fhs.size());
}
use of org.kie.internal.utils.KieHelper in project drools by kiegroup.
the class TruthMaintenanceTest method testStatedDeleteLogicalAssertion.
@Test
public void testStatedDeleteLogicalAssertion() {
// BZ-1317026
String drl = "rule R1 when\n" + "then\n" + " insertLogical( \"test\" ); \n" + "end\n";
KieSession ksession = new KieHelper().addContent(drl, ResourceType.DRL).build().newKieSession();
ksession.fireAllRules();
Collection<FactHandle> fhs = ksession.getFactHandles(new ClassObjectFilter(String.class));
assertEquals(1, fhs.size());
for (FactHandle fh : fhs) {
ksession.delete(fh, FactHandle.State.STATED);
}
fhs = ksession.getFactHandles(new ClassObjectFilter(String.class));
assertEquals(1, fhs.size());
for (FactHandle fh : fhs) {
ksession.delete(fh, FactHandle.State.LOGICAL);
}
fhs = ksession.getFactHandles(new ClassObjectFilter(String.class));
assertEquals(0, fhs.size());
}
Aggregations