-
Notifications
You must be signed in to change notification settings - Fork 24
RDF
RDF (Resource Description Framework) provides a syntax for labeled graphs built out of (subject, predicate, object) triples. RDF is closely associated with the Semantic Web and its associated standards and best practices. Hydra, on the other hand, is built on a labeled hypergraph data model (LambdaGraph). In order to map Hydra types and terms into RDF, we need to turn them into sets of triples. There are many possible ways to do this, but SHACL (Shapes Constraint Language) -- and similarly, ShEx (Shape Expressions) -- provides a particularly natural target for mappings, as it provides a very appropriate syntax for capturing Hydra's record-structured schemas and data.
Not all of Hydra is expressible in RDF. Computational terms such as lambdas, function applications, and projections do not have a meaningful representation as RDF triples, and polymorphic types (universally quantified types, type variables, type applications) fall outside what SHACL can express. However, Hydra's data types and terms -- records, unions, literals, lists, sets, optionals, maps, and newtypes -- map naturally to SHACL shapes and RDF triples. This covers the vast majority of practical data modeling use cases.
Hydra includes several RDF-related components: a SHACL coder that maps Hydra types to SHACL shapes and Hydra terms to RDF triples, syntax models for RDF, SHACL, and OWL, serialization to N-Triples format, and tools that use these components in end-to-end workflows.
The SHACL coder maps Hydra types to SHACL shapes, producing a shapes graph that can be used for validation. The mapping is defined in Hydra/Sources/Shacl/Coder.hs.
| Hydra type | SHACL construct |
|---|---|
| Record |
NodeShape with PropertyShape constraints for each field |
| Union |
sh:xone constraint (exactly one of the variant shapes) |
| Optional |
PropertyShape with sh:minCount 0
|
| Set |
PropertyShape with no sh:maxCount (unbounded) |
| Literal types |
sh:datatype constraint with XSD datatype IRI |
Hydra's literal types map to XSD datatypes:
| Hydra literal type | XSD datatype |
|---|---|
| Boolean | xsd:boolean |
| String | xsd:string |
| Binary | xsd:base64Binary |
| Int8 | xsd:byte |
| Int16 | xsd:short |
| Int32 | xsd:int |
| Int64 | xsd:long |
| Uint8 | xsd:unsignedByte |
| Uint16 | xsd:unsignedShort |
| Uint32 | xsd:unsignedInt |
| Uint64 | xsd:unsignedLong |
| Bigint | xsd:integer |
| Float32 | xsd:float |
| Float64 | xsd:double |
Some Hydra type constructs do not have direct SHACL equivalents:
-
Function types (
A -> B): No representation in SHACL -
Polymorphic types (
forall a. ..., type variables, type applications): Not expressible -
List types: Could potentially use
shsh:ListShape, but this is not yet implemented - Map types: No direct SHACL equivalent
When the coder encounters these types, it produces empty constraints rather than failing.
The same coder maps Hydra terms (data values) to RDF descriptions (sets of triples). This mapping is designed so that the resulting RDF data conforms to the SHACL shapes generated from the corresponding types.
| Hydra term | RDF representation |
|---|---|
| Record | RDF description with rdf:type triple and property triples for each field |
| Union | RDF description with rdf:type triple for the variant |
| List | RDF list using rdf:first / rdf:rest / rdf:nil
|
| Literal | RDF literal with XSD datatype IRI |
| Set | Multiple RDF descriptions |
| Map | RDF description with custom key IRIs (urn:key:...) |
| Optional (given) | Encoded inner value |
| Optional (none) | Omitted |
| Newtype (Wrap) | Unwrapped value with rdf:type triple |
Hydra names are converted to IRIs using a URN-based scheme:
-
Type IRIs:
urn:{hydra.name}(e.g.,urn:hydra.core.Type) -
Property IRIs:
urn:{namespace}#{localName}derived from record and field names -
Map key IRIs:
urn:key:{key} -
Blank nodes: Generated with sequential IDs (
_:b0,_:b1, etc.)
RDF graphs are serialized to N-Triples, the line-based plain-text format that's the standard interchange syntax for triples. Unicode in literals is preserved verbatim, so non-ASCII text such as smart quotes, accented letters, and CJK characters round-trips cleanly.
For details of the public API and exact escaping rules, see the hydra-rdf README.
Hydra includes a comprehensive OWL 2 syntax model defined in Hydra/Sources/Owl/Syntax.hs. This model covers the full OWL 2 specification including classes, properties, individuals, datatypes, axioms, and all class expression constructors (intersection, union, complement, restrictions, etc.). The syntax model is code-generated into Haskell and Java.
Note that this is currently a syntax model only; there is no OWL-specific coder analogous to the SHACL coder.
Hydra defines complete syntax models for the following semantic web standards:
| Standard | Source | Description |
|---|---|---|
| RDF 1.1 | Rdf/Syntax.hs | IRIs, literals, blank nodes, triples, graphs, datasets |
| SHACL | Shacl/Model.hs | Node shapes, property shapes, constraints, severity levels |
| OWL 2 | Owl/Syntax.hs | Ontologies, classes, properties, individuals, axioms |
These models serve as the intermediate representation for RDF-related coders and tools. They are defined using Hydra's DSL and code-generated into all target languages.
Several tools use the RDF/SHACL coder:
- AvroWorkflows (AvroWorkflows.hs): Transforms Avro-schema'd JSON data into SHACL RDF. See the Avro demo.
- PG to RDF (Pg/Rdf/Mappings.hs): Converts property graph vertices and edges to RDF descriptions.
- SHACL demo (demos/shacl): End-to-end example that generates SHACL shapes from Hydra kernel types, encodes kernel modules as N-Triples, and validates the data against the shapes.
- GenPG demo (demos/genpg): Transforms relational CSV data into a property graph and emits it as either GraphSON or RDF/SHACL N-Triples.
- Concepts - Core Hydra concepts including the relationship to RDF
- Property graphs - Hydra's property graph support (an alternative graph representation)
- Hydra-RDF README - RDF package reference