Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DROOLS-5964] Implement PMML-Trusty/Scesim backend integration #3344

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,12 @@ public class ScenarioSimulationModel

public enum Type {
RULE,
DMN
DMN,
PMML
}

@XStreamAsAttribute()
private String version = "1.8";
private String version = "1.9";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yesamer
Do you think this is a problem for client side marshaller or in general do you expect any impact on Kogito/BC side?


private Simulation simulation;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public class Settings {

private String dmnFilePath;

private String pmmlFilePath;

private ScenarioSimulationModel.Type type;

private String fileName;
Expand All @@ -38,6 +40,8 @@ public class Settings {

private String dmnName;

private String pmmlModelName;

private boolean skipFromBuild = false;
private boolean stateless = false;

Expand All @@ -57,6 +61,14 @@ public void setDmnFilePath(String dmnFilePath) {
this.dmnFilePath = dmnFilePath;
}

public String getPmmlFilePath() {
return pmmlFilePath;
}

public void setPmmlFilePath(String pmmlFilePath) {
this.pmmlFilePath = pmmlFilePath;
}

public ScenarioSimulationModel.Type getType() {
return type;
}
Expand Down Expand Up @@ -137,6 +149,14 @@ public void setDmnName(String dmnName) {
this.dmnName = dmnName;
}

public String getPmmlModelName() {
return pmmlModelName;
}

public void setPmmlModelName(String pmmlModelName) {
this.pmmlModelName = pmmlModelName;
}

public boolean isSkipFromBuild() {
return skipFromBuild;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,18 @@
<artifactId>kie-dmn-core</artifactId>
</dependency>

<!-- PMML dependencies -->
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-pmml-dependencies</artifactId>
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>org.kie</groupId>
<artifactId>kie-test-util</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

package org.drools.scenariosimulation.backend.util;
package org.drools.scenariosimulation.backend.exceptions;

/**
* Utility that provide classPath scan to retrieve resources
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.drools.scenariosimulation.backend.exceptions;

public class ImpossibleToFindPMMLException extends IllegalArgumentException {

public ImpossibleToFindPMMLException(String message) {
super(message);
}

public ImpossibleToFindPMMLException(String message, Throwable cause) {
super(message, cause);
}

public ImpossibleToFindPMMLException(Throwable cause) {
super(cause.getMessage(), cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.drools.scenariosimulation.backend.fluent;

import java.nio.file.FileSystems;
import java.util.Objects;

import org.drools.core.command.RequestContextImpl;
import org.drools.scenariosimulation.backend.exceptions.ImpossibleToFindPMMLException;
import org.kie.api.KieBase;
import org.kie.api.pmml.PMML4Result;
import org.kie.api.pmml.PMMLRequestData;
import org.kie.api.runtime.KieContainer;
import org.kie.api.runtime.KieRuntimeFactory;
import org.kie.api.runtime.RequestContext;
import org.kie.pmml.api.models.PMMLModel;
import org.kie.pmml.api.runtime.PMMLRuntime;
import org.kie.pmml.evaluator.core.PMMLContextImpl;
import org.kie.pmml.evaluator.core.utils.PMMLRequestDataBuilder;

public class PMMLScenarioExecutableBuilder {

public static final String DEFAULT_APPLICATION = "defaultApplication";
public static final String PMML_RESULT = "pmmlResult";
public static final String PMML_MODEL = "pmmlModel";

private static final String SEPARATOR = FileSystems.getDefault().getSeparator();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it is better to normalise the path to use / instead of handling platform specific separator: with this code I expect that if the scesim file is created on Linux and executed then on a Windows machine it will fail


private final KieContainer kieContainer;
private final PMMLRequestDataBuilder pmmlRequestDataBuilder;
private final String pmmlFilePath;

private PMMLScenarioExecutableBuilder(KieContainer kieContainer, String pmmlFilePath, String pmmlModelName) {
this.kieContainer = kieContainer;
this.pmmlFilePath = pmmlFilePath;


String fileName = pmmlFilePath.contains(SEPARATOR) ? pmmlFilePath.substring(pmmlFilePath.lastIndexOf(SEPARATOR) +1) : pmmlFilePath;
pmmlRequestDataBuilder = new PMMLRequestDataBuilder("correlationid", pmmlModelName, fileName);
}

public static PMMLScenarioExecutableBuilder createBuilder(KieContainer kieContainer, String pmmlFilePath,
String pmmlModelName) {
return new PMMLScenarioExecutableBuilder(kieContainer, pmmlFilePath, pmmlModelName);
}

public void setValue(String key, Object value) {
Class class1 = value.getClass();
pmmlRequestDataBuilder.addParameter(key, value, class1);
}

public RequestContext run() {
Objects.requireNonNull(kieContainer, "KieContainer is null");
final PMMLRequestData pmmlRequestData = pmmlRequestDataBuilder.build();
final KieBase kieBase = kieContainer.getKieBase();
final KieRuntimeFactory kieRuntimeFactory = KieRuntimeFactory.of(kieBase);
final PMMLRuntime pmmlRuntime = kieRuntimeFactory.get(PMMLRuntime.class);
final PMMLModel pmmlModel = pmmlRuntime.getPMMLModel(pmmlRequestData.getModelName())
.orElseThrow(() -> new ImpossibleToFindPMMLException("Failed to retrieve model with name " + pmmlRequestData.getModelName()));
final PMML4Result pmml4Result = pmmlRuntime.evaluate(pmmlRequestData.getModelName(), new PMMLContextImpl(pmmlRequestData));
final RequestContext toReturn = new RequestContextImpl();
toReturn.setOutput(PMML_RESULT, pmml4Result);
toReturn.setOutput(PMML_MODEL, pmmlModel);
return toReturn;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,18 @@ public static String getScesimFileName(String fileFullPath) {
}

public static ScenarioRunnerProvider getSpecificRunnerProvider(Type type) {
if (Type.RULE.equals(type)) {
return RuleScenarioRunner::new;
} else if (Type.DMN.equals(type)) {
return DMNScenarioRunner::new;
} else {
throw new IllegalArgumentException("Impossible to run simulation of type " + type);
if (type == null) {
throw new IllegalArgumentException("Impossible to run simulation of 'null' type");
}
switch (type) {
case RULE:
return RuleScenarioRunner::new;
case DMN:
return DMNScenarioRunner::new;
case PMML:
return PMMLScenarioRunner::new;
default:
throw new IllegalArgumentException("Impossible to run simulation of type " + type);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2021 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.drools.scenariosimulation.backend.runner;

import org.drools.scenariosimulation.backend.expression.ExpressionEvaluatorFactory;
import org.drools.scenariosimulation.backend.runner.model.ScenarioRunnerDTO;
import org.kie.api.runtime.KieContainer;

public class PMMLScenarioRunner extends AbstractScenarioRunner {

public PMMLScenarioRunner(KieContainer kieContainer, ScenarioRunnerDTO scenarioRunnerDTO) {
super(kieContainer,
scenarioRunnerDTO,
ExpressionEvaluatorFactory.create(
kieContainer.getClassLoader(),
scenarioRunnerDTO.getSettings().getType()));
}

@Override
protected AbstractRunnerHelper newRunnerHelper() {
return new PMMLScenarioRunnerHelper();
}
}
Loading