Beispiel #1
0
 public void OnEnable()
 {
     if (map == null)
     {
         map = new NodeDictionary();
     }
 }
        protected override void GenerateTree(object obj) {

            NewReleaseItem newReleaseItem = (NewReleaseItem) obj;
            NewReleaseHierarchyNode root = new NewReleaseHierarchyNode(newReleaseItem, this);
            RootNodes.Add(root);
            NodeDictionary.Add(root.NewReleaseItem.Album.Name, root);
        }
Beispiel #3
0
            public void Set(string key, Node value)
            {
                if (_source != null)
                {
                    _buckets = new Bucket[Capacity];

                    for (var i = 0; i < Capacity; i++)
                    {
                        var sourceBucket = _source._buckets[i];

                        if (sourceBucket != null)
                        {
                            _buckets[i] = new Bucket(sourceBucket);
                        }
                    }

                    _source = null;
                }

                var hashCode = key.GetHashCode() & 0x7FFFFFFF;
                var index    = hashCode % Capacity;

                var bucket = _buckets[index];

                if (bucket == null)
                {
                    bucket          = new Bucket();
                    _buckets[index] = bucket;
                }

                if (bucket.Set(key, value))
                {
                    _size++;
                }
            }
Beispiel #4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="node"></param>
        /// <returns>Returns whether the sub-tree has been fully expanded in the hierarchy.</returns>
        public override void GenerateSubTree(IHierarchyNode node)
        {
            ArtistHierarchyNode subTreeNode       = (ArtistHierarchyNode)node;
            ArtistGroup         relatedArtistList = Client.GetRelatedArtists(subTreeNode.ArtistViewModel.Artist.ID);

            //First determine which artists are new; add those.
            int nodesAdded = 0;

            foreach (Artist relatedArtist in relatedArtistList.Artists)
            {
                if (NodeDictionary.ContainsKey(relatedArtist.Name))
                {
                    continue;
                }

                if (nodesAdded < AddChildrenLimit)
                {
                    ArtistViewModel     artistViewModel   = new ArtistViewModel(relatedArtist);
                    ArtistHierarchyNode relatedArtistNode = new ArtistHierarchyNode(artistViewModel, this, subTreeNode, subTreeNode.Level + 1);
                    subTreeNode.Children.Add(relatedArtistNode);
                    NodeDictionary.Add(relatedArtist.Name, relatedArtistNode);
                    nodesAdded++;
                }
                else
                {
                    return;
                }
            }
        }
Beispiel #5
0
        public bool TryAddNode(Node node)
        {
            if (NodeDictionary.ContainsKey(node.ID) == false)
            {
                Nodes.Add(node);
                NodeDictionary.Add(node.ID, node);
                return(true);
            }

            return(false);
        }
Beispiel #6
0
        public bool TryRemoveNode(Node node)
        {
            if (NodeDictionary.ContainsKey(node.ID))
            {
                Nodes.Remove(node);
                NodeDictionary.Remove(node.ID);
                return(true);
            }

            return(false);
        }
Beispiel #7
0
        void BuildNodeDictionary()
        {
            NodeDictionary.Clear();

            foreach (var node in Nodes)
            {
                if (NodeDictionary.ContainsKey(node.ID) == false)
                {
                    NodeDictionary.Add(node.ID, node);
                }
            }
        }
Beispiel #8
0
 /// <summary>
 /// Collects the current key's object and all objects that depend on the current key into an ever-growing HashSet.
 /// </summary>
 /// <param name="objKey">Fetch the node associated with this object.</param>
 /// <param name="objReturn">HashSet containing all keys that depend on <paramref name="objKey"/> in some way. It's a HashSet to prevent infinite loops in case of cycles</param>
 private void CollectDependents(T objKey, HashSet <T> objReturn)
 {
     if (NodeDictionary.TryGetValue(objKey, out DependencyGraphNode <T> objLoopNode))
     {
         if (objReturn.Add(objLoopNode.MyObject))
         {
             foreach (DependencyGraphNode <T> objDependant in objLoopNode.UpStreamNodes.Where(x => x.DependencyCondition?.Invoke() != false).Select(x => x.Node))
             {
                 CollectDependents(objDependant.MyObject, objReturn);
             }
         }
     }
 }
Beispiel #9
0
 /// <summary>
 /// Collects the current key's object and all objects that depend on the current key into an ever-growing HashSet.
 /// </summary>
 /// <param name="objParentInstance">Instance of the object whose dependencies are being processed, used for conditions.</param>
 /// <param name="objKey">Fetch the node associated with this object.</param>
 /// <param name="objReturn">Collection containing all keys that depend on <paramref name="objKey"/> in some way. It's a HashSet to prevent infinite loops in case of cycles</param>
 protected void CollectDependents(T2 objParentInstance, T objKey, ICollection <T> objReturn)
 {
     if (NodeDictionary.TryGetValue(objKey, out DependencyGraphNode <T, T2> objLoopNode) && !objReturn.Contains(objLoopNode.MyObject))
     {
         objReturn.Add(objLoopNode.MyObject);
         foreach (DependencyGraphNodeWithCondition <T, T2> objNode in objLoopNode.UpStreamNodes)
         {
             if (objNode.DependencyCondition?.Invoke(objParentInstance) != false)
             {
                 CollectDependents(objParentInstance, objNode.Node.MyObject, objReturn);
             }
         }
     }
 }
        protected virtual SiteMapNode CreateNodeFromElement(XElement element, int currentLevel)
        {
            var attributes = new NameValueCollection();

            foreach (var a in element.Attributes())
            {
                attributes.Add(a.Name.ToString(), a.Value);
            }

            string uri;

            try
            {
                if (element.Attribute("uri") != null)
                {
                    uri = element.Attribute("uri").Value;
                }
                else if (element.Attribute("pageId") != null)
                {
                    uri = element.Attribute("pageId").Value;
                }
                else
                {
                    uri = "";
                }
            }
            catch
            {
                _logger.Debug("exception while retrieving uri", LoggingCategory.General);
                uri = "";
            }
            SiteMapNode childNode = new TridionSiteMapNode(this,
                                                           element.Attribute("id").Value,          //key
                                                           uri,
                                                           element.Attribute("url").Value,         //url
                                                           element.Attribute("title").Value,       //title
                                                           element.Attribute("description").Value, //description
                                                           null,                                   //roles
                                                           attributes,                             //attributes
                                                           null,                                   //explicitresourceKeys
                                                           null)
            {
                Level = currentLevel
            };                                  // implicitresourceKey

            NodeDictionary.Add(childNode.Key, childNode);

            return(childNode);
        }
Beispiel #11
0
        /// <summary>
        /// Returns an enumerable containing the current key's object and all objects that depend on the current key.
        /// Warning: DependencyGraphs with any cycles will cause this method to never terminate!
        /// </summary>
        /// <param name="objKey">Fetch the node associated with this object.</param>
        public IEnumerable <T> GetWithAllDependentsUnsafe(T objKey)
        {
            if (NodeDictionary.TryGetValue(objKey, out DependencyGraphNode <T> objLoopNode))
            {
                yield return(objLoopNode.MyObject);

                foreach (DependencyGraphNode <T> objDependant in objLoopNode.UpStreamNodes.Where(x => x.DependencyCondition?.Invoke() != false).Select(x => x.Node))
                {
                    foreach (T objDependantObject in GetWithAllDependentsUnsafe(objDependant.MyObject))
                    {
                        yield return(objDependantObject);
                    }
                }
            }
        }
Beispiel #12
0
        public Boolean Add([CanBeNull] T item)
        {
            if (item is null)
            {
                return(false);
            }

            if (NodeDictionary.ContainsKey(item))
            {
                return(false);
            }

            LinkedListNode <T> node = LinkedList.AddLast(item);

            NodeDictionary.Add(item, node);
            return(true);
        }
Beispiel #13
0
        /// <summary>
        /// Generates the tree from the root artist.
        /// </summary>
        /// <param name="artist"></param>
        protected override void GenerateTree(object obj)
        {
            Artist artist = (Artist)obj;

            if (NodeDictionary.ContainsKey(artist.Name))
            {
                return;
            }

            ArtistViewModel     rootViewModel = new ArtistViewModel(artist);
            ArtistHierarchyNode root          = new ArtistHierarchyNode(rootViewModel, this, null, 0);

            RootNodes.Add(root);
            NodeDictionary.Add(artist.Name, root);

            ArtistGroup relatedArtistList = Client.GetRelatedArtists(artist.ID);

            //First determine which artists are new; add those.
            List <Artist> newArtists = new List <Artist>();
            int           nodesAdded = 0;

            foreach (Artist relatedArtist in relatedArtistList.Artists)
            {
                if (NodeDictionary.ContainsKey(relatedArtist.Name))
                {
                    continue;
                }

                if (nodesAdded < AddChildrenLimit)
                {
                    newArtists.Add(relatedArtist);

                    ArtistViewModel     artistViewModel   = new ArtistViewModel(relatedArtist);
                    ArtistHierarchyNode relatedArtistNode = new ArtistHierarchyNode(artistViewModel, this, root, 1);
                    root.Children.Add(relatedArtistNode);
                    NodeDictionary.Add(relatedArtist.Name, relatedArtistNode);

                    nodesAdded++;
                }
                else
                {
                    break;
                }
            }
        }
        /// <summary>
        /// Returns an enumerable containing the current key's object and all objects that depend on the current key.
        /// Warning: DependencyGraphs with any cycles will cause this method to never terminate!
        /// </summary>
        /// <param name="objParentInstance">Instance of the object whose dependencies are being processed, used for conditions.</param>
        /// <param name="objKey">Fetch the node associated with this object.</param>
        public IEnumerable <T> GetWithAllDependentsUnsafe(T2 objParentInstance, T objKey)
        {
            if (NodeDictionary.TryGetValue(objKey, out DependencyGraphNode <T, T2> objLoopNode))
            {
                yield return(objLoopNode.MyObject);

                foreach (DependencyGraphNodeWithCondition <T, T2> objNode in objLoopNode.UpStreamNodes)
                {
                    if (objNode.DependencyCondition?.Invoke(objParentInstance) != false)
                    {
                        foreach (T objDependantObject in GetWithAllDependentsUnsafe(objParentInstance, objNode.Node.MyObject))
                        {
                            yield return(objDependantObject);
                        }
                    }
                }
            }
        }
Beispiel #15
0
        /// <summary>
        /// remove node from the net
        /// </summary>
        /// <param name="datas"></param>
        public void RemoveNodes(params Node[] datas)
        {
            if (datas.Length == 0)
            {
                nodes      = new NodeDictionary();
                removedIDs = new List <int>();
                uniqueID   = 1;
                return;
            }

            foreach (var node in datas)
            {
                if (nodes.ContainsKey(node.id))
                {
                    // disconnect all the neighbors
                    foreach (var neighborID in node.neighbors)
                    {
                        var neighborNode = GetNode(neighborID);
                        if (neighborNode != null && neighborNode.neighbors.Contains(node.id))
                        {
                            neighborNode.neighbors.Remove(node.id);
                        }
                    }
                    //reset downstream connection
                    foreach (var nodeID in node.downstreams)   //TODO  move to neighbor check
                    {
                        var checkNode = GetNode(nodeID);
                        if (checkNode != null && checkNode.downstreams != null && checkNode.downstreams.Contains(node.id))
                        {
                            checkNode.downstreams.Remove(node.id);
                        }
                    }

                    //Recycle ID
                    if (!removedIDs.Contains(node.id))
                    {
                        removedIDs.Add(node.id);
                    }

                    //Remove node
                    nodes.Remove(node.id);
                }
            }
        }
Beispiel #16
0
 private void Assert_AreDAGs(DirectedGraphContext[] directed_graphs_in_json, bool invert = false)
 {
     foreach (var expected in directed_graphs_in_json)
     {
         var dag = new DirectedAcyclicGraph <Node>(expected.Json);
         expected.Roots.Assert_NoDifferences(dag.Template.Roots);
         expected.Sinks.Assert_NoDifferences(dag.Template.Sinks);
         Assert.IsTrue(dag.Template.IsDirectedAcyclicGraph ^ invert);
         if (!string.IsNullOrEmpty(expected.NodeRelations))
         {
             var nodes = new NodeDictionary(expected.NodeRelations);
             foreach (var n in nodes.ToArray())
             {
                 dag[n.Key]._children.Select(x => x.variable).ToArray().Assert_NoDifferences(n.Value["children"]);
                 dag[n.Key]._parents.Select(x => x.variable).ToArray().Assert_NoDifferences(n.Value["parents"]);
                 dag.Remove(n.Key);
             }
             Assert.IsTrue(dag.Count == 0);
         }
     }
 }
Beispiel #17
0
        /// <summary>
        /// Returns a collection containing the current key's object and all objects that depend on the current key.
        /// Slower but idiot-proof compared to GetWithAllDependentsUnsafe().
        /// </summary>
        /// <param name="objKey">Fetch the node associated with this object.</param>
        public ICollection <T> GetWithAllDependents(T objKey)
        {
            HashSet <T> objReturn = new HashSet <T>();

            if (NodeDictionary.TryGetValue(objKey, out DependencyGraphNode <T> objLoopNode))
            {
                if (objReturn.Add(objLoopNode.MyObject))
                {
                    foreach (DependencyGraphNode <T> objDependant in objLoopNode.UpStreamNodes.Where(x => x.DependencyCondition?.Invoke() != false).Select(x => x.Node))
                    {
                        CollectDependents(objDependant.MyObject, objReturn);
                    }
                }
            }
            else
            {
                objReturn.Add(objKey);
            }

            return(objReturn);
        }
        private void AddToHistory(Node previousNode, Node currentCandidateNode)
        {
            if (previousNode == currentCandidateNode)
            {
                return;
            }

            // Add node to dictionary first
            NodeDictionary.Add(previousNode.Id, previousNode);


            if (NodeHistory.ContainsKey(previousNode.Id))
            {
                NodeHistory[previousNode.Id].Add(currentCandidateNode.Id);
            }
            else
            {
                NodeHistory[previousNode.Id] = new List <string>();
            }

            NodeHistory[previousNode.Id].Add(currentCandidateNode.Id);
        }
Beispiel #19
0
        public Boolean Insert(Int32 index, T?item)
        {
            if (index < 0 || index >= Count)
            {
                throw new ArgumentNullException(nameof(index));
            }

            if (item is null)
            {
                return(false);
            }

            if (NodeDictionary.ContainsKey(item))
            {
                return(false);
            }

            LinkedListNode <T> node = LinkedList.Insert(index, item);

            NodeDictionary.Add(item, node);
            return(true);
        }
Beispiel #20
0
        public void TestOscilattors()
        {
            var oscillators = new OscilattorContext[]
            {
                new OscilattorContext {
                    Object        = new Oscillator <Node>(),
                    Loops         = new string[] { "A", "B" },
                    NodeRelations = "{'A':{'children':['B'],'parents':['B']},'B':{'children':['A'],'parents':['A']}}"
                },
                new OscilattorContext {
                    Object        = new Oscillator <Node>("{'A':['B'],'B':['A'],'C':['A']}"),
                    Loops         = new string[] { "A", "B" },
                    NodeRelations = "{'A':{'children':['B'],'parents':['B','C']},'B':{'children':['A'],'parents':['A']},'C':{'children':['A'],'parents':[]}}"
                },
                new OscilattorContext {
                    Object        = new Oscillator <Node>("{'A':['B','C'],'B':['A']}"),
                    Loops         = new string[] { "A", "B" },
                    NodeRelations = "{'A':{'children':['B','C'],'parents':['B']},'B':{'children':['A'],'parents':['A']},'C':{'children':[],'parents':['A']}}"
                }
            };

            foreach (var expected in oscillators)
            {
                //{string:[string]} directed graph testing i.e. compact serialization form.
                expected.Loops.Assert_NoDifferences(expected.Object.Loops.Select(x => x.Key).ToArray());
                expected.Loops.Assert_NoDifferences(expected.Object.Loops.SelectMany(x => x.Value).Distinct().ToArray());

                //{string:{string:[string]}} directed graph testing i.e. independent element form.
                var nodes = new NodeDictionary(expected.NodeRelations);
                foreach (var n in nodes.ToArray())
                {
                    expected.Object.Graph[n.Key]._children.Select(x => x.variable).ToArray().Assert_NoDifferences(n.Value["children"]);
                    expected.Object.Graph[n.Key]._parents.Select(x => x.variable).ToArray().Assert_NoDifferences(n.Value["parents"]);
                    expected.Object.Graph.Remove(n.Key);
                }
                Assert.IsTrue(expected.Object.Graph.Count == 0);
            }
        }
Beispiel #21
0
        public static NodeStatus Run(Node node, NodeDictionary nodeDictionary)
        {
            NodeState nodeState;

            if (!nodeDictionary.ContainsKey(node))
            {
                nodeState = new Dictionary <string, object>();
                node.OnInitialize(nodeDictionary, nodeState);
                nodeDictionary[node] = nodeState;
            }
            else
            {
                nodeState = nodeDictionary[node];
            }

            var status = node.OnTick(nodeDictionary, nodeState);

            if (status == NodeStatus.Success)
            {
                node.OnSuccess(nodeDictionary, nodeState);
            }

            return(status);
        }
        /// <summary>
        /// Returns a collection containing the current key's object and all objects that depend on the current key.
        /// Slower but idiot-proof compared to GetWithAllDependentsUnsafe().
        /// </summary>
        /// <param name="objParentInstance">Instance of the object whose dependencies are being processed, used for conditions.</param>
        /// <param name="objKey">Fetch the node associated with this object.</param>
        public HashSet <T> GetWithAllDependents(T2 objParentInstance, T objKey)
        {
            HashSet <T> objReturn = new HashSet <T>();

            if (NodeDictionary.TryGetValue(objKey, out DependencyGraphNode <T, T2> objLoopNode))
            {
                if (objReturn.Add(objLoopNode.MyObject))
                {
                    foreach (DependencyGraphNodeWithCondition <T, T2> objNode in objLoopNode.UpStreamNodes)
                    {
                        if (objNode.DependencyCondition?.Invoke(objParentInstance) != false)
                        {
                            CollectDependents(objParentInstance, objNode.Node.MyObject, objReturn);
                        }
                    }
                }
            }
            else
            {
                objReturn.Add(objKey);
            }

            return(objReturn);
        }
        /// <summary>
        /// Returns a collection containing the current key's object and all objects that depend on the current key.
        /// Slower but idiot-proof compared to GetWithAllDependentsUnsafe().
        /// </summary>
        /// <param name="objParentInstance">Instance of the object whose dependencies are being processed, used for conditions.</param>
        /// <param name="objKey">Fetch the node associated with this object.</param>
        /// <param name="blnUsePool">Whether to fetch the returned HashSet{string} from a pool or to just create a new one on the stack.</param>
        public HashSet <string> GetWithAllDependents(T objParentInstance, string objKey, bool blnUsePool)
        {
            HashSet <string> setReturn = blnUsePool ? Utils.StringHashSetPool.Get() : new HashSet <string>();

            if (NodeDictionary.TryGetValue(objKey, out DependencyGraphNode <string, T> objLoopNode))
            {
                if (setReturn.Add(objLoopNode.MyObject))
                {
                    foreach (DependencyGraphNodeWithCondition <string, T> objNode in objLoopNode.UpStreamNodes)
                    {
                        if (objNode.DependencyCondition?.Invoke(objParentInstance) != false)
                        {
                            CollectDependents(objParentInstance, objNode.Node.MyObject, setReturn);
                        }
                    }
                }
            }
            else
            {
                setReturn.Add(objKey);
            }

            return(setReturn);
        }
Beispiel #24
0
 public NodeDictionary(NodeDictionary source)
 {
     _buckets = source._buckets;
     _size    = source._size;
     _source  = source;
 }
Beispiel #25
0
 public DBlockNode()
 {
     _Children = new NodeDictionary(this);
 }
Beispiel #26
0
 public bool TryGetNode(string id, out Node node)
 {
     return(NodeDictionary.TryGetValue(id, out node));
 }
Beispiel #27
0
 public Node(IIndexable owner, Node source)
 {
     Owner      = owner;
     ChildNodes = new NodeDictionary(source.ChildNodes);
     Items      = new List <TItem>(source.Items);
 }
Beispiel #28
0
        public void BuildGraph()
        {
            PropertyDictionary = new PropertyDictionary();
            foreach (var property in Properties)
            {
                PropertyDictionary.Add(property.Guid, property);
            }

            NodeDictionary = new NodeDictionary();
            foreach (var node in Nodes)
            {
                NodeDictionary.Add(node.Guid, node);
            }

            // Link node lines with actual properties, find previous node and actor node where necessary
            var propertyNodes = Nodes.Where(node => node.Type == NodeType.PROP).ToDictionary(node => node.Guid, node => node);

            // convert each conversation line reference from port to property lists using edge list
            foreach (var node in Nodes)
            {
                if (node.Type != NodeType.SELF && node.Type != NodeType.NPC)
                {
                    continue;
                }
                foreach (var line in node.Lines)
                {
                    line.Checks   = new List <string>();
                    line.Triggers = new List <string>();
                    string setNext = null;
                    foreach (var edge in Edges)
                    {
                        // Find triggers
                        if (line.TriggerPort == edge.FromPort)
                        {
                            var nodeGuid = edge.ToNode;
                            line.Triggers.Add(propertyNodes[nodeGuid].Temp_PropertyNodeGuid);
                        }

                        // Find checks, only for NPC nodes
                        if (node.Type == NodeType.NPC && line.CheckPort == edge.ToPort && NodeDictionary[edge.FromNode].Type == NodeType.PROP)
                        {
                            var nodeGuid = edge.FromNode;
                            line.Checks.Add(propertyNodes[nodeGuid].Temp_PropertyNodeGuid);
                        }

                        // Find next node
                        if (edge.FromNode == node.Guid && line.Next == edge.FromPort)
                        {
                            setNext = edge.ToNode;
                        }
                    }

                    line.Next = setNext;
                }

                foreach (var edge in Edges)
                {
                    // Find actor node
                    if (edge.ToNode == node.Guid && node.Type == NodeType.NPC && propertyNodes.ContainsKey(edge.FromNode) && PropertyDictionary[propertyNodes[edge.FromNode].Temp_PropertyNodeGuid].Type == PropertyType.Actor)
                    {
                        node.ActorGuid = propertyNodes[edge.FromNode].Temp_PropertyNodeGuid;
                    }

                    // Find previous node
                    if (edge.ToNode == node.Guid && (NodeDictionary[edge.FromNode].Type == NodeType.NPC || NodeDictionary[edge.FromNode].Type == NodeType.SELF))
                    {
                        node.Previous = edge.FromNode;
                    }
                }
            }

            // Remove property nodes from Nodes and NodeDictionary
            var copyOfNodes = Nodes.ToList();

            copyOfNodes.ForEach(node => {
                if (node.Type == NodeType.NPC || node.Type == NodeType.SELF)
                {
                    return;
                }
                NodeDictionary.Remove(node.Guid);
                Nodes.Remove(node);
            });

            // Find start node
            foreach (var node in Nodes)
            {
                if (!string.IsNullOrEmpty(node.Previous))
                {
                    continue;
                }

                if (!string.IsNullOrEmpty(StartNode) && StartNode != node.Guid)
                {
                    Debug.LogWarning("Multiple nodes without a previous node detected! Defaulting to the first one found to be the start node.");
                    continue;
                }

                StartNode = node.Guid;
            }
        }
Beispiel #29
0
 public Net(Node[] data)
 {
     nodes = new NodeDictionary();
     AddNodes(data);
 }
Beispiel #30
0
 public bool HasNode(string id)
 {
     return(NodeDictionary.ContainsKey(id));
 }
Beispiel #31
0
 public DMethod()
 {
     children = new NodeDictionary(this);
 }
Beispiel #32
0
 public Net()
 {
     nodes = new NodeDictionary();
 }