Search in sources :

Example 1 with ClassPathResource

use of org.drools.core.io.impl.ClassPathResource 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();
}
Also used : KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) InputStream(java.io.InputStream) KieHelper(org.kie.internal.utils.KieHelper) KieSession(org.kie.api.runtime.KieSession) ClassPathResource(org.drools.core.io.impl.ClassPathResource)

Example 2 with ClassPathResource

use of org.drools.core.io.impl.ClassPathResource in project drools by kiegroup.

the class KnowledgeBuilderImpl method parseChangeSet.

private ChangeSet parseChangeSet(Resource resource) throws IOException, SAXException {
    XmlChangeSetReader reader = new XmlChangeSetReader(this.configuration.getSemanticModules());
    if (resource instanceof ClassPathResource) {
        reader.setClassLoader(((ClassPathResource) resource).getClassLoader(), ((ClassPathResource) resource).getClazz());
    } else {
        reader.setClassLoader(this.configuration.getClassLoader(), null);
    }
    Reader resourceReader = null;
    try {
        resourceReader = resource.getReader();
        return reader.read(resourceReader);
    } finally {
        if (resourceReader != null) {
            resourceReader.close();
        }
    }
}
Also used : XmlPackageReader(org.drools.compiler.compiler.xml.XmlPackageReader) Reader(java.io.Reader) XmlChangeSetReader(org.drools.core.xml.XmlChangeSetReader) StringReader(java.io.StringReader) XmlChangeSetReader(org.drools.core.xml.XmlChangeSetReader) ClassPathResource(org.drools.core.io.impl.ClassPathResource)

Example 3 with ClassPathResource

use of org.drools.core.io.impl.ClassPathResource in project drools by kiegroup.

the class TraitTest method testTraitActualTypeCodeWithEntities.

void testTraitActualTypeCodeWithEntities(String trig, VirtualPropertyMode mode) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("org/drools/compiler/factmodel/traits/testComplexDonShed.drl"), ResourceType.DRL);
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    TraitFactory.setMode(mode, kbase);
    kbase.addPackages(kbuilder.getKnowledgePackages());
    KieSession ksession = kbase.newKieSession();
    ksession.insert(trig);
    ksession.fireAllRules();
    TraitableBean ent = (TraitableBean) ksession.getGlobal("core");
    assertEquals(CodedHierarchyImpl.stringToBitSet("1"), ent.getCurrentTypeCode());
    ksession.insert("b");
    ksession.fireAllRules();
    assertEquals(CodedHierarchyImpl.stringToBitSet("11"), ent.getCurrentTypeCode());
    ksession.insert("c");
    ksession.fireAllRules();
    assertEquals(CodedHierarchyImpl.stringToBitSet("1011"), ent.getCurrentTypeCode());
    ksession.insert("e");
    ksession.fireAllRules();
    assertEquals(CodedHierarchyImpl.stringToBitSet("11011"), ent.getCurrentTypeCode());
    ksession.insert("-c");
    ksession.fireAllRules();
    assertEquals(CodedHierarchyImpl.stringToBitSet("11"), ent.getCurrentTypeCode());
    ksession.insert("dg");
    ksession.fireAllRules();
    assertEquals(CodedHierarchyImpl.stringToBitSet("111111"), ent.getCurrentTypeCode());
    ksession.insert("-f");
    ksession.fireAllRules();
    assertEquals(CodedHierarchyImpl.stringToBitSet("111"), ent.getCurrentTypeCode());
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) TraitableBean(org.drools.core.factmodel.traits.TraitableBean) ClassPathResource(org.drools.core.io.impl.ClassPathResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 4 with ClassPathResource

use of org.drools.core.io.impl.ClassPathResource in project drools by kiegroup.

the class AgendaRuleFlowGroupsTest method createSession.

private CommandBasedStatefulKnowledgeSession createSession(long id, String... rules) {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    for (String rule : rules) {
        kbuilder.add(new ClassPathResource(rule), ResourceType.DRL);
    }
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    kbase.addPackages(kbuilder.getKnowledgePackages());
    Environment env = createEnvironment(context);
    if (locking) {
        env.set(EnvironmentName.USE_PESSIMISTIC_LOCKING, true);
    }
    if (id == -1) {
        return (CommandBasedStatefulKnowledgeSession) JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
    } else {
        return (CommandBasedStatefulKnowledgeSession) JPAKnowledgeService.loadStatefulKnowledgeSession(id, kbase, null, env);
    }
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) DroolsPersistenceUtil.createEnvironment(org.drools.persistence.util.DroolsPersistenceUtil.createEnvironment) Environment(org.kie.api.runtime.Environment) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) ClassPathResource(org.drools.core.io.impl.ClassPathResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Example 5 with ClassPathResource

use of org.drools.core.io.impl.ClassPathResource in project drools by kiegroup.

the class RuleFlowGroupRollbackTest method createSession.

private CommandBasedStatefulKnowledgeSession createSession() {
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(new ClassPathResource("ruleflowgroup_rollback.drl"), ResourceType.DRL);
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    kbase.addPackages(kbuilder.getKnowledgePackages());
    Environment env = createEnvironment(context);
    if (locking) {
        env.set(EnvironmentName.USE_PESSIMISTIC_LOCKING, true);
    }
    return (CommandBasedStatefulKnowledgeSession) JPAKnowledgeService.newStatefulKnowledgeSession(kbase, null, env);
}
Also used : KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) Environment(org.kie.api.runtime.Environment) CommandBasedStatefulKnowledgeSession(org.drools.core.command.impl.CommandBasedStatefulKnowledgeSession) ClassPathResource(org.drools.core.io.impl.ClassPathResource) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase)

Aggregations

ClassPathResource (org.drools.core.io.impl.ClassPathResource)36 Test (org.junit.Test)23 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)18 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)15 VerifierBuilder (org.drools.verifier.builder.VerifierBuilder)11 AbstractBaseTest (org.jbpm.test.util.AbstractBaseTest)11 VerifierReport (org.drools.verifier.data.VerifierReport)10 ProcessInstance (org.kie.api.runtime.process.ProcessInstance)10 StatefulKnowledgeSession (org.kie.internal.runtime.StatefulKnowledgeSession)9 TestWorkItemHandler (org.jbpm.persistence.session.objects.TestWorkItemHandler)5 WorkItem (org.kie.api.runtime.process.WorkItem)5 IOException (java.io.IOException)4 Resource (org.kie.api.io.Resource)4 KieSession (org.kie.api.runtime.KieSession)4 ArrayList (java.util.ArrayList)3 PMMLResource (org.drools.compiler.compiler.PMMLResource)3 ScopesAgendaFilter (org.drools.verifier.builder.ScopesAgendaFilter)3 KieBase (org.kie.api.KieBase)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Reader (java.io.Reader)2