-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathOrderModelInfo.cs
93 lines (79 loc) · 3.16 KB
/
OrderModelInfo.cs
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
using MarkdownProcessor;
using MarkdownProcessor.NodeSet;
using System.Collections.Generic;
using System.Linq;
namespace Opc2Aml
{
public class NamespaceInfo
{
public NamespaceInfo( string uri, HashSet<string> dependencies )
{
Uri = uri;
Dependencies = dependencies.ToList<string>();
}
public int Count()
{
return Dependencies.Count;
}
public ushort Index { get; set; }
public string Uri { get; set; }
public List<string> Dependencies { get; set; }
}
internal class OrderModelInfo
{
public List<ModelInfo> GetProcessOrder( List<ModelInfo> initial )
{
List<ModelInfo> processed = new List<ModelInfo>();
Dictionary<string, ModelInfo> initialSet = new Dictionary<string, ModelInfo>();
foreach( ModelInfo modelInfo in initial )
{
initialSet.Add( modelInfo.NamespaceUri, modelInfo );
}
Dictionary<string, HashSet<string>> compiled = new Dictionary<string, HashSet<string>>();
foreach( KeyValuePair<string, ModelInfo> pair in initialSet )
{
HashSet<string> dependencies = new HashSet<string>();
AddCompiled( pair.Key, dependencies, initialSet );
compiled.Add( pair.Key, dependencies );
}
List<NamespaceInfo> buildNamespaces = new List<NamespaceInfo>();
// find the one with the most dependencies, the least dependencies
foreach( KeyValuePair<string, HashSet<string>> pair in compiled )
{
buildNamespaces.Add( new NamespaceInfo( pair.Key, pair.Value ) );
}
List<NamespaceInfo> alphabetic = buildNamespaces.OrderBy( uri => uri.Uri ).ToList();
List<NamespaceInfo> ordered = alphabetic.OrderBy( count => count.Count() ).ToList();
foreach( NamespaceInfo buildNamespace in ordered )
{
processed.Add( initialSet[ buildNamespace.Uri ] );
}
return processed;
}
public void AddCompiled( string uri, HashSet<string> set,
Dictionary<string, ModelInfo> source )
{
if( source.TryGetValue( uri, out ModelInfo modelInfo ) )
{
if ( modelInfo.NodeSet != null &&
modelInfo.NodeSet.Models != null )
{
foreach( ModelTableEntry entry in modelInfo.NodeSet.Models )
{
if ( entry.RequiredModel != null )
{
foreach( ModelTableEntry requiredModel in entry.RequiredModel)
{
if ( !string.IsNullOrEmpty( requiredModel.ModelUri ) )
{
set.Add( requiredModel.ModelUri );
AddCompiled( requiredModel.ModelUri, set, source );
}
}
}
}
}
}
}
}
}