-
Notifications
You must be signed in to change notification settings - Fork 0
/
reasoner.js
64 lines (61 loc) · 2.3 KB
/
reasoner.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
'use strict'
const debug = require('debug')('reasoner')
const rdf = require('rdf-ext')
const DatasetStore = require('rdf-store-dataset')
/**
* abstract class Reasoner
* Follows RDFJS specification API for Store
* - import(dataStream) to import data to be inferred
* - reason() to infere the data manually
* or use { options.autoinfere = true } in Reasoner constructor for automatic reasoning after every change. It's false by default
* - match(subject, predicate, object, graph) to get data from inferred graph
* - matchInSource(subject, predicate, object, graph) in graph of imported data
* TODO:
* - sameAs/isDefinedBy/... data fetching
*/
class Reasoner extends DatasetStore {
constructor (options) {
super(options)
options = options || {}
this.inferred = options.inferred || this.factory.dataset()
this.autoinfere = options.autoinfere !== false
}
reason () {
throw new Error('Not implemented. Reasoner is an abstract class. implement reason(): EventEmitter')
// Hint for implementing:
// - reason over this.dataset and put resulting triples into a new dataset saved in this.inferred.
// - emit 'end' Event
}
match (subject, predicate, object, graph) {
return this.inferred.match(subject, predicate, object, graph).toStream()
}
matchInSource (subject, predicate, object, graph) {
return super.match(subject, predicate, object, graph)
}
import (stream, options) {
return this.autoInfereEvent(super.import(stream, options))
}
remove (stream) {
return this.autoInfereEvent(super.remove(stream))
}
removeMatches (subject = null, predicate = null, object = null, graph = null) {
return this.autoInfereEvent(super.removeMatches(subject, predicate, object, graph))
}
deleteGraph (subject = null, predicate = null, object = null, graph = null) {
return this.autoInfereEvent(super.deleteGraph(subject, predicate, object, graph))
}
autoInfereEvent (event) {
if (!this.autoinfere) {
return event
}
debug('autoinfering...')
return rdf.asEvent(() => {
return Promise.resolve().then(() => {
return rdf.waitFor(event).then(() => {
return rdf.waitFor(this.reason())
}).catch((err) => { return Promise.reject(err) })
}).catch((err) => { return Promise.reject(err) })
})
}
}
module.exports = Reasoner