-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGraphGenerator.cs
More file actions
109 lines (93 loc) · 4.65 KB
/
GraphGenerator.cs
File metadata and controls
109 lines (93 loc) · 4.65 KB
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using JsonLd.Graph;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace IoTPnP.GraphGenerator
{
public class GraphGenerator
{
const string ClassJsonContextName = "Class.json";
const string InterfaceJsonContextName = "Interface.json";
const string TemplateJsonContextName = "CapabilityModel.json";
public string GenerateGraph(string contextDir, string metamodelDir, string outputDir = null)
{
Console.WriteLine("Parse metamodel graph...");
Graph metamodelGraph = new Graph();
GetMetamodel(contextDir, metamodelDir, ref metamodelGraph);
Console.WriteLine("Serialize graph to json file...");
string graphJsonString = JsonConvert.SerializeObject(metamodelGraph, Formatting.Indented);
if (string.IsNullOrEmpty(outputDir))
{
outputDir = Directory.GetCurrentDirectory();
}
string graphJsonFile = Path.Combine(outputDir, "graph.json");
File.WriteAllText(graphJsonFile, graphJsonString);
return graphJsonFile;
}
private void GetMetamodel(string contextDir, string metamodelDir, ref Graph metamodelGraph)
{
if (!Directory.Exists(contextDir))
{
throw new DirectoryNotFoundException($"context directory doesn't exist: {metamodelDir}");
}
if (!Directory.Exists(metamodelDir))
{
throw new DirectoryNotFoundException($"metamodel directory doesn't exist: {metamodelDir}");
}
// Load JSON-LD context objects
JObject classContextJsonObject = LoadJsonObject(Path.Combine(contextDir, ClassJsonContextName));
JObject interfaceContextJsonObject = LoadJsonObject(Path.Combine(contextDir, InterfaceJsonContextName));
JObject templateContextJsonObject = LoadJsonObject(Path.Combine(contextDir, TemplateJsonContextName));
GraphFactoryOptions graphFactoryOptions = new GraphFactoryOptions();
graphFactoryOptions.SetDefaultContext(interfaceContextJsonObject);
// Build metamodel graph
List<string> fileNames = new List<string>();
fileNames.AddRange(Directory.GetFiles(metamodelDir, "*.json"));
fileNames.AddRange(Directory.GetFiles(metamodelDir, "*.jsonld"));
foreach (string fileName in fileNames)
{
Console.WriteLine("Parse JSON-LD file: {0}", fileName);
string json = File.ReadAllText(fileName);
JObject jsonObj = JObject.Parse(json);
// Replace the context IRI with a local context object
var contextObject = jsonObj["@context"];
int tokenCount = contextObject.Children().Count();
for (int i = 0; i < tokenCount; i++)
{
string context = contextObject[i].ToString();
// Replace context IRI http://azureiot.com/v0/contexts/Class.json with local context object
if (context.EndsWith(ClassJsonContextName, StringComparison.OrdinalIgnoreCase))
{
contextObject[i] = classContextJsonObject["@context"];
}
// Replace context IRI http://azureiot.com/v0/contexts/Interface.json with local context object
else if (context.EndsWith(InterfaceJsonContextName, StringComparison.OrdinalIgnoreCase))
{
contextObject[i] = interfaceContextJsonObject["@context"];
}
// Replace context IRI http://azureiot.com/v0/contexts/Template.json with local context object
else if (context.EndsWith(TemplateJsonContextName, StringComparison.OrdinalIgnoreCase))
{
contextObject[i] = templateContextJsonObject["@context"];
}
}
Graph loadedGraph = GraphFactory.CreateGraph(jsonObj, graphFactoryOptions);
metamodelGraph.Merge(loadedGraph);
}
string[] subDirectories = Directory.GetDirectories(metamodelDir);
foreach (string subDirectory in subDirectories)
{
GetMetamodel(contextDir, subDirectory, ref metamodelGraph);
}
}
private JObject LoadJsonObject(string jsonFilePath)
{
string json = File.ReadAllText(jsonFilePath);
JObject jsonObj = JObject.Parse(json);
return jsonObj;
}
}
}