Search in sources :

Example 61 with Resource

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);
}
Also used : PropertyChangeListener(java.beans.PropertyChangeListener) Command(org.kie.api.command.Command) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 62 with Resource

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);
}
Also used : TrackingAgendaEventListener(org.drools.testcoverage.common.listener.TrackingAgendaEventListener) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) KieSession(org.kie.api.runtime.KieSession) Test(org.junit.Test)

Example 63 with Resource

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;
}
Also used : KnowledgeBuilderError(org.kie.internal.builder.KnowledgeBuilderError) VerifierError(org.drools.verifier.VerifierError) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Resource(org.kie.api.io.Resource) KnowledgeBuilderConfiguration(org.kie.internal.builder.KnowledgeBuilderConfiguration) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 64 with Resource

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);
}
Also used : Resource(org.kie.api.io.Resource) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) KieSession(org.kie.api.runtime.KieSession) KieBuilder(org.kie.api.builder.KieBuilder) Test(org.junit.Test)

Example 65 with Resource

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();
}
Also used : Resource(org.kie.api.io.Resource) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer) Before(org.junit.Before)

Aggregations

Resource (org.kie.api.io.Resource)162 Test (org.junit.Test)100 KieSession (org.kie.api.runtime.KieSession)50 KieServices (org.kie.api.KieServices)43 KieBase (org.kie.api.KieBase)42 ArrayList (java.util.ArrayList)30 KieContainer (org.kie.api.runtime.KieContainer)27 StringReader (java.io.StringReader)23 ReleaseId (org.kie.api.builder.ReleaseId)23 KieBuilder (org.kie.api.builder.KieBuilder)20 KieModule (org.kie.api.builder.KieModule)20 KieFileSystem (org.kie.api.builder.KieFileSystem)19 ClassPathResource (org.drools.core.io.impl.ClassPathResource)18 InternalKieModule (org.drools.compiler.kie.builder.impl.InternalKieModule)17 FactType (org.kie.api.definition.type.FactType)17 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)16 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)14 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)14 IOException (java.io.IOException)13 PMMLResource (org.drools.compiler.compiler.PMMLResource)11