简体   繁体   English

如何实现更新对象的依赖树?

[英]How to implement a dependency tree of updating objects?

Edit : I am not looking for an implementation but just for some keywords to search and methodologies to get me started. 编辑 :我不是在寻找一种实现,而只是在寻找一些关键词和方法来使我入门。

I am struggling with generating a dependency tree where child nodes are updated by an external process and the requirement is to update all parent nodes of updated child nodes. 我正在努力生成一个依赖关系树,其中子节点由外部进程更新,并且要求是更新已更新的子节点的所有父节点。

Example: Imagine a tree such as this: O [parent], O(l) [left child], O(r) [right child], O(ll), O(lr), O(rl), and O(rr). 示例:想象一棵这样的树:O [父],O(l)[左子],O(r)[右子],O(ll),O(lr),O(rl)和O( RR)。 O(ll), O(lr), O(rl), and O(rr) have reference to a data collection that is updated at random intervals). O(11),O(1r),O(r1)和O(rr)引用了以随机间隔更新的数据集合。

I want to implement a pull process, where at certain intervals a process checks whether O is updated. 我要实现一个拉过程,在该过程中,每隔一定的时间检查O是否已更新。 "Updated" is defined as being updated when all child nodes are updated, else just the cached value(result) of that node is used. “更新”定义为在所有子节点都更新时更新,否则仅使用该节点的缓存值(结果)。 The pull process's job is to ensure that O is updated when any of the child nodes is not updated. 提取过程的工作是确保在未更新任何子节点时更新O。 This means that the process needs to traverse the tree and check whether O(ll), O(lr), O(rl), and O(rr) are updated. 这意味着该过程需要遍历树并检查O(ll),O(lr),O(rl)和O(rr)是否已更新。 If the data collection, those child nodes reference, are updated since the last update of those child nodes then those child nodes need to be updated as function of the changed data collection. 如果自这些子节点的最后一次更新以来更新了那些子节点引用的数据集合,则需要根据已更改的数据集合来更新那些子节点。 If the data collection is updated and hence the child nodes O(ll), O(lr), O(rl), and O(rr) are updated as well and this means that O(l) and O(r) also need to be updated and subsequently O will also be updated. 如果数据集合被更新,因此子节点O(ll),O(lr),O(rl)和O(rr)也被更新,这意味着O(l)和O(r)也需要被更新,随后O也将被更新。 Each child node is input to its parent node. 每个子节点都输入到其父节点。

The complexity here is that each child node is shared among different trees, meaning, a child node of one tree can also be any child node of another tree. 这里的复杂性是每个子节点在不同的树之间共享,也就是说,一棵树的子节点也可以是另一棵树的任何子节点。 The purpose of this structure is to avoid the re-calculation of a child node when it is already up-to-date. 这种结构的目的是避免在子节点已经是最新的情况下重新计算。 The child nodes are shared if different trees implement a child node with the exact same functionality (function and parameterization) as the already existing child node. 如果不同的树实现的子节点具有与现有子节点完全相同的功能(功能和参数化),则子节点将共享。

I am stuck with the design of this structure and how to go about implementing it. 我一直沉迷于这种结构的设计以及如何实现它。 I have not provided code yet because I am stuck with the design thought process. 我还没有提供代码,因为我坚持设计思想过程。 Essentially each child is function and depends on dependent functions itself. 本质上,每个孩子都是功能,并且依赖于相关功能本身。

What makes me wonder is whether C# offers the ability to decorate methods and classes in order to simplify the checking of whether a node is updated or not. 让我感到奇怪的是,C#是否提供装饰方法和类的功能,以简化对节点是否更新的检查。 Also does lazy evaluation play a role in this process at all? 惰性评估在这个过程中是否也起作用?

I suggest defining a class that keeps track whether its children have been updated via a flag, eg a Boolean named Dirty . 我建议定义一个类,以跟踪其子级是否已通过标志(例如,名为Dirty的布尔值)进行了更新。 A node in the tree can tell its parents to become dirty by raising an event. 树中的节点可以通过引发事件来告诉其父节点变脏。 When a node returns its own value, it should check the flag and recompute its own value only when needed. 当节点返回自己的值时,它应检查该标志并仅在需要时重新计算其自己的值。 When recomputing, it should check the Value of each child, each of which will then check its own dirty flag, and so on, recursively. 重新计算时,它应检查每个子项的值,然后每个子项将递归检查其自己的脏标志,依此类推。

class Node<T> 
{
    event EventHandler Changed;

    private T _value;
    private bool _dirty = true;
    private List<Node<T>> _children = new List<Node<T>>();

    public void AddChild(Node<T> child)
    {
        child.Changed += (s,e) => _dirty = true;
        _children.Add(child);
    }

    protected void OnChanged()
    {
        if (Changed != null) Changed(this, new EventArgs());
    }

    public T Value
    {
        get
        {
            if (_dirty)
            {
                this.Value = ComputeValueFromChildren();
                _dirty = false;
            }
            return _value;
        }
        set
        {
            _value = value;
            OnChanged();
        }
    }

    private T ComputeValueFromChildren()
    {
        var values = _children.Select( child => child.Value );
        //Return the new value based on the children
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM