Exemplo n.º 1
0
 private RegionTreeNode ConvertToTreeNode(RegionTree regionTree)
 {
     //这里可以写成automap,懒得写了~
     return(new RegionTreeNode()
     {
         Id = regionTree.Id,
         RegionCode = regionTree.RegionCode,
         RegionParentCode = regionTree.RegionParentCode,
         AdministrativeLevel = regionTree.AdministrativeLevel,
         Name = regionTree.Name,
         Children = new List <RegionTreeNode>()
     });
 }
Exemplo n.º 2
0
        public void TestRegionTree()
        {
            var seg0_10 = intervals <int> .Closed <int>(0, 10);

            var seg2_4 = intervals <int> .Closed <int>(2, 4);

            var seg3_5 = intervals <int> .Closed <int>(3, 5);

            var seg4_5 = intervals <int> .Closed <int>(4, 5);

            var seg1_6 = intervals <int> .Closed <int>(1, 6);

            var seg0 = intervals <int> .Singleton <int>(0);

            var seg2 = intervals <int> .Singleton <int>(2);

            var seg3 = intervals <int> .Singleton <int>(3);

            var tree0 = RegionTree.empty <IntKeyWrapper, intervals <int> >();
            var tree1 = RegionTree.write(seg0_10, Wrap(0), tree0);
            var tree2 = RegionTree.write(seg2_4, Wrap(1), tree1);
            var tree3 = RegionTree.write(seg3_5, Wrap(2), tree2);

            RegionTree.checkInvariant(tree0);
            RegionTree.checkInvariant(tree1);
            RegionTree.checkInvariant(tree2);
            RegionTree.checkInvariant(tree3);
            var loc0   = RegionTree.localize(seg0, tree3);
            var loc2   = RegionTree.localize(seg2, tree3);
            var loc3   = RegionTree.localize(seg3, tree3);
            var loc4_5 = RegionTree.localize(seg4_5, tree3);
            var loc1_6 = RegionTree.localize(seg1_6, tree3);

            RegionTree.checkInvariant(regionTree <IntKeyWrapper, intervals <int> > .NewNode(loc0));
            RegionTree.checkInvariant(regionTree <IntKeyWrapper, intervals <int> > .NewNode(loc2));
            RegionTree.checkInvariant(regionTree <IntKeyWrapper, intervals <int> > .NewNode(loc3));
            RegionTree.checkInvariant(regionTree <IntKeyWrapper, intervals <int> > .NewNode(loc4_5));
            RegionTree.checkInvariant(regionTree <IntKeyWrapper, intervals <int> > .NewNode(loc1_6));
            Assert.AreEqual(1, PersistentDict.size(loc0));
            Assert.AreEqual(1, PersistentDict.size(loc2));
            Assert.AreEqual(1, PersistentDict.size(loc3));
            Assert.AreEqual(1, PersistentDict.size(loc4_5));
            Assert.AreEqual(3, PersistentDict.size(loc1_6));
            Assert.AreEqual(0, PersistentDict.toSeq(loc0).Single().Item2.Item1.Key);
            Assert.AreEqual(1, PersistentDict.toSeq(loc2).Single().Item2.Item1.Key);
            Assert.AreEqual(2, PersistentDict.toSeq(loc3).Single().Item2.Item1.Key);
            Assert.AreEqual(2, PersistentDict.toSeq(loc4_5).Single().Item2.Item1.Key);
        }
Exemplo n.º 3
0
        public IEnumerable <RegionTree> RegionTrees()
        {
            // 可是使用断点获取json数据
            var regionList = Resolve <IRegionService>().GetList();
            var regionJson = new List <RegionTree>();

            foreach (var item in regionList)
            {
                var region = new RegionTree {
                    Name     = item.Name,
                    Id       = item.RegionId,
                    ParentId = item.ParentId
                };
                if (regionJson.Where(x => x.Id == item.RegionId).Count() == 0)
                {
                    regionJson.Add(region);
                }
            }

            return(regionJson);
        }
Exemplo n.º 4
0
        private async Task <RegionTreeNode> GetTree(RegionTree d)
        {
            RegionTreeNode    tree = new RegionTreeNode();
            List <RegionTree> list = new List <RegionTree>();
            //向上查询树
            var parent = await db.Regions.FirstOrDefaultAsync(x => x.RegionCode == d.RegionParentCode);

            list.Add(d);
            if (parent != null)
            {
                list.Add(parent);
            }
            int count = 0;     //防止死循环

            while (parent != null && count < 5)
            {
                parent = await db.Regions.FirstOrDefaultAsync(x => x.RegionCode == parent.RegionParentCode);

                if (parent != null)
                {
                    list.Add(parent);
                }
                count++;
            }
            //处理成树结构
            tree          = ConvertToTreeNode(list[list.Count - 1]);
            tree.Children = new List <RegionTreeNode>();
            var node = tree.Children;

            for (int i = list.Count - 2; i >= 0; i--)
            {
                var t = ConvertToTreeNode(list[i]);
                node.Add(t);
                node[0].Children = new List <RegionTreeNode>();
                node             = node[0].Children;
            }
            return(tree);
        }
Exemplo n.º 5
0
        /// <summary>
        /// Maps a RegionTree to a grid space. Uses a Greedy algorithm.
        /// </summary>
        /// <param name="r">The RegionTree used for the mapping</param>
        /// <returns>Null if unsuccesful, a dictionary of Points & RegionTreeMarkers otherwise.</returns>
        public override Dictionary<Point, RegionMarker> mapToWorld(RegionTree tree)
        {
            Dictionary<Point, RegionMarker> markers = new Dictionary<Point, RegionMarker>(); //will contain the RegionMarker objects for this world
            Dictionary<RegionTreeNode, Point> parents = new Dictionary<RegionTreeNode, Point>(); //will contain every node that has already been given a location

            List<RegionTreeNode> queue = new List<RegionTreeNode>(); //The list of nodes to be processed
            Random generator = new Random(10); //Initializes a random number generator

            queue.Add(tree.root); //Adds the root node to the queue

            //While there are still nodes in the queue
            while (queue.Count > 0)
            {
                RegionTreeNode currNode = queue.ElementAt(0); //The current node is the head of the queue
                RegionTreeNode parent = currNode.Parent; //The parent of the current node
                queue.AddRange(currNode.children); //Adds the children of the current node to the queue
                queue.Remove(currNode); //Removes the current node from the queue

                if (parent == null) //If the current node is the root of the tree
                {
                    //Place root node at the origin
                    Point loc = new Point(0, 0);
                    RegionMarker marker = new RegionMarker(loc, false, false, false, false, currNode.region.environment.getName());
                    parents.Add(currNode, loc);
                    markers.Add(loc, marker);
                }
                else
                {
                    //If the parent node of the current node is not in the Dictionary already, throw an Exception. Should never occur
                    if (!parents.ContainsKey(parent))
                        throw new Exception("Parent not in Dictionary");

                    Point parLoc = parents[parent]; //Gets the location of this node's parent node
                    bool success = false; //whether or not a placement can be found for this node
                    List<int> tried = new List<int>(); //The directions that have been tried so far

                    //Attempts to place the child node in one of the four directions, randomly chosen until succesful
                    while (!success || tried.Count < 4)
                    {
                        int direction = generator.Next(4); //Randomly generated direction
                        if (!tried.Contains(direction)) //If this direction has not already been done, add it to the list of tried directions
                            tried.Add(direction);

                        Point placement = parLoc; //The location of the placement. Initialized to the parent location
                        if (direction == 0) placement = RegionTreeMapper.getNorth(parLoc); //If north
                        else if (direction == 1) placement = RegionTreeMapper.getSouth(parLoc); //If south
                        else if (direction == 2) placement = RegionTreeMapper.getEast(parLoc); //If east
                        else if (direction == 3) placement = RegionTreeMapper.getWest(parLoc); //If west

                        success = !markers.ContainsKey(placement); //If the location of the chosen direction already has a node in it

                        if (success) //If the location is available
                        {
                            RegionMarker parentMarker = markers[parLoc]; //Get the marker of the parent
                            RegionMarker childMarker = new RegionMarker(placement, false, false, false, false, currNode.region.environment.getName()); //initializes a RegionMarker for this node
                            if (direction == 0) //If North
                            {
                                parentMarker.connectsTop = true; //Parent is connected at the top
                                childMarker.connectsBottom = true; //Child is connected at the bottom
                            }
                            else if (direction == 1) //If South
                            {
                                parentMarker.connectsBottom = true; //Parent is connected at the bottom
                                childMarker.connectsTop = true; //Child is connected at the top
                            }
                            else if (direction == 2) //If East
                            {
                                parentMarker.connectsRight = true; //Parent is connected at the right
                                childMarker.connectsLeft = true; //Child is connected at the left
                            }
                            else if (direction == 3) //If West
                            {
                                parentMarker.connectsLeft = true; //Parent is connected at the left
                                childMarker.connectsRight = true; //Child is connected at the right
                            }

                            parents.Add(currNode, placement); //Add the newly created node's location to the parents Dictionary
                            markers.Add(placement, childMarker); //Adds the newly created node to the markers
                        }
                        else //If the location is unavailable, the algorithm failed and returns null
                            return null;
                    }

                }
            }
            return markers;
        }
Exemplo n.º 6
0
 /// <summary>
 /// Maps a RegionTree to a grid space.
 /// </summary>
 /// <param name="r">The RegionTree used for the mapping</param>
 /// <returns>Null if unsuccesful, a dictionary of Points & RegionTreeMarkers otherwise.</returns>
 public abstract Dictionary<Point, RegionMarker> mapToWorld(RegionTree r);
        private const int RANDOM_SEED = 10; //The seed used for the random number generator

        #endregion Fields

        #region Constructors

        /// <summary>
        /// Initializes a RegionTreeMapper with the given RegionTree
        /// </summary>
        /// <param name="sol">The RegionTree that goes with this mapper</param>
        /// <param name="mapper">The MappingAlgorithm used to perform the mapping</param>
        public RegionTreeMapper(RegionTree sol, MappingAlgorithm mapAl)
        {
            tree = sol;
            ma = mapAl;
        }