-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
142 lines (129 loc) · 5.28 KB
/
index.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
const nn = require('@rdfjs/data-model').namedNode
const namespace = require('@rdfjs/namespace')
import { Quad, NamedNode, Source } from 'rdf-js'
export interface ResultNode {
type :NamedNode,
iri :string,
distance :number
}
const ns = {
vf: namespace('https://w3id.org/valueflows#'),
rdf: namespace('http://www.w3.org/1999/02/22-rdf-syntax-ns#')
}
function subjects (quads :Quad[]) {
return quads.map(quad => quad.subject as NamedNode)
}
function objects (quads :Quad[]) {
return quads.map(quad => quad.object as NamedNode)
}
async function match (store :Source, subject ?:NamedNode, predicate ?:NamedNode, object ?:NamedNode, graph ?:NamedNode) {
const results :Quad[] = []
// @ts-ignore
for await(const quad of store.match(subject, predicate, object, graph)) {
results.push(quad)
}
return results
}
// TODO: use https://github.com/rdf-ext/clownface
async function outNodes (store :Source, subject :NamedNode, predicate :NamedNode) {
return objects(await match(store, subject, predicate, null))
}
async function inNodes (store :Source, object :NamedNode, predicate :NamedNode) {
return subjects(await match(store, null, predicate, object))
}
export function track (store :Source, iri :string) :AsyncIterableIterator<ResultNode>
export function track (store :Source, iri :NamedNode) :AsyncIterableIterator<ResultNode>
export async function * track (store :any, iri :any) :AsyncIterableIterator<ResultNode> {
if (!iri.termType) iri = nn(iri)
const visited :NamedNode[] = []
yield * await tracker(store, visited, iri as NamedNode)
}
async function * tracker (store :Source, visited :NamedNode[], current :NamedNode, distance = 0) :AsyncIterableIterator<ResultNode> {
if (visited.some(node => node.equals(current))) return
visited.push(current)
const types = await outNodes(store, current, ns.rdf('type'))
if (types.some(node => node.equals(ns.vf('EconomicResource')))) {
yield {
type: ns.vf('EconomicResource'),
iri: current.value,
distance
}
// find events affecting it
const events = await inNodes(store, current, ns.vf('affects'))
for (let event of events) yield * await tracker(store, visited, event, distance + 1)
}
if (types.some(node => node.equals(ns.vf('Process')))) {
yield {
type: ns.vf('Process'),
iri: current.value,
distance
}
// find events
const events = await inNodes(store, current, ns.vf('outputOf'))
for (let event of events) yield * await tracker(store, visited, event, distance + 1)
}
if (types.some(node => node.equals(ns.vf('EconomicEvent')))) {
yield {
type: ns.vf('EconomicEvent'),
iri: current.value,
distance
}
// find processes taking it as input
const inputToProcesses = await outNodes(store, current, ns.vf('inputOf'))
for (let process of inputToProcesses) yield * await tracker(store, visited, process, distance + 1)
// find affected resources only if process takes it as an output
const outputToProcesses = await outNodes(store, current, ns.vf('outputOf'))
if (outputToProcesses.length) {
const resources = await outNodes(store, current, ns.vf('affects'))
for (let resource of resources) yield * await tracker(store, visited, resource, distance + 1)
}
}
}
export function trace (store :Source, iri :string) :AsyncIterableIterator<ResultNode>
export function trace (store :Source, iri :NamedNode) :AsyncIterableIterator<ResultNode>
export async function * trace (store :any, iri :any) :AsyncIterableIterator<ResultNode> {
if (!iri.termType) iri = nn(iri)
const visited :NamedNode[] = []
yield * await tracer(store, visited, iri as NamedNode)
}
async function * tracer (store :Source, visited :NamedNode[], current :NamedNode, distance = 0) :AsyncIterableIterator<ResultNode> {
if (visited.some(node => node.equals(current))) return
visited.push(current)
const types = await outNodes(store, current, ns.rdf('type'))
if (types.some(node => node.equals(ns.vf('EconomicResource')))) {
yield {
type: ns.vf('EconomicResource'),
iri: current.value,
distance
}
// find events affecting it
const events = await inNodes(store, current, ns.vf('affects'))
for (let event of events) yield * await tracer(store, visited, event, distance + 1)
}
if (types.some(node => node.equals(ns.vf('Process')))) {
yield {
type: ns.vf('Process'),
iri: current.value,
distance
}
// find events
const events = await inNodes(store, current, ns.vf('inputOf'))
for (let event of events) yield * await tracer(store, visited, event, distance + 1)
}
if (types.some(node => node.equals(ns.vf('EconomicEvent')))) {
yield {
type: ns.vf('EconomicEvent'),
iri: current.value,
distance
}
// find processes taking it as output
const outputToProcesses = await outNodes(store, current, ns.vf('outputOf'))
for (let process of outputToProcesses) yield * await tracer(store, visited, process, distance + 1)
// find affected resources only if process takes it as an input
const inputToProcesses = await outNodes(store, current, ns.vf('inputOf'))
if (inputToProcesses.length) {
const resources = await outNodes(store, current, ns.vf('affects'))
for (let resource of resources) yield * await tracer(store, visited, resource, distance +1)
}
}
}