use of org.kie.api.io.Resource in project drools by kiegroup.
the class PropertyListenerTest method runTest.
@Test
public void runTest() {
final KieServices kieServices = KieServices.Factory.get();
final Resource drlResource = kieServices.getResources().newClassPathResource("propertyListenerTest.drl", getClass());
final KieBase kieBase = KieBaseUtil.getKieBaseAndBuildInstallModule(TestConstants.PACKAGE_REGRESSION, kieBaseTestConfiguration, drlResource);
final KieSession session = kieBase.newKieSession();
final ArrayList<Person> people = new ArrayList<Person>();
people.add(new Person("Test 1"));
people.add(new Person("Test 2"));
LOGGER.info("== Listeners attached before rules ==");
for (Person person : people) {
for (PropertyChangeListener listener : person.getBoundSupport().getPropertyChangeListeners()) {
LOGGER.info("Listener attached of type: " + listener);
}
}
// call rules
final List<Command<?>> commands = new ArrayList<Command<?>>();
commands.add(kieServices.getCommands().newInsert(people));
commands.add(kieServices.getCommands().newFireAllRules());
session.execute(kieServices.getCommands().newBatchExecution(commands, null));
session.dispose();
LOGGER.info("Session disposed");
LOGGER.info("== Listeners attached after rules (should be none) ==");
printAndAssertListenersFromPeople(people);
// update name to trigger update
people.get(0).setName("Test 3");
LOGGER.info("== Listeners attached after updating name (should be none) ==");
printAndAssertListenersFromPeople(people);
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class UnwantedStringConversionTest method testStringToDecimalConversion.
@Test
public void testStringToDecimalConversion() {
final Resource drlResource = KieServices.Factory.get().getResources().newClassPathResource("unwantedStringConversionTest.drl", getClass());
final KieBase kieBase = KieBaseUtil.getKieBaseAndBuildInstallModule(TestConstants.PACKAGE_REGRESSION, kieBaseTestConfiguration, drlResource);
final KieSession ksession = kieBase.newKieSession();
final TrackingAgendaEventListener listener = new TrackingAgendaEventListener();
ksession.addEventListener(listener);
final Message message = new Message();
final SomeEntity someEntity = new SomeEntity("1.5");
message.setSomeEntity(someEntity);
message.setMessage("1.5");
ksession.insert(message);
ksession.fireAllRules();
Assertions.assertThat(listener.isRuleFired("R1")).isFalse();
Assertions.assertThat(listener.isRuleFired("R2")).isFalse();
Assertions.assertThat(listener.rulesCount()).isEqualTo(0);
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class VerifierKnowledgeBaseBuilder method newVerifierKnowledgeBase.
public KieBase newVerifierKnowledgeBase(VerifierConfiguration configuration) {
InternalKnowledgeBase verifierKnowledgeBase = KnowledgeBaseFactory.newKnowledgeBase();
KnowledgeBuilderConfiguration kbuilderConfiguration = KnowledgeBuilderFactory.newKnowledgeBuilderConfiguration();
kbuilderConfiguration.setProperty("drools.dialect.java.compiler", "JANINO");
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder(kbuilderConfiguration);
if (configuration.getVerifyingResources() != null) {
for (Resource resource : configuration.getVerifyingResources().keySet()) {
kbuilder.add(resource, configuration.getVerifyingResources().get(resource));
}
}
if (kbuilder.hasErrors()) {
Iterator<KnowledgeBuilderError> errors = kbuilder.getErrors().iterator();
while (errors.hasNext()) {
this.errors.add(new VerifierError("Error compiling verifier rules: " + errors.next().getMessage()));
}
}
verifierKnowledgeBase.addPackages(kbuilder.getKnowledgePackages());
return verifierKnowledgeBase;
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class DeclarationWithOrTest method testBindingWithOrInLHS.
/**
* Verifies that the rule with binding and OR in LHS compiles and works as expected.
*/
@Test
public void testBindingWithOrInLHS() {
final Resource resource = KieServices.Factory.get().getResources().newReaderResource(new StringReader(DRL));
resource.setTargetPath(TestConstants.DRL_TEST_TARGET_PATH);
final KieBuilder kbuilder = KieUtil.getKieBuilderFromResources(kieBaseTestConfiguration, true, resource);
final KieSession ksession = KieBaseUtil.getDefaultKieBaseFromKieBuilder(kieBaseTestConfiguration, kbuilder).newKieSession();
final List<String> list = new ArrayList<String>();
ksession.setGlobal("list", list);
try {
ksession.insert("y");
ksession.fireAllRules();
} finally {
ksession.dispose();
}
Assertions.assertThat(list).as("Unexpected element in result global").containsExactly(FACT);
}
use of org.kie.api.io.Resource in project drools by kiegroup.
the class GenericsWithModifyTest method initialize.
@Before
public void initialize() throws IOException {
final Resource resource = KieServices.Factory.get().getResources().newByteArrayResource(DRL.getBytes(Charset.forName("UTF-8")));
resource.setTargetPath(TestConstants.DRL_TEST_TARGET_PATH);
final KieBuilder kbuilder = KieUtil.getKieBuilderFromResources(kieBaseTestConfiguration, true, resource);
final KieContainer kieContainer = KieServices.Factory.get().newKieContainer(kbuilder.getKieModule().getReleaseId());
this.kieSession = kieContainer.newKieSession();
}
Aggregations