io.automatiko.engine.decision.dmn.DmnRuntimeProvider Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of automatiko-decision-dmn Show documentation
Show all versions of automatiko-decision-dmn Show documentation
DMN Decision support for Automatiko Engine
package io.automatiko.engine.decision.dmn;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.function.Consumer;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.drools.core.io.impl.ByteArrayResource;
import org.drools.core.io.impl.ClassPathResource;
import org.drools.core.io.impl.FileSystemResource;
import org.drools.kiesession.rulebase.InternalKnowledgeBase;
import org.kie.api.io.ResourceConfiguration;
import org.kie.api.io.ResourceWithConfiguration;
import org.kie.dmn.api.core.DMNCompiler;
import org.kie.dmn.api.core.DMNModel;
import org.kie.dmn.api.core.DMNRuntime;
import org.kie.dmn.api.core.event.DMNRuntimeEventListener;
import org.kie.dmn.api.marshalling.DMNMarshaller;
import org.kie.dmn.backend.marshalling.v1x.DMNMarshallerFactory;
import org.kie.dmn.core.assembler.DMNAssemblerService;
import org.kie.dmn.core.assembler.DMNResource;
import org.kie.dmn.core.assembler.DMNResourceDependenciesSorter;
import org.kie.dmn.core.compiler.DMNCompilerConfigurationImpl;
import org.kie.dmn.core.compiler.DMNCompilerImpl;
import org.kie.dmn.core.compiler.DMNProfile;
import org.kie.dmn.core.compiler.profiles.ExtendedDMNProfile;
import org.kie.dmn.core.impl.DMNRuntimeImpl;
import org.kie.dmn.core.impl.DMNRuntimeKB;
import org.kie.dmn.feel.util.Either;
import org.kie.dmn.model.api.Definitions;
import io.automatiko.engine.api.io.Resource;
public class DmnRuntimeProvider {
public static DMNRuntime fromClassPath(String... location) {
return fromResources(Stream.of(location).map(l -> new org.drools.core.io.impl.ClassPathResource(l))
.collect(Collectors.toList()))
.getOrElseThrow(e -> new RuntimeException("Error initalizing DMNRuntime", e));
}
public static DMNRuntime fromFiles(String... location) {
return fromResources(Stream.of(location).map(l -> new org.drools.core.io.impl.FileSystemResource(l))
.collect(Collectors.toList()))
.getOrElseThrow(e -> new RuntimeException("Error initalizing DMNRuntime", e));
}
public static DMNRuntime from(io.automatiko.engine.services.io.ByteArrayResource... resources) {
return fromResources(Stream.of(resources).map(l -> new org.drools.core.io.impl.ByteArrayResource(l))
.collect(Collectors.toList()))
.getOrElseThrow(e -> new RuntimeException("Error initalizing DMNRuntime", e));
}
public static DMNRuntime from(List resources) {
return fromResources(resources.stream().map(l -> convert(l)).collect(Collectors.toList()))
.getOrElseThrow(e -> new RuntimeException("Error initalizing DMNRuntime", e));
}
private static Either fromResources(Collection resources) {
ExtendedDMNProfile profile = new ExtendedDMNProfile();
DMNCompilerConfigurationImpl cc = new DMNCompilerConfigurationImpl();
cc.addExtensions(profile.getExtensionRegisters());
cc.addDRGElementCompilers(profile.getDRGElementCompilers());
cc.addFEELProfile(profile);
DMNCompiler dmnCompiler = new DMNCompilerImpl(cc);
List dmnResources = new ArrayList<>();
for (org.kie.api.io.Resource r : resources) {
Definitions definitions;
try {
definitions = getMarshaller(cc).unmarshal(r.getReader());
} catch (IOException e) {
return Either.ofLeft(e);
}
DMNResource dmnResource = new DMNResource(definitions, new ResourceWithConfiguration() {
@Override
public ResourceConfiguration getResourceConfiguration() {
// TODO Auto-generated method stub
return null;
}
@Override
public org.kie.api.io.Resource getResource() {
return (org.kie.api.io.Resource) r;
}
@Override
public Consumer
© 2015 - 2024 Weber Informatics LLC | Privacy Policy