Search in sources :

Example 71 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class DMNUpdateTest method testReplace.

@Test
public void testReplace() {
    final KieServices ks = KieServices.Factory.get();
    final KieContainer kieContainer = KieHelper.getKieContainer(ks.newReleaseId("org.kie", "dmn-test", "1.0.0"), ks.getResources().newClassPathResource("0001-input-data-string.dmn", this.getClass()));
    DMNRuntime runtime = kieContainer.newKieSession().getKieRuntime(DMNRuntime.class);
    Assert.assertNotNull(runtime);
    assertThat(runtime.getModels(), hasSize(1));
    DMNContext dmnContext = runtime.newContext();
    dmnContext.set("Full Name", "John Doe");
    DMNResult evaluateAll = runtime.evaluateAll(runtime.getModels().get(0), dmnContext);
    assertThat(evaluateAll.getDecisionResultByName("Greeting Message").getResult(), is("Hello John Doe"));
    ReleaseId v101 = ks.newReleaseId("org.kie", "dmn-test", "1.0.1");
    Resource newClassPathResource = ks.getResources().newClassPathResource("0001-input-data-string-itIT.dmn", this.getClass());
    newClassPathResource.setTargetPath("0001-input-data-string.dmn");
    KieHelper.createAndDeployJar(ks, v101, newClassPathResource);
    Results updateResults = kieContainer.updateToVersion(v101);
    assertThat(updateResults.hasMessages(Level.ERROR), is(false));
    runtime = kieContainer.newKieSession().getKieRuntime(DMNRuntime.class);
    Assert.assertNotNull(runtime);
    assertThat(runtime.getModels(), hasSize(1));
    DMNContext dmnContext2 = runtime.newContext();
    dmnContext2.set("Full Name", "John Doe");
    DMNResult evaluateAll2 = runtime.evaluateAll(runtime.getModels().get(0), dmnContext2);
    assertThat(evaluateAll2.getDecisionResultByName("Greeting Message").getResult(), is("Salve John Doe"));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) Results(org.kie.api.builder.Results) DMNContext(org.kie.dmn.api.core.DMNContext) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) ReleaseId(org.kie.api.builder.ReleaseId) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) KieContainer(org.kie.api.runtime.KieContainer) Test(org.junit.Test)

Example 72 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class DMNRuntimeUtil method createRuntimeWithAdditionalResources.

public static DMNRuntime createRuntimeWithAdditionalResources(final String resourceName, final Class testClass, final String... additionalResources) {
    final KieServices ks = KieServices.Factory.get();
    Resource mainResource = ks.getResources().newClassPathResource(resourceName, testClass);
    List<Resource> totalResources = new ArrayList<>();
    totalResources.add(mainResource);
    for (String add : additionalResources) {
        totalResources.add(ks.getResources().newClassPathResource(add, testClass));
    }
    final KieContainer kieContainer = KieHelper.getKieContainer(ks.newReleaseId("org.kie", "dmn-test-" + UUID.randomUUID(), "1.0"), totalResources.toArray(new Resource[] {}));
    final DMNRuntime runtime = typeSafeGetKieRuntime(kieContainer);
    Assert.assertNotNull(runtime);
    return runtime;
}
Also used : Resource(org.kie.api.io.Resource) ArrayList(java.util.ArrayList) KieServices(org.kie.api.KieServices) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) KieContainer(org.kie.api.runtime.KieContainer)

Example 73 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class DroolsAbstractPMMLTest method getModelSession.

protected KieSession getModelSession(String[] pmmlSources, boolean verbose) {
    KieServices ks = KieServices.Factory.get();
    KieRepository kr = ks.getRepository();
    KieFileSystem kfs = ks.newKieFileSystem();
    for (int j = 0; j < pmmlSources.length; j++) {
        Resource res = ResourceFactory.newClassPathResource(pmmlSources[j]).setResourceType(ResourceType.PMML);
        kfs.write(res);
    }
    KieModuleModel model = ks.newKieModuleModel();
    model.setConfigurationProperty("drools.propertySpecific", "ALLOWED");
    KieBaseModel kbModel = model.newKieBaseModel(DEFAULT_KIEBASE).addPackage(BASE_PACK).setEventProcessingMode(EventProcessingOption.CLOUD);
    kfs.writeKModuleXML(model.toXML());
    KieBuilder kb = ks.newKieBuilder(kfs);
    kb.buildAll();
    if (kb.getResults().hasMessages(Message.Level.ERROR)) {
        throw new RuntimeException("Build Errors:\n" + kb.getResults().toString());
    }
    KieContainer kContainer = ks.newKieContainer(kr.getDefaultReleaseId());
    // KieBase kieBase = kContainer.getKieBase("SampleMine_SampleMineSegmentation_SEGMENT_1");
    KieBase kieBase = kContainer.getKieBase();
    setKbase(kieBase);
    return kieBase.newKieSession();
}
Also used : KieBaseModel(org.kie.api.builder.model.KieBaseModel) KieRepository(org.kie.api.builder.KieRepository) KieFileSystem(org.kie.api.builder.KieFileSystem) KieModuleModel(org.kie.api.builder.model.KieModuleModel) KieBase(org.kie.api.KieBase) Resource(org.kie.api.io.Resource) KieServices(org.kie.api.KieServices) KieBuilder(org.kie.api.builder.KieBuilder) KieContainer(org.kie.api.runtime.KieContainer)

Example 74 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class DecisionTreeTest method testMissingTreeWeighted2.

@Test
public void testMissingTreeWeighted2() throws Exception {
    Resource res = ResourceFactory.newClassPathResource(source2);
    KieBase kbase = new KieHelper().addResource(res, ResourceType.PMML).build();
    RuleUnitExecutor executor = RuleUnitExecutor.create().bind(kbase);
    KieRuntimeLogger console = ((InternalRuleUnitExecutor) executor).addConsoleLogger();
    PMMLRequestData requestData = new PMMLRequestData("123", "Missing");
    requestData.addRequestParam(new ParameterInfo<>("123", "fld1", Double.class, -1.0));
    requestData.addRequestParam(new ParameterInfo<>("123", "fld2", Double.class, -1.0));
    requestData.addRequestParam(new ParameterInfo<>("123", "fld3", String.class, "miss"));
    PMML4Result resultHolder = new PMML4Result();
    DataSource<PMMLRequestData> data = executor.newDataSource("request");
    DataSource<PMML4Result> results = executor.newDataSource("results");
    DataSource<PMML4Data> pmmlData = executor.newDataSource("pmmlData");
    List<String> possiblePackages = calculatePossiblePackageNames("Missing");
    Class<? extends RuleUnit> unitClass = getStartingRuleUnit("RuleUnitIndicator", (InternalKnowledgeBase) kbase, possiblePackages);
    assertNotNull(unitClass);
    int x = executor.run(unitClass);
    data.insert(requestData);
    results.insert(resultHolder);
    executor.run(unitClass);
    console.close();
    System.out.println(resultHolder);
    Collection<?> objects = ((InternalRuleUnitExecutor) executor).getSessionObjects();
    objects.forEach(o -> {
        System.out.println(o);
    });
    pmmlData.forEach(pd -> {
        System.out.println(pd);
    });
    AbstractTreeToken token = (AbstractTreeToken) resultHolder.getResultValue("MissingTreeToken", null);
    assertNotNull(token);
    assertEquals(0.6, token.getConfidence(), 1e-6);
    assertEquals("null", token.getCurrent());
    assertEquals(100.0, token.getTotalCount(), 0.0);
    Object fld9 = resultHolder.getResultValue("Fld9", null);
    assertNotNull(fld9);
    String value = (String) resultHolder.getResultValue("Fld9", "value");
    assertNotNull(value);
    assertEquals("tgtX", value);
}
Also used : InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) PMMLRequestData(org.kie.api.pmml.PMMLRequestData) PMML4Result(org.kie.api.pmml.PMML4Result) Resource(org.kie.api.io.Resource) PMML4Data(org.kie.api.pmml.PMML4Data) KieHelper(org.kie.internal.utils.KieHelper) KieRuntimeLogger(org.kie.api.logger.KieRuntimeLogger) InternalRuleUnitExecutor(org.drools.core.impl.InternalRuleUnitExecutor) RuleUnitExecutor(org.kie.api.runtime.rule.RuleUnitExecutor) KieBase(org.kie.api.KieBase) AbstractTreeToken(org.kie.pmml.pmml_4_2.model.tree.AbstractTreeToken) Test(org.junit.Test) DroolsAbstractPMMLTest(org.kie.pmml.pmml_4_2.DroolsAbstractPMMLTest)

Example 75 with Resource

use of org.kie.api.io.Resource in project drools by kiegroup.

the class KieHelper method deployJarIntoRepository.

private static KieModule deployJarIntoRepository(KieServices ks, byte[] jar) {
    Resource jarRes = ks.getResources().newByteArrayResource(jar);
    KieModule km = ks.getRepository().addKieModule(jarRes);
    return km;
}
Also used : Resource(org.kie.api.io.Resource) InternalKieModule(org.drools.compiler.kie.builder.impl.InternalKieModule) KieModule(org.kie.api.builder.KieModule)

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