You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kie.apache.org by "tiagobento (via GitHub)" <gi...@apache.org> on 2024/02/23 16:05:59 UTC

[PR] kie-issues#888: Decision Services & multiple DRDs: Adding a Decision Service to a DRD [incubator-kie-tools]

tiagobento opened a new pull request, #2181:
URL: https://github.com/apache/incubator-kie-tools/pull/2181

   Closes https://github.com/apache/incubator-kie-issues/issues/888
   
   
   Before:
   
   https://github.com/apache/incubator-kie-tools/assets/1584568/8a83a7e7-b2b3-4480-83be-c05f0a418c71
   
   After:
   
   https://github.com/apache/incubator-kie-tools/assets/1584568/d131d864-34e2-41c4-984e-510bdcbf5961
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org


Re: [PR] kie-issues#888: Decision Services & multiple DRDs: Adding a Decision Service to a DRD [incubator-kie-tools]

Posted by "tiagobento (via GitHub)" <gi...@apache.org>.
tiagobento commented on code in PR #2181:
URL: https://github.com/apache/incubator-kie-tools/pull/2181#discussion_r1505014410


##########
packages/dmn-editor/src/store/computed/computeDecisionServiceHrefsByDecisionHrefs.ts:
##########
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { getDecisionServicePropertiesRelativeToThisDmn } from "../../mutations/addExistingDecisionServiceToDrd";
+import { buildXmlHref } from "../../xml/xmlHrefs";
+import { State } from "../Store";
+
+export function computeDecisionServiceHrefsByDecisionHrefs({
+  thisDmnsNamespace,
+  drgElementsNamespace,
+  drgElements,
+}: {
+  thisDmnsNamespace: string;
+  drgElementsNamespace: string;
+  drgElements: State["dmn"]["model"]["definitions"]["drgElement"];
+}) {
+  drgElements ??= [];
+  const ret = new Map<string, string[]>();

Review Comment:
   Renamed



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org


Re: [PR] kie-issues#888: Decision Services & multiple DRDs: Adding a Decision Service to a DRD [incubator-kie-tools]

Posted by "tiagobento (via GitHub)" <gi...@apache.org>.
tiagobento merged PR #2181:
URL: https://github.com/apache/incubator-kie-tools/pull/2181


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org


Re: [PR] kie-issues#888: Decision Services & multiple DRDs: Adding a Decision Service to a DRD [incubator-kie-tools]

Posted by "tiagobento (via GitHub)" <gi...@apache.org>.
tiagobento commented on code in PR #2181:
URL: https://github.com/apache/incubator-kie-tools/pull/2181#discussion_r1500875690


##########
packages/dmn-editor/src/diagram/Diagram.tsx:
##########
@@ -357,39 +367,61 @@ export const Diagram = React.forwardRef<DiagramRef, { container: React.RefObject
             });
 
             const namespaceName = getXmlNamespaceDeclarationName({
-              model: state.dmn.model.definitions,
+              rootElement: state.dmn.model.definitions,
               namespace: externalNode.externalDrgElementNamespace,
             });
 
-            if (!namespaceName) {
-              throw new Error(`Can't find namespace name for '${externalNode.externalDrgElementNamespace}'.`);
-            }
+            const externalNodeHref = buildXmlHref({
+              namespace: externalNode.externalDrgElementNamespace,
+              id: externalNode.externalDrgElementId,
+            });
 
-            addShape({
-              definitions: state.dmn.model.definitions,
-              drdIndex: state.diagram.drdIndex,
-              nodeType: externalNodeType,
-              shape: {
-                "@_dmnElementRef": buildXmlQName({
-                  type: "xml-qname",
-                  prefix: namespaceName,
-                  localPart: externalDrgElement["@_id"]!,
-                }),
-                "@_isCollapsed": true,
-                "dc:Bounds": {
-                  "@_x": dropPoint.x,
-                  "@_y": dropPoint.y,
-                  "@_width": defaultExternalNodeDimensions["@_width"],
-                  "@_height": defaultExternalNodeDimensions["@_height"],
+            if (externalDrgElement.__$$element === "decisionService") {
+              addExistingDecisionServiceToDrd({
+                decisionService: externalDrgElement,
+                decisionServiceNamespace: externalNodeDmn.model.definitions["@_namespace"],
+                drdIndex: state.diagram.drdIndex,
+                dropPoint,
+                externalDmnsIndex,
+                thisDmnsDefinitions: state.dmn.model.definitions,
+                thisDmnsIndexedDrd: state.computed(state).indexedDrd(),
+                thisDmnsNamespace: state.dmn.model.definitions["@_namespace"],
+              });
+            } else if (externalDrgElement.__$$element === "decision") {
+              // TODO: Tiago --> Add logic for Decision contained by DS.

Review Comment:
   Will be addressed on https://github.com/apache/incubator-kie-issues/issues/894



##########
packages/dmn-editor/src/mutations/addExistingDecisionServiceToDrd.ts:
##########
@@ -0,0 +1,248 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import {
+  DMN15__tDecisionService,
+  DMN15__tDefinitions,
+} from "@kie-tools/dmn-marshaller/dist/schemas/dmn-1_5/ts-gen/types";
+import { DECISION_SERVICE_COLLAPSED_DIMENSIONS } from "../diagram/nodes/DefaultSizes";
+import { NODE_TYPES } from "../diagram/nodes/NodeTypes";
+import { Computed } from "../store/Store";
+import { computeDecisionServiceHrefsByDecisionHrefs } from "../store/computed/computeDecisionServiceHrefsByDecisionHrefs";
+import { computeIndexedDrd } from "../store/computed/computeIndexes";
+import { xmlHrefToQName } from "../xml/xmlHrefToQName";
+import { buildXmlHref, parseXmlHref } from "../xml/xmlHrefs";
+import { addShape } from "./addShape";
+import { repositionNode } from "./repositionNode";
+
+/**
+ * When adding a Decision Service to a DRD, we need to bring all its encapsulated and output Decisions with it,
+ * copying their layout from other DRDs, or formatting with autolayout.
+ */
+export function addExistingDecisionServiceToDrd({
+  decisionServiceNamespace,
+  decisionService,
+  externalDmnsIndex,
+  thisDmnsNamespace,
+  thisDmnsDefinitions,
+  thisDmnsIndexedDrd,
+  drdIndex,
+  dropPoint,
+}: {
+  decisionServiceNamespace: string;
+  decisionService: DMN15__tDecisionService;
+  externalDmnsIndex: ReturnType<Computed["getExternalModelTypesByNamespace"]>["dmns"];
+  thisDmnsNamespace: string;
+  thisDmnsDefinitions: DMN15__tDefinitions;
+  thisDmnsIndexedDrd: ReturnType<Computed["indexedDrd"]>;
+  drdIndex: number;
+  dropPoint: { x: number; y: number };
+}) {
+  const decisionServiceDmnDefinitions =
+    !decisionServiceNamespace || decisionServiceNamespace === thisDmnsNamespace
+      ? thisDmnsDefinitions
+      : externalDmnsIndex.get(decisionServiceNamespace)?.model.definitions;
+  if (!decisionServiceDmnDefinitions) {
+    throw new Error(`DMN MUTATION: Can't find definitions for model with namespace ${decisionServiceNamespace}`);
+  }
+  const { decisionServiceNamespaceForHref, containedDecisionHrefsRelativeToThisDmn } =
+    getDecisionServicePropertiesRelativeToThisDmn({
+      thisDmnsNamespace,
+      decisionServiceNamespace,
+      decisionService,
+    });
+
+  const decisionServiceHrefRelativeToThisDmn = buildXmlHref({
+    namespace: decisionServiceNamespaceForHref,
+    id: decisionService["@_id"]!,
+  });
+
+  const decisionServiceHrefsByDecisionHrefsRelativeToThisDmn = computeDecisionServiceHrefsByDecisionHrefs({
+    thisDmnsNamespace,
+    drgElementsNamespace: decisionServiceNamespace,
+    drgElements: decisionServiceDmnDefinitions.drgElement,
+  });
+
+  const doesThisDrdHaveConflictingDecisionService = containedDecisionHrefsRelativeToThisDmn.some((decisionHref) =>
+    (decisionServiceHrefsByDecisionHrefsRelativeToThisDmn.get(decisionHref) ?? []).some((d) =>
+      thisDmnsIndexedDrd.dmnShapesByHref.has(d)
+    )
+  );
+
+  if (doesThisDrdHaveConflictingDecisionService) {
+    // There's already, in this DRD, a Decision Service in expanded form that contains a Decision that is contained by the Decision Service we're adding.
+    // As the DMN specification doesn't allow two copies of the same DRG element to be depicted in the same DRD, we can't add the Decision Service in expanded form.
+    // To not disallow depicting the Decision Service in this DRD, though, we add it in collpased form.
+    addShape({
+      definitions: thisDmnsDefinitions,
+      drdIndex,
+      nodeType: NODE_TYPES.decisionService,
+      shape: {
+        "@_dmnElementRef": xmlHrefToQName(decisionServiceHrefRelativeToThisDmn, thisDmnsDefinitions),
+        "@_isCollapsed": true,
+        "dc:Bounds": {
+          "@_x": dropPoint.x,
+          "@_y": dropPoint.y,
+          "@_width": DECISION_SERVICE_COLLAPSED_DIMENSIONS.width,
+          "@_height": DECISION_SERVICE_COLLAPSED_DIMENSIONS.height,
+        },
+      },
+    });
+    return;
+  }
+
+  const drds = decisionServiceDmnDefinitions["dmndi:DMNDI"]?.["dmndi:DMNDiagram"] ?? [];
+
+  let indexedDrd: ReturnType<Computed["indexedDrd"]> | undefined;
+  for (let i = 0; i < drds.length; i++) {
+    if (thisDmnsNamespace === decisionServiceNamespace && i === drdIndex) {
+      continue; // Skip the current DRD!
+    }
+
+    const _indexedDrd = computeIndexedDrd(thisDmnsNamespace, decisionServiceDmnDefinitions, i);
+    const dsShape = _indexedDrd.dmnShapesByHref.get(decisionServiceHrefRelativeToThisDmn);
+    const hasCompleteExpandedDepictionOfDecisionService =
+      dsShape &&
+      !(dsShape["@_isCollapsed"] ?? false) &&
+      containedDecisionHrefsRelativeToThisDmn.every((dHref) => _indexedDrd.dmnShapesByHref.has(dHref));
+
+    if (hasCompleteExpandedDepictionOfDecisionService) {
+      indexedDrd = _indexedDrd;
+      break; // Found a DRD with a complete expanded depiction of the Decision Service.
+    }
+  }
+
+  if (!indexedDrd) {
+    // There's no DRD which inclues a complete expanded depiction of the Decision Service. Let's proceed with auto-layout.
+    // TODO: Tiago

Review Comment:
   Will be addressed on https://github.com/apache/incubator-kie-issues/issues/972



##########
packages/dmn-editor/src/diagram/Diagram.tsx:
##########
@@ -398,31 +430,65 @@ export const Diagram = React.forwardRef<DiagramRef, { container: React.RefObject
             DMN15__tDefinitions["drgElement"]
           >;
 
-          const nodeType = getNodeTypeFromDmnObject(drgElement);
-          if (nodeType === undefined) {
-            throw new Error("DMN DIAGRAM: It wasn't possible to determine the node type");
-          }
-
           dmnEditorStoreApi.setState((state) => {
+            const nodeType = getNodeTypeFromDmnObject(drgElement);
+            if (nodeType === undefined) {
+              throw new Error("DMN DIAGRAM: It wasn't possible to determine the node type");
+            }
+
             const defaultNodeDimensions = DEFAULT_NODE_SIZES[nodeType]({
               snapGrid: state.diagram.snapGrid,
               isAlternativeInputDataShape: state.computed(state).isAlternativeInputDataShape(),
             });
-            addShape({
-              definitions: state.dmn.model.definitions,
-              drdIndex: state.diagram.drdIndex,
-              nodeType,
-              shape: {
-                "@_dmnElementRef": buildXmlQName({ type: "xml-qname", localPart: drgElement["@_id"]! }),
-                "@_isCollapsed": false,
-                "dc:Bounds": {
-                  "@_x": dropPoint.x,
-                  "@_y": dropPoint.y,
-                  "@_width": defaultNodeDimensions["@_width"],
-                  "@_height": defaultNodeDimensions["@_height"],
+
+            if (drgElement.__$$element === "decisionService") {
+              addExistingDecisionServiceToDrd({
+                decisionService: drgElement,
+                decisionServiceNamespace: state.dmn.model.definitions["@_namespace"],
+                drdIndex: state.diagram.drdIndex,
+                dropPoint,
+                externalDmnsIndex: state.computed(state).getExternalModelTypesByNamespace(externalModelsByNamespace)
+                  .dmns,
+                thisDmnsDefinitions: state.dmn.model.definitions,
+                thisDmnsIndexedDrd: state.computed(state).indexedDrd(),
+                thisDmnsNamespace: state.dmn.model.definitions["@_namespace"],
+              });
+            } else if (drgElement.__$$element === "decision") {
+              // TODO: Tiago --> Add logic for Decision contained by DS.

Review Comment:
   Will be addressed on https://github.com/apache/incubator-kie-issues/issues/894



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org


Re: [PR] kie-issues#888: Decision Services & multiple DRDs: Adding a Decision Service to a DRD [incubator-kie-tools]

Posted by "ljmotta (via GitHub)" <gi...@apache.org>.
ljmotta commented on code in PR #2181:
URL: https://github.com/apache/incubator-kie-tools/pull/2181#discussion_r1504170259


##########
packages/dmn-editor/src/store/computed/computeDecisionServiceHrefsByDecisionHrefs.ts:
##########
@@ -0,0 +1,63 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { getDecisionServicePropertiesRelativeToThisDmn } from "../../mutations/addExistingDecisionServiceToDrd";
+import { buildXmlHref } from "../../xml/xmlHrefs";
+import { State } from "../Store";
+
+export function computeDecisionServiceHrefsByDecisionHrefs({
+  thisDmnsNamespace,
+  drgElementsNamespace,
+  drgElements,
+}: {
+  thisDmnsNamespace: string;
+  drgElementsNamespace: string;
+  drgElements: State["dmn"]["model"]["definitions"]["drgElement"];
+}) {
+  drgElements ??= [];
+  const ret = new Map<string, string[]>();

Review Comment:
   Could you please add a comment or a better name?



##########
packages/dmn-editor/src/mutations/addExistingDecisionServiceToDrd.ts:
##########
@@ -0,0 +1,248 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import {
+  DMN15__tDecisionService,
+  DMN15__tDefinitions,
+} from "@kie-tools/dmn-marshaller/dist/schemas/dmn-1_5/ts-gen/types";
+import { DECISION_SERVICE_COLLAPSED_DIMENSIONS } from "../diagram/nodes/DefaultSizes";
+import { NODE_TYPES } from "../diagram/nodes/NodeTypes";
+import { Computed } from "../store/Store";
+import { computeDecisionServiceHrefsByDecisionHrefs } from "../store/computed/computeDecisionServiceHrefsByDecisionHrefs";
+import { computeIndexedDrd } from "../store/computed/computeIndexes";
+import { xmlHrefToQName } from "../xml/xmlHrefToQName";
+import { buildXmlHref, parseXmlHref } from "../xml/xmlHrefs";
+import { addShape } from "./addShape";
+import { repositionNode } from "./repositionNode";
+
+/**
+ * When adding a Decision Service to a DRD, we need to bring all its encapsulated and output Decisions with it,
+ * copying their layout from other DRDs, or formatting with autolayout.
+ */
+export function addExistingDecisionServiceToDrd({
+  decisionServiceNamespace,
+  decisionService,
+  externalDmnsIndex,
+  thisDmnsNamespace,
+  thisDmnsDefinitions,
+  thisDmnsIndexedDrd,
+  drdIndex,
+  dropPoint,
+}: {
+  decisionServiceNamespace: string;
+  decisionService: DMN15__tDecisionService;
+  externalDmnsIndex: ReturnType<Computed["getExternalModelTypesByNamespace"]>["dmns"];
+  thisDmnsNamespace: string;
+  thisDmnsDefinitions: DMN15__tDefinitions;
+  thisDmnsIndexedDrd: ReturnType<Computed["indexedDrd"]>;
+  drdIndex: number;
+  dropPoint: { x: number; y: number };
+}) {
+  const decisionServiceDmnDefinitions =
+    !decisionServiceNamespace || decisionServiceNamespace === thisDmnsNamespace
+      ? thisDmnsDefinitions
+      : externalDmnsIndex.get(decisionServiceNamespace)?.model.definitions;
+  if (!decisionServiceDmnDefinitions) {
+    throw new Error(`DMN MUTATION: Can't find definitions for model with namespace ${decisionServiceNamespace}`);
+  }
+  const { decisionServiceNamespaceForHref, containedDecisionHrefsRelativeToThisDmn } =
+    getDecisionServicePropertiesRelativeToThisDmn({
+      thisDmnsNamespace,
+      decisionServiceNamespace,
+      decisionService,
+    });
+
+  const decisionServiceHrefRelativeToThisDmn = buildXmlHref({
+    namespace: decisionServiceNamespaceForHref,
+    id: decisionService["@_id"]!,
+  });
+
+  const decisionServiceHrefsByDecisionHrefsRelativeToThisDmn = computeDecisionServiceHrefsByDecisionHrefs({
+    thisDmnsNamespace,
+    drgElementsNamespace: decisionServiceNamespace,
+    drgElements: decisionServiceDmnDefinitions.drgElement,
+  });
+
+  const doesThisDrdHaveConflictingDecisionService = containedDecisionHrefsRelativeToThisDmn.some((decisionHref) =>
+    (decisionServiceHrefsByDecisionHrefsRelativeToThisDmn.get(decisionHref) ?? []).some((d) =>
+      thisDmnsIndexedDrd.dmnShapesByHref.has(d)
+    )
+  );
+
+  if (doesThisDrdHaveConflictingDecisionService) {
+    // There's already, in this DRD, a Decision Service in expanded form that contains a Decision that is contained by the Decision Service we're adding.
+    // As the DMN specification doesn't allow two copies of the same DRG element to be depicted in the same DRD, we can't add the Decision Service in expanded form.
+    // To not disallow depicting the Decision Service in this DRD, though, we add it in collpased form.
+    addShape({
+      definitions: thisDmnsDefinitions,
+      drdIndex,
+      nodeType: NODE_TYPES.decisionService,
+      shape: {
+        "@_dmnElementRef": xmlHrefToQName(decisionServiceHrefRelativeToThisDmn, thisDmnsDefinitions),
+        "@_isCollapsed": true,
+        "dc:Bounds": {
+          "@_x": dropPoint.x,
+          "@_y": dropPoint.y,
+          "@_width": DECISION_SERVICE_COLLAPSED_DIMENSIONS.width,
+          "@_height": DECISION_SERVICE_COLLAPSED_DIMENSIONS.height,
+        },
+      },
+    });
+    return;
+  }
+
+  const drds = decisionServiceDmnDefinitions["dmndi:DMNDI"]?.["dmndi:DMNDiagram"] ?? [];
+
+  let indexedDrd: ReturnType<Computed["indexedDrd"]> | undefined;
+  for (let i = 0; i < drds.length; i++) {
+    if (thisDmnsNamespace === decisionServiceNamespace && i === drdIndex) {
+      continue; // Skip the current DRD!
+    }
+
+    const _indexedDrd = computeIndexedDrd(thisDmnsNamespace, decisionServiceDmnDefinitions, i);
+    const dsShape = _indexedDrd.dmnShapesByHref.get(decisionServiceHrefRelativeToThisDmn);
+    const hasCompleteExpandedDepictionOfDecisionService =
+      dsShape &&
+      !(dsShape["@_isCollapsed"] ?? false) &&
+      containedDecisionHrefsRelativeToThisDmn.every((dHref) => _indexedDrd.dmnShapesByHref.has(dHref));
+
+    if (hasCompleteExpandedDepictionOfDecisionService) {
+      indexedDrd = _indexedDrd;
+      break; // Found a DRD with a complete expanded depiction of the Decision Service.
+    }
+  }
+
+  if (!indexedDrd) {

Review Comment:
   All the logic below shouldn't be inside the for? What will happen if we have multiple DRDs with the decision service on its expaded form?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org


Re: [PR] kie-issues#888: Decision Services & multiple DRDs: Adding a Decision Service to a DRD [incubator-kie-tools]

Posted by "tiagobento (via GitHub)" <gi...@apache.org>.
tiagobento commented on code in PR #2181:
URL: https://github.com/apache/incubator-kie-tools/pull/2181#discussion_r1505015666


##########
packages/dmn-editor/src/mutations/addExistingDecisionServiceToDrd.ts:
##########
@@ -0,0 +1,248 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import {
+  DMN15__tDecisionService,
+  DMN15__tDefinitions,
+} from "@kie-tools/dmn-marshaller/dist/schemas/dmn-1_5/ts-gen/types";
+import { DECISION_SERVICE_COLLAPSED_DIMENSIONS } from "../diagram/nodes/DefaultSizes";
+import { NODE_TYPES } from "../diagram/nodes/NodeTypes";
+import { Computed } from "../store/Store";
+import { computeDecisionServiceHrefsByDecisionHrefs } from "../store/computed/computeDecisionServiceHrefsByDecisionHrefs";
+import { computeIndexedDrd } from "../store/computed/computeIndexes";
+import { xmlHrefToQName } from "../xml/xmlHrefToQName";
+import { buildXmlHref, parseXmlHref } from "../xml/xmlHrefs";
+import { addShape } from "./addShape";
+import { repositionNode } from "./repositionNode";
+
+/**
+ * When adding a Decision Service to a DRD, we need to bring all its encapsulated and output Decisions with it,
+ * copying their layout from other DRDs, or formatting with autolayout.
+ */
+export function addExistingDecisionServiceToDrd({
+  decisionServiceNamespace,
+  decisionService,
+  externalDmnsIndex,
+  thisDmnsNamespace,
+  thisDmnsDefinitions,
+  thisDmnsIndexedDrd,
+  drdIndex,
+  dropPoint,
+}: {
+  decisionServiceNamespace: string;
+  decisionService: DMN15__tDecisionService;
+  externalDmnsIndex: ReturnType<Computed["getExternalModelTypesByNamespace"]>["dmns"];
+  thisDmnsNamespace: string;
+  thisDmnsDefinitions: DMN15__tDefinitions;
+  thisDmnsIndexedDrd: ReturnType<Computed["indexedDrd"]>;
+  drdIndex: number;
+  dropPoint: { x: number; y: number };
+}) {
+  const decisionServiceDmnDefinitions =
+    !decisionServiceNamespace || decisionServiceNamespace === thisDmnsNamespace
+      ? thisDmnsDefinitions
+      : externalDmnsIndex.get(decisionServiceNamespace)?.model.definitions;
+  if (!decisionServiceDmnDefinitions) {
+    throw new Error(`DMN MUTATION: Can't find definitions for model with namespace ${decisionServiceNamespace}`);
+  }
+  const { decisionServiceNamespaceForHref, containedDecisionHrefsRelativeToThisDmn } =
+    getDecisionServicePropertiesRelativeToThisDmn({
+      thisDmnsNamespace,
+      decisionServiceNamespace,
+      decisionService,
+    });
+
+  const decisionServiceHrefRelativeToThisDmn = buildXmlHref({
+    namespace: decisionServiceNamespaceForHref,
+    id: decisionService["@_id"]!,
+  });
+
+  const decisionServiceHrefsByDecisionHrefsRelativeToThisDmn = computeDecisionServiceHrefsByDecisionHrefs({
+    thisDmnsNamespace,
+    drgElementsNamespace: decisionServiceNamespace,
+    drgElements: decisionServiceDmnDefinitions.drgElement,
+  });
+
+  const doesThisDrdHaveConflictingDecisionService = containedDecisionHrefsRelativeToThisDmn.some((decisionHref) =>
+    (decisionServiceHrefsByDecisionHrefsRelativeToThisDmn.get(decisionHref) ?? []).some((d) =>
+      thisDmnsIndexedDrd.dmnShapesByHref.has(d)
+    )
+  );
+
+  if (doesThisDrdHaveConflictingDecisionService) {
+    // There's already, in this DRD, a Decision Service in expanded form that contains a Decision that is contained by the Decision Service we're adding.
+    // As the DMN specification doesn't allow two copies of the same DRG element to be depicted in the same DRD, we can't add the Decision Service in expanded form.
+    // To not disallow depicting the Decision Service in this DRD, though, we add it in collpased form.
+    addShape({
+      definitions: thisDmnsDefinitions,
+      drdIndex,
+      nodeType: NODE_TYPES.decisionService,
+      shape: {
+        "@_dmnElementRef": xmlHrefToQName(decisionServiceHrefRelativeToThisDmn, thisDmnsDefinitions),
+        "@_isCollapsed": true,
+        "dc:Bounds": {
+          "@_x": dropPoint.x,
+          "@_y": dropPoint.y,
+          "@_width": DECISION_SERVICE_COLLAPSED_DIMENSIONS.width,
+          "@_height": DECISION_SERVICE_COLLAPSED_DIMENSIONS.height,
+        },
+      },
+    });
+    return;
+  }
+
+  const drds = decisionServiceDmnDefinitions["dmndi:DMNDI"]?.["dmndi:DMNDiagram"] ?? [];
+
+  let indexedDrd: ReturnType<Computed["indexedDrd"]> | undefined;
+  for (let i = 0; i < drds.length; i++) {
+    if (thisDmnsNamespace === decisionServiceNamespace && i === drdIndex) {
+      continue; // Skip the current DRD!
+    }
+
+    const _indexedDrd = computeIndexedDrd(thisDmnsNamespace, decisionServiceDmnDefinitions, i);
+    const dsShape = _indexedDrd.dmnShapesByHref.get(decisionServiceHrefRelativeToThisDmn);
+    const hasCompleteExpandedDepictionOfDecisionService =
+      dsShape &&
+      !(dsShape["@_isCollapsed"] ?? false) &&
+      containedDecisionHrefsRelativeToThisDmn.every((dHref) => _indexedDrd.dmnShapesByHref.has(dHref));
+
+    if (hasCompleteExpandedDepictionOfDecisionService) {
+      indexedDrd = _indexedDrd;
+      break; // Found a DRD with a complete expanded depiction of the Decision Service.
+    }
+  }
+
+  if (!indexedDrd) {

Review Comment:
   We only use the first DRD with the complete, expanded depiction of the DS as reference. The for above this line will break once one is found.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@kie.apache.org
For additional commands, e-mail: commits-help@kie.apache.org