Examples

Run these examples from docs/examples after installing ontouml-json2graph. The small project is a quick starting point; the catalog model shows a larger conversion. The commands write results under docs/examples/output, which can be removed after use.

Minimal example

The minimal project contains one class. The documentation tests execute this same input and the following two examples.

{
  "id": "example-project",
  "name": "Minimal example",
  "type": "Project",
  "model": {
    "id": "example-model",
    "name": "Minimal model",
    "type": "Package",
    "contents": [
      {
        "id": "person",
        "name": "Person",
        "type": "Class",
        "stereotype": "kind"
      }
    ]
  }
}

Run the command-line conversion:

python -m json2graph.decode -i minimal-project.json -o output -f ttl --silent

Or use the Python library:

"""Run the canonical ontouml-json2graph library example."""

from json2graph.library import decode_json_project, save_graph_file

graph = decode_json_project("minimal-project.json")
save_graph_file(graph, "minimal-project.ttl", "ttl")

Catalog example: Simple Genealogy Ontology

The complete JSON project comes from the OntoUML Model Catalog. The source record gives the pinned revision, upstream JSON blob, and its CC BY 4.0 license. This JSON is complete and executable; the short excerpts below are for reading only.

A class in the source:

      "id" : "7H_33OGFS_j2pM9q",
      "name" : "Person",
      "description" : null,
      "type" : "Class",
      "propertyAssignments" : null,
      "stereotype" : "kind",

The source also defines a material relation, parentOf:

      "id" : "Iv_33OGFS_j2pM.d",
      "name" : "parentOf",
      "description" : null,
      "type" : "Relation",
      "propertyAssignments" : null,
      "stereotype" : "material",

Convert the model

To produce model-only Turtle, run:

python -m json2graph.decode -i catalog/genealogy2013.json -o output --model_only --silent

This writes output/genealogy2013.ttl, with classes such as Person, Parent, and Registration, and the parentOf relation. In Python, obtain an RDFLib graph directly:

from json2graph.library import decode_json_model

graph = decode_json_model("catalog/genealogy2013.json")

Complete project versus model only

Run these commands separately (both write the same output filename):

python -m json2graph.decode -i catalog/genealogy2013.json -o output --silent
python -m json2graph.decode -i catalog/genealogy2013.json -o output --model_only --silent

The corresponding Python calls are decode_json_project("catalog/genealogy2013.json") and decode_json_model("catalog/genealogy2013.json").

Output

Domain model elements

Packages

Project

Diagram and views

Complete project

Yes

Yes

Yes

Yes

Model only

Yes

No

No

No

The complete source contains diagram Paths with ordered points. A complete conversion emits an expected PathPointOrderWarning because the OntoUML Vocabulary cannot represent their order. The graph is still written. Model-only conversion removes the Paths and avoids this warning. --silent hides log messages, not Python warnings. See Limitations and diagnostics for details.

Choose a base URI

To place generated resources under an explicit namespace:

python -m json2graph.decode -i catalog/genealogy2013.json -o output --model_only --base-uri https://example.org/genealogy# --silent

The equivalent Python call is:

graph = decode_json_model("catalog/genealogy2013.json", base_uri="https://example.org/genealogy#")

Generate provenance

The CLI can write transformation metadata to a separate Turtle file:

python -m json2graph.decode -i catalog/genealogy2013.json -o output --model_only --transformation-metadata sidecar --silent

This writes output/genealogy2013.ttl and output/genealogy2013.provenance.ttl. The Python library supports embedded transformation metadata with transformation_metadata="embedded"; it does not write a sidecar file.

For additional options, see the command-line guide, Python library guide, and Policies and configuration.