danielzhe commented on code in PR #2508:
URL:
https://github.com/apache/incubator-kie-tools/pull/2508#discussion_r1707968711
##########
packages/dmn-editor/src/mutations/addDecisionToDecisionService.ts:
##########
@@ -25,25 +25,48 @@ import { SnapGrid } from "../store/Store";
import { MIN_NODE_SIZES } from "../diagram/nodes/DefaultSizes";
import { NODE_TYPES } from "../diagram/nodes/NodeTypes";
import { Normalized } from "../normalization/normalize";
+import { ExternalModelsIndex } from "../DmnEditor";
+import { buildXmlHref, parseXmlHref } from "../xml/xmlHrefs";
+import { DmnLatestModel } from "@kie-tools/dmn-marshaller/dist";
+import { xmlHrefToQName } from "../xml/xmlHrefToQName";
export function addDecisionToDecisionService({
definitions,
- decisionId,
+ decisionHref,
decisionServiceId,
drdIndex,
snapGrid,
+ externalModelsByNamespace,
}: {
definitions: Normalized<DMN15__tDefinitions>;
- decisionId: string;
+ decisionHref: string;
decisionServiceId: string;
drdIndex: number;
snapGrid: SnapGrid;
+ externalModelsByNamespace: ExternalModelsIndex | undefined;
}) {
- console.debug(`DMN MUTATION: Adding Decision '${decisionId}' to Decision
Service '${decisionServiceId}'`);
+ console.debug(`DMN MUTATION: Adding Decision '${decisionHref}' to Decision
Service '${decisionServiceId}'`);
- const decision = definitions.drgElement?.find((s) => s["@_id"] ===
decisionId);
- if (decision?.__$$element !== "decision") {
- throw new Error(`DMN MUTATION: DRG Element with id '${decisionId}' is
either not a Decision or doesn't exist.`);
+ const href = parseXmlHref(decisionHref);
+
+ const externalModel = externalModelsByNamespace?.[href.namespace ?? ""];
+ if (href.namespace && !externalModel) {
+ throw new Error(`DMN MUTATION: Namespace '${href.namespace}' not found.`);
+ }
+
+ if (externalModel && (externalModel.model as
Normalized<DmnLatestModel>).definitions) {
+ const externalDrgs = (externalModel.model as
Normalized<DmnLatestModel>).definitions.drgElement;
+ const decision = externalDrgs?.find((drgElement) => drgElement["@_id"] ===
href.id);
Review Comment:
This is a very good topic.
I like to split in variables because I find it easier to "think"
step-by-step and have less complexity per line.
Also, in this case, I'm giving "meaning" to what `(externalModel.model as
Normalized<DmnLatestModel>).definitions.drgElement`: it is the "external drgs".
So I don't have to care where it comes from in line 59 or what it is. It is the
"external drgs that come from somewhere". Ok, "somewhere" is just a line above,
but in my mind, looking at line 59, it is a "problem solved" that I don't have
to care about anymore.
But the main point is that if someone new looks at the code it can easily
get what `(externalModel.model as
Normalized<DmnLatestModel>).definitions.drgElement` means.
--
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]