Search in sources :

Example 21 with DRGElement

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));
}
Also used : Import(org.kie.dmn.model.api.Import) Definitions(org.kie.dmn.model.api.Definitions) ArrayList(java.util.ArrayList) DMNShape(org.kie.dmn.model.api.dmndi.DMNShape) DRGElement(org.kie.dmn.model.api.DRGElement) DRGElement(org.kie.dmn.model.api.DRGElement) Test(org.junit.Test)

Example 22 with DRGElement

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);
}
Also used : Import(org.kie.dmn.model.api.Import) HashMap(java.util.HashMap) Definitions(org.kie.dmn.model.api.Definitions) DRGElement(org.kie.dmn.model.api.DRGElement) Test(org.junit.Test)

Example 23 with DRGElement

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));
}
Also used : TInputData(org.kie.dmn.model.v1_2.TInputData) Import(org.kie.dmn.model.api.Import) TDecisionService(org.kie.dmn.model.v1_2.TDecisionService) QName(javax.xml.namespace.QName) Definitions(org.kie.dmn.model.api.Definitions) TDecision(org.kie.dmn.model.v1_2.TDecision) TInformationItem(org.kie.dmn.model.v1_2.TInformationItem) TInformationItem(org.kie.dmn.model.v1_2.TInformationItem) InformationItem(org.kie.dmn.model.api.InformationItem) DRGElement(org.kie.dmn.model.api.DRGElement) Test(org.junit.Test)

Example 24 with DRGElement

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();
    }
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNItemDefinitionInfo(org.kie.server.api.model.dmn.DMNItemDefinitionInfo) LoggerFactory(org.slf4j.LoggerFactory) ItemDefNodeImpl(org.kie.dmn.core.ast.ItemDefNodeImpl) DynamicDMNContextBuilder(org.kie.dmn.core.internal.utils.DynamicDMNContextBuilder) DMNContextKS(org.kie.server.api.model.dmn.DMNContextKS) ServiceResponse(org.kie.server.api.model.ServiceResponse) ReleaseId(org.kie.api.builder.ReleaseId) KieRuntimeFactory(org.kie.api.runtime.KieRuntimeFactory) DMNModel(org.kie.dmn.api.core.DMNModel) Map(java.util.Map) DMNFEELComparablePeriodSerializer(org.kie.server.services.dmn.modelspecific.DMNFEELComparablePeriodSerializer) TypeReference(com.fasterxml.jackson.core.type.TypeReference) DMNRuntimeEventListener(org.kie.dmn.api.core.event.DMNRuntimeEventListener) DMNResultKS(org.kie.server.api.model.dmn.DMNResultKS) DMNQNameInfo(org.kie.server.api.model.dmn.DMNQNameInfo) Collection(java.util.Collection) InputData(org.kie.dmn.model.api.InputData) Collectors(java.util.stream.Collectors) KieServerRegistry(org.kie.server.services.api.KieServerRegistry) MSConsts(org.kie.server.services.dmn.modelspecific.MSConsts) List(java.util.List) Response(javax.ws.rs.core.Response) DMNDecisionInfo(org.kie.server.api.model.dmn.DMNDecisionInfo) DMNContext(org.kie.dmn.api.core.DMNContext) ItemDefinition(org.kie.dmn.model.api.ItemDefinition) Optional(java.util.Optional) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode) QName(javax.xml.namespace.QName) DMNDecisionServiceInfo(org.kie.server.api.model.dmn.DMNDecisionServiceInfo) KogitoDMNResult(org.kie.server.services.dmn.modelspecific.KogitoDMNResult) OASGenerator(org.kie.server.services.dmn.modelspecific.OASGenerator) DMNEvaluationUtils(org.kie.dmn.core.internal.utils.DMNEvaluationUtils) DMNModelInfo(org.kie.server.api.model.dmn.DMNModelInfo) MarshallerHelper(org.kie.server.services.impl.marshal.MarshallerHelper) Definitions(org.kie.dmn.model.api.Definitions) KieContainerInstanceImpl(org.kie.server.services.impl.KieContainerInstanceImpl) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DMNMarshallerFactory(org.kie.dmn.backend.marshalling.v1x.DMNMarshallerFactory) PrometheusMetricsDMNListener(org.kie.server.services.prometheus.PrometheusMetricsDMNListener) Decision(org.kie.dmn.model.api.Decision) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) InputDataNodeImpl(org.kie.dmn.core.ast.InputDataNodeImpl) DMNEvaluationResult(org.kie.dmn.core.internal.utils.DMNEvaluationUtils.DMNEvaluationResult) XMLConstants(javax.xml.XMLConstants) Status(javax.ws.rs.core.Response.Status) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) DMNModelInfoList(org.kie.server.api.model.dmn.DMNModelInfoList) Logger(org.slf4j.Logger) DMNUnaryTestsInfo(org.kie.server.api.model.dmn.DMNUnaryTestsInfo) PrometheusKieServerExtension(org.kie.server.services.prometheus.PrometheusKieServerExtension) BusinessKnowledgeModel(org.kie.dmn.model.api.BusinessKnowledgeModel) DMNInputDataInfo(org.kie.server.api.model.dmn.DMNInputDataInfo) DecisionServiceNode(org.kie.dmn.api.core.ast.DecisionServiceNode) DRGElement(org.kie.dmn.model.api.DRGElement) ContainerLocatorProvider(org.kie.server.services.impl.locator.ContainerLocatorProvider) Definitions(org.kie.dmn.model.api.Definitions) BusinessKnowledgeModel(org.kie.dmn.model.api.BusinessKnowledgeModel) KieContainerInstanceImpl(org.kie.server.services.impl.KieContainerInstanceImpl) DMNRuntime(org.kie.dmn.api.core.DMNRuntime) DMNModel(org.kie.dmn.api.core.DMNModel) Decision(org.kie.dmn.model.api.Decision) DRGElement(org.kie.dmn.model.api.DRGElement)

Example 25 with DRGElement

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());
    }
}
Also used : DMNMarshaller(org.kie.dmn.api.marshalling.DMNMarshaller) DMNOASResult(org.kie.dmn.openapi.model.DMNOASResult) DecisionCloudEventMetaFactoryGenerator(org.kie.kogito.codegen.decision.events.DecisionCloudEventMetaFactoryGenerator) Definitions(org.kie.dmn.model.api.Definitions) ArrayList(java.util.ArrayList) Decision(org.kie.dmn.model.api.Decision) BusinessKnowledgeModel(org.kie.dmn.model.api.BusinessKnowledgeModel) DMNModel(org.kie.dmn.api.core.DMNModel) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DRGElement(org.kie.dmn.model.api.DRGElement)

Aggregations

DRGElement (org.kie.dmn.model.api.DRGElement)25 Definitions (org.kie.dmn.model.api.Definitions)15 Test (org.junit.Test)10 ArrayList (java.util.ArrayList)8 ItemDefinition (org.kie.dmn.model.api.ItemDefinition)8 Artifact (org.kie.dmn.model.api.Artifact)7 BusinessContextElement (org.kie.dmn.model.api.BusinessContextElement)7 Decision (org.kie.dmn.model.api.Decision)7 Import (org.kie.dmn.model.api.Import)7 BusinessKnowledgeModel (org.kie.dmn.model.api.BusinessKnowledgeModel)6 InputData (org.kie.dmn.model.api.InputData)6 DMNModel (org.kie.dmn.api.core.DMNModel)4 Association (org.kie.dmn.model.api.Association)4 DecisionService (org.kie.dmn.model.api.DecisionService)4 ElementCollection (org.kie.dmn.model.api.ElementCollection)4 KnowledgeSource (org.kie.dmn.model.api.KnowledgeSource)4 OrganizationUnit (org.kie.dmn.model.api.OrganizationUnit)4 PerformanceIndicator (org.kie.dmn.model.api.PerformanceIndicator)4 TextAnnotation (org.kie.dmn.model.api.TextAnnotation)4 DMNShape (org.kie.dmn.model.api.dmndi.DMNShape)4