-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpurerdf2hkrdf.js
94 lines (71 loc) · 2.13 KB
/
purerdf2hkrdf.js
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
/*
* Copyright (c) 2016-present, IBM Research
* Licensed under The MIT License [see LICENSE for details]
*/
"use strict";
const { promisify } = require("util");
const Parser = require("./parser");
const Serializer = require("./serializer");
const GraphFactory = require("./graphfactory");
const {HK_NULL} = require("./constants");
const Timer = require("ninja-util/timer");
let fs = require("fs");
let outFile = null;
let mimeType = "application/trig";
const DefaultConversionOptions = {
createContext: true,
setNodeContext: true,
convertHK: true,
convertOwl: false,
convertNumber: true,
compressReification: true,
skipRefNodes: true,
inverseRefNode: true,
reifyArray: false,
textLiteralAsNode: false,
textLiteralAsNodeEncoding: 'property',
defaultGraph: `<${HK_NULL}>`
};
if(process.argv.length > 2)
{
let inputPath = process.argv[2];
if(process.argv.length > 3)
{
outFile = process.argv[3];
}
if(process.argv.length > 4)
{
mimeType = process.argv[4];
console.log("Mime type", mimeType);
}
let inputData = fs.readFileSync(inputPath, "utf-8");
let t = new Timer();
const serializeGraph = promisify(GraphFactory.serializeGraph);
GraphFactory.parseGraph(inputData, mimeType, async (err, graph) =>
{
if(err)
{
console.log(err);
console.log("Could not load input data");
return;
}
t.tick("Parsing rdf time");
let options = Object.assign({}, DefaultConversionOptions);
let hkentities = Parser.parseGraph(graph, options);
let graphToImport = GraphFactory.createGraph("application/trig");
Serializer.serialize(hkentities, options, graphToImport)
const serializedData = await serializeGraph(graphToImport);
t.tick("Conversion done");
if(!outFile)
{
console.log(serializedData);
}
else
{
fs.writeFileSync(outFile, serializedData, {encoding: "utf-8"})
console.log("Written");
}
});
}
// console.log(out);
// console.log(JSON.stringify(Object.values(triples), null, " "));