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
8 changes: 6 additions & 2 deletions src/components/CustomPortModel.ts
Original file line number Diff line number Diff line change
@@ -73,15 +73,19 @@ 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 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;
}
70 changes: 47 additions & 23 deletions src/components/xircuitBodyWidget.tsx
Original file line number Diff line number Diff line change
@@ -180,6 +180,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 => {
@@ -455,6 +456,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';
@@ -483,46 +487,66 @@ 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 '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 = ' |= '
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';
}
}
}