ShEx
Load ShEx schemas and ShapeMaps, validate RDF data, and use ShExPath, precompiled schemas and external shapes.
Load, check and validate
#![allow(unused)]
fn main() {
use rudof_lib::{Rudof, RudofConfig};
use rudof_lib::formats::{
InputSpec, ShExFormat, ShapeMapFormat, ShExValidationSortByMode,
ResultShExValidationFormat,
};
use std::str::FromStr;
let mut rudof = Rudof::new(RudofConfig::default());
let rdf_data_input = vec![InputSpec::from_str(
r#"
<alice> <name> "Alice" ;
<age> 30 .
"#
).unwrap()];
let base_nodes = "http://example.org";
// Load RDF data into Rudof's state
rudof.load_data()
.with_data(&rdf_data_input)
.with_base(&base_nodes)
.execute()
.unwrap();
let shex_schema_input = InputSpec::from_str(
r#"
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
<PersonShape> {
<name> xsd:string ;
<age> xsd:integer
}
"#,
).unwrap();
let base_shapes = "http://example.org";
// Check ShEx schema well-formedness and check for negative cycles in the dependency graph,
// serializing the checking results to standard output
rudof.check_shex_schema(&shex_schema_input, &mut std::io::stdout())
.with_shex_schema_format(&ShExFormat::ShExC)
.with_base(&base_shapes)
.execute()
.unwrap();
// Load ShEx schema into Rudof's state
rudof.load_shex_schema(&shex_schema_input)
.with_shex_schema_format(&ShExFormat::ShExC)
.with_base(&base_shapes)
.execute()
.unwrap();
// Serialize the loaded ShEx schema to standard output
rudof.serialize_shex_schema(&mut std::io::stdout())
.with_shape(&"http://example.org/PersonShape")
.with_show_statistics(true)
.with_show_dependencies(true)
.with_show_time(true)
.with_result_shex_format(&ShExFormat::ShExJ)
.execute()
.unwrap();
let shapemap_input = InputSpec::from_str(
r#"
<alice>@<PersonShape>
"#
).unwrap();
// Load shapemap into Rudof's state
rudof.load_shapemap(&shapemap_input)
.with_shapemap_format(&ShapeMapFormat::Compact)
.with_base_nodes(&base_nodes)
.with_base_shapes(&base_shapes)
.execute()
.unwrap();
// Serialize the loaded shapemap to standard output
rudof.serialize_shapemap(&mut std::io::stdout())
.with_result_shapemap_format(&ShapeMapFormat::Compact)
.with_show_colors(false)
.execute()
.unwrap();
// Run validation storing the result into Rudof´s state
rudof.validate_shex().execute().unwrap();
// Serialize the validation results to standard output
rudof.serialize_shex_validation_results(&mut std::io::stdout())
.with_shex_validation_sort_order_mode(&ShExValidationSortByMode::Node)
.with_result_shex_validation_format(&ResultShExValidationFormat::Details)
.execute()
.unwrap();
// Reset ShEx schema state
rudof.reset_shex_schema().execute();
// Reset shapemap state
rudof.reset_shapemap().execute();
// Reset ShEx validation state (ShEx schema, shapemap and validation results)
rudof.reset_shex().execute();
}
More ShEx operations
ShExPath, precompiled schemas, external shapes and ShapeMap entries:
#![allow(unused)]
fn main() {
use rudof_lib::{Rudof, RudofConfig};
use rudof_lib::formats::{InputSpec, ShExFormat};
use std::str::FromStr;
let mut rudof = Rudof::new(RudofConfig::default());
let shex_schema_input = InputSpec::from_str(
r#"
prefix : <http://example.org/>
prefix xsd: <http://www.w3.org/2001/XMLSchema#>
:PersonShape { :name xsd:string ; :knows @:PersonShape * }
"#
).unwrap();
rudof.load_shex_schema(&shex_schema_input)
.with_shex_schema_format(&ShExFormat::ShExC)
.execute()
.unwrap();
// Evaluate a ShExPath against the loaded schema
let path = "@<http://example.org/PersonShape>~<http://example.org/name>";
for m in rudof.evaluate_shex_path(path).execute().unwrap() {
println!("{}: {}", m.location, m.value);
}
// Add a node and shape pair to the current shapemap
rudof.add_node_shape_to_shapemap("http://example.org/alice")
.with_shape("http://example.org/PersonShape")
.execute()
.unwrap();
// Write the compiled schema to a precompiled cache, then load it again
let mut cache = Vec::new();
rudof.compile_shex_schema_to_file(&mut cache).execute().unwrap();
std::fs::write("person.shexc.bin", &cache).unwrap();
let cache_input = InputSpec::from_str("person.shexc.bin").unwrap();
rudof.load_shex_schema_precompiled(&cache_input).execute().unwrap();
// Resolve EXTERNAL shapes with the shape declarations of another ShEx file
for info in Rudof::list_external_resolvers() {
println!("{}: {}", info.spec_syntax, info.description);
}
rudof.add_external_resolver("schema:external.shex").unwrap();
rudof.clear_external_resolvers();
}
The precompiled ShEx schemas guide and the ShExPath guide in the user manual explain these features.