rudof generate
rudof generate creates synthetic RDF data from a ShEx, SHACL or PG-Schema schema. The data conforms to the schema by default. A configuration file can set the size, the values and the optional properties, and can add controlled errors.
Usage
rudof generate --schema <SCHEMA_FILE> [OPTIONS]
The command detects the schema language from the file: .shex is ShEx, .ttl is SHACL and .pgs is PG-Schema. Use -f to set it. The default is 10 entities, written as Turtle to the terminal.
Examples
Generate data from ShEx
The schema user.shex:
prefix : <http://example.org/>
prefix schema: <http://schema.org/>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
start = @:User
:User {
schema:name xsd:string ;
schema:knows @:User * ;
:status [ :Active :Waiting ] ? ;
}
Generate 3 users:
$ rudof generate -s user.shex -n 3 --seed 42 -o users.ttl
The file users.ttl (the order of the triples can change):
<http://example.org/User-3> <http://schema.org/name> "Beta233" ;
a <http://example.org/User> .
<http://example.org/User-2> <http://schema.org/name> "Delta608" ;
<http://example.org/status> <http://example.org/Waiting> ;
<http://schema.org/knows> <http://example.org/User-3> ;
a <http://example.org/User> .
<http://example.org/User-1> <http://schema.org/name> "Delta142" ;
a <http://example.org/User> .
Generate data from SHACL
The shapes education.ttl:
@prefix : <http://example.org/> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
:Person a sh:NodeShape ;
sh:closed true ;
sh:property [
sh:path :name ;
sh:minCount 1;
sh:maxCount 1;
sh:datatype xsd:string ;
] ;
sh:property [
sh:path :birthDate ;
sh:maxCount 1;
sh:datatype xsd:date ;
] ;
sh:property [
sh:path :enrolledIn ;
sh:node :Course ;
] .
:Course a sh:NodeShape;
sh:closed true ;
sh:property [
sh:path :name ;
sh:minCount 1;
sh:maxCount 1;
sh:datatype xsd:string ;
] .
$ rudof generate -s education.ttl -n 4 --seed 7 -o education_data.ttl
The output has people with names, optional birth dates and courses:
<http://example.org/Person-2> <http://example.org/name> "Delta314" ;
<http://example.org/birthDate> "2016-03-22"^^<http://www.w3.org/2001/XMLSchema#date> ;
<http://example.org/enrolledIn> <http://example.org/Course-2> ;
a <http://example.org/Person> .
<http://example.org/Course-2> <http://example.org/name> "Delta223" ;
a <http://example.org/Course> .
Reproducible data
The same --seed gives the same data on each run, with any number of threads:
rudof generate -s user.shex -n 10 --seed 42 -o reproducible.ttl
Other output formats
rudof generate -s user.shex -n 5 -r ntriples -o data.nt
rudof generate -s user.shex -n 5 -r rdfxml -o data.rdf
Large datasets
Use -p to set the number of threads:
rudof generate -s education.ttl -n 10000 -p 8 -o large_dataset.ttl
Configuration files
--generator-config <FILE> loads the generation settings, a TOML (or JSON) file that
starts with version = 2. Unknown keys are errors. --entities, --result-format,
--seed, --parallel and --output-file on the command line override the matching
setting in the file. The full format is described in the
rudof_generate README,
with one example per use in rudof_generate/examples/configs/.
Basic configuration file
version = 2
[run]
seed = 42 # same seed => same graph, whatever `threads` is
threads = 4
scale = { entities = 1000 } # a total; or { per_shape = 1000 }
[defaults]
fill = 0.8 # probability that an OPTIONAL property is generated
cardinality = "balanced" # "min" | "max" | "uniform" | "balanced"
[output]
dir = "out" # writes out/data.ttl and out/data.shapemap
formats = ["turtle"] # "turtle" | "ntriples" | "pg"
Per-shape and per-property settings
version = 2
[prefixes]
ex = "http://example.org/"
[[shape]]
match = "ex:PersonShape"
count = 300
fill = 0.5
[[property]]
match = { shape = "ex:PersonShape", path = "ex:email" }
fill = 1.0
[values.datatypes."xsd:integer"]
generator = "integer"
parameters = { min = 18, max = 65 }
Controlled errors and target coherence
version = 2
[violations] # break 10% of the entities; out/labels.jsonl lists what must fail
rate = 0.1
[targets] # search the fill until the output has this coherence
coherence = 0.8
[output]
dir = "out"
[violations] breaks a share (rate) of the generated entities, one constraint in each. The operators key selects how. Without it, rudof uses every operator that applies to the schema:
| Operator | What it does |
|---|---|
drop-required | removes a required property |
exceed-max | adds a value beyond the maximum cardinality |
wrong-datatype, ill-typed-literal | a value of another datatype, or an invalid lexical form of the declared one |
out-of-range, pattern-break, not-in-value-set | a value outside a range, a pattern or a value set |
dangling-reference | a reference to a node that does not conform to the referenced shape |
closed-extra | a property that a closed shape does not allow |
wrong-language, duplicate-language, lang-string-for-string | language tags that break sh:languageIn, sh:uniqueLang or a plain string |
labels.jsonl then says what a correct validator of the schema’s language must report,
one line per node:
direct: the node was broken, with theoperatorand theproperty.induced: the node fails through a reference to a broken node, with itscause.trap: the node was changed, but it still conforms. Examples are a language tag in another case, or030for30under a range. A validator that reports a trap is wrong.
PG-Schema and property graphs
A .pgs schema generates a property graph:
rudof generate -s persons.pgs --generator-config pg.toml
version = 2
[run]
scale = { per_shape = 500 }
[violations]
rate = 0.2
[violations.rdf] # the report for the RDF copy of the graph
closed = true # keep PG-Schema's closed records (false: open shapes)
keys_required = true # keep its required keys (false: optional keys)
[output]
dir = "out"
formats = ["pg", "turtle"] # out/data.pg, out/data.typemap, out/data.ttl, out/data.shapemap
data.pg is the graph in the property graph format of rudof. data.typemap gives the type of each node and edge, for rudof pgschema-validate. With turtle, rudof also writes the graph as RDF. In this RDF encoding, a ShEx or SHACL schema can express the same rules as the PG-Schema. With errors, labels.jsonl reports the results for PG-Schema, by node and edge. labels-rdf.jsonl reports the results for ShEx and SHACL on the RDF copy, with the defaults of [violations.rdf].
Validate the output
--validate validates the generated data against the schema it came from, and exits with
an error on violations:
- ShEx: each node against the shape that generated it, through the ShapeMap.
- SHACL: through the targets of the shapes.
rudof generate -s schema.shex -n 1000 -o out/data.ttl --validate
Output files
With an output file, the generator also writes a statistics file next to it, for example users.stats.json for users.ttl:
{
"total_triples": 8,
"total_subjects": 3,
"total_predicates": 4,
"total_objects": 6,
"generation_time": "0ms",
"shape_counts": {
"http://example.org/User": 3
},
"conformance_metrics": {
"total_generated_triples": 8,
"valid_triples": 8,
"triple_validity_percentage": 100.0,
"original_schema_constraints": 7,
"represented_constraints_in_unified": 7,
"shape_translation_loss_percentage": 0.0
}
}
Supported schema features
| Language | Supported |
|---|---|
| ShEx | Shape expressions and constraints. Cardinality (*, +, ?, {n,m}). Datatypes. Node kinds (IRI, literal, blank node). Value sets. References between shapes (@:Shape). CLOSED and EXTRA. Language tags (language sets and stems). |
| SHACL | Node shapes and property shapes. sh:minCount, sh:maxCount. sh:datatype, sh:nodeKind. sh:in, sh:hasValue, sh:pattern and ranges. sh:node, sh:class, sh:qualifiedValueShape. sh:closed. sh:languageIn, sh:uniqueLang. |
| PG-Schema | Node types with one or more labels. Required, OPTIONAL and list keys (T *, T {m,n}). CHECK ranges and REGEX patterns. Edge types between typed endpoints ((:@T)). |
Edge types with properties are not supported. The generator skips them and writes a warning.
Troubleshooting
| Problem | Solution |
|---|---|
| “Schema from stdin is not supported” | Give the schema as a file path with -s. |
| “File already exists” | Add --force-overwrite, or delete the file. |
| The output is empty or very small | Increase the number of entities with -n, or make sure that the schema has shapes. |
| The generation is slow | Use more threads with -p. |
More examples
A user directory
Schema users.shex:
prefix : <http://example.org/>
prefix schema: <http://schema.org/>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
:User {
schema:name xsd:string ;
schema:email xsd:string ;
:age xsd:integer ? ;
}
Generate the data:
rudof generate -s users.shex -n 20 -o users.ttl
Course enrollment
Schema courses.ttl:
@prefix : <http://example.org/> .
@prefix sh: <http://www.w3.org/ns/shacl#> .
@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .
:Student a sh:NodeShape ;
sh:property [
sh:path :studentId ;
sh:datatype xsd:string ;
sh:minCount 1 ;
sh:maxCount 1 ;
] ;
sh:property [
sh:path :name ;
sh:datatype xsd:string ;
sh:minCount 1 ;
] ;
sh:property [
sh:path :enrolledIn ;
sh:node :Course ;
] .
:Course a sh:NodeShape ;
sh:property [
sh:path :courseName ;
sh:datatype xsd:string ;
sh:minCount 1 ;
] .
Generate the data:
rudof generate -s courses.ttl -n 50 -o enrollment_data.ttl
A large dataset with a configuration file
Config large_config.toml:
version = 2
[run]
seed = 12345
threads = 8
scale = { entities = 5000 }
[output]
dir = "large_dataset"
formats = ["ntriples"]
stats = true
Generate the data:
rudof generate --schema courses.ttl --generator-config large_config.toml
Options
$ rudof generate --help
Generate synthetic RDF data from ShEx or SHACL schemas
Usage: rudof generate [OPTIONS] --schema <Schema file (ShEx or SHACL)>
Options:
-s, --schema <Schema file (ShEx or SHACL)>
-f, --schema-format <Schema format>
[default: auto]
[possible values: auto, shex, shacl]
-n, --entities <Number of entities to generate>
Number of entities to generate. Defaults to 10, unless `--generator-config` is given and its `entity_count` is left to take effect instead
-r, --result-format <Output RDF format (default: turtle, or the format of --generator-config)>
[possible values: turtle, ntriples, rdfxml, trig, n3, nquads, jsonld, pg, html]
--seed <Random seed for reproducible generation>
-p, --parallel <Number of parallel threads>
--generator-config <Generator config file (TOML or JSON)>
Generator config file (TOML or JSON) controlling entity distribution, cardinality strategy, per-field generators, etc.
Distinct from `--config-file`/`-c`, which is the common rudof config (`[rdf]`, `[shex]`, ...) shared by every subcommand. `--entities`, `--result-format`, `--seed`, `--parallel` and `--output-file` all override the matching setting in this file when both are given.
--validate
Validate the generated data against the input schema (ShEx: each node against the shape that generated it, via `<output>.shapemap`; SHACL: the shapes' targets). Requires `--output`; exits with an error on violations
-c, --config-file <FILE>
Config file name
-o, --output-file <FILE>
Output file name, default = terminal
--force-overwrite
Force overwrite to output file if it already exists
-h, --help
Print help (see a summary with '-h')
See also
shex-validateandshacl-validate: validate the generated data.- Data generation in Rust.
- The example schemas and configurations of the generator.