-
Notifications
You must be signed in to change notification settings - Fork 0
/
dependencymodel.go
284 lines (237 loc) · 7.79 KB
/
dependencymodel.go
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package main
import (
"encoding/xml"
"path/filepath"
"strings"
// fxml "github.com/m29h/xml"
"fmt"
"os"
"github.com/yaricom/goGraphML/graphml"
)
// test := &DependencyModel{
// XmiVers: "2.0",
// XmlnsXMI: "http://www.omg.org/XMI",
// XmlnsDM: "http://www.example.org/dependencyModel",
// Paragon : Paragon{Description: "Company OK", Probability: "1.0", Type: "AND"},
// Paragons: []Paragon{
// {Description: "Personnel OK", Probability: "1.0", Type: "AND", Paragons: []Paragon{
// {Description: "Accepting Orders OK", Probability: "1.0", Type: "AND"},
// }},
// {Description: "Postage OK", Probability: "1.0", Type: "AND", Paragons: []Paragon{
// {Description: "Processing Orders OK", Probability: "1.0", Type: "AND"},
// }},
// {Description: "Services OK", Probability: "1.0", Type: "AND", Paragons: []Paragon{
// {Description: "AWS OK", Probability: "1.0", Type: "AND", Paragons: []Paragon{
// {Description: "HTTP OK", Probability: "1.0", Type: "AND"},
// {Description: "SMTP OK", Probability: "1.0", Type: "AND"},
// }},
// {Description: "Email OK", Probability: "1.0", Type: "AND", Paragons: []Paragon{
// {Description: "IMAP OK", Probability: "1.0", Type: "AND"},
// }},
// {Description: "Spreadsheet OK", Probability: "1.0", Type: "AND"},
// }},
// },
// }
type DependencyModel struct {
XMLName xml.Name `xml:"http://www.example.org/dependencyModel Paragon"`
// XMLName xml.Name `xml:"dependencyModel:Paragon"` // go1.22+ with patch for namesapce prefixes
XmlnsXMI string `xml:"xmlns:xmi,attr"`
XmiVers string `xml:"xmi:version,attr"`
XmlnsDM string `xml:"xmlns:dependencyModel,attr"`
Paragon `xml:"paragon,attr"`
Paragons []Paragon `xml:"paragon"`
filename string
name string
}
type Paragon struct {
XMLName xml.Name `xml:"paragon"`
Description string `xml:"description,attr"`
Probability string `xml:"probability,attr"`
Type string `xml:"Type,attr,omitempty"`
Paragons []Paragon `xml:"paragon"`
}
/*********************************************************************/
func (dm *DependencyModel) Init(file string) {
dm.XmiVers = "2.0"
dm.XmlnsXMI = "http://www.omg.org/XMI"
dm.XmlnsDM = "http://www.example.org/dependencyModel"
dm.filename = file
dm.name = strings.TrimSuffix(filepath.Base(file), filepath.Ext(file)) + " DM"
}
/*********************************************************************/
func (dm *DependencyModel) ParseFile(file string) error {
/* TODO: Read this data from the XML file */
dm.Init(file)
/* 1. Parse the file and create tree */
rawdata, err := os.ReadFile(file)
if err != nil {
return err
}
if err := xml.Unmarshal(rawdata, &dm); err != nil {
return err
}
return nil
}
const (
DM int = iota
SAND
Unknown
)
/*********************************************************************/
func (dm *DependencyModel) ParseFileGraphML(file string) error {
/* 1. Parse the whole file and create inital tree */
rawdata, err := os.Open(file)
if err != nil {
return err
}
defer rawdata.Close()
/* GraphML */
gml := graphml.NewGraphML("")
err = gml.Decode(rawdata)
if err != nil {
return err
}
/* Make sure there is only one graph */
if len(gml.Graphs) != 1 {
return fmt.Errorf("%d graphs found. Needs 1.", len(gml.Graphs))
}
graph := gml.Graphs[0]
/* Check if the SrcFormat is defined. */
Src := Unknown
if gml.GetKey("SrcFormat", graphml.KeyForGraph) != nil {
for _, a := range graph.Data {
if a.Key == gml.GetKey("SrcFormat", graphml.KeyForGraph).ID {
if a.Value == "SAND" {
Src = SAND
}
}
}
}
/* 2. Get nodes */
nodeIndex := make(map[string]Paragon)
for _, node := range graph.Nodes {
switch Src {
case SAND:
/* TODO: DM does not have the notion of Sequental AND. */
operator := "AND"
if gml.GetKey("Operator", graphml.KeyForNode) != nil {
for _, a := range node.Data {
if a.Key == gml.GetKey("Operator", graphml.KeyForNode).ID {
if a.Value == "OR" {
operator = "OR"
}
}
}
}
nodeIndex[node.ID] = Paragon{Description: node.Description, Probability: "1", Type: operator}
default:
if len(node.Data) != 2 {
return fmt.Errorf("Node %s '%s' has more data '%d' than I wanted 2.", node.ID, node.Description, len(node.Data))
}
nodeIndex[node.ID] = Paragon{Description: node.Description, Probability: node.Data[0].Value, Type: node.Data[1].Value}
}
}
/* 3. Map the Edges */
/* Assume the first node is the root node. */
edges := make(map[string]*graphml.Edge)
for _, edge := range graph.Edges {
edges[edge.ID] = edge
}
/* Intialise the Dependency Model */
dm.Init(strings.TrimSuffix(filepath.Base(file), filepath.Ext(file)) + " DM")
/* Create the root node, and its children. */
dm.Paragon = nodeIndex[graph.Nodes[0].ID]
for _, edge := range edges {
if nodeIndex[edge.Source].Description == dm.Paragon.Description {
dm.Paragons = append(dm.Paragons, nodeIndex[edge.Target])
delete(edges, edge.ID)
}
}
/* While there are no edges left, for each child, call addWalk. */
for len(edges) != 0 {
for i := range dm.Paragons {
dm.addWalk(&dm.Paragons[i], edges, nodeIndex)
}
}
return nil
}
/*********************************************************************/
func (dm *DependencyModel) addWalk(parent *Paragon, edges map[string]*graphml.Edge, nodeIndex map[string]Paragon) {
/* for each edge see if it has an edge source */
for _, edge := range edges {
if nodeIndex[edge.Source].Description == parent.Description {
/* if there is an edge source, add the child */
parent.Paragons = append(parent.Paragons, nodeIndex[edge.Target])
/* remove the edge from the abanonded child list */
delete(edges, edge.ID)
}
}
/* for all the children see if they have children. */
for i := range parent.Paragons {
dm.addWalk(&parent.Paragons[i], edges, nodeIndex)
}
}
/*********************************************************************/
func (dm *DependencyModel) GraphML() (*graphml.GraphML, error) {
gml := graphml.NewGraphML(dm.name)
attributes := make(map[string]interface{})
graph, err := gml.AddGraph(dm.name, graphml.EdgeDirectionDirected, attributes)
if err != nil {
return gml, err
}
nodeIndex := make(map[string]*graphml.Node)
for _, edges := range dm.GetInternal() {
/* Add Nodes */
node := edges[len(edges)-1]
attributes := make(map[string]interface{})
attributes["Probability"] = node.Probability
attributes["Type"] = node.Type
nodeIndex[node.Description], err = graph.AddNode(attributes, node.Description)
if err != nil {
return gml, err
}
/*Add Edges */
if len(edges) > 1 {
parent := edges[len(edges)-2]
n1 := nodeIndex[parent.Description]
n2 := nodeIndex[node.Description]
attributes = make(map[string]interface{})
attributes["Type"] = parent.Type
_, err = graph.AddEdge(n1, n2, attributes, graphml.EdgeDirectionDefault, parent.Type)
if err != nil {
return gml, err
}
}
}
return gml, nil
}
/*********************************************************************/
func (dm DependencyModel) GetInternal() [][]Paragon {
res := [][]Paragon{}
res = append(res, []Paragon{dm.Paragon})
dm.interalWalk([]Paragon{dm.Paragon}, dm.Paragons, &res)
return res
}
/*********************************************************************/
func (dm DependencyModel) interalWalk(prefix []Paragon, children []Paragon, res *[][]Paragon) {
for _, child := range children {
line := []Paragon{}
for _, p := range prefix {
line = append(line, p)
}
line = append(line, child)
// fmt.Println("-", line)
*res = append(*res, line)
if len(child.Paragons) != 0 {
dm.interalWalk(append(prefix, child), child.Paragons, res)
}
}
}
/*********************************************************************/
func (dm DependencyModel) String() string {
out, err := xml.MarshalIndent(dm, " ", " ")
if err != nil {
panic(err)
}
return xml.Header + string(out)
}