use of org.kie.dmn.api.core.DMNModel in project drools by kiegroup.
the class DMNAssemblerService method compileResourceToModel.
private DMNModel compileResourceToModel(KnowledgeBuilderImpl kbuilderImpl, DMNCompiler dmnCompiler, Resource resource, Collection<DMNModel> dmnModels) {
DMNModel model = dmnCompiler.compile(resource, dmnModels);
if (model != null) {
String namespace = model.getNamespace();
PackageRegistry pkgReg = kbuilderImpl.getOrCreatePackageRegistry(new PackageDescr(namespace));
InternalKnowledgePackage kpkgs = pkgReg.getPackage();
kpkgs.addCloningResource(DMN_COMPILER_CACHE_KEY, dmnCompiler);
Map<ResourceType, ResourceTypePackage> rpkg = kpkgs.getResourceTypePackages();
DMNPackageImpl dmnpkg = (DMNPackageImpl) rpkg.get(ResourceType.DMN);
if (dmnpkg == null) {
dmnpkg = new DMNPackageImpl(namespace);
rpkg.put(ResourceType.DMN, dmnpkg);
} else {
if (dmnpkg.getModel(model.getName()) != null) {
kbuilderImpl.addBuilderResult(new DMNKnowledgeBuilderError(ResultSeverity.ERROR, resource, namespace, "Duplicate model name " + model.getName() + " in namespace " + namespace));
logger.error("Duplicate model name {} in namespace {}", model.getName(), namespace);
}
}
dmnpkg.addModel(model.getName(), model);
dmnpkg.addProfiles(kbuilderImpl.getCachedOrCreate(DMN_PROFILES_CACHE_KEY, () -> getDMNProfiles(kbuilderImpl)));
} else {
kbuilderImpl.addBuilderResult(new DMNKnowledgeBuilderError(ResultSeverity.ERROR, resource, "Unable to compile DMN model for the resource"));
logger.error("Unable to compile DMN model for resource {}", resource.getSourcePath());
}
return model;
}
use of org.kie.dmn.api.core.DMNModel in project drools by kiegroup.
the class DMNAssemblerService method internalAddResource.
private DMNModel internalAddResource(KnowledgeBuilderImpl kbuilder, DMNCompiler dmnCompiler, ResourceWithConfiguration r, Collection<DMNModel> dmnModels) throws Exception {
r.getBeforeAdd().accept(kbuilder);
DMNModel dmnModel = compileResourceToModel(kbuilder, dmnCompiler, r.getResource(), dmnModels);
r.getAfterAdd().accept(kbuilder);
return dmnModel;
}
use of org.kie.dmn.api.core.DMNModel in project drools by kiegroup.
the class DMNCompilerImpl method compile.
@Override
public DMNModel compile(Reader source, Collection<DMNModel> dmnModels) {
try {
Definitions dmndefs = getMarshaller().unmarshal(source);
DMNModel model = compile(dmndefs, dmnModels);
return model;
} catch (Exception e) {
logger.error("Error compiling model from source.", e);
}
return null;
}
use of org.kie.dmn.api.core.DMNModel in project drools by kiegroup.
the class ValidatorTest method testDryRun.
@Test
public void testDryRun() {
DMNRuntime runtime = DMNRuntimeUtil.createRuntime("0001-input-data-string.dmn", DMNInputRuntimeTest.class);
DMNModel dmnModel = runtime.getModel("https://github.com/kiegroup/drools/kie-dmn", "_0001-input-data-string");
assertThat(dmnModel, notNullValue());
Definitions definitions = dmnModel.getDefinitions();
assertThat(definitions, notNullValue());
DMNValidatorFactory.newValidator().validate(definitions);
}
use of org.kie.dmn.api.core.DMNModel in project drools by kiegroup.
the class InvokeFunction method invoke.
public FEELFnResult<Object> invoke(@ParameterName("ctx") EvaluationContext ctx, @ParameterName("namespace") String namespace, @ParameterName("model name") String modelName, @ParameterName("decision name") String decisionName, @ParameterName("parameters") Map<String, Object> parameters) {
DMNRuntime dmnRuntime = ctx.getDMNRuntime();
if (namespace == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "namespace", "cannot be null"));
}
if (modelName == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "model name", "cannot be null"));
}
if (decisionName == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "decision name", "cannot be null"));
}
if (parameters == null) {
return FEELFnResult.ofError(new InvalidParametersEvent(FEELEvent.Severity.ERROR, "parameters", "cannot be null"));
}
FEELEvent capturedException = null;
try {
ctx.enterFrame();
DMNModel dmnModel = dmnRuntime.getModel(namespace, modelName);
if (dmnModel == null) {
return FEELFnResult.ofError(new FEELEventBase(FEELEvent.Severity.ERROR, "Cannot find model '" + modelName + "' in namespace " + namespace, null));
}
if (dmnModel.getDecisionByName(decisionName) == null) {
return FEELFnResult.ofError(new FEELEventBase(FEELEvent.Severity.ERROR, "Cannot find decision '" + decisionName + "' in the model", null));
}
DMNContext dmnContext = dmnRuntime.newContext();
dmnContext.getAll().putAll(parameters);
DMNResult requiredDecisionResult = dmnRuntime.evaluateByName(dmnModel, dmnContext, decisionName);
if (requiredDecisionResult.hasErrors()) {
return FEELFnResult.ofError(new FEELEventBase(FEELEvent.Severity.ERROR, "Errors occurred while invoking the external decision: " + requiredDecisionResult.getMessages(), null));
}
return FEELFnResult.ofResult(requiredDecisionResult.getContext().get(decisionName));
} catch (Exception e) {
capturedException = new FEELEventBase(FEELEvent.Severity.ERROR, "Error invoking function", new RuntimeException("Error invoking function " + getName() + ".", e));
} finally {
ctx.exitFrame();
}
return FEELFnResult.ofError(capturedException);
}
Aggregations