Property graphs
Validate property graphs with PG-Schema, and use the embedded property graph database.
PG-Schema validation
#![allow(unused)]
fn main() {
use rudof_lib::{Rudof, RudofConfig};
use rudof_lib::formats::{InputSpec, DataFormat, PgSchemaFormat, ResultPgSchemaValidationFormat};
use std::str::FromStr;
let mut rudof = Rudof::new(RudofConfig::default());
let pg_data_input = vec![InputSpec::from_str(
r#"
(n1 {"Student"}["name": "Alice", "age": 23])
(n2_wrong {"Student"}["name": "Bob", "age": 12])
(n3_wrong {"Student"}["name": "Carol", "age": "unknown"])
"#,
).unwrap()];
// Load Property Graph data into Rudof's state
rudof.load_data()
.with_data(&pg_data_input)
.with_data_format(&DataFormat::Pg)
.execute()
.unwrap();
let pg_schema_input = InputSpec::from_str(
r#"
CREATE NODE TYPE ( AdultStudentType: Student {
name: STRING ,
age: INTEGER CHECK > 18
})
"#).unwrap();
// Load Property Graph Schema into Rudof's state
rudof.load_pg_schema(&pg_schema_input)
.with_pg_schema_format(&PgSchemaFormat::PgSchemaC)
.execute()
.unwrap();
// Serialize the loaded Property Graph Schema to standard output
rudof.serialize_pg_schema(&mut std::io::stdout())
.with_result_pg_schema_format(&PgSchemaFormat::PgSchemaC)
.execute()
.unwrap();
let typemap_input = InputSpec::from_str(
r#"
n1: AdultStudentType,
n2_wrong: AdultStudentType,
n3_wrong: AdultStudentType
"#,).unwrap();
// Load typemap into Rudof's state
rudof.load_typemap(&typemap_input).execute().unwrap();
// Run validation storing the result into Rudof´s state
rudof.validate_pgschema().execute().unwrap();
// Serialize the validation results to standard output
rudof.serialize_pgschema_validation_results(&mut std::io::stdout())
.with_result_pg_schema_validation_format(&ResultPgSchemaValidationFormat::Details)
.with_show_colors(false)
.execute()
.unwrap();
// Reset Property Graph schema shapes state
rudof.reset_pg_schema().execute();
// Reset typemap state
rudof.reset_typemap().execute();
// Reset Property Graph schema validation state (PG schema, typemap and validation results)
rudof.reset_pg_schema_validation().execute();
}
Property graph database
rudof includes an embedded property graph database. It needs the pg-db feature, which is on by default. It is available on native targets only.
#![allow(unused)]
fn main() {
use rudof_lib::{Rudof, RudofConfig};
use rudof_lib::formats::{DataFormat, DdlDialect, InputSpec};
use std::str::FromStr;
let mut rudof = Rudof::new(RudofConfig::default());
let data = vec![InputSpec::from_str(
r#"
prefix : <http://example.org/>
:alice a :Person ; :name "Alice" ; :knows :bob .
:bob a :Person ; :name "Bob" .
"#
).unwrap()];
// Show the DDL that creates the node and edge tables for this data
let ddl = rudof.pg_db_ddl(&data)
.with_dialect(&DdlDialect::Cypher)
.with_data_format(&DataFormat::Turtle)
.execute()
.unwrap();
println!("{ddl}");
// Connect to an in-memory database and load the data
let info = rudof.connect_pg_db(None).with_in_memory(true).execute().unwrap();
println!("storage version {}", info.storage_version);
let (nodes, edges) = rudof.load_pg_db(&data, &mut std::io::stdout())
.with_data_format(&DataFormat::Turtle)
.execute()
.unwrap();
// Run a Cypher query
let query = InputSpec::from_str("MATCH (p) RETURN count(p)").unwrap();
let result = rudof.query_cypher(&query).execute().unwrap();
println!("{:?} {:?}", result.columns, result.rows);
rudof.reset_pg_db_connection().execute();
}
The connect, load and ddl pages of the user manual show the same operations in the command line tool.