Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨Allow multiple link for port with string, list and dict type #137

Merged
merged 10 commits into from
Apr 8, 2022
11 changes: 9 additions & 2 deletions src/components/port/CustomPortModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,22 @@ 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){
// 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 has other link";
port.getNode().getOptions().extras["tip"]=`Port doesn't allow multi-link of ${thisPortType} type`;
port.getNode().setSelected(true);
return false;
}
Expand Down
69 changes: 46 additions & 23 deletions src/components/xircuitBodyWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export const BodyWidget: FC<BodyWidgetProps> = ({
const xircuitLogger = new Log(app);
const contextRef = useRef(context);
const notInitialRender = useRef(false);
const needAppend = useRef("");

const onChange = useCallback(
(): void => {
Expand Down Expand Up @@ -468,6 +469,9 @@ export const BodyWidget: FC<BodyWidgetProps> = ({
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';
Expand Down Expand Up @@ -496,46 +500,65 @@ export const BodyWidget: FC<BodyWidgetProps> = ({

//Get the id of the node of the connected link
let linkSourceNodeId = allPort[port]["links"][portLink]["sourcePort"]["parent"]["options"]["id"];
let equalSign = ' = ';
let sourcePortLabelStructure;

// When port is 'string', 'list' and 'dict' type
// append values if there's multiple link connected
if (port.includes('string') ||
port.includes('list') ||
port.includes('dict')
) {
if (needAppend.current == label) {
switch (sourceNodeType) {
case "dict":
equalSign = ' |= '
break;
default:
equalSign = ' += '
break;
}
}
needAppend.current = label;
}

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";
}

else if (sourceNodeType == 'dict') {
pythonCode += ' ' + bindingName + '.' + label + '.value = ' + "{" + sourcePortLabel + "}" + "\n";
}

else {
pythonCode += ' ' + bindingName + '.' + label + '.value = ' + sourcePortLabel + "\n";
switch (sourceNodeType) {
case "string":
sourcePortLabelStructure = "'" + sourcePortLabel + "'";
break;
case "list":
sourcePortLabelStructure = "[" + sourcePortLabel + "]";
break;
case "tuple":
sourcePortLabelStructure = "(" + sourcePortLabel + ")";
break;
case "dict":
sourcePortLabelStructure = "{" + sourcePortLabel + "}";
break;
default:
sourcePortLabelStructure = sourcePortLabel;
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();
sourceNodeName = sourceNodeName.split(": ");
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';
}
}
}
Expand Down