summaryrefslogtreecommitdiff
path: root/Source/ModelViewer/DataModel.cs
blob: c2ffdf4b3bd400c76700398fcfcfd5a1db52d06f (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Microsoft.Boogie.ModelViewer
{
  public interface ILanguageProvider
  {
    bool IsMyModel(Model m);
    IEnumerable<IState> GetStates(Model m);
  }

  [Flags]
  public enum NodeState
  {
    Normal = 0,
    Changed = 1
  }

  public interface IEdgeName
  {
    int CompareTo(IEdgeName other);
    string FullName();
    string ShortName();
    IEnumerable<Model.Element> Dependencies { get; }
    int Score { get; }
  }

  public interface IDisplayNode
  {
    /// <summary>
    ///  Used for indexing the state tree.
    /// </summary>
    IEdgeName Name { get; }

    /// <summary>
    /// Used to determine aliasing. Can be null.
    /// </summary>
    Model.Element Element { get; }

    bool Expandable { get; }
    IEnumerable<IDisplayNode> Expand();

    // Things displayed to the user.
    NodeState State { get; }
    IEnumerable<string> Values { get; }
    string ToolTip { get; }

    object ViewSync { get; set; }
  }

  public interface IState
  {
    string Name { get; }
    IEnumerable<IDisplayNode> Nodes { get; }
  }


  public class TopState : IState
  {
    protected IDisplayNode[] children;
    protected string name;

    public TopState(string name, IEnumerable<IDisplayNode> nodes)
    {
      this.name = name;
      children = nodes.ToArray();
    }

    public string Name
    {
      get { return name; }
    }

    public IEnumerable<IDisplayNode> Nodes
    {
      get { return children; }
    }
  }
  
  public abstract class DisplayNode : IDisplayNode
  {
    protected IEdgeName name;

    public DisplayNode(string n)
    { 
      name = new EdgeName(n);
    }

    public DisplayNode(IEdgeName n)
    {
      name = n;
    }

    public virtual IEnumerable<string> Values
    {
      get { yield break; }
    }

    public virtual string ToolTip
    {
      get { return null; }
    }

    public virtual bool Expandable
    {
      get { return false; }
    }

    public virtual IEnumerable<IDisplayNode> Expand()
    {
      yield break;
    }

    public virtual NodeState State { get { return NodeState.Normal; } }

    public object ViewSync { get; set; }

    public virtual IEdgeName Name
    {
      get { return name; }
    }

    public virtual Model.Element Element
    {
      get { return null; }
    }
  }

  public class ContainerNode<T> : DisplayNode
  {
    protected Func<T, IDisplayNode> convert;
    protected IEnumerable<T> data;

    public ContainerNode(IEdgeName name, Func<T, IDisplayNode> convert, IEnumerable<T> data) : base(name)
    {
      this.convert = convert;
      this.data = data;
    }

    public ContainerNode(string name, Func<T, IDisplayNode> convert, IEnumerable<T> data)
      : this(new EdgeName(name), convert, data)
    {
    }

    public override bool Expandable { get { return true; } }
    public override IEnumerable<IDisplayNode> Expand()
    {
      foreach (var f in data) {
        var res = convert(f);
        if (res != null)
          yield return res;
      }
    }
  }


  public static class Util
  {
    public static void Assert(bool cond)
    {
      if (!cond) throw new System.Exception("assertion violation");
    }

    public static IEnumerable<T> Empty<T>() { yield break; }

    public static IEnumerable<T> Singleton<T>(T e) { yield return e; }

    public static IEnumerable<T> Concat1<T>(this IEnumerable<T> s, T e) { return s.Concat(Singleton(e)); }

    public static IEnumerable<T> Map<S, T>(this IEnumerable<S> inp, Func<S, T> conv)
    {
      foreach (var s in inp) yield return conv(s);
    }

    public static void Iter<T>(this IEnumerable<T> inp, Action<T> fn)
    {
      foreach (var s in inp) fn(s);
    }

    public static T OrElse<T>(T a, T b)
      where T : class
    {
      if (a != null) return a;
      return b;
    }
  }

}