Skip to content

Plan, apply and roll back changes to a production ontology, with a blast radius report and a proof an auditor can re-check.

feat: virtualized tree view, 13-step deep builder, /sketch command

FABIOTESSc03aa91265d5
diff --git a/README.md b/README.md
index a5c776d2..b9a05b18 100644
--- a/README.md
+++ b/README.md
@@ -33,7 +33,7 @@
Open Ontologies is a **Rust MCP server** and **desktop Studio** for AI-native ontology engineering. It exposes **43 tools** that let Claude build, validate, query, diff, lint, version, reason over, align, and persist RDF/OWL ontologies using an in-memory Oxigraph triple store — with Terraform-style lifecycle management, a marketplace of 32 standard ontologies, clinical crosswalks, semantic embeddings, and a full lineage audit trail.
-The **Studio** wraps the engine in a visual desktop environment: 3D force-directed graph, AI chat panel, Protégé-style property inspector, and lineage viewer.
+The **Studio** wraps the engine in a visual desktop environment: D3 horizontal tree with cross-links, 3D force-directed graph, AI chat panel, Protégé-style property inspector, and lineage viewer.
No JVM. No Protégé. No GUI required.
@@ -211,12 +211,11 @@ The first launch compiles the Tauri shell (~2 min). Subsequent launches start in
| Feature | Description |
| --- | --- |
-| **3D Graph Canvas** | Spring-based force-directed OWL graph (Three.js / WebGL). Grey edges = subClassOf hierarchy, gold edges = object property domain/range links. Drag to orbit, scroll to zoom, click to inspect, right-click to add, Delete to remove. Clusters of related classes self-organise into visual neighbourhoods. |
+| **D3 Tree + 3D Graph** | Two view modes: a D3.js horizontal tree (default) showing the full class/property hierarchy with collapsible branches, cross-links, and node type filtering — and a 3D force-directed graph (Three.js / WebGL) with spring physics. Both support zoom, pan, click to inspect, and keyboard shortcuts. |
| **AI Agent Chat** | Natural language ontology engineering via Claude Opus 4.6 + Agent SDK. Type *"Build me a Pizza ontology"* and watch it appear in the graph. Claude calls validate, load, reason, lint, enforce, and save automatically — you see each tool call in real time. |
| **Property Inspector** | Protege-style inline triple editor. Click any node to see its `rdfs:subClassOf`, `rdfs:label`, `rdfs:domain`, `rdfs:range` and all other triples. Edit in place, hover to delete, `+ Add` for new triples. Changes are immediately reflected in the graph. |
| **Lineage Panel** | Full audit trail from SQLite: every plan, apply, enforce, drift, monitor, and align event, grouped by session with timestamps. See exactly what Claude did and in what order. |
| **Named Save** | `⌘S` to save as `~/.open-ontologies/<name>.ttl`. Auto-saves to `studio-live.ttl` after every mutation so you never lose work. |
-| **Load from file** | Drag a `.ttl` file onto the window or use the menu to load an existing ontology. The graph renders immediately. |
### Keyboard shortcuts
@@ -225,9 +224,12 @@ The first launch compiles the Tauri shell (~2 min). Subsequent launches start in
| `⌘J` | Toggle AI chat panel |
| `⌘I` | Toggle property inspector |
| `⌘S` | Save ontology |
-| `Delete` | Remove selected node |
+| `F` | Fit graph to viewport (tree view) |
+| `R` | Reset zoom (tree view) |
+| `Esc` | Deselect node |
+| `Shift+click` | Collapse/expand branch (tree view) |
| `Scroll` | Zoom in/out |
-| `Click + drag` | Orbit camera |
+| `Click + drag` | Pan (tree) / orbit (3D) |
---
@@ -321,7 +323,7 @@ Full benchmark writeup: [docs/benchmarks.md](docs/benchmarks.md)
## IES Support
-[IES (Information Exchange Standard)](https://github.com/IES-Org) is the UK National Digital Twin Programme's core ontology framework. It uses a 4D extensionalist (BORO) approach for modelling entities, events, states, and relationships. Open Ontologies supports the **full IES stack** — all three layers, SHACL shapes, and 42+ example datasets.
+[IES (Information Exchange Standard)](https://github.com/IES-Org) is the UK National Digital Twin Programme's core ontology framework. It uses a 4D extensionalist (BORO) approach for modelling entities, events, states, and relationships. Open Ontologies supports the **full IES stack** — all three layers, SHACL shapes, and 26+ example datasets.
### The IES Layers
@@ -330,22 +332,24 @@ The marketplace includes all three tiers of the IES framework:
```text
onto_marketplace install ies-top # ToLO — BORO foundations (~22 classes)
onto_marketplace install ies-core # Core — persons, states, events (~131 classes)
-onto_marketplace install ies # Common — full ontology (513 classes, 206 properties)
+onto_marketplace install ies # Common — full ontology (511 classes, 206 properties)
```
### Benchmark
| Metric | IES Common |
| --- | --- |
-| Classes | 513 |
-| Object properties | 206 |
-| Triples loaded | 4,040 |
+| Classes | 511 |
+| Object properties | 162 |
+| Datatype properties | 44 |
+| Total properties | 206 |
+| Triples loaded | 4,041 |
| + RDFS inferred | **+3,094 (+77%)** |
| Fetch time | 911ms |
| RDFS reasoning | 63ms |
| Lint issues | 0 |
-IES is the second-largest ontology in the marketplace by class count (after Schema.org). RDFS reasoning produces the richest inference gain of any non-general ontology — 241 State subclasses, 117 ClassOfEntity subclasses, and 102 Event subclasses all generating transitive chains.
+IES is the second-largest ontology in the marketplace by class count (after Schema.org). RDFS reasoning produces the richest inference gain of any non-general ontology — State, ClassOfEntity, and Event subclasses all generating deep transitive chains.
### Example Data
@@ -539,7 +543,7 @@ onto_enforce --pack hierarchy
# → reports max depth, avg depth, hierarchy density
```
-Tested on IES Common (513 classes), the tool found 24 flat spots. A clean-room agent — with no prior context — proposed 38 intermediate grouping classes based solely on the domain meaning of the flagged children:
+Tested on IES Common (511 classes), the tool found 24 flat spots. A clean-room agent — with no prior context — proposed 38 intermediate grouping classes based solely on the domain meaning of the flagged children:
```mermaid
graph LR
@@ -582,7 +586,7 @@ graph LR
| Metric | Before | After | Change |
| --- | ---: | ---: | ---: |
-| Classes | 513 | 551 | +38 |
+| Classes | 511 | 549 | +38 |
| RDFS inferred | 3,094 | 3,422 | **+328 (+10.6%)** |
The same tool, applied to any ontology, produces the same kind of improvement. The intermediate classes emerge from domain knowledge — not from reference to any other implementation.
@@ -690,7 +694,7 @@ flowchart TD
```mermaid
flowchart TD
subgraph UI["React UI (Vite + Tailwind CSS)"]
- Graph["3D Graph Canvas\n3d-force-graph / Three.js"]
+ Graph["D3 Tree + 3D Graph\nD3.js / Three.js"]
Chat["AI Chat Panel\nZustand store"]
Inspector["Property Inspector\nInline SPARQL edit"]
Lineage["Lineage Panel\nAudit trail"]
@@ -754,7 +758,7 @@ flowchart TD
| Embeddings runtime | tract-onnx — pure Rust ONNX (optional) |
| Desktop shell | Tauri 2 |
| Frontend | React 19, Vite 7, TypeScript 5.8, Tailwind CSS 4 |
-| 3D graph | 3d-force-graph 1.79 (Three.js / WebGL) |
+| Graph views | D3.js 7 (tree layout) + 3d-force-graph 1.79 (Three.js / WebGL) |
| UI state | Zustand 5 |
| AI agent | Claude Opus 4.6 via Agent SDK (Node.js sidecar) |
diff --git a/studio/src-tauri/sidecars/agent/dist/index.js b/studio/src-tauri/sidecars/agent/dist/index.js
index 6f1f233a..699f55f5 100644
--- a/studio/src-tauri/sidecars/agent/dist/index.js
+++ b/studio/src-tauri/sidecars/agent/dist/index.js
@@ -98,10 +98,13 @@ function isBuildRequest(msg) {
return (lower.includes('build') || lower.includes('create') || lower.includes('make') || lower.includes('generate'))
&& (lower.includes('ontology') || lower.includes('about'));
}
+function isSketchRequest(msg) {
+ return msg.toLowerCase().includes('sketch');
+}
function extractDomain(msg) {
const patterns = [
/(?:about|for|on|of)\s+(.+)/i,
- /(?:build|create|make|generate)\s+(?:a\s+|an\s+|the\s+)?(?:\w+\s+)?(?:ontology\s+)?(?:about|for|on|of)\s+(.+)/i,
+ /(?:build|create|make|generate|sketch)\s+(?:a\s+|an\s+|the\s+)?(?:\w+\s+)?(?:ontology\s+)?(?:about|for|on|of)\s+(.+)/i,
];
for (const p of patterns) {
const m = msg.match(p);
@@ -111,57 +114,208 @@ function extractDomain(msg) {
return match.trim().replace(/[.!?]+$/, '');
}
}
- return msg.replace(/^(build|create|make|generate)\s+(an?\s+)?ontology\s*/i, '').trim() || msg;
+ return msg.replace(/^(build|create|make|generate|sketch)\s+(an?\s+)?ontology\s*/i, '').trim() || msg;
}
// --- Multi-step build within ONE session ---
async function handleBuild(domain) {
const ns = domain.toLowerCase().replace(/[^a-z0-9]+/g, '-');
- // Step 1: Clear + load classes
- send({ type: 'text', content: `**Building ontology: ${domain}**\n\n---\n**Step 1/4:** Classes hierarchy...` });
- await runTurn(`Build an ontology about "${domain}". Use namespace @prefix : <http://example.org/${ns}#> .
-
-Step 1: Call onto_clear. Then call onto_load with Turtle containing:
-- 80-150 owl:Class declarations organised in a subClassOf hierarchy 5-7 levels deep
-- A root class, 5-8 major branches, each with 3-6 sub-branches, each with 2-5 leaves
-- Enumerate ALL real-world subtypes exhaustively
+ const prefix = `@prefix : <http://example.org/${ns}#> .`;
+ const DEEPEN = (branchDesc) => `Call onto_query to find leaf classes (classes with no subclasses) in ${branchDesc}. ` +
+ `Then call onto_load with Turtle using the SAME namespace adding more subclass levels. ` +
+ `For every leaf, ask yourself: "Can this be subdivided further?" If yes, add 3-8 subclasses. ` +
+ `Enumerate ALL real-world subtypes — do not stop at 2-3 examples. ` +
+ `Every class needs rdfs:label and rdfs:comment. ` +
+ `IMPORTANT: Add at most 80-120 classes in this step, then stop. Call onto_stats after. Do NOT save yet.`;
+ const steps = [
+ {
+ label: 'Step 1: Foundation — root + major branches + 3 levels',
+ prompt: `Build an ontology about "${domain}". Use namespace ${prefix}
+
+Call onto_clear. Then call onto_load with Turtle containing:
+- An owl:Ontology declaration
+- A root class for the domain
+- 8-12 major branch classes under the root (think of ALL major aspects/dimensions of "${domain}")
+- For each branch, 4-8 subclasses
+- For each of those, 3-6 further subclasses
- Every class MUST have rdfs:label and rdfs:comment
+- Be EXHAUSTIVE — list every subtype you can think of
+
+Call onto_stats after. Do NOT save yet — many more steps coming.`,
+ },
+ {
+ label: 'Step 2: Deepen — first major branch to maximum depth',
+ prompt: DEEPEN('the FIRST major branch (the first child of the root)'),
+ },
+ {
+ label: 'Step 3: Deepen — second major branch to maximum depth',
+ prompt: DEEPEN('the SECOND major branch'),
+ },
+ {
+ label: 'Step 4: Deepen — third major branch to maximum depth',
+ prompt: DEEPEN('the THIRD major branch'),
+ },
+ {
+ label: 'Step 5: Deepen — fourth and fifth major branches',
+ prompt: DEEPEN('the FOURTH and FIFTH major branches'),
+ },
+ {
+ label: 'Step 6: Deepen — all remaining branches',
+ prompt: DEEPEN('ALL remaining major branches that have not been deepened yet'),
+ },
+ {
+ label: 'Step 7: Deepen — second pass on shallow areas',
+ prompt: `Call onto_query to find ALL leaf classes. For any branch that is still less than 6 levels deep, add more subclasses. ` +
+ `Also think: are there any major subtypes or categories I missed entirely? Add them now. ` +
+ `Be ruthlessly exhaustive. Call onto_load with the additional Turtle. Call onto_stats after. Do NOT save yet.`,
+ },
+ {
+ label: 'Step 8: Object properties — structural relationships',
+ prompt: `Now add object properties. Call onto_load with Turtle containing 50-70 owl:ObjectProperty declarations.
+
+EVERY property MUST have: rdfs:domain, rdfs:range, rdfs:label, rdfs:comment.
+
+Cover ALL relationship types:
+- Compositional: hasPart/isPartOf, contains/isContainedIn, hasComponent/isComponentOf
+- Causal: causes, prevents, triggers, treats, inhibits, enables
+- Associative: isAssociatedWith, isRelatedTo, dependsOn, influences
+- Hierarchical: hasSubtype, isExampleOf, instantiates
+- Build rdfs:subPropertyOf hierarchies (3-4 levels deep)
+- Add owl:inverseOf for EVERY directional property
+- Mark owl:TransitiveProperty, owl:SymmetricProperty, owl:FunctionalProperty
+
+Call onto_stats after. Do NOT save yet.`,
+ },
+ {
+ label: 'Step 9: Object properties — roles, temporal, spatial',
+ prompt: `Add MORE object properties. Call onto_load with Turtle containing 50-70 MORE owl:ObjectProperty declarations.
+
+Focus on what's MISSING — look at the classes and ask "how does X relate to Y?" for every pair of branches:
+- Role/participation: hasRole, participatesIn, performs, undergoes, produces, consumes
+- Temporal: precedes, follows, during, overlaps, startsWith, endsWith
+- Spatial: isLocatedIn, isNear, surrounds, isAdjacentTo, isWithin
+- Ownership: owns, belongsTo, isOwnedBy, manages, controls
+- Agent: hasAgent, hasPatient, hasBeneficiary, hasInstrument
+- More rdfs:subPropertyOf hierarchies and owl:inverseOf pairs
+
+Call onto_stats after. Do NOT save yet.`,
+ },
+ {
+ label: 'Step 10: Datatype properties — all attributes',
+ prompt: `Add datatype properties. Call onto_load with Turtle containing 40-60 owl:DatatypeProperty declarations.
-Call onto_stats after loading to verify. Do NOT call onto_save yet — more content coming.`);
- // Step 2: Properties (SAME session — agent has context)
- send({ type: 'text', content: `\n---\n**Step 2/4:** Properties...` });
- await runTurn(`Now add properties to the ontology. Call onto_load with Turtle using the SAME namespace as before containing:
-
-- 25-40 owl:ObjectProperty declarations, each with rdfs:domain, rdfs:range, rdfs:label, rdfs:comment
-- Build rdfs:subPropertyOf hierarchies (e.g., hasParticipant > hasAgent > hasDriver)
-- Add owl:inverseOf pairs for bidirectional relationships
-- Mark owl:TransitiveProperty (isPartOf), owl:SymmetricProperty (isRelatedTo), owl:FunctionalProperty where appropriate
-- 15-20 owl:DatatypeProperty declarations with rdfs:domain, rdfs:range (xsd types), rdfs:label, rdfs:comment
-
-Call onto_stats after loading. Do NOT call onto_save yet.`);
- // Step 3: Axioms + individuals (SAME session)
- send({ type: 'text', content: `\n---\n**Step 3/4:** Axioms + individuals...` });
- await runTurn(`Now add axioms and individuals. Call onto_load with Turtle using the SAME namespace containing:
-
-- owl:disjointWith between ALL sibling classes that cannot overlap
-- 15-20 owl:NamedIndividual instances — real-world examples with rdf:type and property values
-
-Call onto_stats after loading. Do NOT call onto_save yet.`);
- // Step 4: Reason + save (SAME session)
- send({ type: 'text', content: `\n---\n**Step 4/4:** Reasoning + save...` });
- await runTurn(`Final step. Run:
+Each with: rdfs:domain, rdfs:range (xsd types), rdfs:label, rdfs:comment.
+
+Go through EVERY major branch and add ALL attributes:
+- Identifiers, names, codes, labels, descriptions, titles
+- Dates (birth, creation, modification, expiry, start, end)
+- Quantities (weight, height, length, count, duration, price, score, rating, percentage)
+- Measurements (temperature, speed, volume, area, concentration)
+- Boolean flags (isActive, isVerified, isPublic, isRequired, isOptional, isDeprecated)
+- Statuses, categories, priorities, levels, grades
+- Text fields (notes, comments, instructions, definitions)
+
+Call onto_stats after. Do NOT save yet.`,
+ },
+ {
+ label: 'Step 11: Axioms — disjoints everywhere',
+ prompt: `Add disjoint axioms. Call onto_load with Turtle containing owl:disjointWith between ALL sibling classes that cannot overlap.
+
+Go through EVERY branch systematically:
+- Root children: all major branches that are mutually exclusive
+- Within each branch: siblings that cannot overlap
+- Target: 60+ disjoint axiom pairs minimum
+
+Call onto_stats after. Do NOT save yet.`,
+ },
+ {
+ label: 'Step 12: Individuals — real-world examples',
+ prompt: `Add named individuals. Call onto_load with Turtle containing 25-40 owl:NamedIndividual instances.
+
+Spread them across ALL major branches — at least 3-4 individuals per branch.
+Each individual needs:
+- rdf:type (the most specific class)
+- rdfs:label and rdfs:comment
+- 3-5 property values (both object and datatype properties)
+
+Call onto_stats after. Do NOT save yet.`,
+ },
+ {
+ label: 'Step 13: Reason + save',
+ prompt: `Final step. Run:
1. onto_reason with profile "rdfs"
2. onto_stats — report the final counts
3. onto_save with path "~/.open-ontologies/studio-live.ttl"
-Report the final ontology statistics.`);
- send({ type: 'text', content: `\n---\n**Build complete.** Refresh the tree view to see the full graph.` });
+Report the final ontology statistics.`,
+ },
+ ];
+ send({ type: 'text', content: `**Building maximum-depth ontology: ${domain}** (${steps.length} steps)\n` });
+ for (let i = 0; i < steps.length; i++) {
+ const step = steps[i];
+ send({ type: 'text', content: `\n---\n**${step.label}**` });
+ try {
+ await runTurn(step.prompt);
+ }
+ catch (e) {
+ send({ type: 'text', content: `Step failed: ${e}. Continuing...` });
+ }
+ }
+ send({ type: 'text', content: `\n---\n**Build complete.** The graph should now be visible in the tree view.` });
+}
+// --- Quick sketch: 3-step lightweight build ---
+async function handleSketch(domain) {
+ const ns = domain.toLowerCase().replace(/[^a-z0-9]+/g, '-');
+ const steps = [
+ {
+ label: 'Step 1/3: Classes + properties',
+ prompt: `Build a quick ontology about "${domain}". Use namespace @prefix : <http://example.org/${ns}#> .
+
+Call onto_clear. Then call onto_load with ONE Turtle block containing:
+- 50-80 owl:Class in a subClassOf hierarchy 4-5 levels deep
+- 15-25 owl:ObjectProperty each with rdfs:domain, rdfs:range, rdfs:label, rdfs:comment
+- 8-12 owl:DatatypeProperty
+- owl:inverseOf pairs for key relationships
+- rdfs:label and rdfs:comment on every class and property
+
+Call onto_stats after. Do NOT save yet.`,
+ },
+ {
+ label: 'Step 2/3: Axioms + individuals',
+ prompt: `Call onto_load with Turtle adding:
+- owl:disjointWith between sibling classes (15+ pairs)
+- 10-15 owl:NamedIndividual with rdf:type and property values
+
+Call onto_stats after. Do NOT save yet.`,
+ },
+ {
+ label: 'Step 3/3: Reason + save',
+ prompt: `Run onto_reason (profile "rdfs"), then onto_stats, then onto_save ("~/.open-ontologies/studio-live.ttl"). Report final statistics.`,
+ },
+ ];
+ send({ type: 'text', content: `**Sketching ontology: ${domain}** (3 steps)\n` });
+ for (let i = 0; i < steps.length; i++) {
+ const step = steps[i];
+ send({ type: 'text', content: `\n---\n**${step.label}**` });
+ try {
+ await runTurn(step.prompt);
+ }
+ catch (e) {
+ send({ type: 'text', content: `Step failed: ${e}. Continuing...` });
+ }
+ }
+ send({ type: 'text', content: `\n---\n**Sketch complete.** Use /expand to deepen any branch.` });
}
// --- Handle a chat message ---
async function handleMessage(userMessage) {
try {
- if (isBuildRequest(userMessage)) {
+ if (isSketchRequest(userMessage)) {
+ const domain = extractDomain(userMessage);
+ sessionId = undefined;
+ await handleSketch(domain);
+ send({ type: 'done', mutated: true });
+ }
+ else if (isBuildRequest(userMessage)) {
const domain = extractDomain(userMessage);
- // Fresh session for each new build
sessionId = undefined;
await handleBuild(domain);
send({ type: 'done', mutated: true });
diff --git a/studio/src-tauri/sidecars/agent/index.ts b/studio/src-tauri/sidecars/agent/index.ts
index 28c32560..287282a2 100644
--- a/studio/src-tauri/sidecars/agent/index.ts
+++ b/studio/src-tauri/sidecars/agent/index.ts
@@ -112,10 +112,14 @@ function isBuildRequest(msg: string): boolean {
&& (lower.includes('ontology') || lower.includes('about'));
}
+function isSketchRequest(msg: string): boolean {
+ return msg.toLowerCase().includes('sketch');
+}
+
function extractDomain(msg: string): string {
const patterns = [
/(?:about|for|on|of)\s+(.+)/i,
- /(?:build|create|make|generate)\s+(?:a\s+|an\s+|the\s+)?(?:\w+\s+)?(?:ontology\s+)?(?:about|for|on|of)\s+(.+)/i,
+ /(?:build|create|make|generate|sketch)\s+(?:a\s+|an\s+|the\s+)?(?:\w+\s+)?(?:ontology\s+)?(?:about|for|on|of)\s+(.+)/i,
];
for (const p of patterns) {
const m = msg.match(p);
@@ -124,74 +128,221 @@ function extractDomain(msg: string): string {
if (match) return match.trim().replace(/[.!?]+$/, '');
}
}
- return msg.replace(/^(build|create|make|generate)\s+(an?\s+)?ontology\s*/i, '').trim() || msg;
+ return msg.replace(/^(build|create|make|generate|sketch)\s+(an?\s+)?ontology\s*/i, '').trim() || msg;
}
// --- Multi-step build within ONE session ---
async function handleBuild(domain: string): Promise<void> {
const ns = domain.toLowerCase().replace(/[^a-z0-9]+/g, '-');
+ const prefix = `@prefix : <http://example.org/${ns}#> .`;
+
+ const DEEPEN = (branchDesc: string) =>
+ `Call onto_query to find leaf classes (classes with no subclasses) in ${branchDesc}. ` +
+ `Then call onto_load with Turtle using the SAME namespace adding more subclass levels. ` +
+ `For every leaf, ask yourself: "Can this be subdivided further?" If yes, add 3-8 subclasses. ` +
+ `Enumerate ALL real-world subtypes — do not stop at 2-3 examples. ` +
+ `Every class needs rdfs:label and rdfs:comment. ` +
+ `IMPORTANT: Add at most 80-120 classes in this step, then stop. Call onto_stats after. Do NOT save yet.`;
+
+ const steps = [
+ {
+ label: 'Step 1: Foundation — root + major branches + 3 levels',
+ prompt: `Build an ontology about "${domain}". Use namespace ${prefix}
+
+Call onto_clear. Then call onto_load with Turtle containing:
+- An owl:Ontology declaration
+- A root class for the domain
+- 8-12 major branch classes under the root (think of ALL major aspects/dimensions of "${domain}")
+- For each branch, 4-8 subclasses
+- For each of those, 3-6 further subclasses
+- Every class MUST have rdfs:label and rdfs:comment
+- Be EXHAUSTIVE — list every subtype you can think of
- // Step 1: Clear + load classes
- send({ type: 'text', content: `**Building ontology: ${domain}**\n\n---\n**Step 1/4:** Classes hierarchy...` });
- await runTurn(
- `Build an ontology about "${domain}". Use namespace @prefix : <http://example.org/${ns}#> .
+Call onto_stats after. Do NOT save yet — many more steps coming.`,
+ },
+ {
+ label: 'Step 2: Deepen — first major branch to maximum depth',
+ prompt: DEEPEN('the FIRST major branch (the first child of the root)'),
+ },
+ {
+ label: 'Step 3: Deepen — second major branch to maximum depth',
+ prompt: DEEPEN('the SECOND major branch'),
+ },
+ {
+ label: 'Step 4: Deepen — third major branch to maximum depth',
+ prompt: DEEPEN('the THIRD major branch'),
+ },
+ {
+ label: 'Step 5: Deepen — fourth and fifth major branches',
+ prompt: DEEPEN('the FOURTH and FIFTH major branches'),
+ },
+ {
+ label: 'Step 6: Deepen — all remaining branches',
+ prompt: DEEPEN('ALL remaining major branches that have not been deepened yet'),
+ },
+ {
+ label: 'Step 7: Deepen — second pass on shallow areas',
+ prompt: `Call onto_query to find ALL leaf classes. For any branch that is still less than 6 levels deep, add more subclasses. ` +
+ `Also think: are there any major subtypes or categories I missed entirely? Add them now. ` +
+ `Be ruthlessly exhaustive. Call onto_load with the additional Turtle. Call onto_stats after. Do NOT save yet.`,
+ },
+ {
+ label: 'Step 8: Object properties — structural relationships',
+ prompt: `Now add object properties. Call onto_load with Turtle containing 50-70 owl:ObjectProperty declarations.
+
+EVERY property MUST have: rdfs:domain, rdfs:range, rdfs:label, rdfs:comment.
+
+Cover ALL relationship types:
+- Compositional: hasPart/isPartOf, contains/isContainedIn, hasComponent/isComponentOf
+- Causal: causes, prevents, triggers, treats, inhibits, enables
+- Associative: isAssociatedWith, isRelatedTo, dependsOn, influences
+- Hierarchical: hasSubtype, isExampleOf, instantiates
+- Build rdfs:subPropertyOf hierarchies (3-4 levels deep)
+- Add owl:inverseOf for EVERY directional property
+- Mark owl:TransitiveProperty, owl:SymmetricProperty, owl:FunctionalProperty
+
+Call onto_stats after. Do NOT save yet.`,
+ },
+ {
+ label: 'Step 9: Object properties — roles, temporal, spatial',
+ prompt: `Add MORE object properties. Call onto_load with Turtle containing 50-70 MORE owl:ObjectProperty declarations.
+
+Focus on what's MISSING — look at the classes and ask "how does X relate to Y?" for every pair of branches:
+- Role/participation: hasRole, participatesIn, performs, undergoes, produces, consumes
+- Temporal: precedes, follows, during, overlaps, startsWith, endsWith
+- Spatial: isLocatedIn, isNear, surrounds, isAdjacentTo, isWithin
+- Ownership: owns, belongsTo, isOwnedBy, manages, controls
+- Agent: hasAgent, hasPatient, hasBeneficiary, hasInstrument
+- More rdfs:subPropertyOf hierarchies and owl:inverseOf pairs
+
+Call onto_stats after. Do NOT save yet.`,
+ },
+ {
+ label: 'Step 10: Datatype properties — all attributes',
+ prompt: `Add datatype properties. Call onto_load with Turtle containing 40-60 owl:DatatypeProperty declarations.
+
+Each with: rdfs:domain, rdfs:range (xsd types), rdfs:label, rdfs:comment.
+
+Go through EVERY major branch and add ALL attributes:
+- Identifiers, names, codes, labels, descriptions, titles
+- Dates (birth, creation, modification, expiry, start, end)
+- Quantities (weight, height, length, count, duration, price, score, rating, percentage)
+- Measurements (temperature, speed, volume, area, concentration)
+- Boolean flags (isActive, isVerified, isPublic, isRequired, isOptional, isDeprecated)
+- Statuses, categories, priorities, levels, grades
+- Text fields (notes, comments, instructions, definitions)
+
+Call onto_stats after. Do NOT save yet.`,
+ },
+ {
+ label: 'Step 11: Axioms — disjoints everywhere',
+ prompt: `Add disjoint axioms. Call onto_load with Turtle containing owl:disjointWith between ALL sibling classes that cannot overlap.
-Step 1: Call onto_clear. Then call onto_load with Turtle containing:
-- 80-150 owl:Class declarations organised in a subClassOf hierarchy 5-7 levels deep
-- A root class, 5-8 major branches, each with 3-6 sub-branches, each with 2-5 leaves
-- Enumerate ALL real-world subtypes exhaustively
-- Every class MUST have rdfs:label and rdfs:comment
+Go through EVERY branch systematically:
+- Root children: all major branches that are mutually exclusive
+- Within each branch: siblings that cannot overlap
+- Target: 60+ disjoint axiom pairs minimum
-Call onto_stats after loading to verify. Do NOT call onto_save yet — more content coming.`
- );
+Call onto_stats after. Do NOT save yet.`,
+ },
+ {
+ label: 'Step 12: Individuals — real-world examples',
+ prompt: `Add named individuals. Call onto_load with Turtle containing 25-40 owl:NamedIndividual instances.
- // Step 2: Properties (SAME session — agent has context)
- send({ type: 'text', content: `\n---\n**Step 2/4:** Properties...` });
- await runTurn(
- `Now add properties to the ontology. Call onto_load with Turtle using the SAME namespace as before containing:
+Spread them across ALL major branches — at least 3-4 individuals per branch.
+Each individual needs:
+- rdf:type (the most specific class)
+- rdfs:label and rdfs:comment
+- 3-5 property values (both object and datatype properties)
-- 25-40 owl:ObjectProperty declarations, each with rdfs:domain, rdfs:range, rdfs:label, rdfs:comment
-- Build rdfs:subPropertyOf hierarchies (e.g., hasParticipant > hasAgent > hasDriver)
-- Add owl:inverseOf pairs for bidirectional relationships
-- Mark owl:TransitiveProperty (isPartOf), owl:SymmetricProperty (isRelatedTo), owl:FunctionalProperty where appropriate
-- 15-20 owl:DatatypeProperty declarations with rdfs:domain, rdfs:range (xsd types), rdfs:label, rdfs:comment
+Call onto_stats after. Do NOT save yet.`,
+ },
+ {
+ label: 'Step 13: Reason + save',
+ prompt: `Final step. Run:
+1. onto_reason with profile "rdfs"
+2. onto_stats — report the final counts
+3. onto_save with path "~/.open-ontologies/studio-live.ttl"
-Call onto_stats after loading. Do NOT call onto_save yet.`
- );
+Report the final ontology statistics.`,
+ },
+ ];
- // Step 3: Axioms + individuals (SAME session)
- send({ type: 'text', content: `\n---\n**Step 3/4:** Axioms + individuals...` });
- await runTurn(
- `Now add axioms and individuals. Call onto_load with Turtle using the SAME namespace containing:
+ send({ type: 'text', content: `**Building maximum-depth ontology: ${domain}** (${steps.length} steps)\n` });
-- owl:disjointWith between ALL sibling classes that cannot overlap
-- 15-20 owl:NamedIndividual instances — real-world examples with rdf:type and property values
+ for (let i = 0; i < steps.length; i++) {
+ const step = steps[i];
+ send({ type: 'text', content: `\n---\n**${step.label}**` });
+ try {
+ await runTurn(step.prompt);
+ } catch (e) {
+ send({ type: 'text', content: `Step failed: ${e}. Continuing...` });
+ }
+ }
-Call onto_stats after loading. Do NOT call onto_save yet.`
- );
+ send({ type: 'text', content: `\n---\n**Build complete.** The graph should now be visible in the tree view.` });
+}
- // Step 4: Reason + save (SAME session)
- send({ type: 'text', content: `\n---\n**Step 4/4:** Reasoning + save...` });
- await runTurn(
- `Final step. Run:
-1. onto_reason with profile "rdfs"
-2. onto_stats — report the final counts
-3. onto_save with path "~/.open-ontologies/studio-live.ttl"
+// --- Quick sketch: 3-step lightweight build ---
+
+async function handleSketch(domain: string): Promise<void> {
+ const ns = domain.toLowerCase().replace(/[^a-z0-9]+/g, '-');
+
+ const steps = [
+ {
+ label: 'Step 1/3: Classes + properties',
+ prompt: `Build a quick ontology about "${domain}". Use namespace @prefix : <http://example.org/${ns}#> .
+
+Call onto_clear. Then call onto_load with ONE Turtle block containing:
+- 50-80 owl:Class in a subClassOf hierarchy 4-5 levels deep
+- 15-25 owl:ObjectProperty each with rdfs:domain, rdfs:range, rdfs:label, rdfs:comment
+- 8-12 owl:DatatypeProperty
+- owl:inverseOf pairs for key relationships
+- rdfs:label and rdfs:comment on every class and property
-Report the final ontology statistics.`
- );
+Call onto_stats after. Do NOT save yet.`,
+ },
+ {
+ label: 'Step 2/3: Axioms + individuals',
+ prompt: `Call onto_load with Turtle adding:
+- owl:disjointWith between sibling classes (15+ pairs)
+- 10-15 owl:NamedIndividual with rdf:type and property values
- send({ type: 'text', content: `\n---\n**Build complete.** Refresh the tree view to see the full graph.` });
+Call onto_stats after. Do NOT save yet.`,
+ },
+ {
+ label: 'Step 3/3: Reason + save',
+ prompt: `Run onto_reason (profile "rdfs"), then onto_stats, then onto_save ("~/.open-ontologies/studio-live.ttl"). Report final statistics.`,
+ },
+ ];
+
+ send({ type: 'text', content: `**Sketching ontology: ${domain}** (3 steps)\n` });
+
+ for (let i = 0; i < steps.length; i++) {
+ const step = steps[i];
+ send({ type: 'text', content: `\n---\n**${step.label}**` });
+ try {
+ await runTurn(step.prompt);
+ } catch (e) {
+ send({ type: 'text', content: `Step failed: ${e}. Continuing...` });
+ }
+ }
+
+ send({ type: 'text', content: `\n---\n**Sketch complete.** Use /expand to deepen any branch.` });
}
// --- Handle a chat message ---
async function handleMessage(userMessage: string): Promise<void> {
try {
- if (isBuildRequest(userMessage)) {
+ if (isSketchRequest(userMessage)) {
+ const domain = extractDomain(userMessage);
+ sessionId = undefined;
+ await handleSketch(domain);
+ send({ type: 'done', mutated: true });
+ } else if (isBuildRequest(userMessage)) {
const domain = extractDomain(userMessage);
- // Fresh session for each new build
sessionId = undefined;
await handleBuild(domain);
send({ type: 'done', mutated: true });
diff --git a/studio/src/components/ChatPanel.tsx b/studio/src/components/ChatPanel.tsx
index 6e67905a..5ec4ea72 100644
--- a/studio/src/components/ChatPanel.tsx
+++ b/studio/src/components/ChatPanel.tsx
@@ -3,7 +3,8 @@ import ReactMarkdown from 'react-markdown';
import { useChat, ChatMessage } from '../hooks/useChat';
const SLASH_COMMANDS = [
- { cmd: '/build', description: 'Build a new ontology', prompt: 'Build an ontology about ' },
+ { cmd: '/build', description: 'Deep build (IES-level)', prompt: 'Build an ontology about ' },
+ { cmd: '/sketch', description: 'Quick sketch (prototype)', prompt: 'Sketch an ontology about ' },
{ cmd: '/expand', description: 'Expand current ontology', prompt: 'Expand the current ontology with more detail about ' },
{ cmd: '/validate', description: 'Run full validation', prompt: 'Run onto_validate and onto_lint on the current ontology and report all issues' },
{ cmd: '/reason', description: 'Run OWL reasoning', prompt: 'Run onto_reason with profile owl-rl on the current ontology' },
diff --git a/studio/src/components/Layout.tsx b/studio/src/components/Layout.tsx
index aae4644b..951815f2 100644
--- a/studio/src/components/Layout.tsx
+++ b/studio/src/components/Layout.tsx
@@ -1,13 +1,12 @@
import { useState, useEffect, useRef } from 'react';
import { useEngine } from '../hooks/useEngine';
-import { GraphCanvas } from './GraphCanvas';
import { TreeView } from './TreeView';
import { ChatPanel } from './ChatPanel';
import { PropertyInspector } from './PropertyInspector';
import { LineagePanel } from './LineagePanel';
import * as mcp from '../lib/mcp-client';
-type ViewMode = '3d' | 'tree';
+type ViewMode = 'tree';
export function Layout() {
const [showChat, setShowChat] = useState(true);
@@ -122,14 +121,6 @@ export function Layout() {
<div className="ml-auto flex gap-2">
{/* View mode toggle */}
- {(['tree', '3d'] as ViewMode[]).map(mode => (
- <button key={mode} onClick={() => setViewMode(mode)}
- className="text-xs px-2 py-1 rounded"
- style={{ background: viewMode === mode ? 'var(--accent)' : 'var(--bg-panel)',
- color: viewMode === mode ? 'var(--bg-primary)' : 'var(--text-secondary)' }}>
- {mode === '3d' ? '3D' : 'Tree'}
- </button>
- ))}
<div className="w-px mx-1" style={{ background: 'var(--border)' }} />
<button onClick={() => setShowChat(!showChat)}
className="text-xs px-2 py-1 rounded"
@@ -156,11 +147,7 @@ export function Layout() {
<div className="flex-1 flex overflow-hidden">
{/* Graph canvas */}
<div className="flex-1 relative">
- {viewMode === 'tree' ? (
- <TreeView onNodeSelect={setSelectedNode} />
- ) : (
- <GraphCanvas onNodeSelect={setSelectedNode} />
- )}
+ <TreeView onNodeSelect={setSelectedNode} />
</div>
{/* Inspector panel */}
diff --git a/studio/src/components/TreeView.tsx b/studio/src/components/TreeView.tsx
index 01cd7b4e..af71b395 100644
--- a/studio/src/components/TreeView.tsx
+++ b/studio/src/components/TreeView.tsx
@@ -1,5 +1,4 @@
-import { useRef, useEffect, useCallback, useState } from 'react';
-import * as d3 from 'd3';
+import { useState, useEffect, useCallback, useRef, useMemo } from 'react';
import * as mcp from '../lib/mcp-client';
import { useEngine } from '../hooks/useEngine';
@@ -11,24 +10,16 @@ interface SparqlBinding {
[key: string]: { type: string; value: string };
}
-type NodeType = 'Class' | 'ObjectProperty' | 'DatatypeProperty' | 'AnnotationProperty' | 'Individual' | 'Restriction' | 'Ontology';
+type NodeType = 'Class' | 'ObjectProperty' | 'DatatypeProperty' | 'Individual';
-interface OntologyNode {
+interface TreeNode {
id: string;
label: string;
uri: string;
nodeType: NodeType;
- children: OntologyNode[];
- _childCount?: number;
- propertyCount?: number; // how many properties reference this class
- connectionCount?: number; // total edges (for sizing)
-}
-
-interface CrossLink {
- sourceId: string;
- targetId: string;
- label: string;
- linkType: 'domain' | 'range' | 'equivalentClass' | 'disjointWith' | 'restriction';
+ children: TreeNode[];
+ childCount: number;
+ depth: number;
}
function parseSparqlResults(text: string): SparqlBinding[] {
@@ -61,844 +52,532 @@ function shortUri(uri: string): string {
return uri;
}
-// Node type colors (Catppuccin Mocha)
-const NODE_TYPE_COLORS: Record<NodeType, string> = {
- Ontology: '#f38ba8', // pink
- Class: '#89b4fa', // blue
- ObjectProperty: '#a6e3a1', // green
- DatatypeProperty: '#f9e2af', // yellow
- AnnotationProperty: '#cba6f7', // mauve
- Individual: '#fab387', // peach
- Restriction: '#94e2d5', // teal
-};
-
-const NODE_TYPE_SIZES: Record<NodeType, number> = {
- Ontology: 14,
- Class: 8,
- ObjectProperty: 7,
- DatatypeProperty: 6,
- AnnotationProperty: 5,
- Individual: 6,
- Restriction: 5,
+const TYPE_COLORS: Record<NodeType, string> = {
+ Class: '#89b4fa',
+ ObjectProperty: '#a6e3a1',
+ DatatypeProperty: '#f9e2af',
+ Individual: '#fab387',
};
-function getColor(type: NodeType): string {
- return NODE_TYPE_COLORS[type] ?? '#a6adc8';
-}
-
-function getSize(type: NodeType, connectionCount: number): number {
- const base = NODE_TYPE_SIZES[type] ?? 6;
- // Scale up for highly connected nodes
- return base + Math.min(connectionCount * 0.3, 6);
-}
-
-// ── SPARQL Queries ──────────────────────────────────────────────
-
-const CLASSES_QUERY = `PREFIX owl: <http://www.w3.org/2002/07/owl#>
+const QUERIES = {
+ classes: `PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?c ?label WHERE {
{ ?c a owl:Class } UNION { ?c a rdfs:Class }
OPTIONAL { ?c rdfs:label ?label }
FILTER(!isBlank(?c))
-}`;
-
-const SUBCLASS_QUERY = `PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
+}`,
+ subclass: `PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
SELECT ?sub ?parent WHERE {
?sub rdfs:subClassOf ?parent .
{ ?sub a owl:Class } UNION { ?sub a rdfs:Class }
FILTER(!isBlank(?sub) && !isBlank(?parent))
-}`;
-
-const OBJ_PROPS_QUERY = `PREFIX owl: <http://www.w3.org/2002/07/owl#>
+}`,
+ objProps: `PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
-SELECT ?p ?label ?domain ?range ?parent WHERE {
+SELECT ?p ?label ?parent WHERE {
{ ?p a owl:ObjectProperty } UNION { ?p a rdf:Property }
OPTIONAL { ?p rdfs:label ?label }
- OPTIONAL { ?p rdfs:domain ?domain . FILTER(!isBlank(?domain)) }
- OPTIONAL { ?p rdfs:range ?range . FILTER(!isBlank(?range)) }
- OPTIONAL { ?p rdfs:subPropertyOf ?parent . FILTER(!isBlank(?parent)) }
- FILTER(!isBlank(?p))
-}`;
-
-const DATA_PROPS_QUERY = `PREFIX owl: <http://www.w3.org/2002/07/owl#>
-PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
-SELECT ?p ?label ?domain ?parent WHERE {
- ?p a owl:DatatypeProperty .
- OPTIONAL { ?p rdfs:label ?label }
- OPTIONAL { ?p rdfs:domain ?domain . FILTER(!isBlank(?domain)) }
OPTIONAL { ?p rdfs:subPropertyOf ?parent . FILTER(!isBlank(?parent)) }
FILTER(!isBlank(?p))
-}`;
-
-const ANNOTATION_PROPS_QUERY = `PREFIX owl: <http://www.w3.org/2002/07/owl#>
+}`,
+ dataProps: `PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
SELECT ?p ?label WHERE {
- ?p a owl:AnnotationProperty .
+ ?p a owl:DatatypeProperty .
OPTIONAL { ?p rdfs:label ?label }
FILTER(!isBlank(?p))
-}`;
-
-const INDIVIDUALS_QUERY = `PREFIX owl: <http://www.w3.org/2002/07/owl#>
+}`,
+ individuals: `PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
-PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
SELECT ?ind ?label ?type WHERE {
- ?ind a ?type .
- ?type a owl:Class .
+ ?ind a ?type . ?type a owl:Class .
OPTIONAL { ?ind rdfs:label ?label }
FILTER(!isBlank(?ind) && ?type != owl:Class && ?type != rdfs:Class && ?type != owl:ObjectProperty && ?type != owl:DatatypeProperty && ?type != owl:AnnotationProperty && ?type != owl:NamedIndividual)
-} LIMIT 200`;
-
-const EQUIV_QUERY = `PREFIX owl: <http://www.w3.org/2002/07/owl#>
-SELECT ?a ?b WHERE {
- ?a owl:equivalentClass ?b .
- FILTER(!isBlank(?a) && !isBlank(?b) && ?a != ?b)
-}`;
-
-const DISJOINT_QUERY = `PREFIX owl: <http://www.w3.org/2002/07/owl#>
-SELECT ?a ?b WHERE {
- ?a owl:disjointWith ?b .
- FILTER(!isBlank(?a) && !isBlank(?b))
-}`;
-
-function countDescendants(node: OntologyNode): number {
- if (!node.children || node.children.length === 0) return 0;
+} LIMIT 500`,
+};
+
+function countDescendants(node: TreeNode): number {
let count = node.children.length;
for (const c of node.children) count += countDescendants(c);
+ node.childCount = count;
return count;
}
-export function TreeView({ onNodeSelect }: TreeViewProps) {
- const containerRef = useRef<HTMLDivElement>(null);
- const svgRef = useRef<SVGSVGElement | null>(null);
- const gRef = useRef<d3.Selection<SVGGElement, unknown, null, undefined> | null>(null);
- const zoomRef = useRef<d3.ZoomBehavior<SVGSVGElement, unknown> | null>(null);
- const treeDataRef = useRef<OntologyNode | null>(null);
- const crossLinksRef = useRef<CrossLink[]>([]);
- const collapsedRef = useRef<Set<string>>(new Set());
- const hiddenTypesRef = useRef<Set<NodeType>>(new Set());
- const { status, refreshStats } = useEngine();
- const [stats, setStats] = useState({ classes: 0, properties: 0, individuals: 0, depth: 0, crossLinks: 0 });
- const [hiddenTypes, setHiddenTypes] = useState<Set<NodeType>>(new Set());
- const [typeCounts, setTypeCounts] = useState<Map<NodeType, number>>(new Map());
-
- const filterTree = useCallback((node: OntologyNode): OntologyNode => {
- const copy = { ...node };
- if (collapsedRef.current.has(node.id)) {
- copy._childCount = countDescendants(node);
- copy.children = [];
- } else if (node.children) {
- copy.children = node.children
- .filter(c => !hiddenTypesRef.current.has(c.nodeType))
- .map(c => filterTree(c));
- }
- return copy;
- }, []);
-
- const renderTree = useCallback(() => {
- const svg = svgRef.current;
- const g = gRef.current;
- const treeData = treeDataRef.current;
- if (!svg || !g || !treeData) return;
-
- g.selectAll('*').remove();
-
- const filteredData = filterTree(treeData);
- const hierarchy = d3.hierarchy(filteredData, d => d.children);
-
- const leafCount = hierarchy.leaves().length;
- const rect = containerRef.current?.getBoundingClientRect();
- const viewW = rect?.width ?? 800;
- const viewH = rect?.height ?? 600;
-
- const treeHeight = Math.max(viewH - 80, leafCount * 24);
- const treeWidth = Math.max(viewW - 300, hierarchy.height * 200);
-
- const treeLayout = d3.tree<OntologyNode>()
- .size([treeHeight, treeWidth])
- .separation((a, b) => (a.parent === b.parent ? 1 : 1.4));
-
- treeLayout(hierarchy);
-
- const xOff = 180;
- const yOff = 40;
-
- // Build node position map for cross-links
- const nodePositions = new Map<string, { x: number; y: number }>();
- hierarchy.descendants().forEach(d => {
- nodePositions.set(d.data.id, { x: (d.y ?? 0) + xOff, y: (d.x ?? 0) + yOff });
- });
-
- // Draw tree links
- g.selectAll('.tree-link')
- .data(hierarchy.links())
- .enter().append('path')
- .attr('class', 'tree-link')
- .attr('fill', 'none')
- .attr('stroke', d => getColor(d.target.data.nodeType))
- .attr('stroke-opacity', 0.25)
- .attr('stroke-width', 1.5)
- .attr('d', d3.linkHorizontal<d3.HierarchyPointLink<OntologyNode>, d3.HierarchyPointNode<OntologyNode>>()
- .x(d => d.y + xOff)
- .y(d => d.x + yOff) as unknown as string);
-
- // Draw cross-links (domain→range, equivalentClass, disjointWith)
- const visibleCrossLinks = crossLinksRef.current.filter(cl => {
- const s = nodePositions.get(cl.sourceId);
- const t = nodePositions.get(cl.targetId);
- return s && t;
- });
+function matchesSearch(node: TreeNode, term: string): boolean {
+ if (node.label.toLowerCase().includes(term) || node.id.toLowerCase().includes(term)) return true;
+ return node.children.some(c => matchesSearch(c, term));
+}
- const crossLinkColor: Record<string, string> = {
- domain: '#a6e3a199',
- range: '#f9e2af99',
- equivalentClass: '#cba6f799',
- disjointWith: '#f38ba866',
- restriction: '#94e2d566',
- };
-
- g.selectAll('.cross-link')
- .data(visibleCrossLinks)
- .enter().append('path')
- .attr('class', 'cross-link')
- .attr('fill', 'none')
- .attr('stroke', d => crossLinkColor[d.linkType] ?? '#585b7044')
- .attr('stroke-opacity', 0.2)
- .attr('stroke-width', 1)
- .attr('stroke-dasharray', d => d.linkType === 'disjointWith' ? '2,4' : '4,4')
- .attr('d', d => {
- const s = nodePositions.get(d.sourceId)!;
- const t = nodePositions.get(d.targetId)!;
- const mx = (s.x + t.x) / 2;
- return `M${s.x},${s.y}C${mx},${s.y},${mx},${t.y},${t.x},${t.y}`;
- });
+// Flatten visible nodes for virtualized rendering
+interface FlatNode {
+ node: TreeNode;
+ indent: number;
+ isExpanded: boolean;
+ hasChildren: boolean;
+}
- // Draw nodes
- const nodes = g.selectAll('.tree-node')
- .data(hierarchy.descendants())
- .enter().append('g')
- .attr('class', d => {
- let cls = 'tree-node';
- if (collapsedRef.current.has(d.data.id)) cls += ' collapsed';
- return cls;
- })
- .attr('transform', d => `translate(${(d.y ?? 0) + xOff},${(d.x ?? 0) + yOff})`)
- .style('cursor', 'pointer')
- .on('click', (event: MouseEvent, d) => {
- event.stopPropagation();
- if (event.shiftKey || event.metaKey) {
- toggleCollapse(d.data.id);
- } else {
- if (d.data.id === '__ontology__') return;
- onNodeSelect({ id: d.data.id, label: d.data.label, uri: d.data.uri });
- }
- })
- .on('dblclick', (event: MouseEvent, d) => {
- event.stopPropagation();
- toggleCollapse(d.data.id);
- })
- .on('mouseover', function(_event: MouseEvent, d) {
- d3.select(this).select('circle')
- .attr('stroke', '#cdd6f4').attr('stroke-width', 3);
-
- // Highlight path to root
- const ancestors = new Set<string>();
- let current: d3.HierarchyNode<OntologyNode> | null = d;
- while (current) { ancestors.add(current.data.id); current = current.parent; }
-
- g.selectAll<SVGPathElement, d3.HierarchyPointLink<OntologyNode>>('.tree-link')
- .attr('stroke-opacity', l =>
- ancestors.has(l.source.data.id) && ancestors.has(l.target.data.id) ? 0.8 : 0.08
- )
- .attr('stroke-width', l =>
- ancestors.has(l.source.data.id) && ancestors.has(l.target.data.id) ? 2.5 : 1
- );
-
- // Highlight connected cross-links
- g.selectAll<SVGPathElement, CrossLink>('.cross-link')
- .attr('stroke-opacity', cl =>
- cl.sourceId === d.data.id || cl.targetId === d.data.id ? 0.6 : 0.05
- )
- .attr('stroke-width', cl =>
- cl.sourceId === d.data.id || cl.targetId === d.data.id ? 2 : 1
- );
- })
- .on('mouseout', function() {
- d3.select(this).select('circle')
- .attr('stroke', '#1e1e2e').attr('stroke-width', 2);
- g.selectAll('.tree-link').attr('stroke-opacity', 0.25).attr('stroke-width', 1.5);
- g.selectAll('.cross-link').attr('stroke-opacity', 0.2).attr('stroke-width', 1);
- });
+function flattenTree(nodes: TreeNode[], expanded: Set<string>, searchTerm: string, indent: number): FlatNode[] {
+ const result: FlatNode[] = [];
+ for (const node of nodes) {
+ const visible = !searchTerm || matchesSearch(node, searchTerm);
+ if (!visible) continue;
+ const isExpanded = expanded.has(node.id);
+ result.push({ node, indent, isExpanded, hasChildren: node.children.length > 0 });
+ if (isExpanded) {
+ const children = searchTerm
+ ? node.children.filter(c => matchesSearch(c, searchTerm))
+ : node.children;
+ result.push(...flattenTree(children, expanded, searchTerm, indent + 1));
+ }
+ }
+ return result;
+}
- // Node circles — different shapes per type
- nodes.append('circle')
- .attr('r', d => getSize(d.data.nodeType, d.data.connectionCount ?? 0))
- .attr('fill', d => getColor(d.data.nodeType))
- .attr('stroke', '#1e1e2e')
- .attr('stroke-width', 2)
- .style('filter', 'drop-shadow(0 0 4px rgba(0,0,0,0.4))');
-
- // Property nodes get a diamond overlay
- nodes.filter(d => d.data.nodeType === 'ObjectProperty' || d.data.nodeType === 'DatatypeProperty')
- .append('rect')
- .attr('x', d => -getSize(d.data.nodeType, 0) * 0.5)
- .attr('y', d => -getSize(d.data.nodeType, 0) * 0.5)
- .attr('width', d => getSize(d.data.nodeType, 0))
- .attr('height', d => getSize(d.data.nodeType, 0))
- .attr('transform', 'rotate(45)')
- .attr('fill', 'none')
- .attr('stroke', d => getColor(d.data.nodeType))
- .attr('stroke-width', 1)
- .attr('stroke-opacity', 0.4)
- .style('pointer-events', 'none');
-
- // Collapse indicator
- nodes.filter(d => (d.data.children && d.data.children.length > 0) || (d.data._childCount != null && d.data._childCount > 0))
- .append('text')
- .attr('x', 0).attr('y', 1)
- .attr('text-anchor', 'middle')
- .attr('dominant-baseline', 'central')
- .style('font-size', d => `${getSize(d.data.nodeType, 0) * 1.1}px`)
- .style('fill', '#1e1e2e')
- .style('font-weight', '700')
- .style('pointer-events', 'none')
- .text(d => collapsedRef.current.has(d.data.id) ? '+' : '');
-
- // Collapsed child count
- nodes.filter(d => d.data._childCount != null && d.data._childCount > 0)
- .append('text')
- .attr('x', d => getSize(d.data.nodeType, 0) + 4)
- .attr('y', -8)
- .attr('text-anchor', 'start')
- .style('font-size', '9px')
- .style('fill', '#6c7086')
- .style('pointer-events', 'none')
- .text(d => `(${d.data._childCount})`);
-
- // Labels
- nodes.append('text')
- .attr('x', d => getSize(d.data.nodeType, d.data.connectionCount ?? 0) + 8)
- .attr('y', 4)
- .attr('text-anchor', 'start')
- .style('font-size', d => d.data.nodeType === 'Ontology' ? '14px' : d.depth <= 2 ? '12px' : '11px')
- .style('font-weight', d => d.data.children.length > 0 || d.data.nodeType === 'Ontology' ? '600' : '400')
- .style('fill', d => d.data.nodeType === 'Ontology' ? '#cdd6f4' : '#bac2de')
- .style('font-family', '-apple-system, BlinkMacSystemFont, "SF Pro Text", sans-serif')
- .style('pointer-events', 'none')
- .text(d => {
- const lbl = d.data.label || d.data.id;
- return lbl.length > 45 ? lbl.substring(0, 45) + '...' : lbl;
- });
+const ROW_HEIGHT = 28;
+const OVERSCAN = 20;
- // Property count badge on classes
- nodes.filter(d => d.data.nodeType === 'Class' && d.data.propertyCount != null && d.data.propertyCount > 0)
- .append('text')
- .attr('x', d => {
- const lbl = d.data.label || d.data.id;
- const truncated = lbl.length > 45 ? lbl.substring(0, 45) + '...' : lbl;
- return getSize(d.data.nodeType, d.data.connectionCount ?? 0) + 8 + truncated.length * 6.5;
- })
- .attr('y', 4)
- .attr('text-anchor', 'start')
- .style('font-size', '9px')
- .style('fill', '#585b70')
- .style('pointer-events', 'none')
- .text(d => `[${d.data.propertyCount}p]`);
-
- }, [filterTree, onNodeSelect]);
-
- const toggleCollapse = useCallback((id: string) => {
- if (collapsedRef.current.has(id)) collapsedRef.current.delete(id);
- else collapsedRef.current.add(id);
- renderTree();
- }, [renderTree]);
-
- const fitGraph = useCallback(() => {
- const svg = svgRef.current;
- const g = gRef.current;
- if (!svg || !g) return;
- const gNode = g.node();
- if (!gNode) return;
- const bbox = gNode.getBBox();
- if (bbox.width === 0 || bbox.height === 0) return;
-
- const rect = containerRef.current?.getBoundingClientRect();
- const w = rect?.width ?? 800;
- const h = rect?.height ?? 600;
-
- const pad = 60;
- const scale = Math.min(w / (bbox.width + pad * 2), h / (bbox.height + pad * 2), 1.5);
- const transform = d3.zoomIdentity
- .translate(w / 2, h / 2)
- .scale(scale)
- .translate(-(bbox.x + bbox.width / 2), -(bbox.y + bbox.height / 2));
-
- if (zoomRef.current) {
- d3.select(svg).transition().duration(500).call(zoomRef.current.transform, transform);
- }
- }, []);
+export function TreeView({ onNodeSelect }: TreeViewProps) {
+ const { status, refreshStats } = useEngine();
+ const [roots, setRoots] = useState<TreeNode[]>([]);
+ const [expanded, setExpanded] = useState<Set<string>>(new Set());
+ const [selectedId, setSelectedId] = useState<string | null>(null);
+ const [searchTerm, setSearchTerm] = useState('');
+ const [stats, setStats] = useState({ classes: 0, properties: 0, individuals: 0, depth: 0 });
+ const [typeCounts, setTypeCounts] = useState<Map<NodeType, number>>(new Map());
+ const [hiddenTypes, setHiddenTypes] = useState<Set<NodeType>>(new Set());
+ const scrollRef = useRef<HTMLDivElement>(null);
+ const [scrollTop, setScrollTop] = useState(0);
+ const [viewHeight, setViewHeight] = useState(600);
- // Load full ontology from SPARQL
const loadTree = useCallback(async () => {
try {
- const [classesText, subclassText, objPropsText, dataPropsText, annotPropsText, individualsText, equivText, disjointText] = await Promise.all([
- mcp.sparqlQuery(CLASSES_QUERY),
- mcp.sparqlQuery(SUBCLASS_QUERY),
- mcp.sparqlQuery(OBJ_PROPS_QUERY),
- mcp.sparqlQuery(DATA_PROPS_QUERY),
- mcp.sparqlQuery(ANNOTATION_PROPS_QUERY),
- mcp.sparqlQuery(INDIVIDUALS_QUERY),
- mcp.sparqlQuery(EQUIV_QUERY),
- mcp.sparqlQuery(DISJOINT_QUERY),
+ const [classesText, subclassText, objPropsText, dataPropsText, individualsText] = await Promise.all([
+ mcp.sparqlQuery(QUERIES.classes),
+ mcp.sparqlQuery(QUERIES.subclass),
+ mcp.sparqlQuery(QUERIES.objProps),
+ mcp.sparqlQuery(QUERIES.dataProps),
+ mcp.sparqlQuery(QUERIES.individuals),
]);
- const classBindings = parseSparqlResults(classesText);
- const subclassBindings = parseSparqlResults(subclassText);
- const objPropBindings = parseSparqlResults(objPropsText);
- const dataPropBindings = parseSparqlResults(dataPropsText);
- const annotPropBindings = parseSparqlResults(annotPropsText);
- const individualBindings = parseSparqlResults(individualsText);
- const equivBindings = parseSparqlResults(equivText);
- const disjointBindings = parseSparqlResults(disjointText);
-
- // ── Build node registry ────────────────────────────────────
- const allNodes = new Map<string, { label: string; uri: string; nodeType: NodeType; connections: number; propertyCount: number }>();
+ const nodeMap = new Map<string, { label: string; uri: string; nodeType: NodeType }>();
const parentToChildren = new Map<string, Set<string>>();
const hasParent = new Set<string>();
- const crossLinks: CrossLink[] = [];
-
- // Track property counts per class
- const classPropertyCount = new Map<string, number>();
// Classes
- for (const b of classBindings) {
- const uri = b.c?.value;
- if (!uri) continue;
+ for (const b of parseSparqlResults(classesText)) {
+ const uri = b.c?.value; if (!uri) continue;
const id = shortUri(uri);
- if (!allNodes.has(id)) {
- allNodes.set(id, { label: b.label?.value || id, uri, nodeType: 'Class', connections: 0, propertyCount: 0 });
- }
+ if (!nodeMap.has(id)) nodeMap.set(id, { label: b.label?.value || id, uri, nodeType: 'Class' });
}
- // SubClass edges (tree structure)
- for (const b of subclassBindings) {
- const subUri = b.sub?.value;
- const parentUri = b.parent?.value;
+ // SubClass hierarchy
+ for (const b of parseSparqlResults(subclassText)) {
+ const subUri = b.sub?.value, parentUri = b.parent?.value;
if (!subUri || !parentUri) continue;
- const sid = shortUri(subUri);
- const pid = shortUri(parentUri);
-
- if (!allNodes.has(pid)) {
- allNodes.set(pid, { label: pid, uri: parentUri, nodeType: 'Class', connections: 0, propertyCount: 0 });
- }
-
+ const sid = shortUri(subUri), pid = shortUri(parentUri);
+ if (!nodeMap.has(pid)) nodeMap.set(pid, { label: pid, uri: parentUri, nodeType: 'Class' });
if (!parentToChildren.has(pid)) parentToChildren.set(pid, new Set());
parentToChildren.get(pid)!.add(sid);
hasParent.add(sid);
-
- const pn = allNodes.get(pid)!; pn.connections++;
- const sn = allNodes.get(sid)!; sn.connections++;
}
- // Object properties — add to tree under a "Properties" branch, create cross-links for domain/range
+ // Object properties
const propParentToChildren = new Map<string, Set<string>>();
const propHasParent = new Set<string>();
- for (const b of objPropBindings) {
- const uri = b.p?.value;
- if (!uri) continue;
- const id = shortUri(uri);
- if (!allNodes.has(id)) {
- allNodes.set(id, { label: b.label?.value || id, uri, nodeType: 'ObjectProperty', connections: 0, propertyCount: 0 });
- }
- // Property hierarchy
- if (b.parent?.value) {
- const pid = shortUri(b.parent.value);
- if (!allNodes.has(pid)) {
- allNodes.set(pid, { label: pid, uri: b.parent.value, nodeType: 'ObjectProperty', connections: 0, propertyCount: 0 });
- }
- if (!propParentToChildren.has(pid)) propParentToChildren.set(pid, new Set());
- propParentToChildren.get(pid)!.add(id);
- propHasParent.add(id);
- }
- // Domain cross-link
- if (b.domain?.value) {
- const did = shortUri(b.domain.value);
- crossLinks.push({ sourceId: id, targetId: did, label: 'domain', linkType: 'domain' });
- const dn = allNodes.get(did);
- if (dn) { dn.connections++; dn.propertyCount++; }
- const pn = allNodes.get(id)!; pn.connections++;
- classPropertyCount.set(did, (classPropertyCount.get(did) ?? 0) + 1);
- }
- // Range cross-link
- if (b.range?.value) {
- const rid = shortUri(b.range.value);
- crossLinks.push({ sourceId: id, targetId: rid, label: 'range', linkType: 'range' });
- const rn = allNodes.get(rid);
- if (rn) rn.connections++;
- const pn = allNodes.get(id)!; pn.connections++;
- }
- }
-
- // Datatype properties
- for (const b of dataPropBindings) {
- const uri = b.p?.value;
- if (!uri) continue;
+ for (const b of parseSparqlResults(objPropsText)) {
+ const uri = b.p?.value; if (!uri) continue;
const id = shortUri(uri);
- if (!allNodes.has(id)) {
- allNodes.set(id, { label: b.label?.value || id, uri, nodeType: 'DatatypeProperty', connections: 0, propertyCount: 0 });
- }
+ if (!nodeMap.has(id)) nodeMap.set(id, { label: b.label?.value || id, uri, nodeType: 'ObjectProperty' });
if (b.parent?.value) {
const pid = shortUri(b.parent.value);
if (!propParentToChildren.has(pid)) propParentToChildren.set(pid, new Set());
propParentToChildren.get(pid)!.add(id);
propHasParent.add(id);
}
- if (b.domain?.value) {
- const did = shortUri(b.domain.value);
- crossLinks.push({ sourceId: id, targetId: did, label: 'domain', linkType: 'domain' });
- classPropertyCount.set(did, (classPropertyCount.get(did) ?? 0) + 1);
- }
}
- // Annotation properties
- for (const b of annotPropBindings) {
- const uri = b.p?.value;
- if (!uri) continue;
+ // Data properties
+ for (const b of parseSparqlResults(dataPropsText)) {
+ const uri = b.p?.value; if (!uri) continue;
const id = shortUri(uri);
- if (!allNodes.has(id)) {
- allNodes.set(id, { label: b.label?.value || id, uri, nodeType: 'AnnotationProperty', connections: 0, propertyCount: 0 });
- }
+ if (!nodeMap.has(id)) nodeMap.set(id, { label: b.label?.value || id, uri, nodeType: 'DatatypeProperty' });
}
// Individuals
- const individualsByClass = new Map<string, string[]>();
- for (const b of individualBindings) {
- const uri = b.ind?.value;
- const typeUri = b.type?.value;
+ const indsByClass = new Map<string, string[]>();
+ for (const b of parseSparqlResults(individualsText)) {
+ const uri = b.ind?.value, typeUri = b.type?.value;
if (!uri || !typeUri) continue;
- const id = shortUri(uri);
- const typeId = shortUri(typeUri);
- if (!allNodes.has(id)) {
- allNodes.set(id, { label: b.label?.value || id, uri, nodeType: 'Individual', connections: 0, propertyCount: 0 });
- }
- if (!individualsByClass.has(typeId)) individualsByClass.set(typeId, []);
- individualsByClass.get(typeId)!.push(id);
- }
-
- // Equivalent class cross-links
- for (const b of equivBindings) {
- const a = b.a?.value;
- const bv = b.b?.value;
- if (!a || !bv) continue;
- crossLinks.push({ sourceId: shortUri(a), targetId: shortUri(bv), label: 'equivalentClass', linkType: 'equivalentClass' });
- }
-
- // Disjoint cross-links
- for (const b of disjointBindings) {
- const a = b.a?.value;
- const bv = b.b?.value;
- if (!a || !bv) continue;
- crossLinks.push({ sourceId: shortUri(a), targetId: shortUri(bv), label: 'disjointWith', linkType: 'disjointWith' });
- }
-
- // Set property counts on nodes
- for (const [id, count] of classPropertyCount) {
- const node = allNodes.get(id);
- if (node) node.propertyCount = count;
+ const id = shortUri(uri), tid = shortUri(typeUri);
+ if (!nodeMap.has(id)) nodeMap.set(id, { label: b.label?.value || id, uri, nodeType: 'Individual' });
+ if (!indsByClass.has(tid)) indsByClass.set(tid, []);
+ indsByClass.get(tid)!.push(id);
}
- // ── Build tree hierarchy ───────────────────────────────────
+ // Build tree
const visited = new Set<string>();
- function buildClassNode(id: string): OntologyNode {
+ function buildClassTree(id: string, depth: number): TreeNode {
visited.add(id);
- const data = allNodes.get(id)!;
- const childIds = parentToChildren.get(id) ?? new Set();
- const children: OntologyNode[] = [];
- for (const cid of childIds) {
- if (!visited.has(cid) && allNodes.has(cid)) {
- children.push(buildClassNode(cid));
- }
+ const data = nodeMap.get(id)!;
+ const children: TreeNode[] = [];
+ for (const cid of parentToChildren.get(id) ?? new Set()) {
+ if (!visited.has(cid) && nodeMap.has(cid)) children.push(buildClassTree(cid, depth + 1));
}
- // Add individuals as leaf children
- const inds = individualsByClass.get(id) ?? [];
- for (const iid of inds) {
- if (!visited.has(iid) && allNodes.has(iid)) {
+ for (const iid of indsByClass.get(id) ?? []) {
+ if (!visited.has(iid) && nodeMap.has(iid)) {
visited.add(iid);
- const idata = allNodes.get(iid)!;
- children.push({ id: iid, label: idata.label, uri: idata.uri, nodeType: 'Individual', children: [], connectionCount: idata.connections, propertyCount: 0 });
+ const idata = nodeMap.get(iid)!;
+ children.push({ id: iid, label: idata.label, uri: idata.uri, nodeType: 'Individual', children: [], childCount: 0, depth: depth + 1 });
}
}
children.sort((a, b) => a.label.localeCompare(b.label));
- return { id, label: data.label, uri: data.uri, nodeType: data.nodeType, children, connectionCount: data.connections, propertyCount: data.propertyCount };
+ return { id, label: data.label, uri: data.uri, nodeType: data.nodeType, children, childCount: 0, depth };
}
- function buildPropNode(id: string): OntologyNode {
+ function buildPropTree(id: string, depth: number): TreeNode {
visited.add(id);
- const data = allNodes.get(id)!;
- const childIds = propParentToChildren.get(id) ?? new Set();
- const children: OntologyNode[] = [];
- for (const cid of childIds) {
- if (!visited.has(cid) && allNodes.has(cid)) {
- children.push(buildPropNode(cid));
- }
+ const data = nodeMap.get(id)!;
+ const children: TreeNode[] = [];
+ for (const cid of propParentToChildren.get(id) ?? new Set()) {
+ if (!visited.has(cid) && nodeMap.has(cid)) children.push(buildPropTree(cid, depth + 1));
}
children.sort((a, b) => a.label.localeCompare(b.label));
- return { id, label: data.label, uri: data.uri, nodeType: data.nodeType, children, connectionCount: data.connections, propertyCount: 0 };
+ return { id, label: data.label, uri: data.uri, nodeType: data.nodeType, children, childCount: 0, depth };
}
// Class roots
- const classRoots: string[] = [];
- for (const [id, data] of allNodes) {
- if (data.nodeType === 'Class' && !hasParent.has(id)) classRoots.push(id);
+ const classRoots: TreeNode[] = [];
+ for (const [id, data] of nodeMap) {
+ if (data.nodeType === 'Class' && !hasParent.has(id) && !visited.has(id)) {
+ classRoots.push(buildClassTree(id, 1));
+ }
}
- classRoots.sort((a, b) => (allNodes.get(a)!.label).localeCompare(allNodes.get(b)!.label));
- const classBranches = classRoots.map(id => buildClassNode(id));
-
// Orphan classes
- for (const [id, data] of allNodes) {
+ for (const [id, data] of nodeMap) {
if (data.nodeType === 'Class' && !visited.has(id)) {
- classBranches.push({ id, label: data.label, uri: data.uri, nodeType: 'Class', children: [], connectionCount: data.connections, propertyCount: data.propertyCount });
visited.add(id);
+ classRoots.push({ id, label: data.label, uri: data.uri, nodeType: 'Class', children: [], childCount: 0, depth: 1 });
}
}
+ classRoots.sort((a, b) => a.label.localeCompare(b.label));
// Property roots
- const propRoots: string[] = [];
- for (const [id, data] of allNodes) {
+ const propRoots: TreeNode[] = [];
+ for (const [id, data] of nodeMap) {
if ((data.nodeType === 'ObjectProperty' || data.nodeType === 'DatatypeProperty') && !propHasParent.has(id) && !visited.has(id)) {
- propRoots.push(id);
+ propRoots.push(buildPropTree(id, 1));
}
}
- propRoots.sort((a, b) => (allNodes.get(a)!.label).localeCompare(allNodes.get(b)!.label));
- const propBranches = propRoots.map(id => buildPropNode(id));
-
- // Orphan properties
- for (const [id, data] of allNodes) {
+ for (const [id, data] of nodeMap) {
if ((data.nodeType === 'ObjectProperty' || data.nodeType === 'DatatypeProperty') && !visited.has(id)) {
- propBranches.push({ id, label: data.label, uri: data.uri, nodeType: data.nodeType, children: [], connectionCount: data.connections, propertyCount: 0 });
- visited.add(id);
- }
- }
-
- // Annotation properties
- const annotBranches: OntologyNode[] = [];
- for (const [id, data] of allNodes) {
- if (data.nodeType === 'AnnotationProperty' && !visited.has(id)) {
visited.add(id);
- annotBranches.push({ id, label: data.label, uri: data.uri, nodeType: 'AnnotationProperty', children: [], connectionCount: 0, propertyCount: 0 });
+ propRoots.push({ id, label: data.label, uri: data.uri, nodeType: data.nodeType, children: [], childCount: 0, depth: 1 });
}
}
- annotBranches.sort((a, b) => a.label.localeCompare(b.label));
+ propRoots.sort((a, b) => a.label.localeCompare(b.label));
// Orphan individuals
- const orphanInds: OntologyNode[] = [];
- for (const [id, data] of allNodes) {
+ const orphanInds: TreeNode[] = [];
+ for (const [id, data] of nodeMap) {
if (data.nodeType === 'Individual' && !visited.has(id)) {
visited.add(id);
- orphanInds.push({ id, label: data.label, uri: data.uri, nodeType: 'Individual', children: [], connectionCount: 0, propertyCount: 0 });
+ orphanInds.push({ id, label: data.label, uri: data.uri, nodeType: 'Individual', children: [], childCount: 0, depth: 1 });
}
}
- // ── Assemble root ──────────────────────────────────────────
- const rootChildren: OntologyNode[] = [];
-
- if (classBranches.length > 0) {
- rootChildren.push({
- id: '__classes__', label: `Classes (${classBranches.length})`, uri: '',
- nodeType: 'Class', children: classBranches, connectionCount: 0, propertyCount: 0,
- });
+ // Assemble
+ const treeRoots: TreeNode[] = [];
+ if (classRoots.length > 0) {
+ const branch: TreeNode = { id: '__classes__', label: `Classes (${classRoots.length})`, uri: '', nodeType: 'Class', children: classRoots, childCount: 0, depth: 0 };
+ countDescendants(branch);
+ treeRoots.push(branch);
}
- if (propBranches.length > 0) {
- rootChildren.push({
- id: '__properties__', label: `Properties (${propBranches.length})`, uri: '',
- nodeType: 'ObjectProperty', children: propBranches, connectionCount: 0, propertyCount: 0,
- });
- }
- if (annotBranches.length > 0) {
- rootChildren.push({
- id: '__annotations__', label: `Annotations (${annotBranches.length})`, uri: '',
- nodeType: 'AnnotationProperty', children: annotBranches, connectionCount: 0, propertyCount: 0,
- });
+ if (propRoots.length > 0) {
+ const branch: TreeNode = { id: '__properties__', label: `Properties (${propRoots.length})`, uri: '', nodeType: 'ObjectProperty', children: propRoots, childCount: 0, depth: 0 };
+ countDescendants(branch);
+ treeRoots.push(branch);
}
if (orphanInds.length > 0) {
- rootChildren.push({
- id: '__individuals__', label: `Individuals (${orphanInds.length})`, uri: '',
- nodeType: 'Individual', children: orphanInds, connectionCount: 0, propertyCount: 0,
- });
+ const branch: TreeNode = { id: '__individuals__', label: `Individuals (${orphanInds.length})`, uri: '', nodeType: 'Individual', children: orphanInds, childCount: 0, depth: 0 };
+ countDescendants(branch);
+ treeRoots.push(branch);
}
- const root: OntologyNode = {
- id: '__ontology__', label: 'Ontology', uri: '',
- nodeType: 'Ontology', children: rootChildren, connectionCount: 0, propertyCount: 0,
- };
-
- // Compute stats
- let md = 0;
- function findMaxDepth(node: OntologyNode, d: number) {
- if (d > md) md = d;
- node.children.forEach(c => findMaxDepth(c, d + 1));
- }
- findMaxDepth(root, 0);
- countDescendants(root);
-
- // Count by type
+ // Stats
const tc = new Map<NodeType, number>();
- for (const data of allNodes.values()) {
- tc.set(data.nodeType, (tc.get(data.nodeType) ?? 0) + 1);
- }
+ for (const data of nodeMap.values()) tc.set(data.nodeType, (tc.get(data.nodeType) ?? 0) + 1);
setTypeCounts(tc);
- const totalClasses = tc.get('Class') ?? 0;
- const totalProps = (tc.get('ObjectProperty') ?? 0) + (tc.get('DatatypeProperty') ?? 0) + (tc.get('AnnotationProperty') ?? 0);
- const totalInds = tc.get('Individual') ?? 0;
+ let maxDepth = 0;
+ function findDepth(n: TreeNode) { if (n.depth > maxDepth) maxDepth = n.depth; n.children.forEach(findDepth); }
+ treeRoots.forEach(findDepth);
+
+ setStats({
+ classes: tc.get('Class') ?? 0,
+ properties: (tc.get('ObjectProperty') ?? 0) + (tc.get('DatatypeProperty') ?? 0),
+ individuals: tc.get('Individual') ?? 0,
+ depth: maxDepth,
+ });
- setStats({ classes: totalClasses, properties: totalProps, individuals: totalInds, depth: md, crossLinks: crossLinks.length });
- treeDataRef.current = root;
- crossLinksRef.current = crossLinks;
- collapsedRef.current = new Set();
- renderTree();
- setTimeout(fitGraph, 300);
+ setRoots(treeRoots);
+ // Auto-expand top-level branches
+ setExpanded(new Set(treeRoots.map(r => r.id)));
refreshStats();
} catch (e) {
console.error('Failed to load tree:', e);
}
- }, [renderTree, fitGraph, refreshStats]);
-
- const toggleTypeFilter = useCallback((type: NodeType) => {
- const next = new Set(hiddenTypesRef.current);
- if (next.has(type)) next.delete(type);
- else next.add(type);
- hiddenTypesRef.current = next;
- setHiddenTypes(new Set(next));
- renderTree();
- }, [renderTree]);
-
- // Initialize SVG
+ }, [refreshStats]);
+
+ useEffect(() => { if (status === 'connected') loadTree(); }, [status, loadTree]);
useEffect(() => {
- const el = containerRef.current;
- if (!el) return;
- const rect = el.getBoundingClientRect();
-
- const svg = d3.select(el)
- .append('svg')
- .attr('width', rect.width)
- .attr('height', rect.height)
- .style('background', '#1e1e2e')
- .style('display', 'block');
-
- const g = svg.append('g');
-
- const zoom = d3.zoom<SVGSVGElement, unknown>()
- .scaleExtent([0.02, 8])
- .on('zoom', (event) => g.attr('transform', event.transform.toString()));
- svg.call(zoom);
- svg.on('click', () => onNodeSelect(null));
-
- svgRef.current = svg.node();
- gRef.current = g;
- zoomRef.current = zoom;
-
- const ro = new ResizeObserver(() => {
- const r = el.getBoundingClientRect();
- svg.attr('width', r.width).attr('height', r.height);
+ (window as unknown as Record<string, unknown>).__refreshGraph = loadTree;
+ return () => { delete (window as unknown as Record<string, unknown>).__refreshGraph; };
+ }, [loadTree]);
+
+ const toggleExpand = useCallback((id: string) => {
+ setExpanded(prev => {
+ const next = new Set(prev);
+ if (next.has(id)) next.delete(id); else next.add(id);
+ return next;
});
- ro.observe(el);
+ }, []);
- (window as unknown as Record<string, unknown>).__refreshGraph = loadTree;
+ const handleSelect = useCallback((node: TreeNode) => {
+ if (node.id.startsWith('__')) return;
+ setSelectedId(node.id);
+ onNodeSelect({ id: node.id, label: node.label, uri: node.uri });
+ }, [onNodeSelect]);
+
+ const normalizedSearch = searchTerm.toLowerCase().trim();
+
+ // Auto-expand matching paths during search
+ const effectiveExpanded = useMemo(() => {
+ if (!normalizedSearch) return expanded;
+ const auto = new Set<string>();
+ function walk(n: TreeNode): boolean {
+ const selfMatch = n.label.toLowerCase().includes(normalizedSearch) || n.id.toLowerCase().includes(normalizedSearch);
+ let childMatch = false;
+ for (const c of n.children) { if (walk(c)) childMatch = true; }
+ if (childMatch) auto.add(n.id);
+ return selfMatch || childMatch;
+ }
+ roots.forEach(walk);
+ return auto;
+ }, [roots, normalizedSearch, expanded]);
+
+ // Filter by hidden types
+ const filteredRoots = useMemo(() => {
+ if (hiddenTypes.size === 0) return roots;
+ function filterNode(n: TreeNode): TreeNode | null {
+ if (hiddenTypes.has(n.nodeType) && !n.id.startsWith('__')) return null;
+ const children = n.children.map(filterNode).filter(Boolean) as TreeNode[];
+ if (children.length === 0 && n.children.length > 0 && hiddenTypes.has(n.nodeType)) return null;
+ return { ...n, children };
+ }
+ return roots.map(filterNode).filter(Boolean) as TreeNode[];
+ }, [roots, hiddenTypes]);
+
+ // Flatten for virtual scroll
+ const flatNodes = useMemo(
+ () => flattenTree(filteredRoots, effectiveExpanded, normalizedSearch, 0),
+ [filteredRoots, effectiveExpanded, normalizedSearch]
+ );
- return () => {
- ro.disconnect(); svg.remove();
- svgRef.current = null; gRef.current = null; zoomRef.current = null;
- delete (window as unknown as Record<string, unknown>).__refreshGraph;
- };
- }, [onNodeSelect, loadTree]);
+ // Virtual scroll
+ const totalHeight = flatNodes.length * ROW_HEIGHT;
+ const startIdx = Math.max(0, Math.floor(scrollTop / ROW_HEIGHT) - OVERSCAN);
+ const endIdx = Math.min(flatNodes.length, Math.ceil((scrollTop + viewHeight) / ROW_HEIGHT) + OVERSCAN);
+ const visibleNodes = flatNodes.slice(startIdx, endIdx);
- // Keyboard
useEffect(() => {
- const handler = (e: KeyboardEvent) => {
- const tag = (e.target as HTMLElement)?.tagName;
- if (tag === 'INPUT' || tag === 'TEXTAREA') return;
- if (e.key === 'Escape') onNodeSelect(null);
- else if (e.key === 'f' || e.key === 'F') fitGraph();
- else if (e.key === 'r' || e.key === 'R') {
- if (svgRef.current && zoomRef.current) {
- d3.select(svgRef.current).transition().duration(500).call(zoomRef.current.transform, d3.zoomIdentity);
- }
- }
- };
- window.addEventListener('keydown', handler);
- return () => window.removeEventListener('keydown', handler);
- }, [onNodeSelect, fitGraph]);
+ const el = scrollRef.current;
+ if (!el) return;
+ const onScroll = () => setScrollTop(el.scrollTop);
+ const ro = new ResizeObserver(() => setViewHeight(el.clientHeight));
+ el.addEventListener('scroll', onScroll, { passive: true });
+ ro.observe(el);
+ setViewHeight(el.clientHeight);
+ return () => { el.removeEventListener('scroll', onScroll); ro.disconnect(); };
+ }, []);
- useEffect(() => { if (status === 'connected') loadTree(); }, [status, loadTree]);
+ const expandAll = useCallback(() => {
+ const all = new Set<string>();
+ function walk(n: TreeNode) { if (n.children.length > 0) { all.add(n.id); n.children.forEach(walk); } }
+ roots.forEach(walk);
+ setExpanded(all);
+ }, [roots]);
+
+ const collapseAll = useCallback(() => setExpanded(new Set(roots.map(r => r.id))), [roots]);
+
+ const toggleType = useCallback((type: NodeType) => {
+ setHiddenTypes(prev => {
+ const next = new Set(prev);
+ if (next.has(type)) next.delete(type); else next.add(type);
+ return next;
+ });
+ }, []);
- const typeOrder: NodeType[] = ['Class', 'ObjectProperty', 'DatatypeProperty', 'AnnotationProperty', 'Individual'];
+ const typeOrder: NodeType[] = ['Class', 'ObjectProperty', 'DatatypeProperty', 'Individual'];
return (
- <div className="absolute inset-0" style={{ background: '#1e1e2e' }}>
- <div ref={containerRef} className="w-full h-full" />
-
- {/* Stats bar */}
- <div className="absolute top-3 left-3 flex items-center gap-2" style={{ zIndex: 10 }}>
- {[
- { label: 'classes', value: stats.classes, color: NODE_TYPE_COLORS.Class },
- { label: 'properties', value: stats.properties, color: NODE_TYPE_COLORS.ObjectProperty },
- { label: 'individuals', value: stats.individuals, color: NODE_TYPE_COLORS.Individual },
- { label: 'cross-links', value: stats.crossLinks, color: '#585b70' },
- { label: 'depth', value: stats.depth, color: '#cdd6f4' },
- ].map(s => (
- <span key={s.label} style={{
- background: '#181825', border: '1px solid #313244', borderRadius: 6,
- padding: '3px 10px', fontSize: 10, color: '#6c7086',
- display: 'flex', alignItems: 'center', gap: 4,
- }}>
- <span style={{ color: s.color, fontWeight: 600 }}>{s.value}</span> {s.label}
- </span>
- ))}
- </div>
+ <div className="absolute inset-0 flex flex-col" style={{ background: '#1e1e2e' }}>
+ {/* Header */}
+ <div style={{ padding: '8px 12px', borderBottom: '1px solid #313244', background: '#181825', flexShrink: 0, display: 'flex', flexDirection: 'column', gap: 6 }}>
+ {/* Stats */}
+ <div style={{ display: 'flex', gap: 8, fontSize: 11, color: '#6c7086', flexWrap: 'wrap' }}>
+ {[
+ { label: 'classes', value: stats.classes, color: TYPE_COLORS.Class },
+ { label: 'properties', value: stats.properties, color: TYPE_COLORS.ObjectProperty },
+ { label: 'individuals', value: stats.individuals, color: TYPE_COLORS.Individual },
+ { label: 'depth', value: stats.depth, color: '#cdd6f4' },
+ ].map(s => (
+ <span key={s.label} style={{ display: 'flex', alignItems: 'center', gap: 3 }}>
+ <span style={{ color: s.color, fontWeight: 600 }}>{s.value}</span> {s.label}
+ </span>
+ ))}
+ </div>
- {/* Node type legend (clickable filters) */}
- <div className="absolute top-12 left-3 flex flex-col gap-1" style={{ zIndex: 10 }}>
- {typeOrder.filter(t => (typeCounts.get(t) ?? 0) > 0).map(type => (
- <button
- key={type}
- onClick={() => toggleTypeFilter(type)}
- style={{
- background: '#181825',
- border: '1px solid #313244',
- borderRadius: 6,
- padding: '4px 10px',
- fontSize: 11,
+ {/* Search */}
+ <input
+ type="text"
+ placeholder="Search..."
+ value={searchTerm}
+ onChange={e => setSearchTerm(e.target.value)}
+ style={{
+ width: '100%', padding: '5px 10px', borderRadius: 6, border: '1px solid #313244',
+ background: '#1e1e2e', color: '#cdd6f4', fontSize: 12, outline: 'none',
+ fontFamily: '-apple-system, BlinkMacSystemFont, sans-serif',
+ }}
+ />
+
+ {/* Type filters + controls */}
+ <div style={{ display: 'flex', alignItems: 'center', gap: 4, flexWrap: 'wrap' }}>
+ {typeOrder.filter(t => (typeCounts.get(t) ?? 0) > 0).map(type => (
+ <button key={type} onClick={() => toggleType(type)} style={{
+ background: 'none', border: '1px solid #313244', borderRadius: 4, padding: '1px 6px',
+ fontSize: 10, cursor: 'pointer', display: 'flex', alignItems: 'center', gap: 3,
color: hiddenTypes.has(type) ? '#45475a' : '#bac2de',
- display: 'flex', alignItems: 'center', gap: 8,
- cursor: 'pointer',
+ opacity: hiddenTypes.has(type) ? 0.4 : 1,
textDecoration: hiddenTypes.has(type) ? 'line-through' : 'none',
- opacity: hiddenTypes.has(type) ? 0.5 : 1,
- textAlign: 'left',
- fontFamily: '-apple-system, BlinkMacSystemFont, "SF Pro Text", sans-serif',
- }}
- >
- <span style={{ width: 8, height: 8, borderRadius: '50%', background: getColor(type), display: 'inline-block', flexShrink: 0 }} />
- <span style={{ fontWeight: 600 }}>{type}</span>
- <span style={{ color: '#585b70', fontSize: 10 }}>({typeCounts.get(type) ?? 0})</span>
- </button>
- ))}
- </div>
+ }}>
+ <span style={{ width: 6, height: 6, borderRadius: '50%', background: TYPE_COLORS[type] }} />
+ {type} ({typeCounts.get(type) ?? 0})
+ </button>
+ ))}
+ <div style={{ marginLeft: 'auto', display: 'flex', gap: 3 }}>
+ <button onClick={expandAll} style={{ background: 'none', border: '1px solid #313244', color: '#6c7086', borderRadius: 4, padding: '1px 6px', fontSize: 10, cursor: 'pointer' }}>
+ Expand
+ </button>
+ <button onClick={collapseAll} style={{ background: 'none', border: '1px solid #313244', color: '#6c7086', borderRadius: 4, padding: '1px 6px', fontSize: 10, cursor: 'pointer' }}>
+ Collapse
+ </button>
+ </div>
+ </div>
- {/* Shortcuts */}
- <div className="absolute bottom-3 right-3" style={{ zIndex: 10 }}>
- <span style={{ background: '#181825', border: '1px solid #313244', borderRadius: 6, padding: '4px 10px', fontSize: 10, color: '#45475a' }}>
- Scroll zoom · Drag pan · Click inspect · Shift+click collapse · <b>F</b> fit · <b>R</b> reset · <b>Esc</b> deselect
- </span>
+ {normalizedSearch && (
+ <div style={{ fontSize: 10, color: '#6c7086' }}>{flatNodes.length} matches</div>
+ )}
</div>
- {status !== 'connected' && (
- <div className="absolute inset-0 flex items-center justify-center" style={{ color: '#585b70', zIndex: 5 }}>
- {status === 'connecting' ? 'Connecting to engine...' : 'Engine not connected'}
+ {/* Virtualized tree */}
+ <div ref={scrollRef} style={{ flex: 1, overflow: 'auto' }}>
+ <div style={{ height: totalHeight, position: 'relative' }}>
+ {visibleNodes.map((flat, i) => {
+ const { node, indent, isExpanded, hasChildren } = flat;
+ const isSelected = selectedId === node.id;
+ const color = TYPE_COLORS[node.nodeType] ?? '#a6adc8';
+ const isLeaf = !hasChildren;
+ const isBranch = node.id.startsWith('__');
+ const top = (startIdx + i) * ROW_HEIGHT;
+
+ // Highlight search match
+ let labelEl: React.ReactNode = node.label;
+ if (normalizedSearch) {
+ const idx = node.label.toLowerCase().indexOf(normalizedSearch);
+ if (idx >= 0) {
+ labelEl = <>
+ {node.label.slice(0, idx)}
+ <span style={{ background: '#f9e2af33', color: '#f9e2af', borderRadius: 2, padding: '0 1px' }}>
+ {node.label.slice(idx, idx + normalizedSearch.length)}
+ </span>
+ {node.label.slice(idx + normalizedSearch.length)}
+ </>;
+ }
+ }
+
+ return (
+ <div
+ key={node.id}
+ onClick={() => handleSelect(node)}
+ onDoubleClick={() => hasChildren && toggleExpand(node.id)}
+ style={{
+ position: 'absolute',
+ top,
+ left: 0,
+ right: 0,
+ height: ROW_HEIGHT,
+ paddingLeft: indent * 18 + 10,
+ paddingRight: 10,
+ display: 'flex',
+ alignItems: 'center',
+ gap: 6,
+ cursor: 'pointer',
+ background: isSelected ? '#313244' : 'transparent',
+ borderLeft: isSelected ? `2px solid ${color}` : '2px solid transparent',
+ fontSize: isBranch ? 12 : 11,
+ fontWeight: isBranch ? 600 : hasChildren ? 500 : 400,
+ color: isSelected ? '#cdd6f4' : isBranch ? '#cdd6f4' : hasChildren ? '#bac2de' : '#a6adc8',
+ fontFamily: '-apple-system, BlinkMacSystemFont, sans-serif',
+ userSelect: 'none',
+ }}
+ onMouseEnter={e => { if (!isSelected) e.currentTarget.style.background = '#181825'; }}
+ onMouseLeave={e => { if (!isSelected) e.currentTarget.style.background = 'transparent'; }}
+ >
+ {/* Arrow */}
+ <span
+ onClick={e => { e.stopPropagation(); if (hasChildren) toggleExpand(node.id); }}
+ style={{
+ width: 14, fontSize: 8, color: '#585b70', flexShrink: 0, textAlign: 'center',
+ transition: 'transform 0.1s',
+ transform: hasChildren ? (isExpanded ? 'rotate(90deg)' : 'rotate(0deg)') : 'none',
+ visibility: hasChildren ? 'visible' : 'hidden',
+ }}
+ >
+ {'\u25B6'}
+ </span>
+
+ {/* Type dot */}
+ <span style={{
+ width: isLeaf ? 5 : 7, height: isLeaf ? 5 : 7,
+ borderRadius: isLeaf ? '50%' : 2,
+ background: color, flexShrink: 0, opacity: isLeaf ? 0.6 : 1,
+ }} />
+
+ {/* Label */}
+ <span style={{ overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', flex: 1 }}>
+ {labelEl}
+ </span>
+
+ {/* Child count */}
+ {hasChildren && node.childCount > 0 && (
+ <span style={{ fontSize: 9, color: '#45475a', flexShrink: 0 }}>
+ {node.childCount}
+ </span>
+ )}
+ </div>
+ );
+ })}
</div>
- )}
+ </div>
+
+ {/* Footer */}
+ <div style={{ padding: '4px 12px', borderTop: '1px solid #313244', background: '#181825', fontSize: 9, color: '#45475a', flexShrink: 0 }}>
+ Click to inspect · Double-click to expand · Search to filter
+ </div>
</div>
);
}