modelscript/languages/owl2 at main · modelscript/modelscript · GitHub
Skip to content

Latest commit

 

History

History

owl2

Folders and files

README.md

بِسْمِ اللَّهِ الرَّحْمَٰنِ الرَّحِيمِ
In the name of Allah, the Compassionate, the Merciful

@modelscript/owl2

Web Ontology Language (OWL 2 DL) Functional-Style Syntax and Turtle compiler, CST-to-Arena axiom lowerer, Datalog/Tableau & WebGPU saturation reasoner integration, SPARQL-DL / Leapfrog Triejoin BGP query engine, interactive 2D ontology diagram builder, and MCP server (owl2-mcp) for ModelScript.


Core Capabilities

  • OWL 2 Functional-Style Syntax & Turtle Support (.owl, .owl2, .ofn, .ttl): Native WebAssembly GLR parser (parser.wasm) generated by @modelscript/dsl (buildParser) covering prefixes, ontology headers, imports, entity declarations, class expressions, property characteristics, and individual assertions.
  • 25+ OWL 2 DL Axiom Lowerings into WasmOntologyArena (lowerCstToAxioms): Lowers parsed CST nodes directly into typed OWL2Axiom records for @modelscript/runtime's linear-memory WasmOntologyArena, including:
    • Entity Declarations: ClassDeclaration, ObjectPropertyDeclaration, DataPropertyDeclaration, IndividualDeclaration
    • Class Expression & TBox Axioms: SubClassOf, EquivalentClasses, DisjointClasses, ObjectIntersectionOf, ObjectUnionOf, ObjectComplementOf, ObjectSomeValuesFrom, DataSomeValuesFrom, ObjectAllValuesFrom (UniversalRestriction), ObjectHasSelf (SelfRestriction), ObjectHasValue, ObjectOneOf, and qualified cardinalities (ObjectMinCardinality, ObjectMaxCardinality, ObjectExactCardinality, DataMinCardinality, DataMaxCardinality, DataExactCardinality)
    • RBox Property Axioms: SubObjectPropertyOf, SubDataPropertyOf, InverseObjectProperties, DisjointObjectProperties, ObjectPropertyDomain, ObjectPropertyRange, DataPropertyDomain, DataPropertyRange, FunctionalObjectProperty, InverseFunctionalObjectProperty, ReflexiveObjectProperty, IrreflexiveObjectProperty, SymmetricObjectProperty, AsymmetricObjectProperty, TransitiveObjectProperty, FunctionalDataProperty
    • ABox Individual Assertions: ClassAssertion, ObjectPropertyAssertion, DataPropertyAssertion, NegativeObjectPropertyAssertion, NegativeDataPropertyAssertion, SameIndividual, DifferentIndividuals
  • Datalog/Tableau & WebGPU Saturation Reasoner: Integrates with @modelscript/runtime's hybrid Tableau/Datalog reasoner and WebGPU saturation pipeline for consistency validation (validate_consistency), unsatisfiable class detection, minimal conflict core extraction, and formal justification trees (explain_inferences).
  • SPARQL-DL & Leapfrog Triejoin BGP Querying: Executes worst-case optimal multi-way joins via Leapfrog Triejoin over indexed SPO/POS/OSP triple tables (query_ontology_bgp) and SPARQL-DL property paths (query_ontology_sparql).
  • Interactive 2D Ontology Diagram & Bidirectional Writeback:
    • buildOWL2DiagramData: Synthesizes AntV X6 graph diagrams showing classes, defined classes, object/data properties, individuals, and both asserted and «inferred» subClassOf edges.
    • computeOWL2ElementInsert, computeOWL2ElementDelete, computeOWL2ConnectionInsert, and owl2Language.writeback: Synchronizes visual graph edits and data property assertion changes back into .owl source text.
  • Model Context Protocol (owl2-mcp) Server: Exposes 5 reasoning and query tools (validate_system_consistency, query_ontology_sparql, query_ontology_bgp, explain_inference, trace_fault_propagation), RDF/Turtle triple resources, and diagnostic prompts to AI agents.
  • 5 Bidirectional Polyglot TGG Rules: Synchronizes OWL 2 classes, subclass hierarchies, object properties, and data properties with SysML v2 definitions and Modelica models.

Package Structure

Path Description
src/index.ts Public exports (owl2Language, lowerCstToAxioms, OWL2Factory, diagram builders and writeback editors)
src/language.ts OWL 2 grammar specification, writeback handler, LSP actions, owl2-mcp tool definitions, and 5 polyglot TGG rules
src/cst-lowering.ts lowerCstToAxioms CST walker lowering 25+ OWL 2 DL axiom constructs into OWL2Axiom arena records
src/factory.ts Fluent OWL2Factory builder API for programmatic ontology construction
src/diagram/data.ts buildOWL2DiagramData converting asserted and reasoner-inferred axioms into interactive 2D node-link diagrams
src/diagram/edits.ts computeOWL2ElementInsert, computeOWL2ElementDelete, and computeOWL2ConnectionInsert LSP TextEdit generators
build.js Compiles src/language.ts into dist/parser.wasm and src-gen/bindings.js

Polyglot Triple Graph Grammar (TGG) Rules

@modelscript/owl2 defines 5 bidirectional TGG rules in owl2Language.polyglot.rules:

Rule Name Source (owl2) Target (sysml2 / modelica)
OWL2ClassToSysML2Part ClassDeclaration(iri: iri) sysml2: PartDefinition(declaredName: iri)
OWL2SubClassToSysML2Specialization SubClassOfAxiom(subClass: sub, superClass: sup) sysml2: Specialization(generalName: sup, specificName: sub)
OWL2ObjectPropertyToSysML2Port ObjectPropertyDeclaration(iri: iri) sysml2: PortDefinition(declaredName: iri)
OWL2DataPropertyToSysML2Attribute DataPropertyDeclaration(iri: iri) sysml2: AttributeDefinition(declaredName: iri)
OWL2ClassToModelicaModel ClassDeclaration(iri: iri) modelica: ClassDefinition(name: iri, classKind: "model")

Scripts

Command Description
npm run build Build WASM parser (node build.js) and compile TypeScript
npm run build:parser Force-rebuild parser.wasm and generated bindings
npm test Run parser, CST lowering, and diagram tests via test-runner.js
npm run lint Run ESLint across src/ and tests/
npm run typecheck Run tsc --noEmit type verification
npm run clean Remove dist/, src-gen/, and .wasm build artifacts

Programmatic Usage

import { buildOWL2DiagramData, computeOWL2ConnectionInsert, lowerCstToAxioms } from "@modelscript/owl2";
import { createWasmParser } from "@modelscript/owl2/parser";

// 1. Initialize WASM GLR parser and parse OWL 2 Functional-Style Syntax
const wasmPath = require.resolve("@modelscript/owl2/parser.wasm");
const { parser } = await createWasmParser(wasmPath);

const owlSource = `
Prefix(:=<http://example.org/drone#>)
Ontology(<http://example.org/drone>
  Declaration(Class(:Actuator))
  Declaration(Class(:BrushlessMotor))
  Declaration(ObjectProperty(:drivesRotor))
  SubClassOf(:BrushlessMotor :Actuator)
)
`;

const tree = parser.parse(owlSource);

// 2. Lower CST into WasmOntologyArena axioms
const axioms = lowerCstToAxioms(tree.rootNode, owlSource);

// 3. Build 2D ontology graph diagram with reasoner inferences
const diagram = buildOWL2DiagramData(axioms, undefined, "All", {
  inferredSubClassOf: [{ subClassIri: ":BrushlessMotor", superClassIri: ":Component" }],
});

// 4. Compute bidirectional TextEdits from interactive diagram edge creation
const edits = computeOWL2ConnectionInsert(owlSource, ":BrushlessMotor", ":Rotor", "subClassOf");