use of org.kie.dmn.model.api.DRGElement in project kie-wb-common by kiegroup.
the class DMNMarshallerStandaloneTest method testGetImportedDrgElementsByShape.
@Test
public void testGetImportedDrgElementsByShape() {
final DMNMarshallerStandalone marshaller = spy(getDMNMarshaller());
final List<org.kie.dmn.model.api.DRGElement> importedDRGElements = mock(List.class);
final Map<Import, org.kie.dmn.model.api.Definitions> importDefinitions = mock(Map.class);
final org.kie.dmn.model.api.DRGElement ref1 = mock(org.kie.dmn.model.api.DRGElement.class);
final org.kie.dmn.model.api.DRGElement ref2 = mock(org.kie.dmn.model.api.DRGElement.class);
final org.kie.dmn.model.api.DRGElement ref3 = mock(org.kie.dmn.model.api.DRGElement.class);
final List<DMNShape> dmnShapes = new ArrayList<>();
final DMNShape shape1 = mock(DMNShape.class);
final DMNShape shape2 = mock(DMNShape.class);
final DMNShape shape3 = mock(DMNShape.class);
dmnShapes.add(shape1);
dmnShapes.add(shape2);
dmnShapes.add(shape3);
doReturn("REF1").when(marshaller).getDmnElementRef(shape1);
doReturn("REF2").when(marshaller).getDmnElementRef(shape2);
doReturn("REF3").when(marshaller).getDmnElementRef(shape3);
when(dmnMarshallerImportsHelper.getImportedDRGElements(importDefinitions)).thenReturn(importedDRGElements);
doReturn(Optional.of(ref1)).when(marshaller).getReference(importedDRGElements, "REF1");
doReturn(Optional.of(ref2)).when(marshaller).getReference(importedDRGElements, "REF2");
doReturn(Optional.of(ref3)).when(marshaller).getReference(importedDRGElements, "REF3");
final List<DRGElement> actual = marshaller.getImportedDrgElementsByShape(dmnShapes, importDefinitions);
assertEquals(ref1, actual.get(0));
assertEquals(ref2, actual.get(1));
assertEquals(ref3, actual.get(2));
}
use of org.kie.dmn.model.api.DRGElement in project kie-wb-common by kiegroup.
the class DMNMarshallerImportsHelperStandaloneImplTest method testGetImportedDRGElements.
@Test
public void testGetImportedDRGElements() {
final Map<Import, Definitions> importDefinitions = new HashMap<>();
final Import anImport = mock(Import.class);
final Definitions definitions = mock(Definitions.class);
final DRGElement drgElement1 = mock(DRGElement.class);
final DRGElement drgElement2 = mock(DRGElement.class);
final DRGElement drgElement3 = mock(DRGElement.class);
final List<DRGElement> expectedDRGElements = asList(drgElement1, drgElement2, drgElement3);
doReturn(expectedDRGElements).when(helper).getDrgElementsWithNamespace(definitions, anImport);
importDefinitions.put(anImport, definitions);
final List<DRGElement> actualDRGElements = helper.getImportedDRGElements(importDefinitions);
assertEquals(expectedDRGElements, actualDRGElements);
}
use of org.kie.dmn.model.api.DRGElement in project kie-wb-common by kiegroup.
the class DMNMarshallerImportsHelperStandaloneImplTest method testGetDrgElementsWithNamespace.
@Test
public void testGetDrgElementsWithNamespace() {
final Definitions definitions = mock(Definitions.class);
final Import anImport = mock(Import.class);
final TDecision drgElement1 = new TDecision();
final TInputData drgElement2 = new TInputData();
final TDecisionService drgElement3 = new TDecisionService();
final InformationItem informationItem1 = new TInformationItem();
final InformationItem informationItem2 = new TInformationItem();
final InformationItem informationItem3 = new TInformationItem();
final List<DRGElement> drgElements = asList(drgElement1, drgElement2, drgElement3);
final String namespace = "http://github.com/kiegroup/_something";
final String builtInTypeNumber = BuiltInType.NUMBER.getName();
when(anImport.getName()).thenReturn("model");
when(anImport.getNamespace()).thenReturn(namespace);
informationItem1.setTypeRef(new QName(XMLConstants.NULL_NS_URI, "tUUID", XMLConstants.DEFAULT_NS_PREFIX));
informationItem2.setTypeRef(new QName(XMLConstants.NULL_NS_URI, "tAge", XMLConstants.DEFAULT_NS_PREFIX));
informationItem3.setTypeRef(new QName(XMLConstants.NULL_NS_URI, builtInTypeNumber, XMLConstants.DEFAULT_NS_PREFIX));
drgElement1.setId("0000-1111");
drgElement2.setId("2222-3333");
drgElement3.setId("4444-5555");
drgElement1.setName("Decision");
drgElement2.setName("Input Data");
drgElement3.setName("Decision Service");
drgElement1.setVariable(informationItem1);
drgElement2.setVariable(informationItem2);
drgElement3.setVariable(informationItem3);
when(definitions.getDrgElement()).thenReturn(drgElements);
final List<DRGElement> elements = helper.getDrgElementsWithNamespace(definitions, anImport);
assertEquals(3, elements.size());
final TDecision element1 = (TDecision) elements.get(0);
assertEquals("0000-1111", element1.getId());
assertEquals("model.Decision", element1.getName());
assertEquals("model.tUUID", element1.getVariable().getTypeRef().getLocalPart());
assertEquals(namespace, getNamespace(element1));
final TInputData element2 = (TInputData) elements.get(1);
assertEquals("2222-3333", element2.getId());
assertEquals("model.Input Data", element2.getName());
assertEquals("model.tAge", element2.getVariable().getTypeRef().getLocalPart());
assertEquals(namespace, getNamespace(element2));
final TDecisionService element3 = (TDecisionService) elements.get(2);
assertEquals("4444-5555", element3.getId());
assertEquals("model.Decision Service", element3.getName());
assertEquals(builtInTypeNumber, element3.getVariable().getTypeRef().getLocalPart());
assertEquals(namespace, getNamespace(element3));
}
use of org.kie.dmn.model.api.DRGElement in project droolsjbpm-integration by kiegroup.
the class ModelEvaluatorServiceBase method getModel.
public Response getModel(String containerId, String modelId) {
try {
KieContainerInstanceImpl kContainer = context.getContainer(containerId, ContainerLocatorProvider.get().getLocator());
DMNRuntime dmnRuntime = KieRuntimeFactory.of(kContainer.getKieContainer().getKieBase()).get(DMNRuntime.class);
List<DMNModel> modelsWithID = dmnRuntime.getModels().stream().filter(m -> m.getName().equals(modelId)).collect(Collectors.toList());
if (modelsWithID.isEmpty()) {
return Response.status(Status.NOT_FOUND).entity("No model identifies with modelId: " + modelId).build();
} else if (modelsWithID.size() > 1) {
return Response.status(Status.NOT_FOUND).entity("More than one existing DMN model having modelId: " + modelId).build();
}
DMNModel dmnModel = modelsWithID.get(0);
Definitions definitions = dmnModel.getDefinitions();
for (DRGElement drg : definitions.getDrgElement()) {
if (drg instanceof Decision) {
Decision decision = (Decision) drg;
decision.setExpression(null);
} else if (drg instanceof BusinessKnowledgeModel) {
BusinessKnowledgeModel bkm = (BusinessKnowledgeModel) drg;
bkm.setEncapsulatedLogic(null);
}
}
String xml = DMNMarshallerFactory.newDefaultMarshaller().marshal(definitions);
return Response.ok().entity(xml).build();
} catch (Exception e) {
LOG.error("Error from container '" + containerId + "'", e);
return Response.serverError().entity(e.getMessage()).build();
}
}
use of org.kie.dmn.model.api.DRGElement in project kogito-runtimes by kiegroup.
the class DecisionCodegen method generateAndStoreRestResources.
private void generateAndStoreRestResources() {
// REST resources
List<DecisionRestResourceGenerator> rgs = new ArrayList<>();
List<DMNModel> models = resources.stream().map(DMNResource::getDmnModel).collect(Collectors.toList());
DMNOASResult oasResult = null;
try {
oasResult = DMNOASGeneratorFactory.generator(models).build();
String jsonContent = new ObjectMapper().writeValueAsString(oasResult.getJsonSchemaNode());
storeFile(GeneratedFileType.STATIC_HTTP_RESOURCE, "dmnDefinitions.json", jsonContent);
} catch (Exception e) {
LOGGER.error("Error while trying to generate OpenAPI specification for the DMN models", e);
}
for (DMNModel model : models) {
if (model.getName() == null || model.getName().isEmpty()) {
throw new RuntimeException("Model name should not be empty");
}
boolean stronglyTypedEnabled = Optional.ofNullable(context()).flatMap(c -> c.getApplicationProperty(STRONGLY_TYPED_CONFIGURATION_KEY)).map(Boolean::parseBoolean).orElse(false);
if (stronglyTypedEnabled) {
generateStronglyTypedInput(model);
}
DecisionRestResourceGenerator resourceGenerator = new DecisionRestResourceGenerator(context(), model, applicationCanonicalName()).withStronglyTyped(stronglyTypedEnabled).withOASResult(oasResult, isMPAnnotationsPresent(), isIOSwaggerOASv3AnnotationsPresent());
rgs.add(resourceGenerator);
}
if (context().hasRESTForGenerator(this)) {
for (DecisionRestResourceGenerator resourceGenerator : rgs) {
if (context().getAddonsConfig().usePrometheusMonitoring()) {
generateAndStoreGrafanaDashboards(resourceGenerator);
}
storeFile(REST_TYPE, resourceGenerator.generatedFilePath(), resourceGenerator.generate());
}
}
DMNMarshaller marshaller = DMNMarshallerFactory.newDefaultMarshaller();
for (DMNResource resource : resources) {
DMNModel model = resource.getDmnModel();
Definitions definitions = model.getDefinitions();
for (DRGElement drg : definitions.getDrgElement()) {
if (drg instanceof Decision) {
Decision decision = (Decision) drg;
decision.setExpression(null);
} else if (drg instanceof BusinessKnowledgeModel) {
BusinessKnowledgeModel bkm = (BusinessKnowledgeModel) drg;
bkm.setEncapsulatedLogic(null);
}
}
String relativePath = CodegenStringUtil.escapeIdentifier(model.getNamespace()).replace(".", "/") + "/" + CodegenStringUtil.escapeIdentifier(model.getName()) + ".dmn_nologic";
storeFile(GeneratedFileType.INTERNAL_RESOURCE, relativePath, marshaller.marshal(definitions));
}
if (context().getAddonsConfig().useCloudEvents()) {
final DecisionCloudEventMetaFactoryGenerator ceMetaFactoryGenerator = new DecisionCloudEventMetaFactoryGenerator(context(), models);
storeFile(REST_TYPE, ceMetaFactoryGenerator.generatedFilePath(), ceMetaFactoryGenerator.generate());
}
}
Aggregations