use of org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDecisionService in project kie-wb-common by kiegroup.
the class DecisionServiceConverter method dmnFromNode.
@Override
@SuppressWarnings("unchecked")
public JSITDecisionService dmnFromNode(final Node<View<DecisionService>, ?> node, final Consumer<JSITComponentWidths> componentWidthsConsumer) {
final DecisionService source = (DecisionService) DefinitionUtils.getElementDefinition(node);
final JSITDecisionService ds = new JSITDecisionService();
ds.setId(source.getId().getValue());
final Optional<String> description = Optional.ofNullable(DescriptionPropertyConverter.dmnFromWB(source.getDescription()));
description.ifPresent(ds::setDescription);
ds.setName(source.getName().getValue());
final JSITInformationItem variable = InformationItemPrimaryPropertyConverter.dmnFromWB(source.getVariable(), source);
ds.setVariable(variable);
final List<JSITDMNElementReference> existing_outputDecision = source.getOutputDecision().stream().map(DMNElementReferenceConverter::dmnFromWB).collect(Collectors.toList());
final List<JSITDMNElementReference> existing_encapsulatedDecision = source.getEncapsulatedDecision().stream().map(DMNElementReferenceConverter::dmnFromWB).collect(Collectors.toList());
final List<JSITDMNElementReference> existing_inputDecision = source.getInputDecision().stream().map(DMNElementReferenceConverter::dmnFromWB).collect(Collectors.toList());
final List<JSITDMNElementReference> existing_inputData = source.getInputData().stream().map(DMNElementReferenceConverter::dmnFromWB).collect(Collectors.toList());
final List<JSITDMNElementReference> candidate_outputDecision = new ArrayList<>();
final List<JSITDMNElementReference> candidate_encapsulatedDecision = new ArrayList<>();
final List<JSITDMNElementReference> candidate_inputDecision = new ArrayList<>();
final List<JSITDMNElementReference> candidate_inputData = new ArrayList<>();
final HashSet<InputData> reqInputs = new HashSet();
final HashSet<Decision> reqDecisions = new HashSet();
// DMN spec table 2: Requirements connection rules
final List<Edge<?, ?>> outEdges = (List<Edge<?, ?>>) node.getOutEdges();
for (Edge<?, ?> e : outEdges) {
if (e.getContent() instanceof Child) {
@SuppressWarnings("unchecked") final Node<View<?>, ?> targetNode = e.getTargetNode();
final View<?> targetNodeView = targetNode.getContent();
if (targetNodeView.getDefinition() instanceof DRGElement) {
final DRGElement drgElement = (DRGElement) targetNodeView.getDefinition();
if (drgElement instanceof Decision) {
final Decision decision = (Decision) drgElement;
final JSITDMNElementReference ri = new JSITDMNElementReference();
ri.setHref("#" + decision.getId().getValue());
if (isOutputDecision(targetNode.getContent(), node.getContent())) {
candidate_outputDecision.add(ri);
} else {
candidate_encapsulatedDecision.add(ri);
final List<Node> all = diagramsSession.getNodesFromAllDiagramsWithContentId(drgElement.getContentDefinitionId());
for (final Node other : all) {
if (!Objects.equals(other, targetNode)) {
inspectDecisionForDSReqs(other, reqInputs, reqDecisions);
}
}
}
inspectDecisionForDSReqs(targetNode, reqInputs, reqDecisions);
} else {
throw new UnsupportedOperationException("wrong model definition: a DecisionService is expected to encapsulate only Decision");
}
}
} else if (e.getContent() instanceof View && ((View) e.getContent()).getDefinition() instanceof KnowledgeRequirement) {
// this was taken care by the receiving Decision or BKM.
} else {
throw new UnsupportedOperationException("wrong model definition.");
}
}
reqInputs.stream().sorted(Comparator.comparing(x -> x.getName().getValue())).map(x -> {
final JSITDMNElementReference ri = new JSITDMNElementReference();
ri.setHref("#" + x.getId().getValue());
return ri;
}).forEach(ri -> candidate_inputData.add(Js.uncheckedCast(ri)));
reqDecisions.stream().sorted(Comparator.comparing(x -> x.getName().getValue())).map(x -> {
final JSITDMNElementReference ri = new JSITDMNElementReference();
ri.setHref("#" + x.getId().getValue());
return ri;
}).forEach(rs -> candidate_inputDecision.add(Js.uncheckedCast(rs)));
for (int i = 0; i < candidate_outputDecision.size(); i++) {
final JSITDMNElementReference er = Js.uncheckedCast(candidate_outputDecision.get(i));
candidate_inputDecision.removeIf(x -> x.getHref().equals(er.getHref()));
}
for (int i = 0; i < candidate_encapsulatedDecision.size(); i++) {
final JSITDMNElementReference er = Js.uncheckedCast(candidate_encapsulatedDecision.get(i));
candidate_inputDecision.removeIf(x -> x.getHref().equals(er.getHref()));
}
ds.setInputData(reconcileExistingAndCandidate(existing_inputData, candidate_inputData));
ds.setInputDecision(reconcileExistingAndCandidate(existing_inputDecision, candidate_inputDecision));
ds.setEncapsulatedDecision(reconcileExistingAndCandidate(existing_encapsulatedDecision, candidate_encapsulatedDecision));
ds.setOutputDecision(reconcileExistingAndCandidate(existing_outputDecision, candidate_outputDecision));
DMNExternalLinksToExtensionElements.loadExternalLinksIntoExtensionElements(source, ds);
return ds;
}
use of org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDecisionService in project kie-wb-common by kiegroup.
the class NodeConnector method connect.
void connect(final JSIDMNDiagram dmnDiagram, final List<JSIDMNEdge> edges, final List<JSITAssociation> associations, final List<NodeEntry> nodeEntries, final boolean isDMNDIPresent) {
final Map<String, List<NodeEntry>> entriesById = makeNodeIndex(nodeEntries);
final String diagramId = dmnDiagram.getId();
for (final NodeEntry nodeEntry : nodeEntries) {
final JSITDMNElement element = nodeEntry.getDmnElement();
final Node node = nodeEntry.getNode();
// For imported nodes, we don't have its connections
if (nodeEntry.isIncluded()) {
continue;
}
// DMN spec table 2: Requirements
if (JSITDecision.instanceOf(element)) {
final JSITDecision decision = Js.uncheckedCast(element);
final List<JSITInformationRequirement> jsiInformationRequirements = decision.getInformationRequirement();
for (int i = 0; i < jsiInformationRequirements.size(); i++) {
final JSITInformationRequirement ir = Js.uncheckedCast(jsiInformationRequirements.get(i));
connectEdgeToNodes(INFO_REQ_ID, ir, ir.getRequiredInput(), entriesById, diagramId, edges, isDMNDIPresent, node);
connectEdgeToNodes(INFO_REQ_ID, ir, ir.getRequiredDecision(), entriesById, diagramId, edges, isDMNDIPresent, node);
}
final List<JSITKnowledgeRequirement> jsiKnowledgeRequirements = decision.getKnowledgeRequirement();
for (int i = 0; i < jsiKnowledgeRequirements.size(); i++) {
final JSITKnowledgeRequirement kr = Js.uncheckedCast(jsiKnowledgeRequirements.get(i));
connectEdgeToNodes(KNOWLEDGE_REQ_ID, kr, kr.getRequiredKnowledge(), entriesById, diagramId, edges, isDMNDIPresent, node);
}
final List<JSITAuthorityRequirement> jsiAuthorityRequirements = decision.getAuthorityRequirement();
for (int i = 0; i < jsiAuthorityRequirements.size(); i++) {
final JSITAuthorityRequirement ar = Js.uncheckedCast(jsiAuthorityRequirements.get(i));
connectEdgeToNodes(AUTH_REQ_ID, ar, ar.getRequiredAuthority(), entriesById, diagramId, edges, isDMNDIPresent, node);
}
continue;
}
if (JSITBusinessKnowledgeModel.instanceOf(element)) {
final JSITBusinessKnowledgeModel bkm = Js.uncheckedCast(element);
final List<JSITKnowledgeRequirement> jsiKnowledgeRequirements = bkm.getKnowledgeRequirement();
for (int i = 0; i < jsiKnowledgeRequirements.size(); i++) {
final JSITKnowledgeRequirement kr = Js.uncheckedCast(jsiKnowledgeRequirements.get(i));
connectEdgeToNodes(KNOWLEDGE_REQ_ID, kr, kr.getRequiredKnowledge(), entriesById, diagramId, edges, isDMNDIPresent, node);
}
final List<JSITAuthorityRequirement> jsiAuthorityRequirements = bkm.getAuthorityRequirement();
for (int i = 0; i < jsiAuthorityRequirements.size(); i++) {
final JSITAuthorityRequirement ar = Js.uncheckedCast(jsiAuthorityRequirements.get(i));
connectEdgeToNodes(AUTH_REQ_ID, ar, ar.getRequiredAuthority(), entriesById, diagramId, edges, isDMNDIPresent, node);
}
continue;
}
if (JSITKnowledgeSource.instanceOf(element)) {
final JSITKnowledgeSource ks = Js.uncheckedCast(element);
final List<JSITAuthorityRequirement> jsiAuthorityRequirements = ks.getAuthorityRequirement();
for (int i = 0; i < jsiAuthorityRequirements.size(); i++) {
final JSITAuthorityRequirement ar = Js.uncheckedCast(jsiAuthorityRequirements.get(i));
connectEdgeToNodes(AUTH_REQ_ID, ar, ar.getRequiredInput(), entriesById, diagramId, edges, isDMNDIPresent, node);
connectEdgeToNodes(AUTH_REQ_ID, ar, ar.getRequiredDecision(), entriesById, diagramId, edges, isDMNDIPresent, node);
connectEdgeToNodes(AUTH_REQ_ID, ar, ar.getRequiredAuthority(), entriesById, diagramId, edges, isDMNDIPresent, node);
}
continue;
}
if (JSITDecisionService.instanceOf(element)) {
final JSITDecisionService ds = Js.uncheckedCast(element);
final List<JSITDMNElementReference> encapsulatedDecisions = ds.getEncapsulatedDecision();
forEach(encapsulatedDecisions, er -> {
final String reqInputID = getId(er);
getNode(nodeEntry, reqInputID, entriesById).ifPresent(requiredNode -> {
connectDSChildEdge(node, requiredNode);
});
});
final List<JSITDMNElementReference> outputDecisions = ds.getOutputDecision();
forEach(outputDecisions, er -> {
final String reqInputID = getId(er);
getNode(nodeEntry, reqInputID, entriesById).ifPresent(requiredNode -> {
connectDSChildEdge(node, requiredNode);
});
});
}
}
forEach(associations, association -> {
final String sourceId = getId(association.getSourceRef());
final String targetId = getId(association.getTargetRef());
final List<NodeEntry> source = entriesById.get(sourceId);
final List<NodeEntry> target = entriesById.get(targetId);
final boolean sourcePresent = source != null && source.size() > 0;
final boolean targetPresent = target != null && target.size() > 0;
if (sourcePresent && targetPresent) {
final NodeEntry sourceEntry = source.get(0);
final NodeEntry targetEntry = target.get(0);
final Node sourceNode = sourceEntry.getNode();
final Node targetNode = targetEntry.getNode();
@SuppressWarnings("unchecked") final Edge<View<Association>, ?> myEdge = (Edge<View<Association>, ?>) factoryManager.newElement(diagramId + "#" + association.getId(), ASSOCIATION_ID).asEdge();
final ViewConnector connectionContent = (ViewConnector) myEdge.getContent();
final Id id = new Id(association.getId());
final Description description = new Description(association.getDescription());
final Association definition = new Association(id, description);
connectEdge(myEdge, sourceNode, targetNode);
connectionContent.setDefinition(definition);
connectionContent.setTargetConnection(MagnetConnection.Builder.atCenter(targetNode));
connectionContent.setSourceConnection(MagnetConnection.Builder.atCenter(sourceNode));
}
});
}
use of org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDecisionService in project kie-wb-common by kiegroup.
the class DMNUnmarshaller method unmarshall.
private Promise<Graph> unmarshall(final Metadata metadata, final JSITDefinitions dmnDefinitions, final Map<JSITImport, JSITDefinitions> importDefinitions, final Map<JSITImport, PMMLDocumentMetadata> pmmlDocuments) {
final Map<String, HasComponentWidths> hasComponentWidthsMap = new HashMap<>();
final BiConsumer<String, HasComponentWidths> hasComponentWidthsConsumer = (uuid, hcw) -> {
if (Objects.nonNull(uuid)) {
hasComponentWidthsMap.put(uuid, hcw);
}
};
// Check before the DRG creation ('ensureDRGElementExists').
final boolean isDMNDIPresent = Optional.ofNullable(dmnDefinitions.getDMNDI()).isPresent();
ensureDRGElementExists(dmnDefinitions);
final Definitions wbDefinitions = DefinitionsConverter.wbFromDMN(dmnDefinitions, importDefinitions, pmmlDocuments);
final List<NodeEntry> nodeEntries = modelToStunnerConverter.makeNodes(dmnDefinitions, importDefinitions, isDMNDIPresent, hasComponentWidthsConsumer);
final List<JSITDecisionService> dmnDecisionServices = getDecisionServices(nodeEntries);
// Ensure all locations are updated to relative for Stunner
nodeEntries.forEach(e -> PointUtils.convertToRelativeBounds(e.getNode()));
final Map<String, Diagram> stunnerDiagramsById = new HashMap<>();
final Map<String, DMNDiagramElement> dmnDiagramsById = new HashMap<>();
for (final DMNDiagramElement dmnDiagramElement : wbDefinitions.getDiagramElements()) {
final String dmnDiagramId = dmnDiagramElement.getId().getValue();
final Diagram value = factoryManager.newDiagram(dmnDiagramId, BindableAdapterUtils.getDefinitionSetId(DMNDefinitionSet.class), metadata);
stunnerDiagramsById.put(dmnDiagramId, value);
dmnDiagramsById.put(dmnDiagramId, dmnDiagramElement);
}
final DMNDiagramsSessionState state = dmnDiagramsSession.setState(metadata, stunnerDiagramsById, dmnDiagramsById);
nodeEntries.forEach(nodeEntry -> {
final String diagramId = nodeEntry.getDiagramId();
final Graph graph = stunnerDiagramsById.get(diagramId).getGraph();
graph.addNode(nodeEntry.getNode());
});
final Graph drgGraph = state.getDRGDiagram().getGraph();
loadImportedItemDefinitions(wbDefinitions, importDefinitions);
for (final Diagram value : stunnerDiagramsById.values()) {
final Node<?, ?> dmnDiagramRoot = DMNGraphUtils.findDMNDiagramRoot(value.getGraph());
((View<DMNDiagram>) dmnDiagramRoot.getContent()).getDefinition().setDefinitions(wbDefinitions);
nodeEntries.forEach(nodeEntry -> {
if (Objects.equals(stunnerDiagramsById.get(nodeEntry.getDiagramId()), value)) {
connectRootWithChild(dmnDiagramRoot, nodeEntry.getNode());
}
});
}
// Only connect Nodes to the Diagram that are not referenced by DecisionServices
final List<String> references = new ArrayList<>();
final List<JSITDecisionService> lstDecisionServices = new ArrayList<>(dmnDecisionServices);
for (int iDS = 0; iDS < lstDecisionServices.size(); iDS++) {
final JSITDecisionService jsiDecisionService = Js.uncheckedCast(lstDecisionServices.get(iDS));
final List<JSITDMNElementReference> jsiEncapsulatedDecisions = jsiDecisionService.getEncapsulatedDecision();
if (Objects.nonNull(jsiEncapsulatedDecisions)) {
for (int i = 0; i < jsiEncapsulatedDecisions.size(); i++) {
final JSITDMNElementReference jsiEncapsulatedDecision = Js.uncheckedCast(jsiEncapsulatedDecisions.get(i));
references.add(jsiEncapsulatedDecision.getHref());
}
}
final List<JSITDMNElementReference> jsiOutputDecisions = jsiDecisionService.getOutputDecision();
if (Objects.nonNull(jsiOutputDecisions)) {
for (int i = 0; i < jsiOutputDecisions.size(); i++) {
final JSITDMNElementReference jsiOutputDecision = Js.uncheckedCast(jsiOutputDecisions.get(i));
references.add(jsiOutputDecision.getHref());
}
}
}
// Copy ComponentWidths information
final List<JSITComponentsWidthsExtension> extensions = findComponentsWidthsExtensions(dmnDefinitions.getDMNDI().getDMNDiagram());
extensions.forEach(componentsWidthsExtension -> {
// can be imported from another diagram but the extension is not imported or present in this diagram.
if (Objects.nonNull(componentsWidthsExtension.getComponentWidths())) {
hasComponentWidthsMap.entrySet().forEach(es -> {
final List<JSITComponentWidths> jsiComponentWidths = componentsWidthsExtension.getComponentWidths();
for (int i = 0; i < jsiComponentWidths.size(); i++) {
final JSITComponentWidths jsiWidths = Js.uncheckedCast(jsiComponentWidths.get(i));
if (Objects.equals(jsiWidths.getDmnElementRef(), es.getKey())) {
final List<Double> widths = es.getValue().getComponentWidths();
if (Objects.nonNull(jsiWidths.getWidth())) {
widths.clear();
for (int w = 0; w < jsiWidths.getWidth().size(); w++) {
final double width = jsiWidths.getWidth().get(w).doubleValue();
widths.add(width);
}
}
}
}
});
}
});
return promises.resolve(drgGraph);
}
use of org.kie.workbench.common.dmn.webapp.kogito.marshaller.js.model.dmn12.JSITDecisionService in project kie-wb-common by kiegroup.
the class DecisionServiceConverter method nodeFromDMN.
@Override
public Node<View<DecisionService>, ?> nodeFromDMN(final NodeEntry nodeEntry) {
final JSITDecisionService dmn = Js.uncheckedCast(nodeEntry.getDmnElement());
@SuppressWarnings("unchecked") final Node<View<DecisionService>, ?> node = (Node<View<DecisionService>, ?>) factoryManager.newElement(nodeEntry.getId(), getDefinitionId(DecisionService.class)).asNode();
final Id id = IdPropertyConverter.wbFromDMN(dmn.getId());
final Description description = DescriptionPropertyConverter.wbFromDMN(dmn.getDescription());
final Name name = new Name(dmn.getName());
final InformationItemPrimary informationItem = InformationItemPrimaryPropertyConverter.wbFromDMN(dmn.getVariable(), dmn);
final List<DMNElementReference> outputDecision = new ArrayList<>();
final List<DMNElementReference> encapsulatedDecision = new ArrayList<>();
final List<DMNElementReference> inputDecision = new ArrayList<>();
final List<DMNElementReference> inputData = new ArrayList<>();
final List<JSITDMNElementReference> jsiOutputDecisions = dmn.getOutputDecision();
if (Objects.nonNull(jsiOutputDecisions)) {
for (int i = 0; i < jsiOutputDecisions.size(); i++) {
final JSITDMNElementReference jsiOutputDecision = Js.uncheckedCast(jsiOutputDecisions.get(i));
outputDecision.add(DMNElementReferenceConverter.wbFromDMN(jsiOutputDecision));
}
}
final List<JSITDMNElementReference> jsiEncapsulatedDecisions = dmn.getEncapsulatedDecision();
if (Objects.nonNull(jsiEncapsulatedDecisions)) {
for (int i = 0; i < jsiEncapsulatedDecisions.size(); i++) {
final JSITDMNElementReference jsiEncapsulatedDecision = Js.uncheckedCast(jsiEncapsulatedDecisions.get(i));
encapsulatedDecision.add(DMNElementReferenceConverter.wbFromDMN(jsiEncapsulatedDecision));
}
}
final List<JSITDMNElementReference> jsiInputDecisions = dmn.getInputDecision();
if (Objects.nonNull(jsiInputDecisions)) {
for (int i = 0; i < jsiInputDecisions.size(); i++) {
final JSITDMNElementReference jsiInputDecision = Js.uncheckedCast(jsiInputDecisions.get(i));
inputDecision.add(DMNElementReferenceConverter.wbFromDMN(jsiInputDecision));
}
}
final List<JSITDMNElementReference> jsiInputDatas = dmn.getInputData();
if (Objects.nonNull(jsiInputDatas)) {
for (int i = 0; i < jsiInputDatas.size(); i++) {
final JSITDMNElementReference jsiInputData = Js.uncheckedCast(jsiInputDatas.get(i));
inputData.add(DMNElementReferenceConverter.wbFromDMN(jsiInputData));
}
}
final DecisionService decisionService = new DecisionService(id, description, name, informationItem, outputDecision, encapsulatedDecision, inputDecision, inputData, new StylingSet(), new DecisionServiceRectangleDimensionsSet(), new DecisionServiceDividerLineY());
decisionService.setDiagramId(nodeEntry.getDiagramId());
node.getContent().setDefinition(decisionService);
if (Objects.nonNull(informationItem)) {
informationItem.setParent(decisionService);
}
DMNExternalLinksToExtensionElements.loadExternalLinksFromExtensionElements(dmn, decisionService);
return node;
}
Aggregations