eyemind 0.8.10 → 0.8.12

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/LE-SPEC.md CHANGED
@@ -125,6 +125,79 @@ keeps integer and float terms distinct. Calculations belong in premises,
125
125
  followed by relational conclusions. These calculations do not prove arbitrary
126
126
  algebraic identities or supply induction.
127
127
 
128
+ ## Alternative premises
129
+
130
+ ```text
131
+ Every cat or dog is a pet.
132
+ For every V, if V is registered or V is a provisionalVoter and V is verified
133
+ then V is an eligibleVoter.
134
+ For every X, it is impossible that X is a minor and X is a voter or X is barred.
135
+ ```
136
+
137
+ `or` separates alternative premises of a rule or contradiction. `and` binds
138
+ more tightly, so each alternative is a conjunction; there are no parentheses
139
+ around alternatives. Each alternative compiles to its own LS clause with the
140
+ same conclusion, in the order written: `Every A or B is C.` is
141
+ `implies(A(X), C(X))` followed by `implies(B(X), C(X))`. The sentence's
142
+ declarations are shared, and an alternative need not use every declared
143
+ variable, but each conclusion variable must be supplied by every alternative
144
+ or introduced with `there exists`. An existential conclusion therefore has a
145
+ witness per alternative and firing. The order of premises is checked within
146
+ each alternative.
147
+
148
+ Questions have no `or`: ask one question per alternative, or ask for a relation
149
+ that an `or` rule derives. Conclusions are never disjunctive.
150
+
151
+ ## Aggregates
152
+
153
+ ```text
154
+ For every M and T,
155
+ if M is a member and T is the sum of F for every C and F such that charge(M, C, F)
156
+ then T is an amountDue of M.
157
+ For every V and N,
158
+ if V is a participant and N is the number of C such that choice(V, C) and N is at least 2
159
+ then V is a spoiledVoter.
160
+ For every C and N,
161
+ if C is a candidate and N is the number of V such that
162
+ (choice(V, C) and V is an eligibleVoter and it is not known that V is a spoiledVoter)
163
+ then N is a voteCount of C.
164
+ ```
165
+
166
+ These premise forms collect every solution of a condition:
167
+
168
+ | LE form | Result |
169
+ | --- | --- |
170
+ | `N is the number of X1 and ... such that C` | The number of distinct combinations of `X1 ...` satisfying `C`. |
171
+ | `S is the sum of V for every X1 and ... such that C` | The sum of `V` over those combinations; `0` when there are none. |
172
+ | `M is the maximum of V for every X1 and ... such that C` | The greatest `V` over those combinations; no result when there are none. |
173
+ | `M is the minimum of V for every X1 and ... such that C` | The least `V` over those combinations; no result when there are none. |
174
+
175
+ The variables after `the number of` or `for every` are declared by the
176
+ aggregate, must be new in the sentence, and cannot be used after it. `V` must
177
+ be one of them. The condition `C` is one statement, or a conjunction in
178
+ parentheses; it can use calculations, comparisons and absence tests, but no
179
+ `or` and no other aggregate. Any other variable of `C` must be supplied by an earlier premise. The
180
+ left-hand side is a declared variable or a number, as in the value-of form:
181
+ an unbound variable receives the result and a bound one is compared with it.
182
+
183
+ Each distinct combination of the declared variables' values counts once, however
184
+ many derivations it has. To count distinct values of one variable when the
185
+ condition needs others, derive a relation of that variable first, as
186
+ `choice(V, C)` above does for ballots. Values are summed with LS arithmetic, so
187
+ integers stay exact and a float makes the sum a float; maximum and minimum
188
+ compare numerically.
189
+
190
+ An aggregate compiles to
191
+ `findall_sorted(t(V, X1, ...), C, L)`, followed by goals of backward helper
192
+ relations named `le:distinct`, `le:count`, `le:sum`, `le:max` and `le:min`,
193
+ which remove repeated tuples and fold the rest. The helpers are added after
194
+ the program's own clauses, only when used, so they do not change the rule
195
+ numbers of witnesses; their names cannot be written as LE names. Like a
196
+ negation, an aggregate waits until the relations of its condition are complete,
197
+ so a program cannot aggregate over a relation that depends on the aggregate's
198
+ own result. Questions cannot aggregate: derive the aggregate in a rule and ask
199
+ for its conclusion.
200
+
128
201
  ## Absence and existential conclusions
129
202
 
130
203
  An absence test has exactly this form, followed by one positive statement:
@@ -186,6 +259,9 @@ These programs use the grammar above without additional syntax:
186
259
  | [Pell equations](https://eyereasoner.github.io/eyemind/examples/deck/pell-equation-le) | Recursive arithmetic beyond binary64 integer precision and exact polynomial checks. |
187
260
  | [Course certificates](https://eyereasoner.github.io/eyemind/examples/deck/course-certificates-le) | Recursive requirements, absence of a counterexample, shared existential witnesses and witnesses containing earlier witnesses. |
188
261
  | [Symbolic differentiation](https://eyereasoner.github.io/eyemind/examples/deck/symbolic-derivative-le) | Structural recursion over expression trees, constructed derivatives and evaluation at separate points. |
262
+ | [British citizenship](https://eyereasoner.github.io/eyemind/examples/deck/british-citizenship-le) | Section 1 of the British Nationality Act 1981: alternative premises, a presumption that holds unless the contrary is shown, date comparisons and citizenship through a derived parent. |
263
+ | [Library fines](https://eyereasoner.github.io/eyemind/examples/deck/library-fines-le) | Charges from alternative rules, then the sum, number and maximum per member over distinct combinations. |
264
+ | [Election tally](https://eyereasoner.github.io/eyemind/examples/deck/election-tally-le) | Eligibility by alternatives, distinct choices, an absence test inside a count, winners by maximum with ties, and a quorum. |
189
265
 
190
266
  Their decks state the modeled assumptions and what the checked proofs establish.
191
267
  Bounds and finite expression domains are explicit in the programs.
package/LS-SPEC.md CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ```text
4
4
  Title: The Logic Script (LS) Language and Proof Format
5
- Version: eyemind 0.8.10
5
+ Version: eyemind 0.8.12
6
6
  ```
7
7
 
8
8
  This document specifies Logic Script (LS): the syntax and meaning of its
package/README.md CHANGED
@@ -12,8 +12,8 @@ program, a JavaScript library and a browser application.
12
12
 
13
13
  [Playground](https://eyereasoner.github.io/eyemind/playground/) · [Examples](https://eyereasoner.github.io/eyemind/examples/) · [Specification](https://eyereasoner.github.io/eyemind/LS-SPEC) · [Conformance](https://eyereasoner.github.io/eyemind/conformance/)
14
14
 
15
- The examples are 152 runnable programs, each with a card deck and saved
16
- conclusions, proofs and check reports. The specification defines the language,
15
+ The examples are 156 runnable programs, each with a card deck and saved
16
+ conclusions; all but one streamed example also save proofs and check reports. The specification defines the language,
17
17
  the proof format and how a proof is checked; its conformance suite has
18
18
  1247 cases that test language and proof behavior, and runs
19
19
  [in your browser](https://eyereasoner.github.io/eyemind/playground/conformance).
@@ -227,16 +227,20 @@ its scope. Terms also include exact integers, floats (`1.0`, `1e3`), lists
227
227
  | `For every X, ...` | a universally quantified fact |
228
228
  | `Every A is B.` | `implies(A(X), B(X))` |
229
229
  | `For every X and Y, if ... then ...` | a forward rule |
230
+ | `if A and B or C then ...`, `Every A or B is C.` | one rule per alternative; `and` binds more tightly |
230
231
  | `Y is the value of E` | `is(Y, E)`, with `+ - * /` and parentheses |
231
232
  | `X is greater than`, `less than`, `at least`, `at most`, `equal to`, `different from E` | `gt`, `lt`, `ge`, `le`, `eq`, `ne` |
232
233
  | `it is not known that S` | `not(S)`, with LS's stratified meaning |
234
+ | `N is the number of X such that S` | a count of the distinct values of `X` |
235
+ | `T is the sum`, `maximum`, `minimum of V for every X and V such that S` | a fold of `V` over the distinct combinations |
233
236
  | `then there exists Y such that ...` | a Skolem witness of the firing |
234
237
  | `Find every X such that ...`, `Find whether ...` | `query(...)` |
235
238
  | `It is impossible that ...`, `Contradiction.` | `contradiction(...)` |
236
239
 
237
- Arithmetic and absence tests need their inputs from earlier premises, and
238
- arithmetic follows LS: integers stay exact, division gives a float. An
239
- answer becomes a sentence such as `16 is a square of 4.`; a witness is written
240
+ Arithmetic, absence tests and aggregates need their inputs from earlier
241
+ premises; an aggregate's own variables stay inside it, and a conjunction in its
242
+ condition is put in parentheses. Arithmetic follows LS: integers stay exact,
243
+ division gives a float. An answer becomes a sentence such as `16 is a square of 4.`; a witness is written
240
244
  `witness(rule 4, Y, Alice)`, and conclusions read back as facts of another LE
241
245
  program. Several `.le` files form one program, `--to le` writes LS conclusions
242
246
  as LE, and proofs check the compiled LS, not the English-to-LS translation.
@@ -248,8 +252,11 @@ mapped to the `eval:` and `odrl:` vocabularies; see the
248
252
  [handicap-card LE deck](https://eyereasoner.github.io/eyemind/examples/deck/handicap-card-le).
249
253
  For larger programs, try
250
254
  [shortest paths](https://eyereasoner.github.io/eyemind/examples/deck/shortest-path-le),
251
- [river-crossing plans](https://eyereasoner.github.io/eyemind/examples/deck/river-crossing-le) or
252
- [Pell equations](https://eyereasoner.github.io/eyemind/examples/deck/pell-equation-le).
255
+ [river-crossing plans](https://eyereasoner.github.io/eyemind/examples/deck/river-crossing-le),
256
+ [Pell equations](https://eyereasoner.github.io/eyemind/examples/deck/pell-equation-le),
257
+ [British citizenship](https://eyereasoner.github.io/eyemind/examples/deck/british-citizenship-le),
258
+ [library fines](https://eyereasoner.github.io/eyemind/examples/deck/library-fines-le) or an
259
+ [election tally](https://eyereasoner.github.io/eyemind/examples/deck/election-tally-le).
253
260
 
254
261
  ### RDF 1.2
255
262
 
@@ -295,6 +302,32 @@ standard input or HTTP(S) incrementally. See the
295
302
  [message-boundaries](https://eyereasoner.github.io/eyemind/examples/deck/message-boundaries-n3)
296
303
  and [message-alerts](https://eyereasoner.github.io/eyemind/examples/deck/message-alerts-n3) decks.
297
304
 
305
+ With an [RDF logic profile](#rdf-logic-profiles), `--stream-messages` treats
306
+ the other inputs as a background graph and writes, for each message, the
307
+ triples it adds to what the background alone concludes. The background is
308
+ closed once. Each shape of message, its properties and whether its values
309
+ are IRIs or literals of a datatype, gets the rule instances that can use one
310
+ of its triples, with background facts unfolded where they fix a property or
311
+ class; later messages of that shape only add their own facts. The result is
312
+ the same as running the whole program over the background and the message,
313
+ and the whole program runs instead when new triples could have any property,
314
+ as `owl:sameAs` between individuals makes, or when a negation could retract
315
+ a background conclusion. `--program` prints the specialized program of each
316
+ new shape, and `--stats` says whether each message used one. Streamed
317
+ messages have no proofs.
318
+
319
+ An [output contract](#rdf-logic-profiles) prunes the specialized rules to
320
+ those that can contribute to a triple it asks for, following premises back
321
+ to the rules concluding them. A value counts only where a reader uses it:
322
+ a triple that `owl:sameAs` copies to an equal object serves no rule that
323
+ reads only its subject. See the
324
+ [observations](https://eyereasoner.github.io/eyemind/examples/deck/observations-owl2rl) deck.
325
+
326
+ ```sh
327
+ eyemind --owl2rl --stream-messages ontology.ttl messages.trig # each message's new triples
328
+ eyemind --owl2rl --stream-messages ontology.ttl contract.js messages.trig # only those the contract asks for
329
+ ```
330
+
298
331
  ### SPARQL 1.2 RL
299
332
 
300
333
  A `.srl` file is a [SPARQL 1.2 RL](https://www.w3.org/TR/sparql12-rl/) rule
@@ -317,6 +350,16 @@ Four switches add standalone LS rules to RDF inputs; vocabulary alone never
317
350
  enables one. They combine, inference closes before validation, and the output
318
351
  is Turtle with the new triples and reports.
319
352
 
353
+ A `.js` input is an output contract: a Logic Script program whose statements
354
+ are `query(rdf(S, P, O))` or `query(rdf(S, P, O, G))`. The output then holds
355
+ only the new triples matching one of them, and a proof answers those queries.
356
+
357
+ ```js
358
+ const sosa = (name) => iri('http://www.w3.org/ns/sosa/' + name);
359
+ query(rdf(O, sosa('madeBySensor'), S));
360
+ query(rdf(O, iri('http://www.w3.org/1999/02/22-rdf-syntax-ns#type'), sosa('Observation')));
361
+ ```
362
+
320
363
  - `--owl2rl` adds the RDFS and [OWL 2 RL](https://www.w3.org/TR/owl2-profiles/#Reasoning_in_OWL_2_RL_and_RDF_Graphs)
321
364
  rules, including the datatype map of its table 8: value equality and
322
365
  difference, membership across lexical forms, and ill-typed literals for the
@@ -517,7 +560,7 @@ eyemind [OPTION ...] [FILE ...]
517
560
  | `--json` | With a checking mode, print the report as JSON. |
518
561
  | `--goal GOAL` | Ask `GOAL` after forward reasoning; may be repeated. |
519
562
  | `--unused` | List the clauses that make no difference to the conclusions. |
520
- | `--stream-messages` | Read an RDF Message Log and run the rules independently per message; Node reads files, stdin and HTTP(S) incrementally. |
563
+ | `--stream-messages` | Read an RDF Message Log and run the rules independently per message; Node reads files, stdin and HTTP(S) incrementally. With an RDF logic profile, the other inputs are a background closed once and the rules are specialized per message shape. |
521
564
  | `--stats` | Print reasoning statistics as JSON to standard error. |
522
565
  | `--max-depth N`, `--max-iterations N`, `--max-inferences N` | Bound backward depth (1000000), forward rounds per stratum (1000) and goals started (1000000); a compiled N3, SRL, ODRL or RDF profile program has 1000000, 100000 and 1000000000. |
523
566
  | `--to FORMAT` | Write the conclusions as `turtle`, `trig`, `n-triples`, `n-quads`, `n3`, `le` or `eyemind`; by default LE for LE, N3 for N3, Turtle for SRL, ODRL and RDF profiles, eyemind otherwise. |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eyemind",
3
- "version": "0.8.10",
3
+ "version": "0.8.12",
4
4
  "description": "A dependency-free Logic Script (LS) reasoner with forward and backward reasoning, checkable proofs, and RDF 1.2, Notation3 (N3), SPARQL 1.2 RL (SRL) and Logical English (LE) interoperability.",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
package/src/cli.js CHANGED
@@ -1,8 +1,10 @@
1
1
  // The eyemind command line.
2
2
  import { run, unusedClauses } from './engine.js';
3
- import { OUTPUT_FORMATS, RDF_PROFILES, defaultFormat, inputKind, loadInputs, rdfMessageFormat, scopeOf, writeConclusions } from './formats.js';
3
+ import { OUTPUT_FORMATS, RDF_PROFILES, defaultFormat, inputKind, loadInputs, loadMessageReasoner, rdfMessageFormat, scopeOf, writeConclusions } from './formats.js';
4
4
  import { isRdfMessageLog, replayMessages, RdfMessageReader } from './messages.js';
5
5
  import { checkProof, checkReport, publicReport } from './proof.js';
6
+ import { writeRdf } from './rdf.js';
7
+ import { programText } from './specialize.js';
6
8
  import { EyemindError } from './terms.js';
7
9
  import { VERSION } from './version.js';
8
10
  import { srlContextFromProof } from './srl.js';
@@ -46,6 +48,8 @@ const ALIASES = new Map([['-p', '--proof'], ['-c', '--prove-and-check'], ['-v',
46
48
  // N3 and SPARQL 1.2 RL programs run longer than eyemind's defaults allow.
47
49
  const COMPILED_LIMITS = { maxDepth: 1000000, maxIterations: 100000, maxInferences: 1000000000 };
48
50
 
51
+ const RDF_FORMATS = ['turtle', 'trig', 'n-triples', 'n-quads'];
52
+
49
53
  class UsageError extends Error {}
50
54
 
51
55
  function readSource(path, io) {
@@ -149,17 +153,37 @@ export function main(argv, io) {
149
153
  if (!files.length) files.push('-');
150
154
  if (files.filter((file) => file === '-').length > 1) throw new UsageError('stdin can only be read once');
151
155
  if (streamMessages) {
156
+ const profiles = RDF_PROFILES.filter((p) => rdfLogic[p]);
152
157
  if (odrl) throw new UsageError('--odrl cannot be combined with --stream-messages');
153
- if (Object.keys(rdfLogic).length) throw new UsageError(`${Object.keys(rdfLogic).map((p) => '--' + p).join(' ')} cannot be combined with --stream-messages`);
154
- if (proofFile !== null || options.proof || proveAndCheck || printProgram || unused) {
155
- throw new UsageError('--stream-messages cannot be combined with --proof, --check-proof, --prove-and-check, --program or --unused; replay the whole log to prove its conclusions');
158
+ if (proofFile !== null || options.proof || proveAndCheck || unused || (printProgram && !profiles.length)) {
159
+ throw new UsageError('--stream-messages cannot be combined with --proof, --check-proof, --prove-and-check or --unused, ' +
160
+ 'nor with --program without an RDF logic profile; replay the whole log to prove its conclusions');
161
+ }
162
+ if (profiles.length && format !== null && !RDF_FORMATS.includes(format)) {
163
+ throw new UsageError(`--stream-messages with ${profiles.map((p) => '--' + p).join(' ')} writes ${RDF_FORMATS.join(', ')}, not ${format}`);
156
164
  }
157
165
  const args = argv.filter((arg, i) => arg !== '--stream-messages' && !filePositions.has(i));
158
166
  let messageNumber = 0;
167
+ // The logic profiles close the other inputs once and reason over each
168
+ // message with rules specialized to its shape (rdf-logic.js).
169
+ let reasoner = null;
170
+ const infer = (inputs, message) => {
171
+ reasoner ??= loadMessageReasoner(inputs.map(([path, text]) => [text, path === '-' ? '<stdin>' : path]), io, { base, ...rdfLogic });
172
+ const result = reasoner.infer(message.quads, { ...COMPILED_LIMITS, ...options });
173
+ messageNumber++;
174
+ if (printProgram) {
175
+ if (result.sources) stdout(`// The rules specialized for message ${message.index} and those shaped like it, without their facts.\n${programText(result.sources)}`);
176
+ else if (!result.specialized && messageNumber === 1) stdout(`// These rules are not specialized: ${reasoner.reason ?? 'a message has a shape they cannot be specialized to'}.\n`);
177
+ return 0;
178
+ }
179
+ stdout(writeRdf(result.quads, { format: format ?? 'turtle', prefixes: { ...reasoner.prefixes, ...message.prefixes }, blankNodePrefix: `m${messageNumber}_` }));
180
+ if (stats) stderr(`${JSON.stringify({ message: message.index, specialized: result.specialized, ...result.stats })}\n`);
181
+ return result.haltCode || 0;
182
+ };
159
183
  const request = {
160
184
  files, base,
161
185
  scope: (name, index = files.indexOf(name)) => scopeOf(files.map((f) => ['', f]), name, index),
162
- run: (inputs, name, replay) => main([...args, ...inputs.map(([path]) => path), name], {
186
+ run: (inputs, name, replay, message) => profiles.length ? infer(inputs, message) : main([...args, ...inputs.map(([path]) => path), name], {
163
187
  ...io,
164
188
  // Each result is serialized separately; keep its blank labels
165
189
  // distinct without retaining nodes from previous messages.
@@ -180,7 +204,7 @@ export function main(argv, io) {
180
204
  let code = 0;
181
205
  const scope = request.scope(name, index);
182
206
  const reader = new RdfMessageReader({ format: rdfMessageFormat(name), base: base ?? io.fileIri?.(name), scope, onMessage: (message) => {
183
- code = request.run(inputs, name, replayMessages([message], { scope }));
207
+ code = request.run(inputs, name, replayMessages([message], { scope }), message);
184
208
  return code === 0;
185
209
  } });
186
210
  reader.write(text);
package/src/formats.js CHANGED
@@ -19,14 +19,14 @@
19
19
  // The compiled program is ordinary LS: proofs, checking and --unused
20
20
  // apply to it. SRL execution functions capture a context that a saved proof
21
21
  // retains; compiling with that context reproduces the same program later.
22
- import { EyemindError, Struct } from './terms.js';
22
+ import { EyemindError, Struct, Var } from './terms.js';
23
23
  import { loadSources } from './lang.js';
24
24
  import { readTerms } from './reader.js';
25
25
  import { DEFAULT_GRAPH, parseRdf, rdfDocument, rdfFormat, writeRdf } from './rdf.js';
26
26
  import { resultToN3, compileSources } from './n3.js';
27
27
  import { compileSrl, srlAnswers, srlQuads } from './srl.js';
28
28
  import { compileOdrl } from './odrl.js';
29
- import { compileRdfLogic, RDF_PROFILES } from './rdf-logic.js';
29
+ import { compileRdfLogic, messageReasoner, RDF_PROFILES } from './rdf-logic.js';
30
30
  import { compileLe, writeLe } from './le.js';
31
31
  import { write } from './writer.js';
32
32
  import { isRdfMessageLog, parseRdfMessages } from './messages.js';
@@ -63,7 +63,52 @@ export function loadInputs(inputs, io = null, options = {}) {
63
63
  }
64
64
  }
65
65
 
66
- function load(inputs, io, { base = null, messageInputs = null, srlContext = null, odrl = false, owl2rl = false, qudt = false, skos = false, shacl = false }) {
66
+ // The RDF inputs of the logic profiles as one graph, and the output contract
67
+ // a Logic Script input states, if any.
68
+ function profileGraph(inputs, kinds, rdf, baseOf, switches, io) {
69
+ if (inputs.some(([, name], k) => kinds[k] !== 'rdf' && kinds[k] !== 'js' && name !== '<stdin>')) {
70
+ throw new EyemindError(`${switches} takes RDF inputs, and a Logic Script output contract; standard input is Turtle`);
71
+ }
72
+ const contracts = inputs.filter(([, name], k) => kinds[k] === 'js' && name !== '<stdin>');
73
+ const wanted = contracts.length ? contracts.flatMap(([text, name]) => outputContract(text, name, io)) : null;
74
+ if (contracts.length && !wanted.length) throw new EyemindError(`${switches} needs a query(rdf(S, P, O)) in its output contract`);
75
+ const prefixes = {}, quads = [];
76
+ inputs.forEach(([text, name], k) => {
77
+ if (kinds[k] === 'js' && name !== '<stdin>') return;
78
+ const parsed = name === '<stdin>' ? parseRdf(text, { format: 'turtle', base: baseOf(name), scope: scopeOf(inputs, name, k) }) : rdf(text, name, k);
79
+ if (parsed.triples) throw new EyemindError(`${switches} takes RDF graphs, not RDF Message Logs`);
80
+ Object.assign(prefixes, parsed.prefixes);
81
+ quads.push(...parsed.quads);
82
+ });
83
+ return { quads, prefixes, wanted };
84
+ }
85
+
86
+ // The triple patterns an output contract asks for: a Logic Script program
87
+ // whose statements are query(rdf(S, P, O)) or query(rdf(S, P, O, G)).
88
+ function outputContract(text, name, io) {
89
+ const program = loadSources([[text, name]], io);
90
+ return program.clauses.map((clause) => {
91
+ const goal = clause.forward && clause.heads.length === 1 && clause.heads[0] === 'true' && clause.statedBody.length === 1
92
+ ? clause.statedBody[0] : null;
93
+ if (!(goal instanceof Struct) || goal.name !== 'rdf' || ![3, 4].includes(goal.args.length)) {
94
+ throw new EyemindError(`${name}: an output contract states only query(rdf(S, P, O)) or query(rdf(S, P, O, G))${clause.line ? `, not line ${clause.line}` : ''}`);
95
+ }
96
+ return goal.args.length === 4 ? goal : new Struct('rdf', [...goal.args, new Var(`G#contract${clause.id}`)]);
97
+ });
98
+ }
99
+
100
+ // The reasoner the logic profiles run each RDF Message with, given the
101
+ // background inputs as [text, name] pairs (rdf-logic.js).
102
+ export function loadMessageReasoner(inputs, io = null, options = {}) {
103
+ try {
104
+ return load(inputs, io, { ...options, messages: true });
105
+ } catch (error) {
106
+ if (error instanceof EyemindError || !(error instanceof Error) || error instanceof TypeError || error instanceof ReferenceError) throw error;
107
+ throw new EyemindError(error.message);
108
+ }
109
+ }
110
+
111
+ function load(inputs, io, { base = null, messageInputs = null, srlContext = null, odrl = false, owl2rl = false, qudt = false, skos = false, shacl = false, messages = false }) {
67
112
  const options = { owl2rl, qudt, skos, shacl }, profiles = RDF_PROFILES.filter((p) => options[p]);
68
113
  const switches = profiles.map((p) => '--' + p).join(' ');
69
114
  const kinds = inputs.map(([text, name]) => messageInputs?.has(name) ||
@@ -82,17 +127,10 @@ function load(inputs, io, { base = null, messageInputs = null, srlContext = null
82
127
  }
83
128
  if (profiles.length && odrl) throw new EyemindError(`--odrl cannot be combined with ${switches}`);
84
129
  if (profiles.length) {
85
- if (inputs.some(([, name], k) => kinds[k] !== 'rdf' && name !== '<stdin>')) {
86
- throw new EyemindError(`${switches} takes RDF inputs; standard input is Turtle`);
87
- }
88
- const prefixes = {}, quads = [];
89
- inputs.forEach(([text, name], k) => {
90
- const parsed = name === '<stdin>' ? parseRdf(text, { format: 'turtle', base: baseOf(name), scope: scopeOf(inputs, name, k) }) : rdf(text, name, k);
91
- if (parsed.triples) throw new EyemindError(`${switches} takes RDF graphs, not RDF Message Logs`);
92
- Object.assign(prefixes, parsed.prefixes);
93
- quads.push(...parsed.quads);
94
- });
95
- return compileRdfLogic({ quads, prefixes }, { profiles, source: inputs.map(([, n]) => n).join(', '), context: srlContext });
130
+ const graph = profileGraph(inputs, kinds, rdf, baseOf, switches, io);
131
+ const source = inputs.map(([, n]) => n).join(', ');
132
+ if (messages) return messageReasoner(graph, { profiles, source });
133
+ return compileRdfLogic(graph, { profiles, source, context: srlContext });
96
134
  }
97
135
  if (odrl) {
98
136
  if (inputs.length !== 1) throw new EyemindError('--odrl takes one Turtle snapshot containing policies, requests and SOTW');