summaryrefslogtreecommitdiff
path: root/Source/ModelViewer/TreeSkeleton.cs
blob: 3663f92637a54120d5ca6abeac0bd55305caab38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Microsoft.Boogie.ModelViewer
{
  internal class SkeletonItem
  {
    readonly IEdgeName name;
    readonly List<SkeletonItem> children = new List<SkeletonItem>();
    internal readonly IDisplayNode[] displayNodes;
    internal bool[] isPrimary;
    internal readonly SkeletonItem parent;
    readonly Main main;
    internal readonly int level;
    internal bool expanded, wasExpanded;
    internal bool isMatch;

    public void Iter(Action<SkeletonItem> handler)
    {
      handler(this);
      children.ForEach(u => u.Iter(handler));
    }

    public IEnumerable<SkeletonItem> RecChildren
    {
      get
      {
        if (expanded) {
          foreach (var c in children) {
            yield return c;
            foreach (var ch in c.RecChildren)
              yield return ch;
          }
        }
      }
    }

    public SkeletonItem[] PopulateRoot(IEnumerable<IState> states)
    {
      var i = 0;
      foreach (var s in states) {
        displayNodes[i++] = new ContainerNode<IDisplayNode>(this.name, x => x, s.Nodes);
      }

      return BfsExpand(this);
    }

    public SkeletonItem(Main m, int stateCount)
    {
      name = new EdgeName("<root>");
      main = m;
      displayNodes = new IDisplayNode[stateCount];
      isPrimary = new bool[stateCount];
    }

    internal SkeletonItem(IEdgeName n, SkeletonItem par)
      : this(par.main, par.displayNodes.Length)
    {
      parent = par;
      name = n;
      level = par.level + 1;
    }

    public bool Expandable
    {
      get { return displayNodes.Any(d => d != null && d.Expandable); }
    }

    public bool Expanded
    {
      get { return expanded; }
      set
      {
        expanded = value;
        if (expanded)
          ComputeChildren();
      }
    }

    static SkeletonItem[] BfsExpand(SkeletonItem skel)
    {
      for (int i = 0; i < skel.displayNodes.Length; ++i)
        BfsExpandCore(skel, i);

      var workItems = new Stack<SkeletonItem>();
      var allNodes = new List<SkeletonItem>();
      workItems.Push(skel);

      while (workItems.Count > 0) {
        var s = workItems.Pop();
        if (!s.isPrimary.Any())
          continue;
        allNodes.Add(s);
        s.children.Iter(workItems.Push);
      }

      return allNodes.ToArray();
    }

    static void BfsExpandCore(SkeletonItem skel, int idx)
    {
      var visited = new HashSet<Model.Element>();
      var workItems = new Queue<SkeletonItem>();

      workItems.Enqueue(skel);
      while (workItems.Count > 0) {
        var s = workItems.Dequeue();
        if (s.displayNodes[idx] == null)
          continue;
        var e = s.displayNodes[idx].Element;
        s.isPrimary[idx] = true;
        if (e != null) {
          if (visited.Contains(e))
            continue;
          visited.Add(e);
        }
        s.ComputeChildren();
        s.children.Iter(workItems.Enqueue);
      }
    }

    private void ComputeChildren()
    {
      if (wasExpanded) return;
      wasExpanded = true;

      var created = new Dictionary<string, SkeletonItem>();
      var names = new List<string>();
      for (int i = 0; i < displayNodes.Length; ++i) {
        var dn = displayNodes[i];
        if (dn == null || !dn.Expandable) continue;
        foreach (var child in dn.Expand()) {
          SkeletonItem skelChild;
          var name = child.Name.ShortName();
          if (!created.TryGetValue(name, out skelChild)) {
            skelChild = new SkeletonItem(child.Name, this);
            created.Add(name, skelChild);
            names.Add(name);

          }
          skelChild.displayNodes[i] = child;
        }
      }

      foreach (var name in main.langProvider.SortFields(names)) {
        children.Add(created[name]);
      }
    }

    public bool MatchesWords(string[] words, int stateId)
    {
      var node = displayNodes[stateId];
      if (node == null)
        return false;
      var s1 = node.Name.FullName().ToLower();
      var s2 = node.CanonicalValue.ToLower();
      foreach (var w in words) {
        if (!s1.Contains(w) && !s2.Contains(w))
          return false;
      }
      return true;
    }
  }

}