Ontology¶
Full Project Haystack def model with taxonomy, normalization, and reflection.
Haystack ontology model.
Data model for defs, libs, and namespace resolution over the Haystack taxonomy.
Usage:
from hs_py.ontology import Def, Lib, Namespace, load_lib_from_trio
lib = load_lib_from_trio(lib_trio_text, [defs_trio_text])
ns = Namespace([lib])
site_def = ns.get("site")
Defs¶
Def and Lib frozen dataclasses for ontology definitions.
Ontology definition and library data model.
A Def is a named term in the Haystack ontology, carrying metadata
tags parsed from Trio records. A Lib groups related defs into a
versioned, distributable package.
See: https://project-haystack.org/doc/docHaystack/Defs
- class hs_py.ontology.defs.Def(symbol, tags=<factory>)[source]¶
Bases:
objectA single definition in the Haystack ontology.
- Parameters:
- class hs_py.ontology.defs.Lib(symbol, version='', depends=(), base_uri=None, defs=())[source]¶
Bases:
objectA library of ontology definitions.
- Parameters:
symbol (
Symbol) – Lib symbol (e.g.^lib:ph).version (
str(default:'')) – Semantic version string.depends (
tuple[Symbol,...] (default:())) – Required library symbols.base_uri (
Uri|None(default:None)) – Base URI for RDF export.defs (
tuple[Def,...] (default:())) – All defs defined by this lib.
Namespace¶
Namespace container with symbol resolution and lookup.
Ontology namespace for resolved defs.
The Namespace indexes defs from one or more libs, providing
symbol resolution and taxonomy queries (subtype, supertype, transitive
subtype checking).
See: https://project-haystack.org/doc/docHaystack/Namespaces
- class hs_py.ontology.namespace.Namespace(libs=None)[source]¶
Bases:
objectContainer for resolved ontology definitions.
Indexes defs by both qualified (
ph::site) and unqualified (site) symbol names. Provides taxonomy queries over theistag hierarchy.- find_conjuncts(marker_names)[source]¶
Return conjunct defs whose parts are all present in marker_names.
Uses a pre-built index for efficient lookup.
- get(symbol)[source]¶
Look up a def by symbol name.
Accepts both qualified (
ph::site) and unqualified (site) names.
- hs_py.ontology.namespace.load_defs_from_trio(text)[source]¶
Parse Trio text and return
Defobjects for each record with adeftag.
Taxonomy¶
Subtype tree, tag inheritance, and conjunct resolution.
Taxonomy tree operations over the def hierarchy.
Provides utilities for working with the is tag inheritance tree,
conjunct detection, and tag inheritance through the taxonomy.
See: https://project-haystack.org/doc/docHaystack/Subtyping
- hs_py.ontology.taxonomy.effective_tags(ns, symbol)[source]¶
Compute the effective tag set for a def, inheriting from supertypes.
Walks up the taxonomy tree and merges tags from all supertypes. Tags defined on the def itself take precedence over inherited tags.
- hs_py.ontology.taxonomy.is_conjunct(symbol)[source]¶
Check if a symbol is a conjunct (compound term with
-).Conjuncts like
hot-waterare composed from their dash-separated parts.
- hs_py.ontology.taxonomy.marker_tags(ns, symbol)[source]¶
Return the set of marker tag names for a def and all its supertypes.
This is useful for reflection: determining which marker defs an entity implements based on its tag set.
Normalize¶
Normalization pipeline to compile raw defs into a resolved namespace.
Normalization pipeline for compiling raw defs into a resolved namespace.
Implements a simplified version of the Haystack normalization pipeline:
Parse: convert Trio text to raw tag dicts
Resolve: create Def objects from parsed records
Taxonify: compute conjunct supertypes
Inherit: propagate tags down the taxonomy tree
Validate: check for missing references and cycles
See: https://project-haystack.org/doc/docHaystack/Normalization
- exception hs_py.ontology.normalize.NormalizeError[source]¶
Bases:
ValueErrorRaised when normalization encounters an error.
Reflect¶
Dict-to-def reflection engine for runtime type inference.
Reflection engine for mapping entity dicts to ontology defs.
Reflection determines which defs apply to an entity based on its tag set. This includes detecting simple marker matches, conjunct matches, and computing the full effective def set.
See: https://project-haystack.org/doc/docHaystack/Reflection
- hs_py.ontology.reflect.fits(ns, tags, def_symbol)[source]¶
Check if an entity’s tags fit a given def.
An entity fits a def if the def (or one of its subtypes) appears in the entity’s reflected def set.
- hs_py.ontology.reflect.reflect(ns, tags)[source]¶
Return all defs that apply to an entity with the given tags.
The algorithm: 1. Scan the entity’s tags for marker tags (tags whose value is Marker). 2. For each marker, look up the matching def in the namespace. 3. Check for conjuncts (compound terms matching tag combinations). 4. Include all transitive supertypes.