예제 #1
0
        private Statement GetStatementForOutput(int portIndex)
        {
            if (State == ElementState.Error)
            {
                return(null);
            }

            // Here the "portIndex" is back mapped to the corresponding "Statement"
            // object. However, not all "Statement" objects produce an output port,
            // so "portIndex" cannot be used directly to index into "codeStatements"
            // list. This loop goes through "codeStatements", decrementing "portIndex"
            // along the way to determine the right "Statement" object matching the
            // port index.
            //
            Statement statement = null;
            var       svs       = CodeBlockUtils.GetStatementVariables(codeStatements, true);

            for (int stmt = 0, port = 0; stmt < codeStatements.Count; stmt++)
            {
                if (CodeBlockUtils.DoesStatementRequireOutputPort(svs, stmt))
                {
                    if (port == portIndex)
                    {
                        statement = codeStatements[stmt];
                        break;
                    }

                    port = port + 1;
                }
            }

            return(statement);
        }
예제 #2
0
        protected override bool UpdateValueCore(UpdateValueParams updateValueParams)
        {
            string          name  = updateValueParams.PropertyName;
            string          value = updateValueParams.PropertyValue;
            ElementResolver workspaceElementResolver = updateValueParams.ElementResolver;

            if (name != "Code")
            {
                return(base.UpdateValueCore(updateValueParams));
            }

            value = CodeBlockUtils.FormatUserText(value);

            //Since an empty Code Block Node should not exist, this checks for such instances.
            // If an empty Code Block Node is found, it is deleted. Since the creation and deletion of
            // an empty Code Block Node should not be recorded, this method also checks and removes
            // any unwanted recordings
            if (value == "")
            {
                Code = "";
            }
            else
            {
                if (!value.Equals(Code))
                {
                    SetCodeContent(value, workspaceElementResolver);
                }
            }
            return(true);
        }
예제 #3
0
        private void SetInputPorts()
        {
            // Generate input port data list from the unbound identifiers.
            var inportData = CodeBlockUtils.GenerateInputPortData(inputPortNames);

            foreach (var portData in inportData)
            {
                InPortData.Add(portData);
            }
        }
예제 #4
0
        /// <summary>
        ///  Returns the corresponding output port index for a given defined variable
        /// </summary>
        /// <param name="variableName"></param>
        /// <returns></returns>
        public int GetOutportIndex(string variableName)
        {
            var svs = CodeBlockUtils.GetStatementVariables(codeStatements, true);

            for (int i = 0; i < codeStatements.Count; i++)
            {
                Statement s = codeStatements[i];
                if (CodeBlockUtils.DoesStatementRequireOutputPort(svs, i))
                {
                    List <string> varNames = Statement.GetDefinedVariableNames(s, true);
                    if (varNames.Contains(variableName))
                    {
                        return(i);
                    }
                }
            }
            return(-1);
        }
예제 #5
0
        private void SetOutputPorts()
        {
            var allDefs = CodeBlockUtils.GetDefinitionLineIndexMap(codeStatements);

            if (allDefs.Any() == false)
            {
                return;
            }

            foreach (var def in allDefs)
            {
                string tooltip = def.Key;
                if (tempVariables.Contains(def.Key))
                {
                    tooltip = Formatting.TOOL_TIP_FOR_TEMP_VARIABLE;
                }

                OutPortData.Add(new PortData(string.Empty, tooltip)
                {
                    LineIndex = def.Value - 1, // Logical line index.
                    Height    = Configurations.CodeBlockPortHeightInPixels
                });
            }
        }
예제 #6
0
        private void ProcessCode(ref string errorMessage, ref string warningMessage,
                                 ElementResolver workspaceElementResolver = null)
        {
            code = CodeBlockUtils.FormatUserText(code);
            codeStatements.Clear();

            if (string.IsNullOrEmpty(Code))
            {
                previewVariable = null;
            }

            try
            {
                // During loading of CBN from file, the elementResolver from the workspace is unavailable
                // in which case, a local copy of the ER obtained from the CBN is used
                var resolver   = workspaceElementResolver ?? this.ElementResolver;
                var parseParam = new ParseParam(GUID, code, resolver);

                if (CompilerUtils.PreCompileCodeBlock(libraryServices.LibraryManagementCore, ref parseParam))
                {
                    if (parseParam.ParsedNodes != null)
                    {
                        // Create an instance of statement for each code statement written by the user
                        foreach (var parsedNode in parseParam.ParsedNodes)
                        {
                            // Create a statement variable from the generated nodes
                            codeStatements.Add(Statement.CreateInstance(parsedNode));
                        }

                        SetPreviewVariable(parseParam.ParsedNodes);
                    }
                }

                if (parseParam.Errors.Any())
                {
                    errorMessage = string.Join("\n", parseParam.Errors.Select(m => m.Message));
                    ProcessError();
                    CreateInputOutputPorts();
                    return;
                }

                if (parseParam.Warnings != null)
                {
                    // Unbound identifiers in CBN will have input slots.
                    //
                    // To check function redefinition, we need to check other
                    // CBN to find out if it has been defined yet. Now just
                    // skip this warning.
                    var warnings =
                        parseParam.Warnings.Where(
                            w =>
                            w.ID != WarningID.kIdUnboundIdentifier &&
                            w.ID != WarningID.kFunctionAlreadyDefined);

                    if (warnings.Any())
                    {
                        warningMessage = string.Join("\n", warnings.Select(m => m.Message));
                    }
                }

                if (parseParam.UnboundIdentifiers != null)
                {
                    inputIdentifiers = new List <string>();
                    inputPortNames   = new List <string>();
                    foreach (var kvp in parseParam.UnboundIdentifiers)
                    {
                        inputIdentifiers.Add(kvp.Value);
                        inputPortNames.Add(kvp.Key);
                    }
                }
                else
                {
                    inputIdentifiers.Clear();
                    inputPortNames.Clear();
                }
            }
            catch (Exception e)
            {
                errorMessage    = e.Message;
                previewVariable = null;
                ProcessError();
                return;
            }

            // Set the input and output ports based on the statements
            CreateInputOutputPorts();
        }