1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Core {
|
---|
28 | public class CallGraph : CallNodeBase, ICallGraph {
|
---|
29 | [Storable]
|
---|
30 | private Dictionary<ICallNode, List<ICallNode>> transitions;
|
---|
31 |
|
---|
32 | [Storable]
|
---|
33 | private List<ICallNode> myCallNodes;
|
---|
34 | public ICollection<ICallNode> CallNodes {
|
---|
35 | get { return myCallNodes.AsReadOnly(); }
|
---|
36 | }
|
---|
37 | [Storable]
|
---|
38 | private ICallNode myInitialCallNode;
|
---|
39 | public ICallNode InitialCallNode {
|
---|
40 | get { return myInitialCallNode; }
|
---|
41 | internal set {
|
---|
42 | if (myInitialCallNode != value) {
|
---|
43 | myInitialCallNode = value;
|
---|
44 | OnInitialCallNodeChanged();
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public CallGraph()
|
---|
50 | : base() {
|
---|
51 | transitions = new Dictionary<ICallNode, List<ICallNode>>();
|
---|
52 | myCallNodes = new List<ICallNode>();
|
---|
53 | myInitialCallNode = null;
|
---|
54 | }
|
---|
55 | public CallGraph(CallGraph parentGraph)
|
---|
56 | : base(parentGraph) {
|
---|
57 | transitions = new Dictionary<ICallNode, List<ICallNode>>();
|
---|
58 | myCallNodes = new List<ICallNode>();
|
---|
59 | myInitialCallNode = null;
|
---|
60 | }
|
---|
61 |
|
---|
62 | public override ICloneable Clone(ICloner cloner) {
|
---|
63 | CallGraph clone = (CallGraph)base.Clone(cloner);
|
---|
64 | foreach (ICallNode callNode in CallNodes)
|
---|
65 | clone.AddCallNode((ICallNode)cloner.Clone(callNode));
|
---|
66 | foreach (ICallNode source in CallNodes)
|
---|
67 | foreach (ICallNode target in transitions[source])
|
---|
68 | clone.AddTransition((ICallNode)cloner.Clone(source), (ICallNode)cloner.Clone(target));
|
---|
69 | clone.InitialCallNode = (ICallNode)cloner.Clone(InitialCallNode);
|
---|
70 |
|
---|
71 | return base.Clone(cloner);
|
---|
72 | }
|
---|
73 |
|
---|
74 | public override IView CreateView() {
|
---|
75 | return new CallGraphView(this);
|
---|
76 | }
|
---|
77 |
|
---|
78 | internal void AddCallNode(ICallNode callNode) {
|
---|
79 | myCallNodes.Add(callNode);
|
---|
80 | transitions.Add(callNode, new List<ICallNode>());
|
---|
81 | OnCallNodeAdded(callNode);
|
---|
82 | }
|
---|
83 | public void RemoveCallNode(ICallNode callNode) {
|
---|
84 | if (myCallNodes.Remove(callNode)) {
|
---|
85 | foreach (ICallNode node in CallNodes) { // remove incoming transitions
|
---|
86 | while (transitions[node].Contains(callNode))
|
---|
87 | RemoveTransition(node, callNode);
|
---|
88 | }
|
---|
89 | while (transitions[callNode].Count > 0) // remove outgoing transitions
|
---|
90 | RemoveTransition(callNode, transitions[callNode][0]);
|
---|
91 | transitions.Remove(callNode);
|
---|
92 | OnCallNodeRemoved(callNode);
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | public void AddTransition(ICallNode source, ICallNode target) {
|
---|
97 | transitions[source].Add(target);
|
---|
98 | OnTransitionAdded(source, target);
|
---|
99 | }
|
---|
100 | public void RemoveTransition(ICallNode source, ICallNode target) {
|
---|
101 | if (transitions[source].Remove(target))
|
---|
102 | OnTransitionRemoved(source, target);
|
---|
103 | }
|
---|
104 |
|
---|
105 | internal IList<ICallNode> GetSuccessorNodes(ICallNode callNode) {
|
---|
106 | return transitions[callNode].AsReadOnly();
|
---|
107 | }
|
---|
108 |
|
---|
109 | public event EventHandler<EventArgs<ICallNode>> CallNodeAdded;
|
---|
110 | protected virtual void OnCallNodeAdded(ICallNode callNode) {
|
---|
111 | if (CallNodeAdded != null)
|
---|
112 | CallNodeAdded(this, new EventArgs<ICallNode>(callNode));
|
---|
113 | }
|
---|
114 | public event EventHandler<EventArgs<ICallNode>> CallNodeRemoved;
|
---|
115 | protected virtual void OnCallNodeRemoved(ICallNode callNode) {
|
---|
116 | if (CallNodeRemoved != null)
|
---|
117 | CallNodeRemoved(this, new EventArgs<ICallNode>(callNode));
|
---|
118 | }
|
---|
119 | public event EventHandler InitialCallNodeChanged;
|
---|
120 | protected virtual void OnInitialCallNodeChanged() {
|
---|
121 | if (InitialCallNodeChanged != null)
|
---|
122 | InitialCallNodeChanged(this, new EventArgs());
|
---|
123 | }
|
---|
124 | public event EventHandler<EventArgs<ICallNode, ICallNode>> TransistionAdded;
|
---|
125 | protected virtual void OnTransitionAdded(ICallNode source, ICallNode target) {
|
---|
126 | if (TransistionAdded != null)
|
---|
127 | TransistionAdded(this, new EventArgs<ICallNode,ICallNode>(source, target));
|
---|
128 | }
|
---|
129 | public event EventHandler<EventArgs<ICallNode, ICallNode>> TransistionRemoved;
|
---|
130 | protected virtual void OnTransitionRemoved(ICallNode source, ICallNode target) {
|
---|
131 | if (TransistionRemoved != null)
|
---|
132 | TransistionRemoved(this, new EventArgs<ICallNode, ICallNode>(source, target));
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|