Esempio n. 1
0
 public void Dispose()
 {
     _root     = null;
     _treeSets = null;
     _codemaps = null;
     _tasks    = null;
 }
        static BTTask[] GetAllTasks(BTTree root)
        {
            List <BTTask> taskList = new List <BTTask>();

            taskList.AddRange(root.tasks);

            // Retrieve tasks from proxies too
            var proxies = root.proxies;

            foreach (var proxy in proxies)
            {
                foreach (var a in proxy.target.tasks)
                {
                    if (!taskList.Contains(a))
                    {
                        taskList.Add(a);
                    }
                }
            }
            return(taskList.ToArray());
        }
        public static BTTree GetMain(BTTree[] _trees)
        {
            BTTree main = null;

            if (_trees != null && _trees.Length > 0)
            {
                // Search for the main tree
                foreach (var bt in _trees)
                {
                    if (bt.name.ToLower() == "root")
                    {
                        main = bt;
                    }
                }

                if (main == null)
                {
                    main = _trees[0];
                }
            }

            return(main);
        }
 public override void Dispose()
 {
     target = null;
 }
        static BTTree[] Build(BTLParser.Node[] trees, out CodeMap codemap, bool createCodeMap)
        {
            var           codeMapDict = new Dictionary <BTNode, SubstringLocation>();
            List <BTTree> roots       = new List <BTTree>();

            BTTree root = null;
            // Get all nodes
            var parseNodes = BTLParser.GetNodes(trees);

            var nodeID = new Dictionary <BTLParser.Node, int>();

            for (int i = 0; i < parseNodes.Length; ++i)
            {
                nodeID[parseNodes[i]] = i;
            }


            // create nodes
            var nodes = new BTNode[parseNodes.Length];

            foreach (var n in parseNodes)
            {
                int id = nodeID[n];

                switch (n.token.type)
                {
                case TokenType.Tree:
                    var newRoot = new BTTree();

                    if (n.parameters.Count > 0)
                    {
                        newRoot.name = n.parsedParameters[0] as string;
                    }

                    nodes[id] = newRoot;
                    root      = newRoot;
                    roots.Add(root);

                    break;

                case TokenType.TreeProxy:
                    var rootProxy = new BTTreeProxy();

                    if (n.parameters.Count > 0)
                    {
                        rootProxy.name = n.parsedParameters[0] as string;
                    }

                    nodes[id] = rootProxy;

                    break;


                case TokenType.Value:
                case TokenType.Word:

                    var task = new BTTask();
                    task.taskName = n.token.content.Trim();

                    nodes[id] = task;

                    break;

                case TokenType.Mute:
                    nodes[id] = new BTMute();
                    break;

                case TokenType.Not:
                    nodes[id] = new BTNot();
                    break;

                case TokenType.Repeat:
                    nodes[id] = new BTRepeat();
                    break;

                case TokenType.Fallback:        nodes[id] = new BTFallback(); break;

                case TokenType.Sequence:        nodes[id] = new BTSequence(); break;

                case TokenType.Parallel:        nodes[id] = new BTParallel(); break;

                case TokenType.While:   nodes[id] = new BTWhile(); break;

                case TokenType.Race: nodes[id] = new BTRace(); break;

                case TokenType.Random: nodes[id] = new BTRandom(); break;
                }



                if (nodes[id] != null)
                {
                    nodes[id].i_parameters = n.parsedParameters;
                }

                if (nodes[id] != null)
                {
                    var loc = new SubstringLocation();
                    loc.line   = n.token.line;
                    loc.start  = n.token.substring_start;
                    loc.length = n.token.substring_length;

                    if (n.parameters.Count > 0)
                    {
                        loc.length = n.parseLength;
                    }

                    codeMapDict[nodes[id]] = loc;
                }
            }


            // parenting
            foreach (var n in parseNodes)
            {
                int pid    = nodeID[n];
                var parent = nodes[pid];
                if (parent != null)
                {
                    foreach (var c in n.children)
                    {
                        int cid   = nodeID[c];
                        var child = nodes[cid];
                        if (child != null)
                        {
                            parent.AddChild(child);
                        }
                    }
                }
            }

            if (roots.Count > 0)
            {
                root = roots[0];
            }

            if (createCodeMap)
            {
                codemap = new CodeMap(codeMapDict);
            }
            else
            {
                codemap = null;
            }


            return(roots.ToArray());
        }