Esempio n. 1
0
        private void GetPortDefs()
        {
            PyObject callResult = null;

            try {
                callResult = _ctx.Call("init", new PyObject[0]);
                DisplayPythonError(null);
            }
            catch (PyException e) {
                DisplayPythonError(e);
                Parent.Context.Notify(new GraphNotification(this, GraphNotification.NotificationType.Error,
                                                            $"Python: While executing init function: {e.Message}"));
                return;
            }
            catch (EntryPointNotFoundException e) {
                Parent.Context.Notify(new GraphNotification(this, GraphNotification.NotificationType.Error,
                                                            $"Python: While executing init function: {e.Message}"));
                return;
            }

            var existingInputs  = InputPorts.ToList();
            var existingOutputs = OutputPorts.ToList();

            using (var portDefs = new PyDict(callResult)) {
                using (var portInDefs = new PyList(portDefs["in"])) {
                    foreach (var item in portInDefs)
                    {
                        var tuple    = new PyTuple(item);
                        var portName = new PyString(tuple.Get(0).Handle, false).Value;
                        var portType = new PyString(tuple.Get(1).Handle, false).Value;

                        var existingPort = existingInputs.FirstOrDefault(p => p.Name == portName);

                        try {
                            switch (portType)
                            {
                            case "data":
                                if (existingPort != null)
                                {
                                    if (!existingPort.DataType.Equals(PortDataTypes.TypeIdSignal1D))
                                    {
                                        RemovePort(existingPort);
                                        new NodeSystemLib2.FormatData1D.InputPortData1D(this, portName);
                                    }
                                    existingInputs.Remove(existingPort);
                                }
                                else
                                {
                                    new NodeSystemLib2.FormatData1D.InputPortData1D(this, portName);
                                }
                                break;

                            case "value":
                                if (existingPort != null)
                                {
                                    if (!existingPort.DataType.Equals(PortDataTypes.TypeIdValueDouble))
                                    {
                                        RemovePort(existingPort);
                                        new NodeSystemLib2.FormatValue.InputPortValueDouble(this, portName);
                                    }
                                    existingInputs.Remove(existingPort);
                                }
                                else
                                {
                                    new NodeSystemLib2.FormatValue.InputPortValueDouble(this, portName);
                                }
                                break;

                            default:
                                Parent.Context.Notify(new GraphNotification(this, GraphNotification.NotificationType.Error, $"Python: unknown port format '{portType}' for input port named {portName}"));
                                break;
                            }
                        } catch (Exception e) {
                            Parent.Context.Notify(new GraphNotification(this, GraphNotification.NotificationType.Error, $"Python: error while processing input port: {portName}: {e.Message}"));
                        }
                    }
                }

                foreach (var port in existingInputs)
                {
                    RemovePort(port);
                }

                using (var portOutDefs = new PyList(portDefs["out"])) {
                    foreach (var item in portOutDefs)
                    {
                        var tuple    = new PyTuple(item);
                        var portName = new PyString(tuple.Get(0).Handle, false).Value;
                        var portType = new PyString(tuple.Get(1).Handle, false).Value;

                        var existingPort = existingOutputs.FirstOrDefault(p => p.Name == portName);

                        try {
                            switch (portType)
                            {
                            case "data":
                                if (existingPort != null)
                                {
                                    if (!existingPort.DataType.Equals(PortDataTypes.TypeIdSignal1D))
                                    {
                                        RemovePort(existingPort);
                                        new NodeSystemLib2.FormatData1D.OutputPortData1D(this, portName);
                                    }
                                    existingOutputs.Remove(existingPort);
                                }
                                else
                                {
                                    new NodeSystemLib2.FormatData1D.OutputPortData1D(this, portName);
                                }
                                break;

                            case "value":
                                if (existingPort != null)
                                {
                                    if (!existingPort.DataType.Equals(PortDataTypes.TypeIdValueDouble))
                                    {
                                        RemovePort(existingPort);
                                        new NodeSystemLib2.FormatValue.OutputPortValueDouble(this, portName);
                                    }
                                    existingOutputs.Remove(existingPort);
                                }
                                else
                                {
                                    new NodeSystemLib2.FormatValue.OutputPortValueDouble(this, portName);
                                }
                                break;

                            default:
                                Parent.Context.Notify(new GraphNotification(this, GraphNotification.NotificationType.Error, $"Python: unknown port format '{portType}' for output port named {portName}"));
                                break;
                            }
                        } catch (Exception e) {
                            Parent.Context.Notify(new GraphNotification(this, GraphNotification.NotificationType.Error, $"Python: error while processing output port: {portName}: {e.Message}"));
                        }
                    }
                }
            }
        }