n3-utility
v0.1.0
Published
Reasoning utilities for the Notation 3 reasoning language that work in the browser and Node.
Readme
Notation 3 Utilities
Notation3 (N3) is a language for natively building and reasoning over semantic Knowledge Graphs. Several online tools exist to experiment with N3, such as the Notation3 Editor, the Eyeling Playground and the eye-js playground.
When building applications with N3, developers often rely on a specific reasoning library. Over time, they may want or need to switch to another implementation (e.g. for features, performance, or deployment constraints). However, these libraries usually expose different interfaces, making such transitions non-trivial. This mismatch also surfaces across environments: server-side and browser-based reasoners typically have different interfaces, further complicating reuse.
This package addresses the interface mismatch by providing a uniform function interface reason, the core method provided by the Reasoner class:
reason(dataStore: Store, rules: string): Promise<Store>This method takes an RDF graph (Store) and a set of N3 rules (.n3 string), and returns a new Store containing the inferred results.
This package supports three different implementations of N3:
- Euler Yet another proof Engine - EYE (The OG prolog version) through the
EyeReasonerclass- you do need a prolog install local -> so only works in node if eye is installed correctly
- EYE JS (webassembly version of EYE)
EyeJsReasonerclass - eyeling (A Notation3 (N3) reasoner in JavaScript)
EyelingReasonerclass- Also supports streaming interfaces natively, but check the documentation or ask the maintainers for that purpose
This package supports multiple N3 reasoner implementations, each with their own class implementation that exposes the reason interface:
- EYE (Euler Yet another proof Engine) exposed via the
EyeReasonerclass
EYE is the original Notation3 implementation writtein in Prolog. Thus a local Prolog installation is required and as such only works in Node.js environments where EYE is correctly installed. - EYE JS exposed via the
EyeJsReasonerclass
EYE JS is the WebAssembly-based version of EYE that can run in both Node.js and the browser. - eyeling exposed via the
EyelingReasonerclass
Eyeling is a JavaScript-native N3 reasoner. It also supports streaming interfaces, though these are not exposed through this abstraction (see its documentation for advanced usage).
How to use this library
For Typescript, independent of whether you use it in a browser or Node environment, the code below should produce the following output: :Socrates a :Mortal .
import { Parser, Store } from 'n3'
import { EyelingReasoner } from 'N3-utility'
const data = `
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix : <http://example.org/socrates#>.
:Socrates a :Human.
:Human rdfs:subClassOf :Mortal.
`;
const rules = `
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#>.
@prefix : <http://example.org/socrates#>.
{
?S a ?A .
?A rdfs:subClassOf ?B .
}
=>
{
?S a ?B .
} .
`
const store = new Store(new Parser().parse(data));
const reasoner = new EyelingReasoner();
const result = await reasoner.reason(store, rules);Local development
nvm use # If you use nvm for node version management
# Required because of mismatch between eyereasoner and the new n3 library
npm install -D --legacy-peer-depsTesting
npx playwright install
npm run test:browser Feedback and questions
Do not hesitate to report a bug.
Further questions can also be asked to Wout Slabbinck (developer and maintainer of this repository).
