@dxos/echo-memory 0.0.0-stage → 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +105 -0
- package/dist/lib/Fact.mjs +1 -0
- package/dist/lib/FactFeed.mjs +1 -0
- package/dist/lib/Graph.mjs +1 -0
- package/dist/lib/Hash.mjs +1 -0
- package/dist/lib/Mapping.mjs +1 -0
- package/dist/lib/MemoryError.mjs +1 -0
- package/dist/lib/MemoryQuery.mjs +1 -0
- package/dist/lib/MemoryService.mjs +1 -0
- package/dist/lib/Ontology.mjs +1 -0
- package/dist/lib/Pipeline.mjs +1 -0
- package/dist/lib/Predicate.mjs +1 -0
- package/dist/lib/Rdf.mjs +1 -0
- package/dist/lib/RdfStore.mjs +1 -0
- package/dist/lib/Sparql.mjs +1 -0
- package/dist/lib/Subscription.mjs +1 -0
- package/dist/lib/Trig.mjs +1 -0
- package/dist/lib/Vocab.mjs +1 -0
- package/dist/lib/Vocabularies.mjs +1 -0
- package/dist/lib/chunk-Fact.mjs +245 -0
- package/dist/lib/chunk-Fact.mjs.map +1 -0
- package/dist/lib/chunk-FactFeed.mjs +29 -0
- package/dist/lib/chunk-FactFeed.mjs.map +1 -0
- package/dist/lib/chunk-Graph.mjs +147 -0
- package/dist/lib/chunk-Graph.mjs.map +1 -0
- package/dist/lib/chunk-Hash.mjs +16 -0
- package/dist/lib/chunk-Hash.mjs.map +1 -0
- package/dist/lib/chunk-Mapping.mjs +226 -0
- package/dist/lib/chunk-Mapping.mjs.map +1 -0
- package/dist/lib/chunk-MemoryError.mjs +10 -0
- package/dist/lib/chunk-MemoryError.mjs.map +1 -0
- package/dist/lib/chunk-MemoryQuery.mjs +99 -0
- package/dist/lib/chunk-MemoryQuery.mjs.map +1 -0
- package/dist/lib/chunk-MemoryService.mjs +31 -0
- package/dist/lib/chunk-MemoryService.mjs.map +1 -0
- package/dist/lib/chunk-Ontology.mjs +529 -0
- package/dist/lib/chunk-Ontology.mjs.map +1 -0
- package/dist/lib/chunk-Pipeline.mjs +84 -0
- package/dist/lib/chunk-Pipeline.mjs.map +1 -0
- package/dist/lib/chunk-Predicate.mjs +66 -0
- package/dist/lib/chunk-Predicate.mjs.map +1 -0
- package/dist/lib/chunk-Rdf.mjs +120 -0
- package/dist/lib/chunk-Rdf.mjs.map +1 -0
- package/dist/lib/chunk-RdfStore.mjs +60 -0
- package/dist/lib/chunk-RdfStore.mjs.map +1 -0
- package/dist/lib/chunk-Sparql.mjs +153 -0
- package/dist/lib/chunk-Sparql.mjs.map +1 -0
- package/dist/lib/chunk-Subscription.mjs +44 -0
- package/dist/lib/chunk-Subscription.mjs.map +1 -0
- package/dist/lib/chunk-Trig.mjs +106 -0
- package/dist/lib/chunk-Trig.mjs.map +1 -0
- package/dist/lib/chunk-Vocab.mjs +164 -0
- package/dist/lib/chunk-Vocab.mjs.map +1 -0
- package/dist/lib/chunk-Vocabularies.mjs +343 -0
- package/dist/lib/chunk-Vocabularies.mjs.map +1 -0
- package/dist/lib/chunk-rolldown-runtime.mjs +13 -0
- package/dist/lib/index.mjs +19 -0
- package/dist/types/src/Fact.d.ts +339 -0
- package/dist/types/src/Fact.d.ts.map +1 -0
- package/dist/types/src/Fact.test.d.ts +2 -0
- package/dist/types/src/Fact.test.d.ts.map +1 -0
- package/dist/types/src/FactFeed.d.ts +11 -0
- package/dist/types/src/FactFeed.d.ts.map +1 -0
- package/dist/types/src/FactFeed.test.d.ts +2 -0
- package/dist/types/src/FactFeed.test.d.ts.map +1 -0
- package/dist/types/src/Graph.d.ts +31 -0
- package/dist/types/src/Graph.d.ts.map +1 -0
- package/dist/types/src/Graph.test.d.ts +2 -0
- package/dist/types/src/Graph.test.d.ts.map +1 -0
- package/dist/types/src/Hash.d.ts +3 -0
- package/dist/types/src/Hash.d.ts.map +1 -0
- package/dist/types/src/Mapping.d.ts +18 -0
- package/dist/types/src/Mapping.d.ts.map +1 -0
- package/dist/types/src/Mapping.test.d.ts +2 -0
- package/dist/types/src/Mapping.test.d.ts.map +1 -0
- package/dist/types/src/MemoryError.d.ts +33 -0
- package/dist/types/src/MemoryError.d.ts.map +1 -0
- package/dist/types/src/MemoryQuery.d.ts +136 -0
- package/dist/types/src/MemoryQuery.d.ts.map +1 -0
- package/dist/types/src/MemoryQuery.test.d.ts +2 -0
- package/dist/types/src/MemoryQuery.test.d.ts.map +1 -0
- package/dist/types/src/MemoryService.d.ts +72 -0
- package/dist/types/src/MemoryService.d.ts.map +1 -0
- package/dist/types/src/Ontology.d.ts +115 -0
- package/dist/types/src/Ontology.d.ts.map +1 -0
- package/dist/types/src/Ontology.test.d.ts +2 -0
- package/dist/types/src/Ontology.test.d.ts.map +1 -0
- package/dist/types/src/Pipeline.d.ts +103 -0
- package/dist/types/src/Pipeline.d.ts.map +1 -0
- package/dist/types/src/Predicate.d.ts +17 -0
- package/dist/types/src/Predicate.d.ts.map +1 -0
- package/dist/types/src/Predicate.test.d.ts +2 -0
- package/dist/types/src/Predicate.test.d.ts.map +1 -0
- package/dist/types/src/Rdf.d.ts +33 -0
- package/dist/types/src/Rdf.d.ts.map +1 -0
- package/dist/types/src/Rdf.test.d.ts +2 -0
- package/dist/types/src/Rdf.test.d.ts.map +1 -0
- package/dist/types/src/RdfStore.d.ts +471 -0
- package/dist/types/src/RdfStore.d.ts.map +1 -0
- package/dist/types/src/Sparql.d.ts +58 -0
- package/dist/types/src/Sparql.d.ts.map +1 -0
- package/dist/types/src/Sparql.test.d.ts +2 -0
- package/dist/types/src/Sparql.test.d.ts.map +1 -0
- package/dist/types/src/Subscription.d.ts +100 -0
- package/dist/types/src/Subscription.d.ts.map +1 -0
- package/dist/types/src/Trig.d.ts +8 -0
- package/dist/types/src/Trig.d.ts.map +1 -0
- package/dist/types/src/Trig.test.d.ts +2 -0
- package/dist/types/src/Trig.test.d.ts.map +1 -0
- package/dist/types/src/Vocab.d.ts +90 -0
- package/dist/types/src/Vocab.d.ts.map +1 -0
- package/dist/types/src/Vocabularies.d.ts +45 -0
- package/dist/types/src/Vocabularies.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +19 -0
- package/dist/types/src/index.d.ts.map +1 -0
- package/dist/types/tsconfig.tsbuildinfo +1 -0
- package/package.json +133 -3
- package/src/Fact.test.ts +74 -0
- package/src/Fact.ts +269 -0
- package/src/FactFeed.test.ts +40 -0
- package/src/FactFeed.ts +38 -0
- package/src/Graph.test.ts +106 -0
- package/src/Graph.ts +158 -0
- package/src/Hash.ts +15 -0
- package/src/Mapping.test.ts +197 -0
- package/src/Mapping.ts +320 -0
- package/src/MemoryError.ts +10 -0
- package/src/MemoryQuery.test.ts +67 -0
- package/src/MemoryQuery.ts +114 -0
- package/src/MemoryService.ts +89 -0
- package/src/Ontology.test.ts +94 -0
- package/src/Ontology.ts +571 -0
- package/src/Pipeline.ts +97 -0
- package/src/Predicate.test.ts +47 -0
- package/src/Predicate.ts +89 -0
- package/src/Rdf.test.ts +104 -0
- package/src/Rdf.ts +158 -0
- package/src/RdfStore.ts +111 -0
- package/src/Sparql.test.ts +72 -0
- package/src/Sparql.ts +237 -0
- package/src/Subscription.ts +47 -0
- package/src/Trig.test.ts +199 -0
- package/src/Trig.ts +170 -0
- package/src/Vocab.ts +134 -0
- package/src/Vocabularies.ts +204 -0
- package/src/index.ts +22 -0
- package/README.md +0 -3
package/src/Graph.ts
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
// @import-as-namespace
|
|
6
|
+
|
|
7
|
+
import * as Fact from './Fact.ts';
|
|
8
|
+
import * as Hash from './Hash.ts';
|
|
9
|
+
import * as Mapping from './Mapping.ts';
|
|
10
|
+
import * as Ontology from './Ontology.ts';
|
|
11
|
+
import * as Rdf from './Rdf.ts';
|
|
12
|
+
import * as Trig from './Trig.ts';
|
|
13
|
+
import * as Vocab from './Vocab.ts';
|
|
14
|
+
|
|
15
|
+
/** Everything a graph states once for all its statements: facts that differ in any of it need separate graphs. */
|
|
16
|
+
const provenanceKey = (fact: Fact.Fact): string =>
|
|
17
|
+
JSON.stringify([
|
|
18
|
+
fact.attribution.source,
|
|
19
|
+
fact.attribution.agent,
|
|
20
|
+
fact.attribution.agentLabel,
|
|
21
|
+
fact.attribution.generatedAtTime,
|
|
22
|
+
fact.attribution.wasDerivedFrom,
|
|
23
|
+
fact.attribution.span,
|
|
24
|
+
fact.assertion.quote,
|
|
25
|
+
fact.assertion.validFrom,
|
|
26
|
+
fact.assertion.validTo,
|
|
27
|
+
fact.factuality,
|
|
28
|
+
fact.illocution,
|
|
29
|
+
fact.recordedAt,
|
|
30
|
+
fact.extractor,
|
|
31
|
+
fact.sourceHash,
|
|
32
|
+
fact.pass,
|
|
33
|
+
fact.kind,
|
|
34
|
+
fact.origin,
|
|
35
|
+
fact.supersedes,
|
|
36
|
+
fact.retracts,
|
|
37
|
+
]);
|
|
38
|
+
|
|
39
|
+
export type AssignOptions = {
|
|
40
|
+
/** Names the graph for a fact; facts given the same name and provenance share a graph. Defaults to its source. */
|
|
41
|
+
readonly name?: (fact: Fact.Fact) => string;
|
|
42
|
+
readonly ontology?: Ontology.Ontology;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Puts facts into named graphs: facts that share their provenance (source, speaker, time, quote, factuality,
|
|
47
|
+
* extraction pass, …) share a graph, and a fact that differs in any of it starts another. A fact that already names
|
|
48
|
+
* a graph keeps it. Each fact gets its predicate IRI, and the id it will have once stored; facts come in their plain
|
|
49
|
+
* form, since the ids they will have are only known here.
|
|
50
|
+
*/
|
|
51
|
+
export const assign = (
|
|
52
|
+
facts: readonly Fact.Encoded[],
|
|
53
|
+
{ name, ontology = Ontology.DEFAULT }: AssignOptions = {},
|
|
54
|
+
): Fact.Fact[] =>
|
|
55
|
+
facts.map((input) => {
|
|
56
|
+
const fact = Fact.make(input);
|
|
57
|
+
const label = name?.(fact) ?? fact.attribution.source;
|
|
58
|
+
const key = provenanceKey(fact);
|
|
59
|
+
// Two independent 32-bit hashes: every memory an agent records shares one label, so 32 bits would collide.
|
|
60
|
+
const graph =
|
|
61
|
+
fact.graph ??
|
|
62
|
+
Fact.GraphIri.make(Vocab.graphIri(`${Hash.text(label)}-${Hash.text(key)}${Hash.text(`\u0000${key}`)}`).value);
|
|
63
|
+
const placed: Fact.Fact = {
|
|
64
|
+
...fact,
|
|
65
|
+
graph,
|
|
66
|
+
assertion: { ...fact.assertion, predicateIri: Mapping.predicateOf(fact, ontology) },
|
|
67
|
+
};
|
|
68
|
+
return { ...placed, id: Mapping.idInGraph(placed, graph, ontology) };
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
/** Prefixes TriG output abbreviates. */
|
|
72
|
+
export const PREFIXES: Readonly<Record<string, string>> = {
|
|
73
|
+
dx: Vocab.DX,
|
|
74
|
+
sx: Vocab.SX,
|
|
75
|
+
prov: Vocab.PROV,
|
|
76
|
+
rdf: Vocab.RDF,
|
|
77
|
+
rdfs: Vocab.RDFS,
|
|
78
|
+
owl: Vocab.OWL,
|
|
79
|
+
xsd: Vocab.XSD,
|
|
80
|
+
skos: Vocab.SKOS,
|
|
81
|
+
dcterms: Vocab.DCTERMS,
|
|
82
|
+
schema: Vocab.SCHEMA,
|
|
83
|
+
foaf: Vocab.FOAF,
|
|
84
|
+
org: Vocab.ORG,
|
|
85
|
+
entity: Vocab.ENTITY,
|
|
86
|
+
fact: Vocab.FACT,
|
|
87
|
+
graph: Vocab.GRAPH,
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
/** Prefixes a whole store's TriG export abbreviates: {@link PREFIXES}, the brain namespaces and SIOC. */
|
|
91
|
+
export const EXPORT_PREFIXES: Readonly<Record<string, string>> = { ...PREFIXES, brain: Vocab.BRAIN, sioc: Vocab.SIOC };
|
|
92
|
+
|
|
93
|
+
const write = (quads: Iterable<Rdf.Quad>, prefixes: Record<string, string>): string => {
|
|
94
|
+
const seen = new Set<string>();
|
|
95
|
+
const unique: Rdf.Quad[] = [];
|
|
96
|
+
for (const quad of quads) {
|
|
97
|
+
const key = `${quad.subject.value} ${quad.predicate.value} ${quad.object.termType}:${quad.object.value} ${quad.graph.value}`;
|
|
98
|
+
if (!seen.has(key)) {
|
|
99
|
+
seen.add(key);
|
|
100
|
+
unique.push(quad);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
return Trig.write(unique, { prefixes });
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The facts as TriG: each graph's statements, then its provenance. The brain's namespace reads as `brain:`, and the
|
|
108
|
+
* entities local to each graph as `g1:`, `g2:`, … so a graph-local entity is visibly not a shared one.
|
|
109
|
+
*/
|
|
110
|
+
export const toTriG = (facts: readonly Fact.Fact[], ontology: Ontology.Ontology = Ontology.DEFAULT): string => {
|
|
111
|
+
const quads = facts.flatMap((fact) => Mapping.factToTriples(fact, ontology));
|
|
112
|
+
const graphs = [...new Set(quads.flatMap((quad) => (quad.graph.termType === 'NamedNode' ? [quad.graph.value] : [])))];
|
|
113
|
+
const locals = graphs.filter(
|
|
114
|
+
(graph) =>
|
|
115
|
+
graph !== Vocab.ONTOLOGY_GRAPH &&
|
|
116
|
+
quads.some((quad) => quad.subject.value.startsWith(`${graph}#`) || quad.object.value.startsWith(`${graph}#`)),
|
|
117
|
+
);
|
|
118
|
+
const usesBrain = quads.some((quad) =>
|
|
119
|
+
[quad.subject, quad.predicate, quad.object].some((term) => term.value.startsWith(ontology.namespace)),
|
|
120
|
+
);
|
|
121
|
+
return write(quads, {
|
|
122
|
+
...PREFIXES,
|
|
123
|
+
...(usesBrain ? { brain: ontology.namespace } : {}),
|
|
124
|
+
...Object.fromEntries(locals.map((graph, index) => [`g${index + 1}`, `${graph}#`])),
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
/** The ontology's predicates as TriG: each an `rdf:Property` described in the ontology graph. */
|
|
129
|
+
export const ontologyToTriG = (ontology: Ontology.Ontology): string =>
|
|
130
|
+
write(Ontology.ontologyQuads(ontology), { ...PREFIXES, brain: ontology.namespace });
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* The predicates the facts use that the ontology does not define, as entries minted in its namespace, so the next
|
|
134
|
+
* extraction can reuse them; the first quote that used one is kept as its example.
|
|
135
|
+
*/
|
|
136
|
+
export const mintedPredicates = (
|
|
137
|
+
facts: readonly Fact.Fact[],
|
|
138
|
+
ontology: Ontology.Ontology = Ontology.DEFAULT,
|
|
139
|
+
): Ontology.Entry[] => {
|
|
140
|
+
const minted = new Map<string, Ontology.Entry>();
|
|
141
|
+
for (const fact of facts) {
|
|
142
|
+
const iri = Mapping.predicateOf(fact, ontology);
|
|
143
|
+
if (ontology.get(iri) || minted.has(iri)) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
minted.set(iri, {
|
|
147
|
+
iri,
|
|
148
|
+
name: Ontology.nameFor(fact.assertion.predicate),
|
|
149
|
+
label: fact.assertion.predicate,
|
|
150
|
+
...(fact.assertion.predicateDescription ? { description: fact.assertion.predicateDescription } : {}),
|
|
151
|
+
...(Vocab.brainNamespaceOf(iri) ? { definedBy: Vocab.brainNamespaceOf(iri) } : {}),
|
|
152
|
+
created: fact.recordedAt,
|
|
153
|
+
creator: `${fact.extractor.id}/${fact.extractor.model}`,
|
|
154
|
+
...(fact.assertion.quote ? { example: fact.assertion.quote } : {}),
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
return [...minted.values()];
|
|
158
|
+
};
|
package/src/Hash.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
// @import-as-namespace
|
|
6
|
+
|
|
7
|
+
/** Stable content hash of a text (FNV-1a), with no crypto dependency so it runs the same everywhere. */
|
|
8
|
+
export const text = (text: string): string => {
|
|
9
|
+
let hash = 0x811c9dc5;
|
|
10
|
+
for (let index = 0; index < text.length; index++) {
|
|
11
|
+
hash ^= text.charCodeAt(index);
|
|
12
|
+
hash = Math.imul(hash, 0x01000193);
|
|
13
|
+
}
|
|
14
|
+
return (hash >>> 0).toString(16);
|
|
15
|
+
};
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { describe, test } from 'vitest';
|
|
6
|
+
|
|
7
|
+
import * as Fact from './Fact.ts';
|
|
8
|
+
import * as Graph from './Graph.ts';
|
|
9
|
+
import * as Mapping from './Mapping.ts';
|
|
10
|
+
import * as Rdf from './Rdf.ts';
|
|
11
|
+
import * as Vocab from './Vocab.ts';
|
|
12
|
+
|
|
13
|
+
/** A fact as stored: its predicate carries the IRI it resolved to. */
|
|
14
|
+
const stored = (fact: Fact.Fact): Fact.Fact =>
|
|
15
|
+
Fact.make({
|
|
16
|
+
...fact,
|
|
17
|
+
assertion: { ...fact.assertion, predicateIri: Mapping.predicateOf(fact) },
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
const FACT = Fact.make({
|
|
21
|
+
id: 'fact-1',
|
|
22
|
+
assertion: {
|
|
23
|
+
subject: { kind: 'entity', entity: 'alice' },
|
|
24
|
+
predicate: 'travelsTo',
|
|
25
|
+
object: { kind: 'entity', entity: 'paris' },
|
|
26
|
+
validFrom: '2026-06-12',
|
|
27
|
+
},
|
|
28
|
+
factuality: { value: 'PR+', polarity: '+', confidence: 0.6, nature: 'epistemic' },
|
|
29
|
+
attribution: { agent: 'alice', source: 'dxn:queue:x:m1', generatedAtTime: '2026-06-06T00:00:00.000Z' },
|
|
30
|
+
recordedAt: '2026-06-06T12:00:00.000Z',
|
|
31
|
+
extractor: { id: 'default', model: 'm', version: '1' },
|
|
32
|
+
sourceHash: 'h1',
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
describe('fact ↔ quads mapping', () => {
|
|
36
|
+
test('round-trips a fact through its own graph', ({ expect }) => {
|
|
37
|
+
const quads = Mapping.factToTriples(FACT);
|
|
38
|
+
expect(quads.length).toBeGreaterThan(5);
|
|
39
|
+
const [back] = Mapping.triplesToFacts(quads);
|
|
40
|
+
expect(back).toEqual(stored(FACT));
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
test('round-trips a fact with a literal object and no agent', ({ expect }) => {
|
|
44
|
+
const fact = Fact.make({
|
|
45
|
+
...FACT,
|
|
46
|
+
id: 'fact-2',
|
|
47
|
+
assertion: {
|
|
48
|
+
subject: { kind: 'entity', entity: 'meeting' },
|
|
49
|
+
predicate: 'scheduledFor',
|
|
50
|
+
object: { kind: 'literal', literal: '2026-07-15' },
|
|
51
|
+
},
|
|
52
|
+
attribution: { source: 'dxn:queue:x:m3', generatedAtTime: '2026-06-08T00:00:00.000Z' },
|
|
53
|
+
});
|
|
54
|
+
const [back] = Mapping.triplesToFacts(Mapping.factToTriples(fact));
|
|
55
|
+
expect(back).toEqual(stored(fact));
|
|
56
|
+
});
|
|
57
|
+
|
|
58
|
+
test('round-trips a fact with wasDerivedFrom and span', ({ expect }) => {
|
|
59
|
+
const fact = Fact.make({
|
|
60
|
+
...FACT,
|
|
61
|
+
id: 'fact-3',
|
|
62
|
+
attribution: {
|
|
63
|
+
source: 'dxn:queue:x:m4',
|
|
64
|
+
generatedAtTime: '2026-06-09T00:00:00.000Z',
|
|
65
|
+
wasDerivedFrom: ['dxn:a', 'dxn:b'],
|
|
66
|
+
span: { start: 0, end: 12 },
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
const [back] = Mapping.triplesToFacts(Mapping.factToTriples(fact));
|
|
70
|
+
expect(back).toEqual(stored(fact));
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
test('round-trips the extraction pass', ({ expect }) => {
|
|
74
|
+
const fact = Fact.make({ ...FACT, id: 'fact-pass', pass: 'pass-1' });
|
|
75
|
+
const [back] = Mapping.triplesToFacts(Mapping.factToTriples(fact));
|
|
76
|
+
expect(back).toEqual(stored(fact));
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
test('round-trips entity display labels (preserving surface casing)', ({ expect }) => {
|
|
80
|
+
const fact = Fact.make({
|
|
81
|
+
...FACT,
|
|
82
|
+
id: 'fact-4',
|
|
83
|
+
assertion: {
|
|
84
|
+
subject: { kind: 'entity', entity: 'dxos', label: 'DXOS' },
|
|
85
|
+
predicate: 'is',
|
|
86
|
+
object: { kind: 'entity', entity: 'open-source-project', label: 'an open source project' },
|
|
87
|
+
},
|
|
88
|
+
});
|
|
89
|
+
const [back] = Mapping.triplesToFacts(Mapping.factToTriples(fact));
|
|
90
|
+
expect(back).toEqual(stored(fact));
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
test('omits illocution triples when the fact has no illocution', ({ expect }) => {
|
|
94
|
+
const quads = Mapping.factToTriples(FACT);
|
|
95
|
+
const names = quads.map((quad) => quad.predicate.value);
|
|
96
|
+
expect(names.some((name) => /#(force|mood|addressee)$/.test(name))).toBe(false);
|
|
97
|
+
const [back] = Mapping.triplesToFacts(quads);
|
|
98
|
+
expect(back.illocution).toBeUndefined();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
test('round-trips a full illocution (force, mood, addressee)', ({ expect }) => {
|
|
102
|
+
const fact = Fact.make({
|
|
103
|
+
...FACT,
|
|
104
|
+
id: 'fact-5',
|
|
105
|
+
illocution: { force: 'directive', mood: 'interrogative', addressee: 'bob' },
|
|
106
|
+
});
|
|
107
|
+
const [back] = Mapping.triplesToFacts(Mapping.factToTriples(fact));
|
|
108
|
+
expect(back).toEqual(stored(fact));
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
test('round-trips an illocution with only force', ({ expect }) => {
|
|
112
|
+
const fact = Fact.make({ ...FACT, id: 'fact-6', illocution: { force: 'commissive' } });
|
|
113
|
+
const [back] = Mapping.triplesToFacts(Mapping.factToTriples(fact));
|
|
114
|
+
expect(back).toEqual(stored(fact));
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
test('round-trips an illocution with force and mood only', ({ expect }) => {
|
|
118
|
+
const fact = Fact.make({ ...FACT, id: 'fact-7', illocution: { force: 'directive', mood: 'imperative' } });
|
|
119
|
+
const [back] = Mapping.triplesToFacts(Mapping.factToTriples(fact));
|
|
120
|
+
expect(back).toEqual(stored(fact));
|
|
121
|
+
});
|
|
122
|
+
|
|
123
|
+
test('round-trips an illocution with force and addressee only', ({ expect }) => {
|
|
124
|
+
const fact = Fact.make({
|
|
125
|
+
...FACT,
|
|
126
|
+
id: 'fact-8',
|
|
127
|
+
illocution: { force: 'expressive', addressee: 'carol@example.com' },
|
|
128
|
+
});
|
|
129
|
+
const [back] = Mapping.triplesToFacts(Mapping.factToTriples(fact));
|
|
130
|
+
expect(back).toEqual(stored(fact));
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
test('rejects an invalid stored force', ({ expect }) => {
|
|
134
|
+
const quads = Mapping.factToTriples({ ...FACT, illocution: { force: 'assertive' } }).map((quad) =>
|
|
135
|
+
quad.predicate.value.endsWith('#force')
|
|
136
|
+
? Rdf.quad(quad.subject, quad.predicate, Rdf.literal('bogus'), quad.graph)
|
|
137
|
+
: quad,
|
|
138
|
+
);
|
|
139
|
+
expect(() => Mapping.triplesToFacts(quads)).toThrow();
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
test('throws when required provenance is missing', ({ expect }) => {
|
|
143
|
+
const quads = Mapping.factToTriples(FACT).filter((quad) => !quad.predicate.value.endsWith('#factuality'));
|
|
144
|
+
expect(() => Mapping.triplesToFacts(quads)).toThrow();
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
test('stores the statement as a triple in the graph, and its provenance about the graph', ({ expect }) => {
|
|
148
|
+
const quads = Mapping.factToTriples(FACT);
|
|
149
|
+
const graph = Vocab.factIri(FACT.id).value;
|
|
150
|
+
expect(
|
|
151
|
+
quads.filter((quad) => quad.graph.value === graph && quad.predicate.value === Mapping.predicateOf(FACT)),
|
|
152
|
+
).toHaveLength(1);
|
|
153
|
+
expect(
|
|
154
|
+
quads.find((quad) => quad.predicate.value.endsWith('#quote') || quad.predicate.value.endsWith('#factuality'))
|
|
155
|
+
?.subject.value,
|
|
156
|
+
).toBe(graph);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
test('keeps an IRI entity as itself, and its label and kind in the graph', ({ expect }) => {
|
|
160
|
+
const fact = Fact.make({
|
|
161
|
+
...FACT,
|
|
162
|
+
id: 'fact-iri',
|
|
163
|
+
assertion: {
|
|
164
|
+
subject: { kind: 'entity', entity: 'echo://SPACE/RICH', label: 'Rich Burdon', type: 'person' },
|
|
165
|
+
predicate: 'has role',
|
|
166
|
+
object: { kind: 'entity', entity: 'founder', label: 'Founder' },
|
|
167
|
+
},
|
|
168
|
+
attribution: { agent: 'did:key:z6Mk', source: 'https://dxos.org/team', generatedAtTime: '2026-10-13T00:00:00Z' },
|
|
169
|
+
});
|
|
170
|
+
const quads = Mapping.factToTriples(fact);
|
|
171
|
+
expect(quads[0].subject.value).toBe('echo://SPACE/RICH');
|
|
172
|
+
expect(quads[0].predicate.value).toBe('https://schema.org/jobTitle');
|
|
173
|
+
// A slug is local to the graph that mentions it.
|
|
174
|
+
expect(quads[0].object.value).toBe(`${Vocab.factIri('fact-iri').value}#founder`);
|
|
175
|
+
const [back] = Mapping.triplesToFacts(quads);
|
|
176
|
+
expect(back.assertion.subject).toEqual(fact.assertion.subject);
|
|
177
|
+
expect(back.assertion.object).toEqual(fact.assertion.object);
|
|
178
|
+
expect(back.attribution.agent).toBe('did:key:z6Mk');
|
|
179
|
+
// A known predicate reads back as the ontology's label.
|
|
180
|
+
expect(back.assertion.predicate).toBe('has role');
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
test('a graph holds several statements, each its own fact, sharing the provenance', ({ expect }) => {
|
|
184
|
+
const [first, second] = Graph.assign([
|
|
185
|
+
FACT,
|
|
186
|
+
Fact.make({
|
|
187
|
+
...FACT,
|
|
188
|
+
id: 'other',
|
|
189
|
+
assertion: { ...FACT.assertion, predicate: 'manages', object: { kind: 'entity', entity: 'bob' } },
|
|
190
|
+
}),
|
|
191
|
+
]);
|
|
192
|
+
expect(first.graph).toBe(second.graph);
|
|
193
|
+
expect(first.id).not.toBe(second.id);
|
|
194
|
+
const back = Mapping.triplesToFacts([...Mapping.factToTriples(first), ...Mapping.factToTriples(second)]);
|
|
195
|
+
expect(back).toEqual([first, second]);
|
|
196
|
+
});
|
|
197
|
+
});
|
package/src/Mapping.ts
ADDED
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2026 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
// @import-as-namespace
|
|
6
|
+
|
|
7
|
+
import * as Schema from 'effect/Schema';
|
|
8
|
+
|
|
9
|
+
import * as Fact from './Fact.ts';
|
|
10
|
+
import * as Hash from './Hash.ts';
|
|
11
|
+
import * as Ontology from './Ontology.ts';
|
|
12
|
+
import * as Rdf from './Rdf.ts';
|
|
13
|
+
import * as Vocab from './Vocab.ts';
|
|
14
|
+
|
|
15
|
+
const { quad, defaultGraph, namedNode } = Rdf;
|
|
16
|
+
|
|
17
|
+
const decodeFact = Schema.decodeUnknownSync(Fact.Fact);
|
|
18
|
+
|
|
19
|
+
const localName = (iri: string) => iri.replace(/^.*[#/]/, '');
|
|
20
|
+
|
|
21
|
+
//
|
|
22
|
+
// Storage shape: each statement is a real triple inside a named graph, and the graph's provenance is stated once,
|
|
23
|
+
// about the graph, in the default graph. Entities carry their labels and kinds inside the graph that mentions them,
|
|
24
|
+
// and one known by a label alone has an IRI local to that graph; a predicate the ontology does not define is
|
|
25
|
+
// described (label, comment, defining namespace) in the ontology graph.
|
|
26
|
+
//
|
|
27
|
+
// Invariant: every provenance predicate has a UNIQUE local name (reassembly keys provenance by local name).
|
|
28
|
+
// `attribution.source` is `prov:wasDerivedFrom`; `attribution.wasDerivedFrom` uses `sx:derivedFrom` so the two
|
|
29
|
+
// never collide. `illocution` is flattened to `sx:force` / `sx:mood` / `sx:addressee`.
|
|
30
|
+
//
|
|
31
|
+
|
|
32
|
+
const CLASSES: Record<Fact.EntityKind, string> = {
|
|
33
|
+
person: `${Vocab.SCHEMA}Person`,
|
|
34
|
+
org: `${Vocab.SCHEMA}Organization`,
|
|
35
|
+
place: `${Vocab.SCHEMA}Place`,
|
|
36
|
+
event: `${Vocab.SCHEMA}Event`,
|
|
37
|
+
concept: `${Vocab.SKOS}Concept`,
|
|
38
|
+
thing: `${Vocab.SCHEMA}Thing`,
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
// Facts stored before the kinds moved to schema.org typed entities with `dx:` classes.
|
|
42
|
+
const LEGACY_CLASSES: Record<Fact.EntityKind, string> = {
|
|
43
|
+
person: `${Vocab.DX}Person`,
|
|
44
|
+
org: `${Vocab.DX}Organization`,
|
|
45
|
+
place: `${Vocab.DX}Place`,
|
|
46
|
+
event: `${Vocab.DX}Event`,
|
|
47
|
+
concept: `${Vocab.DX}Concept`,
|
|
48
|
+
thing: `${Vocab.DX}Thing`,
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
const KINDS = new Map(
|
|
52
|
+
[...Object.entries(CLASSES), ...Object.entries(LEGACY_CLASSES)].map(([kind, iri]) => [iri, kind as Fact.EntityKind]),
|
|
53
|
+
);
|
|
54
|
+
|
|
55
|
+
/** The class IRIs an entity's kind is typed with; an `rdf:type` to any other class is a statement, not a kind. */
|
|
56
|
+
export const KIND_CLASSES: ReadonlyArray<string> = [...KINDS.keys()];
|
|
57
|
+
|
|
58
|
+
/** Whether a quad states an entity's kind (an `rdf:type` to a kind class) rather than making a statement. */
|
|
59
|
+
export const isKindQuad = (predicate: string, object: string): boolean =>
|
|
60
|
+
predicate === Vocab.RDF_TYPE && KINDS.has(object);
|
|
61
|
+
|
|
62
|
+
/** The class IRI an entity of this kind is typed with. */
|
|
63
|
+
export const classIri = (kind: Fact.EntityKind): string => CLASSES[kind];
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* RDF has no literal subjects, so a literal subject is stored as the entity it names. An entity slug is local to the
|
|
67
|
+
* graph: the extractor resolved it to no stable IRI, and a label alone does not say two graphs mean one thing.
|
|
68
|
+
*/
|
|
69
|
+
const subjectNode = (term: Fact.Term, graph: string): Rdf.NamedNode =>
|
|
70
|
+
term.kind === 'entity' ? Vocab.entityNode(term.entity, graph) : Vocab.localEntityIri(graph, term.literal);
|
|
71
|
+
|
|
72
|
+
const objectNode = (term: Fact.Term, graph: string): Rdf.NamedNode | Rdf.Literal =>
|
|
73
|
+
term.kind === 'entity' ? Vocab.entityNode(term.entity, graph) : Vocab.str(term.literal);
|
|
74
|
+
|
|
75
|
+
/** The predicate IRI a fact is stored with. */
|
|
76
|
+
export const predicateOf = (fact: Fact.Fact, ontology: Ontology.Ontology = Ontology.DEFAULT): string =>
|
|
77
|
+
fact.assertion.predicateIri ?? ontology.resolve(fact.assertion.predicate).iri;
|
|
78
|
+
|
|
79
|
+
const nodeKey = (term: Rdf.Term): string => (term.termType === 'Literal' ? JSON.stringify(term.value) : term.value);
|
|
80
|
+
|
|
81
|
+
/** A statement's id within a named graph, from the triple itself, so writing and reading agree. */
|
|
82
|
+
const statementId = (graph: string, subject: Rdf.Term, predicate: string, object: Rdf.Term): string =>
|
|
83
|
+
`${graph}#${Hash.text(`${nodeKey(subject)} ${predicate} ${nodeKey(object)}`)}`;
|
|
84
|
+
|
|
85
|
+
/** The id a fact has once stored in `graph` ({@link triplesToFacts} reads it back). */
|
|
86
|
+
export const idInGraph = (
|
|
87
|
+
fact: Fact.Fact,
|
|
88
|
+
graph: string,
|
|
89
|
+
ontology: Ontology.Ontology = Ontology.DEFAULT,
|
|
90
|
+
): Fact.FactId =>
|
|
91
|
+
Fact.FactId.make(
|
|
92
|
+
graph.startsWith(Vocab.FACT)
|
|
93
|
+
? Vocab.factIdFromIri(graph)
|
|
94
|
+
: statementId(
|
|
95
|
+
graph,
|
|
96
|
+
subjectNode(fact.assertion.subject, graph),
|
|
97
|
+
predicateOf(fact, ontology),
|
|
98
|
+
objectNode(fact.assertion.object, graph),
|
|
99
|
+
),
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
/** Expands a fact into its statement, the descriptions of its entities, and its graph's provenance. */
|
|
103
|
+
export const factToTriples = (fact: Fact.Fact, ontology: Ontology.Ontology = Ontology.DEFAULT): Rdf.Quad[] => {
|
|
104
|
+
const graph = namedNode(Fact.graphOf(fact));
|
|
105
|
+
const meta = defaultGraph();
|
|
106
|
+
const predicate = predicateOf(fact, ontology);
|
|
107
|
+
const subject = subjectNode(fact.assertion.subject, graph.value);
|
|
108
|
+
const object = objectNode(fact.assertion.object, graph.value);
|
|
109
|
+
const quads: Rdf.Quad[] = [quad(subject, namedNode(predicate), object, graph)];
|
|
110
|
+
|
|
111
|
+
for (const [node, term] of [
|
|
112
|
+
[subject, fact.assertion.subject],
|
|
113
|
+
[object, fact.assertion.object],
|
|
114
|
+
] as const) {
|
|
115
|
+
if (node.termType !== 'NamedNode') {
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
const label = term.kind === 'entity' ? term.label : term.literal;
|
|
119
|
+
if (label !== undefined) {
|
|
120
|
+
quads.push(quad(node, namedNode(Vocab.RDFS_LABEL), Vocab.str(label), graph));
|
|
121
|
+
}
|
|
122
|
+
if (term.kind === 'entity' && term.type) {
|
|
123
|
+
quads.push(quad(node, namedNode(Vocab.RDF_TYPE), namedNode(classIri(term.type)), graph));
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// A predicate the ontology does not define is described in the ontology graph, never inside the statement's.
|
|
128
|
+
if (!ontology.get(predicate)) {
|
|
129
|
+
const definition: Ontology.Entry = {
|
|
130
|
+
iri: predicate,
|
|
131
|
+
name: Ontology.nameFor(fact.assertion.predicate),
|
|
132
|
+
label: fact.assertion.predicate,
|
|
133
|
+
...(fact.assertion.predicateDescription ? { description: fact.assertion.predicateDescription } : {}),
|
|
134
|
+
...(Vocab.brainNamespaceOf(predicate) ? { definedBy: Vocab.brainNamespaceOf(predicate) } : {}),
|
|
135
|
+
};
|
|
136
|
+
quads.push(...Ontology.toQuads(definition));
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const about = (name: Rdf.NamedNode, value: Rdf.NamedNode | Rdf.Literal) => quads.push(quad(graph, name, value, meta));
|
|
140
|
+
about(namedNode(Vocab.RDF_TYPE), namedNode(Vocab.SX_GRAPH));
|
|
141
|
+
about(Vocab.sx('factuality'), Vocab.str(fact.factuality.value));
|
|
142
|
+
about(Vocab.sx('polarity'), Vocab.str(fact.factuality.polarity));
|
|
143
|
+
about(Vocab.prov('wasDerivedFrom'), Vocab.str(fact.attribution.source));
|
|
144
|
+
about(Vocab.prov('generatedAtTime'), Vocab.str(fact.attribution.generatedAtTime));
|
|
145
|
+
about(Vocab.sx('recordedAt'), Vocab.str(fact.recordedAt));
|
|
146
|
+
about(Vocab.sx('sourceHash'), Vocab.str(fact.sourceHash));
|
|
147
|
+
about(Vocab.sx('extractorId'), Vocab.str(fact.extractor.id));
|
|
148
|
+
about(Vocab.sx('extractorModel'), Vocab.str(fact.extractor.model));
|
|
149
|
+
about(Vocab.sx('extractorVersion'), Vocab.str(fact.extractor.version));
|
|
150
|
+
const optional: ReadonlyArray<[Rdf.NamedNode, string | undefined]> = [
|
|
151
|
+
[Vocab.sx('confidence'), fact.factuality.confidence?.toString()],
|
|
152
|
+
[Vocab.sx('nature'), fact.factuality.nature],
|
|
153
|
+
[Vocab.sx('validFrom'), fact.assertion.validFrom],
|
|
154
|
+
[Vocab.sx('validTo'), fact.assertion.validTo],
|
|
155
|
+
[Vocab.sx('quote'), fact.assertion.quote],
|
|
156
|
+
[Vocab.sx('agentLabel'), fact.attribution.agentLabel],
|
|
157
|
+
[Vocab.sx('spanStart'), fact.attribution.span?.start.toString()],
|
|
158
|
+
[Vocab.sx('spanEnd'), fact.attribution.span?.end.toString()],
|
|
159
|
+
[Vocab.sx('force'), fact.illocution?.force],
|
|
160
|
+
[Vocab.sx('mood'), fact.illocution?.mood],
|
|
161
|
+
[Vocab.sx('addressee'), fact.illocution?.addressee],
|
|
162
|
+
[Vocab.sx('pass'), fact.pass],
|
|
163
|
+
[Vocab.sx('kind'), fact.kind],
|
|
164
|
+
[Vocab.sx('origin'), fact.origin],
|
|
165
|
+
];
|
|
166
|
+
for (const [name, value] of optional) {
|
|
167
|
+
if (value !== undefined) {
|
|
168
|
+
about(name, Vocab.str(value));
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (fact.attribution.agent) {
|
|
172
|
+
about(Vocab.prov('wasAttributedTo'), Vocab.entityNode(fact.attribution.agent, graph.value));
|
|
173
|
+
}
|
|
174
|
+
for (const derived of fact.attribution.wasDerivedFrom ?? []) {
|
|
175
|
+
about(Vocab.sx('derivedFrom'), Vocab.str(derived));
|
|
176
|
+
}
|
|
177
|
+
for (const superseded of fact.supersedes ?? []) {
|
|
178
|
+
about(Vocab.sx('supersedes'), namedNode(superseded));
|
|
179
|
+
}
|
|
180
|
+
for (const retracted of fact.retracts ?? []) {
|
|
181
|
+
about(Vocab.sx('retracts'), namedNode(retracted));
|
|
182
|
+
}
|
|
183
|
+
return quads;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
/** Reassembles (and validates) facts from quads: the inverse of {@link factToTriples}, one fact per statement. */
|
|
187
|
+
export const triplesToFacts = (
|
|
188
|
+
quads: readonly Rdf.Quad[],
|
|
189
|
+
ontology: Ontology.Ontology = Ontology.DEFAULT,
|
|
190
|
+
): Fact.Fact[] => {
|
|
191
|
+
const labels = new Map<string, string>();
|
|
192
|
+
const comments = new Map<string, string>();
|
|
193
|
+
const provenance = new Map<string, Map<string, Rdf.Term[]>>();
|
|
194
|
+
const graphs = new Map<
|
|
195
|
+
string,
|
|
196
|
+
{ statements: Rdf.Quad[]; labels: Map<string, string>; types: Map<string, Fact.EntityKind> }
|
|
197
|
+
>();
|
|
198
|
+
const graphFor = (iri: string) => {
|
|
199
|
+
let graph = graphs.get(iri);
|
|
200
|
+
if (!graph) {
|
|
201
|
+
graphs.set(iri, (graph = { statements: [], labels: new Map(), types: new Map() }));
|
|
202
|
+
}
|
|
203
|
+
return graph;
|
|
204
|
+
};
|
|
205
|
+
|
|
206
|
+
for (const entry of quads) {
|
|
207
|
+
if (entry.graph.termType === 'DefaultGraph') {
|
|
208
|
+
let props = provenance.get(entry.subject.value);
|
|
209
|
+
if (!props) {
|
|
210
|
+
provenance.set(entry.subject.value, (props = new Map()));
|
|
211
|
+
}
|
|
212
|
+
const name = localName(entry.predicate.value);
|
|
213
|
+
props.set(name, [...(props.get(name) ?? []), entry.object]);
|
|
214
|
+
} else if (entry.graph.value === Vocab.ONTOLOGY_GRAPH) {
|
|
215
|
+
const known =
|
|
216
|
+
entry.predicate.value === Vocab.RDFS_LABEL
|
|
217
|
+
? labels
|
|
218
|
+
: entry.predicate.value === Vocab.RDFS_COMMENT
|
|
219
|
+
? comments
|
|
220
|
+
: undefined;
|
|
221
|
+
if (known && !known.has(entry.subject.value)) {
|
|
222
|
+
known.set(entry.subject.value, entry.object.value);
|
|
223
|
+
}
|
|
224
|
+
} else if (entry.predicate.value === Vocab.RDFS_LABEL) {
|
|
225
|
+
const graph = graphFor(entry.graph.value);
|
|
226
|
+
if (!graph.labels.has(entry.subject.value)) {
|
|
227
|
+
graph.labels.set(entry.subject.value, entry.object.value);
|
|
228
|
+
}
|
|
229
|
+
} else {
|
|
230
|
+
const kind = entry.predicate.value === Vocab.RDF_TYPE ? KINDS.get(entry.object.value) : undefined;
|
|
231
|
+
if (kind) {
|
|
232
|
+
graphFor(entry.graph.value).types.set(entry.subject.value, kind);
|
|
233
|
+
} else {
|
|
234
|
+
graphFor(entry.graph.value).statements.push(entry);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const facts: Fact.Fact[] = [];
|
|
240
|
+
for (const [iri, graph] of graphs) {
|
|
241
|
+
const props = provenance.get(iri) ?? new Map<string, Rdf.Term[]>();
|
|
242
|
+
const one = (name: string) => props.get(name)?.[0]?.value;
|
|
243
|
+
const many = (name: string) => props.get(name)?.map((term) => term.value) ?? [];
|
|
244
|
+
const term = (node: Rdf.Term): Fact.Term | undefined =>
|
|
245
|
+
node.termType === 'Literal'
|
|
246
|
+
? { kind: 'literal', literal: node.value }
|
|
247
|
+
: node.termType === 'NamedNode'
|
|
248
|
+
? {
|
|
249
|
+
kind: 'entity',
|
|
250
|
+
entity: Vocab.entityKey(node.value, iri),
|
|
251
|
+
...(graph.labels.has(node.value) ? { label: graph.labels.get(node.value) } : {}),
|
|
252
|
+
...(graph.types.has(node.value) ? { type: graph.types.get(node.value) } : {}),
|
|
253
|
+
}
|
|
254
|
+
: undefined;
|
|
255
|
+
const agent = props.get('wasAttributedTo')?.[0];
|
|
256
|
+
const derivedFrom = many('derivedFrom');
|
|
257
|
+
const supersedes = many('supersedes');
|
|
258
|
+
const retracts = many('retracts');
|
|
259
|
+
const spanStart = one('spanStart');
|
|
260
|
+
const spanEnd = one('spanEnd');
|
|
261
|
+
const force = one('force');
|
|
262
|
+
const own = iri.startsWith(Vocab.FACT);
|
|
263
|
+
|
|
264
|
+
for (const statement of graph.statements) {
|
|
265
|
+
const predicate = statement.predicate.value;
|
|
266
|
+
// An untyped candidate; decoding validates required fields and literal unions, so missing provenance throws.
|
|
267
|
+
const candidate = {
|
|
268
|
+
id: own ? Vocab.factIdFromIri(iri) : statementId(iri, statement.subject, predicate, statement.object),
|
|
269
|
+
...(own ? {} : { graph: iri }),
|
|
270
|
+
assertion: {
|
|
271
|
+
subject: term(statement.subject),
|
|
272
|
+
predicate: labels.get(predicate) ?? ontology.get(predicate)?.label ?? Ontology.labelFromIri(predicate),
|
|
273
|
+
predicateIri: predicate,
|
|
274
|
+
...(comments.has(predicate) && !ontology.get(predicate)
|
|
275
|
+
? { predicateDescription: comments.get(predicate) }
|
|
276
|
+
: {}),
|
|
277
|
+
object: term(statement.object),
|
|
278
|
+
...(one('validFrom') !== undefined ? { validFrom: one('validFrom') } : {}),
|
|
279
|
+
...(one('validTo') !== undefined ? { validTo: one('validTo') } : {}),
|
|
280
|
+
...(one('quote') !== undefined ? { quote: one('quote') } : {}),
|
|
281
|
+
},
|
|
282
|
+
factuality: {
|
|
283
|
+
value: one('factuality'),
|
|
284
|
+
polarity: one('polarity'),
|
|
285
|
+
...(one('confidence') !== undefined ? { confidence: Number(one('confidence')) } : {}),
|
|
286
|
+
...(one('nature') !== undefined ? { nature: one('nature') } : {}),
|
|
287
|
+
},
|
|
288
|
+
...(force !== undefined
|
|
289
|
+
? {
|
|
290
|
+
illocution: {
|
|
291
|
+
force,
|
|
292
|
+
...(one('mood') !== undefined ? { mood: one('mood') } : {}),
|
|
293
|
+
...(one('addressee') !== undefined ? { addressee: one('addressee') } : {}),
|
|
294
|
+
},
|
|
295
|
+
}
|
|
296
|
+
: {}),
|
|
297
|
+
attribution: {
|
|
298
|
+
...(agent !== undefined ? { agent: Vocab.entityKey(agent.value, iri) } : {}),
|
|
299
|
+
...(one('agentLabel') !== undefined ? { agentLabel: one('agentLabel') } : {}),
|
|
300
|
+
source: one('wasDerivedFrom'),
|
|
301
|
+
generatedAtTime: one('generatedAtTime'),
|
|
302
|
+
...(derivedFrom.length > 0 ? { wasDerivedFrom: derivedFrom } : {}),
|
|
303
|
+
...(spanStart !== undefined && spanEnd !== undefined
|
|
304
|
+
? { span: { start: Number(spanStart), end: Number(spanEnd) } }
|
|
305
|
+
: {}),
|
|
306
|
+
},
|
|
307
|
+
recordedAt: one('recordedAt'),
|
|
308
|
+
extractor: { id: one('extractorId'), model: one('extractorModel'), version: one('extractorVersion') },
|
|
309
|
+
sourceHash: one('sourceHash'),
|
|
310
|
+
...(one('pass') !== undefined ? { pass: one('pass') } : {}),
|
|
311
|
+
...(one('kind') !== undefined ? { kind: one('kind') } : {}),
|
|
312
|
+
...(one('origin') !== undefined ? { origin: one('origin') } : {}),
|
|
313
|
+
...(supersedes.length > 0 ? { supersedes } : {}),
|
|
314
|
+
...(retracts.length > 0 ? { retracts } : {}),
|
|
315
|
+
};
|
|
316
|
+
facts.push(decodeFact(candidate));
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
return facts;
|
|
320
|
+
};
|