From 8b8ac6b7f05e16460aefd60f33c464af5cdab022 Mon Sep 17 00:00:00 2001 From: AdrySky Date: Thu, 31 Mar 2022 12:30:10 +0800 Subject: [PATCH 1/9] Refactor code compiler for different data type --- src/components/xircuitBodyWidget.tsx | 33 ++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/src/components/xircuitBodyWidget.tsx b/src/components/xircuitBodyWidget.tsx index 7c038c88..245fa590 100644 --- a/src/components/xircuitBodyWidget.tsx +++ b/src/components/xircuitBodyWidget.tsx @@ -483,6 +483,8 @@ export const BodyWidget: FC = ({ //Get the id of the node of the connected link let linkSourceNodeId = allPort[port]["links"][portLink]["sourcePort"]["parent"]["options"]["id"]; + let equalSign = ' = '; + let sourcePortLabelStructure; if (port.startsWith("parameter")) { @@ -500,17 +502,30 @@ export const BodyWidget: FC = ({ pythonCode += ' ' + bindingName + '.' + label + '.value = ' + "(" + sourcePortLabel + ")" + "\n"; } - else if (sourceNodeType == 'dict') { - pythonCode += ' ' + bindingName + '.' + label + '.value = ' + "{" + sourcePortLabel + "}" + "\n"; - } + if (port.startsWith("parameter")) { - else { - pythonCode += ' ' + bindingName + '.' + label + '.value = ' + sourcePortLabel + "\n"; + if (sourceNodeName.startsWith("Literal")) { + switch (sourceNodeType) { + case "string": + sourcePortLabelStructure = "'" + sourcePortLabel + "'"; + break; + case "list": + sourcePortLabelStructure = "[" + sourcePortLabel + "]"; + break; + case "tuple": + sourcePortLabelStructure = "(" + sourcePortLabel + ")"; + break; + case "dict": + sourcePortLabelStructure = "{" + sourcePortLabel + "}"; + break; + default: + break; } + pythonCode += ' ' + bindingName + '.' + label + '.value' + equalSign + sourcePortLabelStructure + "\n"; + } else if (linkSourceNodeId == sourceNodeId && !sourceNodeName.startsWith("Hyperparameter")) { // Make sure the node id match between connected link and source node // Skip Hyperparameter Components - } else if (linkSourceNodeId == sourceNodeId && !sourceNodeName.startsWith("Hyperparameter")) { - pythonCode += ' ' + bindingName + '.' + label + ' = ' + preBindingName + '.' + sourcePortLabel + '\n'; + pythonCode += ' ' + bindingName + '.' + label + equalSign + preBindingName + '.' + sourcePortLabel + '\n'; } else { sourcePortLabel = sourcePortLabel.replace(/\s+/g, "_"); sourcePortLabel = sourcePortLabel.toLowerCase(); @@ -518,11 +533,11 @@ export const BodyWidget: FC = ({ let paramName = sourceNodeName[sourceNodeName.length - 1]; paramName = paramName.replace(/\s+/g, "_"); paramName = paramName.toLowerCase(); - pythonCode += ' ' + bindingName + '.' + label + '.value = args.' + paramName + '\n'; + pythonCode += ' ' + bindingName + '.' + label + '.value' + equalSign + 'args.' + paramName + '\n'; } } else { - pythonCode += ' ' + bindingName + '.' + label + ' = ' + preBindingName + '.' + sourcePortLabel + '\n'; + pythonCode += ' ' + bindingName + '.' + label + equalSign + preBindingName + '.' + sourcePortLabel + '\n'; } } } From 185c3d8230760e9fbe2c94d7fb7db24880441dd4 Mon Sep 17 00:00:00 2001 From: AdrySky Date: Thu, 31 Mar 2022 12:31:27 +0800 Subject: [PATCH 2/9] Allow multiple link for port with any type --- src/components/CustomPortModel.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/components/CustomPortModel.ts b/src/components/CustomPortModel.ts index 13cc2de7..d8a8f04c 100644 --- a/src/components/CustomPortModel.ts +++ b/src/components/CustomPortModel.ts @@ -80,6 +80,9 @@ export class CustomPortModel extends DefaultPortModel { console.log("port name: ", thisName); console.log("parameter port: ", port.getNode().getInPorts()); if (Object.keys(port.getLinks()).length > 0){ + if(thisName.includes("any")){ + return; + } port.getNode().getOptions().extras["borderColor"]="red"; port.getNode().getOptions().extras["tip"]="Port has other link"; port.getNode().setSelected(true); From 0af9a5d5a575d5a9ca4c37484a6eaefa204d81fd Mon Sep 17 00:00:00 2001 From: AdrySky Date: Thu, 31 Mar 2022 12:32:41 +0800 Subject: [PATCH 3/9] Allow append values when multiple links connected --- src/components/xircuitBodyWidget.tsx | 30 +++++++++++++++------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/components/xircuitBodyWidget.tsx b/src/components/xircuitBodyWidget.tsx index 245fa590..726b53ff 100644 --- a/src/components/xircuitBodyWidget.tsx +++ b/src/components/xircuitBodyWidget.tsx @@ -180,6 +180,7 @@ export const BodyWidget: FC = ({ const xircuitLogger = new Log(app); const contextRef = useRef(context); const notInitialRender = useRef(false); + const needAppend = useRef(""); const onChange = useCallback( (): void => { @@ -441,6 +442,8 @@ export const BodyWidget: FC = ({ } + // Reset appending values + needAppend.current = ""; pythonCode += '\n'; if (startNodeModel) { @@ -486,21 +489,20 @@ export const BodyWidget: FC = ({ let equalSign = ' = '; let sourcePortLabelStructure; - if (port.startsWith("parameter")) { - - if (sourceNodeName.startsWith("Literal")) { - - if (sourceNodeType == 'string') { - pythonCode += ' ' + bindingName + '.' + label + '.value = ' + "'" + sourcePortLabel + "'\n"; - } - - else if (sourceNodeType == 'list') { - pythonCode += ' ' + bindingName + '.' + label + '.value = ' + "[" + sourcePortLabel + "]" + "\n"; - } - - else if (sourceNodeType == 'tuple') { - pythonCode += ' ' + bindingName + '.' + label + '.value = ' + "(" + sourcePortLabel + ")" + "\n"; + // When port is 'any' type, append values if there's multiple link connected + if (port.includes('any')) { + if (needAppend.current == bindingName) { + switch (sourceNodeType) { + case "dict": + equalSign = ' |= ' + break; + default: + equalSign = ' += ' + break; } + } + needAppend.current = bindingName; + } if (port.startsWith("parameter")) { From 70d51f6b1642ead1e6b82e6daeae1f061c539e30 Mon Sep 17 00:00:00 2001 From: AdrySky Date: Tue, 5 Apr 2022 14:57:57 +0800 Subject: [PATCH 4/9] Allow multiple links with same data type --- src/components/CustomPortModel.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/components/CustomPortModel.ts b/src/components/CustomPortModel.ts index d8a8f04c..562996f4 100644 --- a/src/components/CustomPortModel.ts +++ b/src/components/CustomPortModel.ts @@ -73,16 +73,15 @@ export class CustomPortModel extends DefaultPortModel { let thisNode = this.getNode(); let thisNodeModelType = thisNode.getOptions()["extras"]["type"]; let thisName = port.getName(); - + let thisPortType = thisName.split('-')[1]; if (this.isParameterNode(thisNodeModelType) == true){ // if the port you are trying to link ready has other links console.log("port name: ", thisName); console.log("parameter port: ", port.getNode().getInPorts()); if (Object.keys(port.getLinks()).length > 0){ - if(thisName.includes("any")){ - return; - } + // When port and link is the same type, just return + if(thisNodeModelType == thisPortType) return; port.getNode().getOptions().extras["borderColor"]="red"; port.getNode().getOptions().extras["tip"]="Port has other link"; port.getNode().setSelected(true); From 7fa4ab354cbc4ced1e02e2e2aecd1dd093763974 Mon Sep 17 00:00:00 2001 From: AdrySky Date: Tue, 5 Apr 2022 15:01:35 +0800 Subject: [PATCH 5/9] Refactor and change msg --- src/components/CustomPortModel.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/components/CustomPortModel.ts b/src/components/CustomPortModel.ts index 562996f4..befdcc91 100644 --- a/src/components/CustomPortModel.ts +++ b/src/components/CustomPortModel.ts @@ -82,8 +82,10 @@ export class CustomPortModel extends DefaultPortModel { if (Object.keys(port.getLinks()).length > 0){ // When port and link is the same type, just return if(thisNodeModelType == thisPortType) return; + // When port is 'any' type, just return + if(thisName.includes("any")) return; port.getNode().getOptions().extras["borderColor"]="red"; - port.getNode().getOptions().extras["tip"]="Port has other link"; + port.getNode().getOptions().extras["tip"]=`Port only allow multiple link ${thisPortType} type`; port.getNode().setSelected(true); return false; } From 724a2fa845dc29d4f1ee8f443c00e08084a7e5b2 Mon Sep 17 00:00:00 2001 From: AdrySky Date: Tue, 5 Apr 2022 15:21:46 +0800 Subject: [PATCH 6/9] Forgot to add for default type --- src/components/xircuitBodyWidget.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/components/xircuitBodyWidget.tsx b/src/components/xircuitBodyWidget.tsx index 726b53ff..a1b63aaf 100644 --- a/src/components/xircuitBodyWidget.tsx +++ b/src/components/xircuitBodyWidget.tsx @@ -521,6 +521,7 @@ export const BodyWidget: FC = ({ sourcePortLabelStructure = "{" + sourcePortLabel + "}"; break; default: + sourcePortLabelStructure = sourcePortLabel; break; } pythonCode += ' ' + bindingName + '.' + label + '.value' + equalSign + sourcePortLabelStructure + "\n"; From eab68b36086aa05377eb2b37c9b5931efb743052 Mon Sep 17 00:00:00 2001 From: AdrySky Date: Wed, 6 Apr 2022 16:02:41 +0800 Subject: [PATCH 7/9] Include string, list and dict port type --- src/components/xircuitBodyWidget.tsx | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/src/components/xircuitBodyWidget.tsx b/src/components/xircuitBodyWidget.tsx index a1b63aaf..2859c7b9 100644 --- a/src/components/xircuitBodyWidget.tsx +++ b/src/components/xircuitBodyWidget.tsx @@ -489,9 +489,14 @@ export const BodyWidget: FC = ({ let equalSign = ' = '; let sourcePortLabelStructure; - // When port is 'any' type, append values if there's multiple link connected - if (port.includes('any')) { - if (needAppend.current == bindingName) { + // When port is 'any','string', 'list' and 'dict' type + // append values if there's multiple link connected + if (port.includes('any') || + port.includes('string') || + port.includes('list') || + port.includes('dict') + ) { + if (needAppend.current == label) { switch (sourceNodeType) { case "dict": equalSign = ' |= ' @@ -501,7 +506,7 @@ export const BodyWidget: FC = ({ break; } } - needAppend.current = bindingName; + needAppend.current = label; } if (port.startsWith("parameter")) { From 1df9b2b590a411989245c50f9c8ae0fac72a0ac2 Mon Sep 17 00:00:00 2001 From: AdrySky Date: Wed, 6 Apr 2022 16:03:35 +0800 Subject: [PATCH 8/9] Reposition on when to reset the append values --- src/components/xircuitBodyWidget.tsx | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/components/xircuitBodyWidget.tsx b/src/components/xircuitBodyWidget.tsx index 2859c7b9..685728eb 100644 --- a/src/components/xircuitBodyWidget.tsx +++ b/src/components/xircuitBodyWidget.tsx @@ -442,8 +442,6 @@ export const BodyWidget: FC = ({ } - // Reset appending values - needAppend.current = ""; pythonCode += '\n'; if (startNodeModel) { @@ -458,6 +456,9 @@ export const BodyWidget: FC = ({ let bindingName = 'c_' + ++j; let currentNodeModel = getNodeModelById(nodeModels, targetNodeId); let allPort = currentNodeModel.getPorts(); + // Reset appending values + needAppend.current = ""; + for (let port in allPort) { let portIn = allPort[port].getOptions().alignment == 'left'; From 442d67240c26fef590f04ae82aee9ade5747506a Mon Sep 17 00:00:00 2001 From: AdrySky Date: Thu, 7 Apr 2022 16:25:18 +0800 Subject: [PATCH 9/9] Remove multi link of any type --- src/components/port/CustomPortModel.ts | 13 ++++++++----- src/components/xircuitBodyWidget.tsx | 5 ++--- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/components/port/CustomPortModel.ts b/src/components/port/CustomPortModel.ts index befdcc91..b6c9cf5f 100644 --- a/src/components/port/CustomPortModel.ts +++ b/src/components/port/CustomPortModel.ts @@ -80,12 +80,15 @@ export class CustomPortModel extends DefaultPortModel { console.log("port name: ", thisName); console.log("parameter port: ", port.getNode().getInPorts()); if (Object.keys(port.getLinks()).length > 0){ - // When port and link is the same type, just return - if(thisNodeModelType == thisPortType) return; - // When port is 'any' type, just return - if(thisName.includes("any")) return; + // When port is 'string', 'list' and 'dict' type, just return + switch (thisPortType){ + case "string": + case "list": + case "dict": + return; + } port.getNode().getOptions().extras["borderColor"]="red"; - port.getNode().getOptions().extras["tip"]=`Port only allow multiple link ${thisPortType} type`; + port.getNode().getOptions().extras["tip"]=`Port doesn't allow multi-link of ${thisPortType} type`; port.getNode().setSelected(true); return false; } diff --git a/src/components/xircuitBodyWidget.tsx b/src/components/xircuitBodyWidget.tsx index 0b7c5ce3..667b8df5 100644 --- a/src/components/xircuitBodyWidget.tsx +++ b/src/components/xircuitBodyWidget.tsx @@ -503,10 +503,9 @@ export const BodyWidget: FC = ({ let equalSign = ' = '; let sourcePortLabelStructure; - // When port is 'any','string', 'list' and 'dict' type + // When port is 'string', 'list' and 'dict' type // append values if there's multiple link connected - if (port.includes('any') || - port.includes('string') || + if (port.includes('string') || port.includes('list') || port.includes('dict') ) {