Ejemplo n.º 1
0
        /// <summary>
        /// Transforms Node to the Final ProtoAST form
        /// </summary>
        /// <returns></returns>
        public List<AssociativeNode> ToGraphIR(AST graph, GraphCompiler graphCompiler)
        {
            GraphNodeToASTGenerator astGen = new GraphNodeToASTGenerator(graph, graphCompiler);
            astGen.AddNodesToAST();
            List<ProtoCore.AST.AssociativeAST.AssociativeNode> irGraph = astGen.SplitAST();

            return irGraph;
        }
Ejemplo n.º 2
0
        private void InitRunner(Options options)
        {
            graphCompiler = GraphToDSCompiler.GraphCompiler.CreateInstance();
            graphCompiler.SetCore(GraphUtilities.GetCore());
            runner = new ProtoScriptTestRunner();

            executionOptions = options;
            InitOptions();
            InitCore();


            taskQueue = new Queue <Task>();

            workerThread = new Thread(new ThreadStart(TaskExecMethod));
            workerThread.IsBackground = true;
            workerThread.Start();

            staticContext = new ProtoCore.CompileTime.Context();
        }
Ejemplo n.º 3
0
        public void TestDivision01()
        {
            GraphToDSCompiler.GraphCompiler gc = GraphToDSCompiler.GraphCompiler.CreateInstance();
            gc.CreateOperatorNode(1, "/");
            object o1 = 10;

            gc.CreateLiteralNode(2, o1);
            object o2 = 11;

            gc.CreateLiteralNode(3, o2);
            gc.ConnectNodes(2, 0, 1, 0);
            gc.ConnectNodes(3, 0, 1, 1);
            string mmx = gc.GetGraph2String();

            mmx = mmx.Trim();
            ExecutionMirror mirror = thisTest.RunScriptSource(mmx);
            Obj             o      = mirror.GetValue("temp20001");

            Assert.IsTrue((Double)o.Payload == (10 / 11));
        }
Ejemplo n.º 4
0
 /*Tron*/
 public List<SnapshotNode> PrintCodeForSelectedNodes(GraphCompiler originalGC, List<SnapshotNode> inputs)
 {
     this.AddNodesToAST();
     this.MakeConnectionsForAddedNodes();
     bool removeFromRemovedNodes = true;
     this.RemoveNodes(removeFromRemovedNodes);
     this.UpdateModifiedNodes();
     //List<string> code = this.gc.PrintGraph_NodeToCode();
     List<SnapshotNode> code = this.gc.ToCode(this.gc.Graph, originalGC, inputs);
     gc.UpdateDirtyFlags(nodesToModify);
     return code;
 }
 public GraphNodeToASTGenerator(AST graph, GraphCompiler gc)
 {
     this.graph = graph;
     this.gc = gc;
 }
Ejemplo n.º 6
0
        /**/
        public List<uint> ConnectionToUID(List<Connection> input)
        {
            List<uint> result = new List<uint>();
            foreach (Connection inputConnection in input)
            {
                result.Add(inputConnection.OtherNode);
            }
            return result;
        }

        /*Tron: Use for node to code function
         *For each connected component of the graph, generate a respective string of code
         */
        public List<SnapshotNode> ToCode(AST graph, GraphCompiler originalGC, List<SnapshotNode> inputs)
        {
            List<SnapshotNode> result = new List<SnapshotNode>();

            List<Node> li = TopSort.sort(graph);
            tguid = 20000;
            List<string> listIslands = new List<string>();
            List<Node> islandNodes = new List<Node>();
            int countIslands = 0;
            statementList = new AST();
            ModifiedStmtGuidList.Clear();
            List<string> importIslands = new List<string>();
            IEnumerable iter = li;
            List<Node> islandNodeList = new List<Node>();
            //List<List<Node>> listing = new List<List<Node>>();
            List<Node> listing = new List<Node>();
            foreach (Node node in iter)
            {
                if (node != null)
                {
                    if (node is ImportNode)
                    {
                        importIslands.Add(node.ToScript() + ProtoCore.DSASM.Constants.termline);
                    }
                    else if (node.IsIsland)
                    {
                        countIslands++;
                        if (node is ArrayNode)
                        {
                            BuildArrayNodeStatement(node, statementList);
                            if (!islandNodes.Contains(node))
                                islandNodes.Add(node);
                            string island = statementList.GetNode(node.Guid).ToScript() + ProtoCore.DSASM.Constants.termline;
                            if (!listIslands.Contains(island))
                                listIslands.Add(island);
                        }
                        else if (node is LiteralNode)
                        {
                            BuildLiteralNodeStatement(node, statementList);
                            string island = statementList.GetNode(node.Guid).ToScript() + ProtoCore.DSASM.Constants.termline;
                            if (!listIslands.Contains(island))
                                listIslands.Add(island);
                        }
                        else if (node is Func)
                        {
                            BuildFunctionCallStatement(node, statementList);
                            string island = statementList.GetNode(node.Guid).ToScript() + ProtoCore.DSASM.Constants.termline;
                            if (!listIslands.Contains(island))
                                listIslands.Add(island);
                        }
                        else if (node is Operator)
                        {
                            BuildOperatorStatement(node, statementList);
                            string island = statementList.GetNode(node.Guid).ToScript() + ProtoCore.DSASM.Constants.termline;
                            if (!listIslands.Contains(island))
                                listIslands.Add(island);
                        }
                        else if (node is Block)
                        {
                            BuildBlockStatement(node, statementList);
                            islandNodes.Add(node);
                            //string island = statementList.GetNode(node.Guid).ToScript() + ProtoCore.DSASM.Constants.termline;
                            //if (!listIslands.Contains(island))
                              //  listIslands.Add(island);
                        }
                        else if (node is IdentNode)
                        {
                            // comment Jun:
                            // An island identifier node is handled by emitting a null as its rhs
                            statementList.AddNode(node);
                            string contents = statementList.GetNode(node.Guid).ToScript() + "=" + ProtoCore.DSASM.Literal.Null + ProtoCore.DSASM.Constants.termline;
                            listIslands.Add(contents);
                        }
                        else
                        {
                            statementList.AddNode(node);
                            string island = node.ToScript() + ProtoCore.DSASM.Constants.termline;
                            if (!listIslands.Contains(island))
                                listIslands.Add(island);
                        }
                        islandNodeList.Add(node);
                        listing = listing.ToList().Union<Node>(BuildStatement(node, statementList)).ToList();
                        HandleNewNode(node);
                    }
                    else if (node.IsLeaf)
                    {
                        if (node is ArrayNode)
                        {
                            BuildArrayNodeStatement(node, statementList);
                        }
                        else if (node is LiteralNode)
                        {
                            BuildLiteralNodeStatement(node, statementList);
                        }
                        else if (node is Func)
                        {
                            BuildFunctionCallStatement(node, statementList);
                        }
                        else if (node is Operator)
                        {
                            BuildOperatorStatement(node, statementList);
                        }
                        else if (node is Block)
                        {
                            BuildBlockStatement(node, statementList);
                        }
                        else if (node is IdentNode)
                        {
                            statementList.AddNode(node);
                            string contents = statementList.GetNode(node.Guid).ToScript() + "=" + ProtoCore.DSASM.Literal.Null + ProtoCore.DSASM.Constants.termline;
                            listIslands.Add(contents);
                        }
                        HandleNewNode(node);
                    }
                    else if (node.IsRoot && !node.IsIsland)
                    {
                        if (node is Operator)
                        {
                            BuildOperatorStatement(node, statementList);
                        }
                        else if (node is Func)
                        {
                            BuildFunctionCallStatement(node, statementList);
                        }
                        else if (node is Block)
                        {
                            BuildBlockStatement(node, statementList);
                        }
                        //liststat = BuildStatement(node, statementList);
                        //finalScript=finalScript.Union(BuildStatement(node, statementList)).ToList();
                        
                        //comment out for NodeToCode function
                        //listing.Add(BuildStatement(node, statementList));
                        listing = BuildStatement(node, statementList);
                        HandleNewNode(node);
                    }
                    else if (node is Operator)
                    {
                        BuildOperatorStatement(node, statementList);
                        HandleNewNode(node);
                    }
                    else if (node is Func)
                    {
                        BuildFunctionCallStatement(node, statementList);
                        HandleNewNode(node);
                    }
                    else if (node is ArrayNode)
                    {
                        BuildArrayNodeStatement(node, statementList);
                        HandleNewNode(node);
                    }
                    else if (node is LiteralNode)
                    {
                        BuildLiteralNodeStatement(node, statementList);
                        HandleNewNode(node);
                    }
                    else if (node is Block)
                    {
                        BuildBlockStatement(node, statementList);
                        listing = BuildStatement(node, statementList);
                        HandleNewNode(node);
                    }
                }
            }
            StringBuilder builder = new StringBuilder();
            foreach (string island in importIslands)// Loop through all strings
            {
                builder.Append(island);             // Append string to StringBuilder
            }
            foreach (string island in listIslands)  // Loop through all strings
            {
                builder.Append(island);             // Append string to StringBuilder
            }

            /*N2C*/
            #region get connected components of the graph
            List<Node> nodeToCodeInputList = new List<Node>();
            List<List<Node>> listingAlternate = new List<List<Node>>();
            List<List<Node>> listingAlt2 = new List<List<Node>>();
            if (nodeToCodeUIDs.Count != 0)
            {
                foreach (uint nodeID in nodeToCodeUIDs)
                {
                    if (graph.GetNode(nodeID) != null)
                    {
                        nodeToCodeInputList.Add(graph.GetNode(nodeID));
                    }
                    else
                    {
                        if (this.codeBlockUIDMap[nodeID] != null)
                        {
                            foreach (KeyValuePair<int, uint> pair in this.codeBlockUIDMap[nodeID])
                            {
                                nodeToCodeInputList.Add(graph.GetNode(pair.Value));
                            }
                        }
                    }
                }
                //listingAlternate = GetConnectedComponents(nodeToCodeInputList, graph);
                listingAlt2 = GetConnectedComponents_02(nodeToCodeInputList, graph);
            }
            #endregion

            //foreach (List<Node> n1 in listing)
            //{
            //    foreach (Node n2 in n1)
            //        if (!finalScript.Contains(n2))
            //            finalScript.Add(n2);
            //        else
            //        {
            //            finalScript.Remove(n2);
            //            finalScript.Add(n2);
            //        }
            //}
            listing = SortCodeBlocks(listing);
            List<List<Node>> finalList = new List<List<Node>>();
            foreach (List<Node> l1 in listingAlt2)
            {
                List<Node> temp = new List<Node>();
                foreach (Node n1 in l1)
                    foreach (Node listingNode in listing)
                        if (listingNode.Guid == n1.Guid)
                            temp.Add(listingNode);
                if (temp.Count != 0)
                    finalList.Add(temp);
            }            
            islandNodeList = islandNodeList.Union(islandNodes).ToList();
            //islandNodeList = SortCodeBlocks(islandNodeList);
            //if (islandNodeList.Count != 0)
            //    finalList.Add(islandNodeList);

            #region generate code and snapshot node
            uint id = 0;
            foreach (List<Node> nodeList in finalList)
            {
                string output = "";
                List<Node> tempList = SortCodeBlocks(nodeList);
                //tempList.Reverse();
                foreach (Node node in tempList)
                {
                    if (nodeToCodeUIDs.Contains(node.Guid))
                    {
                        if (node.ToCode() != null)
                            output += node.ToCode().Replace(";", "") + ProtoCore.DSASM.Constants.termline;
                    }
                    else
                    {
                        foreach (KeyValuePair<uint, Dictionary<int, uint>> pair in this.codeBlockUIDMap)
                        {
                            if (pair.Value.ContainsValue(node.Guid))
                            {
                                output += node.ToCode().Replace(";", "") + ProtoCore.DSASM.Constants.termline; //ensure only one semicolon at the end of a statement, by request from UI
                            }
                        }
                    }    
                }
                output = output.TrimEnd('\n');
                SnapshotNode ssn = new SnapshotNode();
                ssn.Id = id++;
                ssn.Content = output;
                ssn.Type = SnapshotNodeType.CodeBlock;
                ssn.InputList = new List<Connection>();

                foreach (SnapshotNode inputNode in inputs)
                {
                    foreach (Node subTreeNode in nodeList)
                    {
                        if (inputNode.Id == subTreeNode.Guid)
                        {
                            foreach (Connection c1 in inputNode.InputList)
                            {
                                if (!IsInternalConnection(c1, this))                //the connection is not internal, return it back to UI
                                {
                                    Connection newInputConnection = new Connection();
                                    newInputConnection.OtherNode = c1.OtherNode;
                                    newInputConnection.OtherIndex = c1.OtherIndex;
                                    newInputConnection.IsImplicit = c1.IsImplicit;
                                    string[] tokens = graph.GetNode(c1.OtherNode).Name.Split('=');
                                    newInputConnection.LocalName = tokens[0];
                                    ssn.InputList.Add(newInputConnection);
                                }
                            }
                        }
                        else if (codeBlockUIDMap.ContainsKey(inputNode.Id))         //inputNode was split
                        {
                            if (codeBlockUIDMap[inputNode.Id].ContainsValue(subTreeNode.Guid))
                            {
                                foreach (Connection c1 in inputNode.InputList)
                                {
                                    if (!IsInternalConnection(c1, this))
                                    {
                                        int indexSlot = 0;
                                        foreach (KeyValuePair<int, uint> pair in codeBlockUIDMap[inputNode.Id])
                                        {
                                            if (pair.Value == subTreeNode.Guid)
                                                indexSlot = pair.Key;
                                        }
                                        if (c1.OtherIndex == indexSlot)
                                        {
                                            Connection newInputConnection = new Connection();
                                            newInputConnection.OtherNode = c1.OtherNode;
                                            newInputConnection.OtherIndex = c1.OtherIndex;
                                            foreach (KeyValuePair<uint, Dictionary<int, uint>> pair in originalGC.codeBlockUIDMap)
                                            {
                                                if (pair.Value.ContainsValue(c1.OtherNode))                     //this means if the other node was split, return the original Id that was sent to us by the UI
                                                {
                                                    newInputConnection.OtherNode = pair.Key;
                                                    newInputConnection.OtherIndex = pair.Value.First(x => x.Value == c1.OtherNode).Key;
                                                }
                                            }
                                            newInputConnection.IsImplicit = c1.IsImplicit;
                                            string[] tokens = graph.GetNode(c1.OtherNode).Name.Split('=');
                                            newInputConnection.LocalName = tokens[0];
                                            ssn.InputList.Add(newInputConnection);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                ssn.OutputList = new List<Connection>();
                foreach (SnapshotNode inputNode in inputs)
                {
                    foreach (Node subTreeNode in nodeList)
                    {
                        //if (subTreeNode.Name.Split('=')[0] == outputCnt.LocalName)
                        if (inputNode.Id == subTreeNode.Guid)
                        {
                            foreach (Connection c1 in inputNode.OutputList)
                            {
                                if (!IsInternalConnection(c1, this))
                                {
                                    Connection newOutputConnection = new Connection();
                                    newOutputConnection.OtherNode = c1.OtherNode;
                                    newOutputConnection.OtherIndex = c1.OtherIndex;
                                    newOutputConnection.IsImplicit = c1.IsImplicit;
                                    newOutputConnection.LocalName = c1.LocalName;
                                    ssn.OutputList.Add(newOutputConnection);
                                }
                            }
                        }

                        else if (codeBlockUIDMap.ContainsKey(inputNode.Id))         //inputNode was split
                        {
                            if (codeBlockUIDMap[inputNode.Id].ContainsValue(subTreeNode.Guid))
                            {
                                foreach (Connection c1 in inputNode.OutputList)
                                {
                                    if (!IsInternalConnection(c1, this))
                                    {
                                        int indexSlot = 0;
                                        foreach (KeyValuePair<int, uint> pair in codeBlockUIDMap[inputNode.Id])
                                        {
                                            if (pair.Value == subTreeNode.Guid)
                                            {
                                                indexSlot = pair.Key;
                                                break;
                                            }
                                        }
                                        if (c1.LocalIndex == indexSlot)
                                        {
                                            Connection newOutputConnection = new Connection();
                                            newOutputConnection.OtherNode = c1.OtherNode;
                                            newOutputConnection.OtherIndex = c1.OtherIndex;
                                            foreach (KeyValuePair<uint, Dictionary<int, uint>> pair in originalGC.codeBlockUIDMap)
                                            {
                                                if (pair.Value.ContainsValue(c1.OtherNode))                     //this means if the other node was split, return the original Id that was sent to us by the UI
                                                {
                                                    newOutputConnection.OtherNode = pair.Key;
                                                    newOutputConnection.OtherIndex = pair.Value.First(x => x.Value == c1.OtherNode).Key;
                                                }
                                            }
                                            newOutputConnection.IsImplicit = c1.IsImplicit;
                                            newOutputConnection.LocalName = c1.LocalName;
                                            ssn.OutputList.Add(newOutputConnection);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                result.Add(ssn);
            }
            #endregion

            #region remove _temp_xxx name
            //Dictionary<string, string> tempReplaceValue = new Dictionary<string, string>();
            //for (int i = 0; i < result.Count; i++)
            //{
            //    SnapshotNode ssn = result[i];
            //    string[] statements = ssn.Content.Split(';');
            //    for (int j = 0; j < statements.Length; j++)
            //    {
            //        string statement = statements[j];
            //        string lhsTempName = statement.Split('=')[0].Replace("\n", "").Trim();
            //        foreach (Node astNode in graph.nodeList)
            //        {
            //            if (astNode.Name.Split('=')[0].Trim() == lhsTempName && lhsTempName.StartsWith("_temp_"))
            //            {
            //                //this means in the ast there is some statement _temp_abc = something
            //                //which means the _temp_xxx is generated, not typed in by users
            //                tempReplaceValue.Add(lhsTempName, statement.Split('=')[1].Replace("\n", "").Trim());
            //            }
            //        }
            //    }
            //}
            //for (int i = 0; i < result.Count; i++)
            //{
            //    SnapshotNode ssn = result[i];
            //    foreach (KeyValuePair<string, string> pair in tempReplaceValue)
            //    {
            //        ssn.Content.Replace(pair.Key, pair.Value);
            //    }
            //}
            #endregion

            #region replace _temp_ name with more elegant name
            //int tempId = 0;
            //for (int i = 0; i < result.Count; i++)
            //{
            //    SnapshotNode ssn = result[i];
            //    string[] statements = ssn.Content.Split(';');
            //    for (int j = 0; j < statements.Length; j++)
            //    {
            //        string statement = statements[j];
            //        string lhsTempName = statement.Split('=')[0].Replace("\n", "").Trim();
            //        foreach (Node astNode in graph.nodeList)
            //        {
            //            if (astNode.Name.Split('=')[0].Trim() == lhsTempName && lhsTempName.StartsWith("_temp_"))
            //            {
            //                string newTempName = "temp_" + tempId++;
            //                for (int k = 0; k < result.Count; k++)
            //                {
            //                    result[k].Content = result[k].Content.Replace(lhsTempName, newTempName);
            //                }
            //            }
            //        }
            //        if (statement.Split('=').Length > 1)
            //        {
            //            string rhsTempName = statement.Split('=')[1].Replace("\n", "").Trim();
            //            foreach (Node astNode in graph.nodeList)
            //            {
            //                if (astNode.Name.Split('=')[0].Trim() == rhsTempName && rhsTempName.StartsWith("_temp_"))
            //                {
            //                    string newTempName = "temp_" + tempId++;
            //                    for (int k = 0; k < result.Count; k++)
            //                    {
            //                        result[k].Content = result[k].Content.Replace(rhsTempName, newTempName);
            //                    }
            //                }
            //            }
            //        }
            //    }
            //}
            #endregion

            #region return to original input connections
            for (int i = 0; i < result.Count; i++)
            {
                SnapshotNode ssn = result[i];
                for (int j = 0; j < ssn.InputList.Count; j++)
                {
                    Connection inputConnection = ssn.InputList[j];
                    foreach (KeyValuePair<uint, Dictionary<int, uint>> kvp in originalGC.codeBlockUIDMap)
                    {
                        foreach (KeyValuePair<int, uint> kvp2 in kvp.Value)
                        {
                            if (kvp2.Value == inputConnection.OtherNode)
                            {
                                Connection oldInputConnection = new Connection();
                                oldInputConnection.OtherNode = kvp.Key;
                                oldInputConnection.OtherIndex = inputConnection.OtherIndex;
                                oldInputConnection.IsImplicit = inputConnection.IsImplicit;
                                oldInputConnection.LocalName = inputConnection.LocalName;
                                oldInputConnection.LocalIndex = inputConnection.LocalIndex;
                                ssn.InputList.Remove(inputConnection);
                                ssn.InputList.Insert(j, oldInputConnection);
                            }
                        }
                    }
                }

                for (int j = 0; j < ssn.OutputList.Count; j++)
                {
                    Connection outputConnection = ssn.OutputList[j];
                    foreach (KeyValuePair<uint, Dictionary<int, uint>> kvp in originalGC.codeBlockUIDMap)
                    {
                        foreach (KeyValuePair<int, uint> kvp2 in kvp.Value)
                        {
                            if (kvp2.Value == outputConnection.OtherNode)
                            {
                                Connection oldInputConnection = new Connection();
                                oldInputConnection.OtherNode = kvp.Key;
                                oldInputConnection.OtherIndex = outputConnection.OtherIndex;
                                oldInputConnection.IsImplicit = outputConnection.IsImplicit;
                                oldInputConnection.LocalName = outputConnection.LocalName;
                                oldInputConnection.LocalIndex = outputConnection.LocalIndex;
                                ssn.OutputList.Remove(outputConnection);
                                ssn.OutputList.Insert(j, oldInputConnection);
                            }
                        }
                    }
                }
            }
            #endregion
            /*Chirag's
            foreach (var value in finalScript)
            {
                if (nodeToCodeUIDs.Contains((value as Node).Guid))
                    if (value.ToCode() != null)
                        liststat += value.ToCode() + ProtoCore.DSASM.Constants.termline;
            }
            liststat = builder.ToString() + liststat;
            //liststat += builder.ToString();
            //GraphUtilities.runningUID = GraphToDSCompiler.Constants.UIDStart;
            //result.AddRange(liststat.Split(';'));
            */
            UpdateAddedNodesInModifiedNameList();

            return result;
        }
Ejemplo n.º 7
0
 private bool IsInternalConnection(Connection c1, GraphCompiler oriGC)
 {
     bool result = false;
     if (nodeToCodeUIDs.Contains(c1.OtherNode))
     {
         result = true;
     }  
     else
     {
         foreach (KeyValuePair<uint, Dictionary<int, uint>> kvp in oriGC.codeBlockUIDMap)
         {
             if (nodeToCodeUIDs.Contains(kvp.Key) && kvp.Value.ContainsValue(c1.OtherNode))
                 result = true;
         }
     }
     return result;
 }
Ejemplo n.º 8
0
        private void InitRunner(Options options)
        {
            graphCompiler = GraphToDSCompiler.GraphCompiler.CreateInstance();
            graphCompiler.SetCore(GraphUtilities.GetCore());
            runner = new ProtoScriptTestRunner();

            executionOptions = options;
            InitOptions();
            InitCore();


            taskQueue = new Queue<Task>();

            workerThread = new Thread(new ThreadStart(TaskExecMethod));


            workerThread.IsBackground = true;
            workerThread.Start();

            staticContext = new ProtoCore.CompileTime.Context();

            terminating = false;
            changeSetComputer = new ChangeSetComputer(runnerCore);
            changeSetApplier = new ChangeSetApplier();
        }
Ejemplo n.º 9
0
        public LiveRunner(Configuration configuration)
        {
            this.configuration = configuration;

            graphCompiler = GraphCompiler.CreateInstance();
            graphCompiler.SetCore(GraphUtilities.GetCore());

            runner = new ProtoScriptTestRunner();

            InitCore();

            taskQueue = new Queue<Task>();

            workerThread = new Thread(new ThreadStart(TaskExecMethod));
            workerThread.IsBackground = true;
            workerThread.Start();

            staticContext = new ProtoCore.CompileTime.Context();

            terminating = false;
            changeSetComputer = new ChangeSetComputer(runnerCore);
            changeSetApplier = new ChangeSetApplier();
        }