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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to