-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunInteractiveClient.ts
198 lines (150 loc) · 4.83 KB
/
runInteractiveClient.ts
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
import { program } from "commander"
import { SolidLib } from './SolidLib/Interface/SolidLib';
import { setup } from './setup';
import { DataPlusPlus } from "./SolidLib/Interface/ISolidLib";
import { n3reasoner } from "eyereasoner/dist";
import { DataFactory, Parser, Store } from "n3";
import { write } from "@jeswr/pretty-turtle/dist";
var solidLib = new SolidLib("interactive-client", 'steve');
program
.command('addpolicy')
.argument('policy', 'Policy string')
.action(async (policy) => {
await solidLib.login()
await solidLib.addPolicy(policy)
console.log('')
console.log('Policy added')
await solidLib.logout();
})
program
.command('getdata')
.argument('query', 'Query string')
.option('-t, --trusted', 'return only trusted data')
.option('-s, --stripped', 'return only trusted data')
.action(async (query, options) => {
await solidLib.login()
let data: DataPlusPlus;
let dataString: string;
if (options.trusted) {
data = await solidLib.getDataWithTrust(query, [ "verification", "advertisement" ])
dataString = data.data.data
} else {
data = await solidLib.getData(query, [ "verification", "advertisement" ])
if (options.stripped) {
dataString = await flattenPackageStructure(data.data.data)
} else {
dataString = data.data.data
}
}
if (options.stripped) {
await flattenPackageStructure(dataString)
}
console.log(
`
Data
${dataString}
Agreement
${JSON.stringify(data.agreements, null, 2)}
`
)
await solidLib.logout();
})
program
.command('getlog')
.action(async () => {
await solidLib.login();
let logEntries = await solidLib.getLogEntries()
console.log('')
console.log('Log Entries:')
for (let entry of logEntries) {
console.log(
`
Date:
${entry.date}
Agreement:
${JSON.stringify(entry.agreement, null, 2)}
`
)
}
await solidLib.logout();
})
async function run() {
const close = await setup('steve')
await new Promise(resolve => setTimeout(resolve, 1000));
program.parse(process.argv)
// Wait for keypress to close
const keypress = async () => {
process.stdin.setRawMode(true)
return new Promise<void>(resolve => process.stdin.once('data', () => {
process.stdin.setRawMode(false)
resolve()
}))
}
;(async () => {
await keypress()
})().then(async() => {
console.log('Endpoints closed by keypress')
await close();
process.exit()
})
}
async function flattenPackageStructure(data: string): Promise<string> {
let rule = `
@prefix log: <http://www.w3.org/2000/10/swap/log#> .
@prefix dcterms: <http://purl.org/dc/terms/>.
@prefix odrl: <http://www.w3.org/ns/odrl/2/>.
@prefix pack: <https://example.org/ns/package#>.
@prefix policy: <https://example.org/ns/policy#>.
@prefix sign: <https://example.org/ns/signature#>.
@prefix xsd: <http://www.w3.org/2001/XMLSchema#>.
@prefix rot: <https://purl.org/krdb-core/rot/rot#> .
@prefix : <http://example.org/> .
{
?pack pack:packages ?package .
?package log:includes { [] pack:content ?content } .
?content log:includes { [] pack:packages ?p1 } .
} => {
[] pack:flatPackages ?p1
} .
{
?pack pack:packages ?package .
} => {
[] pack:flatPackages ?package
} .
{
?pack pack:flatPackages ?package .
?package log:includes { [] pack:content ?content } .
?content log:includes { [] pack:packages ?p1 } .
} => {
[] pack:flatPackages ?p1
} .
{
?pack pack:flatPackages ?package .
?package log:includes {
[] sign:signatureHasBeenVerified true;
sign:hasContentSignature [
sign:issuer ?issuer
]
} .
} => {
[] pack:packages ?package ;
pack:assertedBy ?issuer .
} .
{
?pack pack:packages ?package .
?package log:includes { [] pack:content ?content } .
} => ?content .
`
let input = rule + data;
const reasoningResult = await n3reasoner(input);
const reasonedStore = new Store(new Parser({ format: 'text/n3' }).parse(reasoningResult));
const resultingData = [...reasonedStore.match(null, null, null, DataFactory.defaultGraph())].filter(
term => (term.object.termType !== 'BlankNode' || reasonedStore.match(null, null, null, term.object as any).size === 0)
&& !term.predicate.equals(DataFactory.namedNode('https://example.org/ns/package#assertedBy'))
)
const resultingDataString = await write(resultingData, {
format: 'text/n3'
})
return resultingDataString;
}
run()