eyemind 0.8.33 → 0.8.34

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/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.33
5
+ Version: eyemind 0.8.34
6
6
  ```
7
7
 
8
8
  This document specifies Logic Script (LS): the syntax and meaning of its
package/README.md CHANGED
@@ -306,9 +306,11 @@ class; later messages of that shape only add their own facts. The result is
306
306
  the same as running the whole program over the background and the message,
307
307
  and the whole program runs instead when new triples could have any property,
308
308
  as `owl:sameAs` between individuals makes, or when a negation could retract
309
- a background conclusion. `--program` prints the specialized program of each
310
- new shape, and `--stats` says whether each message used one. Streamed
311
- messages have no proofs.
309
+ a background conclusion. With `--qudt`, the compiler reads the units and
310
+ metadata of each message and runs the whole program for it. SHACL contracts
311
+ still filter the input of closed shapes and project the output.
312
+ `--program` prints the specialized program of each new shape, and `--stats`
313
+ says whether each message used one. Streamed messages have no proofs.
312
314
 
313
315
  An [output contract](#rdf-logic-profiles) prunes the specialized rules to
314
316
  those that can contribute to a triple it asks for, following premises back
@@ -331,11 +333,11 @@ a message whose triples are all instances of them uses that program, and one
331
333
  with other triples gets rules of its own shape. When every input shape is
332
334
  closed, a message triple whose property no shape names or ignores is dropped
333
335
  first; `--stats` reports `conforms` and `dropped` per message.
334
- `--shacl-out FILE` reads output shapes as an output contract: each property
335
- shape asks for triples of its path, of the values it allows, and only rules
336
- concluding those, or what such a rule reads, stay. A variable property or
337
- class in a rule takes the values its schema premise has in the background, as
338
- partial evaluation gives them. Planned rules derive what the shapes lead to,
336
+ `--shacl-out FILE` reads output shapes as an output contract. Each property
337
+ shape asks for triples of its path with the values it allows. Planning
338
+ keeps rules that conclude those triples and rules they depend on. A variable
339
+ property or class in a rule takes the values its schema premise has in the
340
+ background, as partial evaluation gives them. Planned rules derive what the shapes lead to,
339
341
  not every OWL 2 RL consequence: equality that the output does not ask for is
340
342
  not derived. See the
341
343
  [shape-planning](https://eyereasoner.github.io/eyemind/examples/deck/shape-planning-owl2rl) deck.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eyemind",
3
- "version": "0.8.33",
3
+ "version": "0.8.34",
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/qudt.js CHANGED
@@ -10,6 +10,9 @@ const U = 'http://qudt.org/vocab/unit/';
10
10
  const C = 'https://eyereasoner.github.io/eyemind/qudt#';
11
11
  const D = 'http://qudt.org/vocab/dimensionvector/';
12
12
  const K = 'http://qudt.org/vocab/quantitykind/';
13
+ const SAME_AS = 'http://www.w3.org/2002/07/owl#sameAs';
14
+ const EXACT_MATCH = 'http://www.w3.org/2004/02/skos/core#exactMatch';
15
+ const ALIGNED_UNIT = C + 'alignedQudtUnit';
13
16
  const dimensions = {
14
17
  length: 'A0E0L1I0M0H0T0D0', area: 'A0E0L2I0M0H0T0D0', volume: 'A0E0L3I0M0H0T0D0',
15
18
  time: 'A0E0L0I0M0H0T1D0', mass: 'A0E0L0I0M1H0T0D0', speed: 'A0E0L1I0M0H0T-1D0',
@@ -90,7 +93,7 @@ implied_by(qu_dimension(U, G, iri('http://qudt.org/vocab/dimensionvector/A0E0L0I
90
93
  implied_by(qu_token(T, U, G), qu_builtin_token(T, U));
91
94
  for (const p of ['symbol', 'ucumCode', 'expression']) implied_by(qu_token(T, U, G), [ent_base(U, qu(p), L, G), srl_str(L, literal(T, datatype(quString)))]);
92
95
  implied_by(qu_align(U, U, G), qu_dimension(U, G, _));
93
- for (const p of [iri('http://www.w3.org/2002/07/owl#sameAs'), iri('http://www.w3.org/2004/02/skos/core#exactMatch'), qc('alignedQudtUnit')]) {
96
+ for (const p of [iri('${SAME_AS}'), iri('${EXACT_MATCH}'), qc('alignedQudtUnit')]) {
94
97
  implied_by(qu_align(Local, U, G), [rdf(Local, p, U, G), qu_dimension(U, G, _)]);
95
98
  }
96
99
  implied_by(qu_custom_profile(P, G), ent_base(P, qc('targetUnit'), _, G));
@@ -254,6 +257,16 @@ export function qudtUnits(quads) {
254
257
  if (['hasFactorUnit', 'scalingOf', 'hasDimensionVector'].some((p) => values(subject, p).length)) resolve(subject);
255
258
  }
256
259
  for (const quad of quads) if (quad[1].args?.[0] === Q + 'unit') resolve(quad[2]);
260
+ // An alignment can be the only mention of a catalog unit, for example
261
+ // when a CDT literal uses a local symbol. OWL and SKOS can reverse it.
262
+ const resolveCatalog = (unit) => {
263
+ if (unit?.name === 'iri' && unit.args[0].startsWith(U)) resolve(unit);
264
+ };
265
+ for (const [subject, predicate, object] of quads) {
266
+ const p = predicate.args?.[0];
267
+ if (p === SAME_AS || p === EXACT_MATCH) resolveCatalog(subject);
268
+ if (p === SAME_AS || p === EXACT_MATCH || p === ALIGNED_UNIT) resolveCatalog(object);
269
+ }
257
270
  // The units of CDT literals by their UCUM codes.
258
271
  const tokens = [];
259
272
  for (const quad of quads) {
package/src/rdf-logic.js CHANGED
@@ -5,7 +5,7 @@ import { rdfDocument } from './rdf.js';
5
5
  import { Lettering, write } from './writer.js';
6
6
  import { owl2rlRules } from './owl2rl.js';
7
7
  import { skosRules } from './skos.js';
8
- import { qudtRules } from './qudt.js';
8
+ import { qudtRules, qudtUnits } from './qudt.js';
9
9
  import { compileShacl } from './shacl.js';
10
10
  import { NODE_FUNCTIONS } from './shacl-expr.js';
11
11
  import { executionContext } from './srl.js';
@@ -19,25 +19,31 @@ export const ENTAILMENT = 'https://eyereasoner.github.io/eyemind/entailment#';
19
19
  export const RDF_PROFILES = ['owl2rl', 'qudt', 'skos', 'shacl'];
20
20
 
21
21
  // wanted: rdf/4 patterns of the output contract, or null for every new triple.
22
- export function compileRdfLogic({ quads, prefixes, shapes = quads, wanted = null }, { profiles, source = '<input>', disallows, evaluate, context, stream = false }) {
22
+ export function compileRdfLogic({ quads, prefixes, shapes = quads, wanted = null }, {
23
+ profiles, source = '<input>', disallows, evaluate, context, stream = false, stableRuleIds = false,
24
+ }) {
23
25
  quads = [...new Map(quads.map((q) => [q.map((t) => write(t)).join(' '), q])).values()];
24
26
  const selected = new Set(profiles), needs = new Set();
25
27
  // SHACL node expressions may call SPARQL functions beyond the value rules.
26
28
  const shacl = selected.has('shacl') ? compileShacl(shapes, { disallows, needs, evaluate }) : '';
27
29
  const own = [...needs].filter((n) => NODE_FUNCTIONS[n]);
28
30
  const execution = ['srl_now', 'srl_uuid', 'sh_ne_rand'].some((n) => needs.has(n)) ? executionContext(context ?? null) : null;
31
+ const data = `facts_from({ text: ${JSON.stringify(rdfDocument(quads))} })\n` +
32
+ `facts_from({ text: ${JSON.stringify(rdfDocument(quads, 'ent_base'))} })\n`;
33
+ const defaultGraph = quads.length ? '' : "fact(ent_graph('default_graph'));\n";
34
+ // In a recompiled stream, the background's witnesses must keep the same
35
+ // clause numbers after a message adds RDF and catalog facts.
29
36
  const text = `// Generated by eyemind from ${String(source).replace(/[\r\n\u2028\u2029]/g, ' ')}, with ${profiles.map((p) => '--' + p).join(' ')}.\n` +
30
37
  '// rdf(S, P, O, G) is the working graph; result_rdf contains conclusions and reports.\n' +
31
- `facts_from({ text: ${JSON.stringify(rdfDocument(quads))} })\n` +
32
- `facts_from({ text: ${JSON.stringify(rdfDocument(quads, 'ent_base'))} })\n` + COMMON +
33
- (quads.length ? '' : "fact(ent_graph('default_graph'));\n") +
38
+ (stableRuleIds ? '' : data) + COMMON + (stableRuleIds ? '' : defaultGraph) +
34
39
  (profiles.some((p) => p !== 'skos') ? rdfValueRules([...needs].filter((n) => !NODE_FUNCTIONS[n])) : '') +
35
40
  own.map((n) => NODE_FUNCTIONS[n] + '\n').join('') +
36
41
  (execution ? `implied_by(srl_execution(${JSON.stringify(execution.now)}, ${JSON.stringify(execution.seed)}), 'true');\n` : '') +
37
42
  (selected.has('owl2rl') ? owl2rlRules : '') +
38
43
  (selected.has('skos') ? skosRules : '') +
39
- (selected.has('qudt') ? qudtRules(quads) : '') +
40
- shacl + (evaluate ? "query(sh_evaluation(R));\n" : REPORTS + (stream ? '' : wanted === null ? RESULT : contracted(wanted)));
44
+ (selected.has('qudt') ? qudtRules(stableRuleIds ? [] : quads) : '') +
45
+ shacl + (evaluate ? "query(sh_evaluation(R));\n" : REPORTS + (stream ? '' : wanted === null ? RESULT : contracted(wanted))) +
46
+ (stableRuleIds ? data + defaultGraph + (selected.has('qudt') ? qudtUnits(quads) : '') : '');
41
47
  return { kind: profiles[0], text, program: loadText(text, source),
42
48
  prefixes: { ...prefixes, ent: ENTAILMENT, ...(selected.has('skos') ? { skos: 'http://www.w3.org/2004/02/skos/core#' } : {}),
43
49
  ...(selected.has('qudt') ? { qconv: 'https://eyereasoner.github.io/eyemind/qudt#', qudt: 'http://qudt.org/schema/qudt/', unit: 'http://qudt.org/vocab/unit/' } : {}),
@@ -261,12 +267,15 @@ export function planRules(program, quads, { input = null, output = null }) {
261
267
  classes: new Set([...background.classes, ...input.classes]) });
262
268
  selected = selected.filter((rule) => reached.has(rule));
263
269
  }
264
- if (output) {
270
+ if (output && !output.variable) {
265
271
  const desired = { predicates: new Set(output.predicates), classes: new Set(output.classes) };
266
272
  const needed = new Set();
267
- const wanted = (head) => (head.variable && desired.predicates.size && !desired.classes.size) ||
268
- [...head.predicates].some((p) => desired.predicates.has(p) && !(p === RDF_TYPE && desired.classes.size)) ||
269
- [...head.classes].some((c) => desired.classes.has(c));
273
+ const wanted = (head) => {
274
+ const anyClass = desired.predicates.has(RDF_TYPE);
275
+ return (head.variable && desired.predicates.size && (!desired.classes.size || anyClass)) ||
276
+ [...head.predicates].some((p) => desired.predicates.has(p)) ||
277
+ [...head.classes].some((c) => anyClass || desired.classes.has(c));
278
+ };
270
279
  for (let changed = true; changed;) {
271
280
  changed = false;
272
281
  for (const rule of selected) {
@@ -285,11 +294,12 @@ export function planRules(program, quads, { input = null, output = null }) {
285
294
 
286
295
  // The properties and classes of triple patterns.
287
296
  const vocabularyOf = (patterns) => {
288
- const into = { predicates: new Set(), classes: new Set() };
297
+ const into = { predicates: new Set(), classes: new Set(), variable: false };
289
298
  for (const pattern of patterns) {
290
- const p = pattern.args[1]?.name === 'iri' ? pattern.args[1].args[0] : null;
299
+ const p = pattern.args[1]?.name === 'iri' ? pattern.args[1].args[0] : undefined;
291
300
  if (p === RDF_TYPE && pattern.args[2]?.name === 'iri' && typeof pattern.args[2].args[0] === 'string') into.classes.add(pattern.args[2].args[0]);
292
- else if (p !== null) into.predicates.add(p);
301
+ else if (typeof p === 'string') into.predicates.add(p);
302
+ else into.variable = true;
293
303
  }
294
304
  return into;
295
305
  };
@@ -307,50 +317,55 @@ const vocabularyOf = (patterns) => {
307
317
  // that does not conform gets one of its own shape.
308
318
  export function messageReasoner({ quads, prefixes, wanted = null, expected = null, planned = false }, { profiles, source = '<input>', limits = {} }) {
309
319
  const options = { profiles, source };
310
- const background = compileRdfLogic({ quads, prefixes }, { ...options, stream: true });
320
+ const whole = { ...options, stableRuleIds: true };
321
+ // QUDT adds catalog facts during compilation from the units and metadata
322
+ // in the current message. Run that compiler on each combined graph.
323
+ const qudt = profiles.includes('qudt'), usePlan = planned && !qudt;
324
+ const background = compileRdfLogic({ quads, prefixes }, qudt ? whole : { ...options, stream: true });
311
325
  // Trusted SHACL shapes plan the rules first, as rdfjs-inference-engine does.
312
- const program = planned ? planRules(background.program, quads, { input: expected && vocabularyOf(expected.patterns),
326
+ const program = usePlan ? planRules(background.program, quads, { input: expected && vocabularyOf(expected.patterns),
313
327
  output: wanted && vocabularyOf(wanted) }) : background.program;
314
- const specializer = new Specializer(program, { quads: ['rdf/4', 'ent_base/4'], limits,
328
+ const specializer = qudt ? null : new Specializer(program, { quads: ['rdf/4', 'ent_base/4'], limits,
315
329
  wanted: wanted && wanted.flatMap((pattern) => [pattern, new Struct('ent_report', pattern.args)]) });
316
330
  // The program for the messages an input shape describes is made at once.
317
- if (expected) specializer.expect(expected.patterns.flatMap((pattern) => [pattern, new Struct('ent_base', pattern.args)]));
331
+ if (expected) specializer?.expect(expected.patterns.flatMap((pattern) => [pattern, new Struct('ent_base', pattern.args)]));
318
332
  let serial = 0;
319
- const kept = (quad) => wanted === null ||
320
- wanted.some((pattern) => unify(new Struct('rdf', quad), freshTerm(pattern, `w${++serial}`), new Env()));
333
+ const matches = (quad, patterns) => patterns.some((pattern) => unify(new Struct('rdf', quad), freshTerm(pattern, `w${++serial}`), new Env()));
334
+ const kept = (quad) => wanted === null || matches(quad, wanted);
321
335
  let alone = null;
322
336
  const answers = (program, limits) => run(program, limits).conclusions.filter((t) => t instanceof Struct && t.name === 'result_rdf');
323
337
  return {
324
338
  prefixes: background.prefixes,
325
- reason: specializer.reason,
339
+ reason: qudt ? 'QUDT resolves units from each message' : specializer.reason,
326
340
  specializer,
327
341
  // limits are run()'s bounds. The result has the added quads, whether
328
342
  // the rules were specialized, and the specialized program's sources the
329
343
  // first time a message has its shape; with input shapes, how many
330
344
  // triples were dropped and whether the rest conform.
331
- infer(message, limits = {}) {
345
+ infer(message, runLimits = limits) {
332
346
  const all = message;
333
347
  if (expected?.allowed) message = message.filter((quad) => quad[1].name === 'iri' && expected.allowed.has(quad[1].args[0]));
334
348
  const shaped = expected ? { dropped: all.length - message.length } : {};
335
349
  const facts = message.flatMap((q) => [new Struct('rdf', q), new Struct('ent_base', q)]);
336
- const special = specializer.specialize(facts);
350
+ const special = specializer?.specialize(facts) ?? null;
337
351
  if (special !== null) {
338
- const result = run(special.program, limits);
352
+ const result = run(special.program, runLimits);
339
353
  const added = result.derived.map((node) => node.goal).filter((t) => !special.old.has(identity(t)) &&
340
354
  (t.name === 'ent_report' || (t.name === 'rdf' && RESOURCES.has(t.args[0].name) && t.args[1].name === 'iri')));
341
355
  return { quads: added.map((t) => t.args).filter(kept), specialized: true, stats: result.stats, haltCode: result.haltCode,
342
356
  sources: special.fresh ? special.sources : null, ...(expected ? { ...shaped, conforms: special.conforms } : {}) };
343
357
  }
344
- if (planned) {
358
+ if (expected) shaped.conforms = message.every((quad) => matches(quad, expected.patterns));
359
+ if (usePlan) {
345
360
  // The planned rules run whole over the background and the message.
346
- alone ??= new Set(run(program, limits).derived.map((node) => identity(node.goal)));
347
- const result = run(program.withFacts(facts), limits);
361
+ alone ??= new Set(run(program, runLimits).derived.map((node) => identity(node.goal)));
362
+ const result = run(program.withFacts(facts), runLimits);
348
363
  const added = result.derived.map((node) => node.goal).filter((t) => !alone.has(identity(t)) &&
349
364
  (t.name === 'ent_report' || (t.name === 'rdf' && RESOURCES.has(t.args[0].name) && t.args[1].name === 'iri')));
350
365
  return { quads: added.map((t) => t.args).filter(kept), specialized: false, stats: result.stats, haltCode: result.haltCode, sources: null, ...shaped };
351
366
  }
352
- alone ??= new Set(answers(compileRdfLogic({ quads, prefixes }, options).program, limits).map(identity));
353
- const result = run(compileRdfLogic({ quads: [...quads, ...message], prefixes, shapes: quads }, options).program, limits);
367
+ alone ??= new Set(answers(qudt ? background.program : compileRdfLogic({ quads, prefixes }, whole).program, runLimits).map(identity));
368
+ const result = run(compileRdfLogic({ quads: [...quads, ...message], prefixes, shapes: quads }, whole).program, runLimits);
354
369
  const added = result.conclusions.filter((t) => t instanceof Struct && t.name === 'result_rdf' && !alone.has(identity(t)));
355
370
  return { quads: added.map((t) => t.args).filter(kept), specialized: false, stats: result.stats, haltCode: result.haltCode, sources: null, ...shaped };
356
371
  },
package/src/version.js CHANGED
@@ -1,2 +1,2 @@
1
1
  // The release this checkout is; ./release keeps it in step with package.json.
2
- export const VERSION = '0.8.33';
2
+ export const VERSION = '0.8.34';