Metadata-Version: 2.4
Name: telicent-ies-toolkit
Version: 0.1.0
Summary: A library for building IES data, with wrapper classes generated from the IES ontology
Author-email: Telicent Ltd <opensource@telicent.io>
License:                                  Apache License
                                   Version 2.0, January 2004
                                http://www.apache.org/licenses/
        
           TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
        
           1. Definitions.
        
              "License" shall mean the terms and conditions for use, reproduction,
              and distribution as defined by Sections 1 through 9 of this document.
        
              "Licensor" shall mean the copyright owner or entity authorized by
              the copyright owner that is granting the License.
        
              "Legal Entity" shall mean the union of the acting entity and all
              other entities that control, are controlled by, or are under common
              control with that entity. For the purposes of this definition,
              "control" means (i) the power, direct or indirect, to cause the
              direction or management of such entity, whether by contract or
              otherwise, or (ii) ownership of fifty percent (50%) or more of the
              outstanding shares, or (iii) beneficial ownership of such entity.
        
              "You" (or "Your") shall mean an individual or Legal Entity
              exercising permissions granted by this License.
        
              "Source" form shall mean the preferred form for making modifications,
              including but not limited to software source code, documentation
              source, and configuration files.
        
              "Object" form shall mean any form resulting from mechanical
              transformation or translation of a Source form, including but
              not limited to compiled object code, generated documentation,
              and conversions to other media types.
        
              "Work" shall mean the work of authorship, whether in Source or
              Object form, made available under the License, as indicated by a
              copyright notice that is included in or attached to the work
              (an example is provided in the Appendix below).
        
              "Derivative Works" shall mean any work, whether in Source or Object
              form, that is based on (or derived from) the Work and for which the
              editorial revisions, annotations, elaborations, or other modifications
              represent, as a whole, an original work of authorship. For the purposes
              of this License, Derivative Works shall not include works that remain
              separable from, or merely link (or bind by name) to the interfaces of,
              the Work and Derivative Works thereof.
        
              "Contribution" shall mean any work of authorship, including
              the original version of the Work and any modifications or additions
              to that Work or Derivative Works thereof, that is intentionally
              submitted to Licensor for inclusion in the Work by the copyright owner
              or by an individual or Legal Entity authorized to submit on behalf of
              the copyright owner. For the purposes of this definition, "submitted"
              means any form of electronic, verbal, or written communication sent
              to the Licensor or its representatives, including but not limited to
              communication on electronic mailing lists, source code control systems,
              and issue tracking systems that are managed by, or on behalf of, the
              Licensor for the purpose of discussing and improving the Work, but
              excluding communication that is conspicuously marked or otherwise
              designated in writing by the copyright owner as "Not a Contribution."
        
              "Contributor" shall mean Licensor and any individual or Legal Entity
              on behalf of whom a Contribution has been received by Licensor and
              subsequently incorporated within the Work.
        
           2. Grant of Copyright License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              copyright license to reproduce, prepare Derivative Works of,
              publicly display, publicly perform, sublicense, and distribute the
              Work and such Derivative Works in Source or Object form.
        
           3. Grant of Patent License. Subject to the terms and conditions of
              this License, each Contributor hereby grants to You a perpetual,
              worldwide, non-exclusive, no-charge, royalty-free, irrevocable
              (except as stated in this section) patent license to make, have made,
              use, offer to sell, sell, import, and otherwise transfer the Work,
              where such license applies only to those patent claims licensable
              by such Contributor that are necessarily infringed by their
              Contribution(s) alone or by combination of their Contribution(s)
              with the Work to which such Contribution(s) was submitted. If You
              institute patent litigation against any entity (including a
              cross-claim or counterclaim in a lawsuit) alleging that the Work
              or a Contribution incorporated within the Work constitutes direct
              or contributory patent infringement, then any patent licenses
              granted to You under this License for that Work shall terminate
              as of the date such litigation is filed.
        
           4. Redistribution. You may reproduce and distribute copies of the
              Work or Derivative Works thereof in any medium, with or without
              modifications, and in Source or Object form, provided that You
              meet the following conditions:
        
              (a) You must give any other recipients of the Work or
                  Derivative Works a copy of this License; and
        
              (b) You must cause any modified files to carry prominent notices
                  stating that You changed the files; and
        
              (c) You must retain, in the Source form of any Derivative Works
                  that You distribute, all copyright, patent, trademark, and
                  attribution notices from the Source form of the Work,
                  excluding those notices that do not pertain to any part of
                  the Derivative Works; and
        
              (d) If the Work includes a "NOTICE" text file as part of its
                  distribution, then any Derivative Works that You distribute must
                  include a readable copy of the attribution notices contained
                  within such NOTICE file, excluding those notices that do not
                  pertain to any part of the Derivative Works, in at least one
                  of the following places: within a NOTICE text file distributed
                  as part of the Derivative Works; within the Source form or
                  documentation, if provided along with the Derivative Works; or,
                  within a display generated by the Derivative Works, if and
                  wherever such third-party notices normally appear. The contents
                  of the NOTICE file are for informational purposes only and
                  do not modify the License. You may add Your own attribution
                  notices within Derivative Works that You distribute, alongside
                  or as an addendum to the NOTICE text from the Work, provided
                  that such additional attribution notices cannot be construed
                  as modifying the License.
        
              You may add Your own copyright statement to Your modifications and
              may provide additional or different license terms and conditions
              for use, reproduction, or distribution of Your modifications, or
              for any such Derivative Works as a whole, provided Your use,
              reproduction, and distribution of the Work otherwise complies with
              the conditions stated in this License.
        
           5. Submission of Contributions. Unless You explicitly state otherwise,
              any Contribution intentionally submitted for inclusion in the Work
              by You to the Licensor shall be under the terms and conditions of
              this License, without any additional terms or conditions.
              Notwithstanding the above, nothing herein shall supersede or modify
              the terms of any separate license agreement you may have executed
              with Licensor regarding such Contributions.
        
           6. Trademarks. This License does not grant permission to use the trade
              names, trademarks, service marks, or product names of the Licensor,
              except as required for reasonable and customary use in describing the
              origin of the Work and reproducing the content of the NOTICE file.
        
           7. Disclaimer of Warranty. Unless required by applicable law or
              agreed to in writing, Licensor provides the Work (and each
              Contributor provides its Contributions) on an "AS IS" BASIS,
              WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
              implied, including, without limitation, any warranties or conditions
              of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
              PARTICULAR PURPOSE. You are solely responsible for determining the
              appropriateness of using or redistributing the Work and assume any
              risks associated with Your exercise of permissions under this License.
        
           8. Limitation of Liability. In no event and under no legal theory,
              whether in tort (including negligence), contract, or otherwise,
              unless required by applicable law (such as deliberate and grossly
              negligent acts) or agreed to in writing, shall any Contributor be
              liable to You for damages, including any direct, indirect, special,
              incidental, or consequential damages of any character arising as a
              result of this License or out of the use or inability to use the
              Work (including but not limited to damages for loss of goodwill,
              work stoppage, computer failure or malfunction, or any and all
              other commercial damages or losses), even if such Contributor
              has been advised of the possibility of such damages.
        
           9. Accepting Warranty or Additional Liability. While redistributing
              the Work or Derivative Works thereof, You may choose to offer,
              and charge a fee for, acceptance of support, warranty, indemnity,
              or other liability obligations and/or rights consistent with this
              License. However, in accepting such obligations, You may act only
              on Your own behalf and on Your sole responsibility, not on behalf
              of any other Contributor, and only if You agree to indemnify,
              defend, and hold each Contributor harmless for any liability
              incurred by, or claims asserted against, such Contributor by reason
              of your accepting any such warranty or additional liability.
        
           END OF TERMS AND CONDITIONS
        
           APPENDIX: How to apply the Apache License to your work.
        
              To apply the Apache License to your work, attach the following
              boilerplate notice, with the fields enclosed by brackets "[]"
              replaced with your own identifying information. (Don't include
              the brackets!)  The text should be enclosed in the appropriate
              comment syntax for the file format. We also recommend that a
              file or class name and description of purpose be included on the
              same "printed page" as the copyright notice for easier
              identification within third-party archives.
        
           Copyright [yyyy] [name of copyright owner]
        
           Licensed under the Apache License, Version 2.0 (the "License");
           you may not use this file except in compliance with the License.
           You may obtain a copy of the License at
        
               http://www.apache.org/licenses/LICENSE-2.0
        
           Unless required by applicable law or agreed to in writing, software
           distributed under the License is distributed on an "AS IS" BASIS,
           WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
           See the License for the specific language governing permissions and
           limitations under the License.
        
Project-URL: Repository, https://github.com/telicent-oss/ies-toolkit
Project-URL: Changelog, https://github.com/telicent-oss/ies-toolkit/blob/main/CHANGELOG.md
Classifier: Development Status :: 3 - Alpha
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: License :: OSI Approved :: Apache Software License
Classifier: Topic :: Software Development
Classifier: Operating System :: OS Independent
Requires-Python: >=3.11
Description-Content-Type: text/markdown
License-File: LICENSE
License-File: NOTICE
Requires-Dist: rdflib<8.0,>=7.0
Requires-Dist: shortuuid>=1.0
Requires-Dist: python-dateutil>=2.9
Provides-Extra: test
Requires-Dist: pytest>=9.0; extra == "test"
Requires-Dist: ruff>=0.1; extra == "test"
Requires-Dist: mypy>=1.7; extra == "test"
Requires-Dist: build>=1.0; extra == "test"
Provides-Extra: dev
Requires-Dist: telicent-ies-toolkit[test]; extra == "dev"
Requires-Dist: pip-tools>=7.0; extra == "dev"
Requires-Dist: pre-commit>=3.5; extra == "dev"
Dynamic: license-file

# IES Toolkit

[![Tests](https://github.com/telicent-oss/ies-toolkit/actions/workflows/python-test-build.yaml/badge.svg)](https://github.com/telicent-oss/ies-toolkit/actions/workflows/python-test-build.yaml)
[![Security](https://github.com/telicent-oss/ies-toolkit/actions/workflows/security.yml/badge.svg)](https://github.com/telicent-oss/ies-toolkit/actions/workflows/security.yml)

> **Alpha release.** This toolkit is built against IES Next RC1. Updates will be made as the ontology moves towards its final official release.

A Python library for building IES data.

Information Exchange Standard (IES) is a UK Government data-exchange standard: a 4D ontology specified in RDF.
It exists to enable compliant, interoperable data exchange — and the goal of this toolkit
is to make producing that data straightforward.

Formal ontologies can be hard to approach, because they are **constructional** rather than
like a traditional data model. Instead of defining entities with a fixed set of fields to
fill in, an ontology like IES gives you a small set of common objects that you assemble by
following well-defined patterns. That flexibility suits domains where requirements change
faster than a traditional schema can be re-engineered. But it also means that, without an
in-depth understanding of the underlying principles (4D, extensional, constructional), it is
easy to end up with structures that don't follow the patterns.

To counter this, we have collected together a number of conventions and helpers used across projects into
a single Python library, to help data engineers get started and encourage appropriate use of
the ontology. The IES patterns are encoded for you: the wrapper classes and their `add_*`
builders are generated directly from the [`ies-top`](https://github.com/IES-Org/ies-top) /
[`ies-core`](https://github.com/IES-Org/ies-core) ontologies, so the IES-correct structure is
the path of least resistance.

This is work in progress, and we want to hear from users about what is missing or not working,
and of course requests for new features. We will keep adding functionality as we identify the
need in real projects.

## Installation

```bash
pip install telicent-ies-toolkit

# From a source checkout, for development
pip install -e ".[dev]"
```

## Getting Started

> The RDF shown in these examples uses conventional prefixes (`data:` for the example data
> namespace; `ies_core:`/`ies_top:`/`iso8601:` for the IES associated namespaces) for readability.

### Basic Usage

Create an RDF data store and start building IES data.

```python
from ies_toolkit.rdflib_plugin import RdfLibPlugIn
from ies_toolkit.ies_core import Person

store = RdfLibPlugIn()

# Create a person, name them, and their birth
sandy = Person(store=store, uri="http://example.org/rdf/SandyWoodward")
sandy.add_name("Sandy Woodward")
sandy.add_birth(date_of_birth="1932-05-01")

print(store.get_rdf("ttl"))
```

This produces:

```turtle
data:SandyWoodward a ies_core:Person ;
    ies_core:hasName data:SandyWoodward_name_1 .

data:SandyWoodward_name_1 a ies_core:Name ;
    ies_core:representationValue "Sandy Woodward"^^xsd:string .

data:SandyWoodward_birth_2 a ies_core:BirthState ;
    ies_top:isAStartOf data:SandyWoodward ;
    ies_top:isPartOf iso8601:19320501 .

iso8601:19320501 a ies_core:ParticularPeriod ;
    ies_core:iso8601PeriodRepresentation "1932-05-01"^^xsd:string .
```

Here the toolkit mints the underlying IES structure:
- The class instantiation creates a `Person` with the supplied URI;
- `add_name` mints a `Name` node linked by `hasName`;
- `add_birth` mints a `BirthState` that `isAStartOf` the person and which `isPartOf` 
the `ParticularPeriod` of "1932-05-01".

You describe the domain in plain method calls; the toolkit builds the IES.

### Using a Default Store

To avoid passing the `store` parameter to every object, set a default store once:

```python
from ies_toolkit.rdflib_plugin import RdfLibPlugIn
from ies_toolkit.ies_core import Person, Organization
from ies_toolkit import rdfs

# Create an RDF store and set it as the default
store = RdfLibPlugIn()
rdfs.set_default_store(store)

# Now create objects without passing store=
sandy = Person(uri="http://example.org/rdf/SandyWoodward")
sandy.add_name("Sandy Woodward")

navy = Organization(uri="http://example.org/rdf/RoyalNavy")
navy.add_name("Royal Navy")

print(store.get_rdf("ttl"))
```

This produces:

```turtle
data:SandyWoodward a ies_core:Person ;
    ies_core:hasName data:SandyWoodward_name_1 .

data:SandyWoodward_name_1 a ies_core:Name ;
    ies_core:representationValue "Sandy Woodward"^^xsd:string .

data:RoyalNavy a ies_core:Organization ;
    ies_core:hasName data:RoyalNavy_name_1 .

data:RoyalNavy_name_1 a ies_core:Name ;
    ies_core:representationValue "Royal Navy"^^xsd:string .
```

You can still pass an explicit `store` to override the default — useful whenever more than
one graph is required, e.g. building multiple independent datasets in one program (e.g. for access control purposes), 
or giving each thread/worker its own store.

### Working with States and time

To time-bound a State, give a date/datetime and the tool builds the canonical structure for
you — it mints the `ParticularPeriod`, mints a **bounding state**, and adds
`bounding isAStartOf/​isAFinishOf self` + `bounding isPartOf period`:

```python
from ies_toolkit.rdflib_plugin import RdfLibPlugIn
from ies_toolkit.ies_top import State

store = RdfLibPlugIn()
employment = State(store=store, uri="http://example.org/rdf/emp1")

employment.starts_in("2024-03-02")      # mints emp1_start + the ParticularPeriod, adds isAStartOf
employment.finishes_in("2025-06-30")    # mints emp1_finish + the ParticularPeriod, adds isAFinishOf

print(store.get_rdf("ttl"))
```

This produces:

```turtle
data:emp1 a ies_top:State .

data:emp1_start a ies_top:State ;
    ies_top:isAStartOf data:emp1 ;
    ies_top:isPartOf iso8601:20240302 .

data:emp1_finish a ies_top:State ;
    ies_top:isAFinishOf data:emp1 ;
    ies_top:isPartOf iso8601:20250630 .

iso8601:20240302 a ies_core:ParticularPeriod ;
    ies_core:iso8601PeriodRepresentation "2024-03-02"^^xsd:string .

iso8601:20250630 a ies_core:ParticularPeriod ;
    ies_core:iso8601PeriodRepresentation "2025-06-30"^^xsd:string .
```

Use `put_in_period("2024-03-02")` when a state's lifetime falls **entirely within** a
single ParticularPeriod: it links *self* directly (`self isPartOf period`), rather than
minting separate start/finish bounding States. This is useful when there is vague knowledge about the precise start and finish of a state.

### Participations

A **participation** is a state of an entity taking part in an activity. Every activity
carries typed `add_<role>(…)` methods for its shaped roles, plus a generic
`add_participant` for any role that has no typed builder. How you supply the participating
entity depends on how it is involved in the activity — whether it is *wholly* involved
(`isTemporalPartOf`) or *partially* involved (`isPartOf`):

```python
from ies_toolkit.rdflib_plugin import RdfLibPlugIn
from ies_toolkit.ies_core import (
    Meeting, Create, Person, Organization,
)

store = RdfLibPlugIn()
fred = Person(store=store, uri="http://example.org/rdf/fred")
acme = Organization(store=store, uri="http://example.org/rdf/acme")

# 1. Wholly involved: the whole entity takes part.
#    e.g. a person attends a party.
party = Meeting(store=store, uri="http://example.org/rdf/party1")
party.add_attendance(fred)

# 2. Partially involved: only part of the entity is involved.
#    e.g. an organisation manufactures something — only part of its workforce did the work.
create = Create(store=store, uri="http://example.org/rdf/create1")
create.add_creator(organization=acme)

# 3. On behalf of: an entity is involved on behalf of a greater whole.
#    e.g. a person attends a meeting as a representative of a company.
meeting = Meeting(store=store, uri="http://example.org/rdf/meeting1")
meeting.add_attendance(person=fred, organization=acme)

# 4. A plain participant: for a role which is unknown: use the foundational method of
#    add_participant
party.add_participant(fred)

print(store.get_rdf("ttl"))
```

This produces one participation state per call — each typed by its role (`Attendance`,
`Creator`) or `Participation` for the generic method:

```turtle
data:fred a ies_core:Person .
data:acme a ies_core:Organization .

# 1. Wholly involved:
data:party1 a ies_core:Meeting .

data:party1_attendance_1 a ies_core:Attendance ;
    ies_top:isPartOf data:party1 ;
    ies_top:isTemporalPartOf data:fred .

# 2. Partially involved:
data:create1 a ies_core:Create .

data:create1_creator_2 a ies_core:Creator ;
    ies_top:isPartOf data:acme ;
    ies_top:isPartOf data:create1 .

# 3. On behalf of:
data:meeting1 a ies_core:Meeting .

data:meeting1_attendance_3 a ies_core:Attendance ;
    ies_top:isPartOf data:acme ;
    ies_top:isPartOf data:meeting1 ;
    ies_top:isTemporalPartOf data:fred .

# 4. A plain participant:
data:party1_participation_4 a ies_core:Participation ;
    ies_top:isPartOf data:party1 ;
    ies_top:isTemporalPartOf data:fred .

```

### Categories: taxonomic extension with SKOS

Most extensions to IES are **taxonomic** — narrower kinds of an existing concept (a more
specific type of `Vehicle`, say). IES
models these with [SKOS categories](https://www.w3.org/TR/skos-reference/) rather than RDF
subclassing, which it reserves for extensions that introduce genuinely *new semantics*. The
SKOS regime is:

- the extension is a `skos:Concept`, linked by `skos:broader` up to the IES class (or
  concept) it refines;
- every instance still carries a real IES `rdf:type` — its type is always an IES class;
- `ies_core:category` ties the instance to the concept.

So however much custom taxonomy you layer on, an instance's `rdf:type` always resolves to an
IES class every consumer understands. Every wrapper takes a `categories=` argument that adds
`ies_core:category`:

```python
from ies_toolkit.rdflib_plugin import RdfLibPlugIn
from ies_toolkit.ies_core import Vehicle

store = RdfLibPlugIn()
AMBULANCE = "http://example.org/vehicles#Ambulance"

# An instance keeps its IES rdf:type (Vehicle); ies_core:category ties it to the concept
ambulance_07 = Vehicle(store=store, uri="http://example.org/rdf/ambulance-07", categories=[AMBULANCE])

print(store.get_rdf("ttl"))
```

This produces (the instance keeps its IES `rdf:type`, with the extension attached as a category):

```turtle
data:ambulance-07 a ies_core:Vehicle ;
    ies_core:category <http://example.org/vehicles#Ambulance> .
```

### Plain RDF: labels and comments

Alongside the IES builders, every entity supports ubiquitous ordinary RDF/RDFS operations such as
`rdfs:label` and `rdfs:comment`.

```python
from ies_toolkit.rdflib_plugin import RdfLibPlugIn
from ies_toolkit.ies_core import Person

store = RdfLibPlugIn()
sandy = Person(store=store, uri="http://example.org/rdf/SandyWoodward")

sandy.add_label("Sandy Woodward")            # rdfs:label
sandy.add_comment("Royal Navy admiral")      # rdfs:comment

print(store.get_rdf("ttl"))
```

This produces:

```turtle
data:SandyWoodward a ies_core:Person ;
    rdfs:label "Sandy Woodward"^^xsd:string ;
    rdfs:comment "Royal Navy admiral"^^xsd:string .
```

### Referencing IES terms directly

Most data generation goes through the wrapper classes and their `add_*` builders. When you
need a raw term — building a triple by hand, or a SPARQL query — use the typed
namespaces `IesTop` / `IesCore`. Attribute access returns a `URIRef`, and a misspelt
term raises `AttributeError` rather than silently producing a wrong URI:

```python
from ies_toolkit.ies_top import IesTop
from ies_toolkit.ies_core import IesCore

store.add_triple(person.uri, IesTop.isPartOf, org.uri, is_literal=False)
IesCore.Person   # -> URIRef("http://purl.org/ies/core/v0/ont/Person")
IesCore.Persn    # -> AttributeError (typo caught)
```

(`IesCore.Person` is the class's *URI*; `Person` is the *wrapper class* you
instantiate to build one.)

### Large datasets

The whole graph is held in memory. For loads consuming a large amount of RAM, consider using
`clear=True`: it serialises the current triples and then empties the graph, so each
call returns only what was built since the last clear. N-Triples is line-based, so
you can stream batches to one file:

```python
store = RdfLibPlugIn()
with open("out.nt", "w") as f:
    for batch in batches:
        for record in batch:
            ...  # build IES objects against `store`
        f.write(store.get_rdf("nt", clear=True))
```

`save_rdf(path, clear=True)` writes to a file directly, but overwrites `path` each
call — give each batch its own filename if you use it that way.

The store is **not thread-safe** — `generate_data_uri` bumps a shared counter and the
in-memory graph assumes a single writer, so build on one thread. To use more cores,
give each thread/process its own `RdfLibPlugIn` and concatenate the output (each
instance mints URIs under its own unique prefix, so they won't collide).

## Architecture (for maintainers)

The wrapper classes are **partly generated** from `ies-top.ttl` / `ies-core.ttl`
and partly hand-written. If you are changing the ontology or adding custom
behaviour, read [docs/architecture.md](docs/architecture.md) first — it explains
the generation pipeline, the generated-vs-hand-written split, and the exact
steps to follow when the ontology changes or new custom code is needed.

```bash
# Regenerate the wrapper classes after an ontology change
python tools/generate_ies.py --target core
python tools/generate_ies.py --target top
```

## Running Tests

```bash
# Run all tests
pytest tests/ -v

# Run a specific group (unit / generator / examples)
pytest tests/unit -v

# Or use the test script
./run_tests.sh
```
