Skip to content

Commit

Permalink
Merge pull request #157 from rudof-project/issue133
Browse files Browse the repository at this point in the history
Issue133
  • Loading branch information
labra authored Sep 17, 2024
2 parents b6f45a2 + 1ac346e commit c2b87bd
Show file tree
Hide file tree
Showing 15 changed files with 646 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ members = [
"shacl_validation",
"shacl_testsuite",
"shapes_converter",
"sparql_service",
"python",
]

Expand Down Expand Up @@ -59,6 +60,7 @@ shex_testsuite = { version = "0.1.0", path = "./shex_testsuite" }
shex_validation = { version = "0.1.0", path = "./shex_validation" }
shex_compact = { version = "0.1.0", path = "./shex_compact" }
srdf = { version = "0.1.0", path = "./srdf" }
sparql_service = { version = "0.1.15", path = "./sparql_service" }

# [dependencies]
# External dependencies
Expand Down
3 changes: 1 addition & 2 deletions rbe/README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# RBE - Rust implementation Regular Bag Expressions

This crate contains an implementation of Regular Bag Expressions in Rust.
This crate contains an implementation of Regular Bag Expressions in Rust.

## More information

- Paper: Semantics and validation of Shapes schemas for RDF, I. Boneva, Jose E. Labra Gayo, Eric Prud'hommeaux, [https://doi.org/10.1007/978-3-319-68288-4_7](https://doi.org/10.1007/978-3-319-68288-4_7)
- A previous project in Scala is [here](https://www.weso.es/shex-s/docs/rbe)

1 change: 1 addition & 0 deletions rudof_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ shacl_ast = { workspace = true, features = ["rdf-star"] }
dctap = { workspace = true }
shapes_converter = { workspace = true, features = ["rdf-star"] }
shacl_validation = { workspace = true }
sparql_service = { workspace = true }

serde = "1.0"
serde_derive = "1.0"
Expand Down
63 changes: 63 additions & 0 deletions rudof_cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,55 @@ pub enum Command {
#[arg(short = 'x', long = "export-mode", value_name = "Result mode")]
output_mode: OutputConvertMode,
},

/// Show information about SPARQL service
Service {
#[arg(short = 's', long = "service", value_name = "SPARQL service name")]
service: InputSpec,

#[arg(
short = 'f',
long = "format",
value_name = "SPARQL service format",
default_value_t = DataFormat::Turtle
)]
service_format: DataFormat,

#[arg(
short = 'o',
long = "output-file",
value_name = "Output file name, default = terminal"
)]
output: Option<PathBuf>,

#[arg(
short = 'r',
long = "result-format",
value_name = "Result service format",
default_value_t = ResultServiceFormat::Internal
)]
result_service_format: ResultServiceFormat,

/// RDF Reader mode
#[arg(
long = "reader-mode",
value_name = "RDF Reader mode",
default_value_t = RDFReaderMode::default(),
value_enum
)]
reader_mode: RDFReaderMode,

/// Config file path, if unset it assumes default config
#[arg(short = 'c', long = "config-file", value_name = "Config file name")]
config: Option<PathBuf>,

#[arg(
long = "force-overwrite",
value_name = "Force overwrite mode",
default_value_t = false
)]
force_overwrite: bool,
},
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
Expand Down Expand Up @@ -871,3 +920,17 @@ impl Display for RDFReaderMode {
}
}
}

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Debug)]
#[clap(rename_all = "lower")]
pub enum ResultServiceFormat {
Internal,
}

impl Display for ResultServiceFormat {
fn fmt(&self, dest: &mut Formatter<'_>) -> Result<(), std::fmt::Error> {
match self {
ResultServiceFormat::Internal => write!(dest, "internal"),
}
}
}
70 changes: 62 additions & 8 deletions rudof_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use shex_ast::SimpleReprSchema;
use shex_ast::{object_value::ObjectValue, shexr::shexr_parser::ShExRParser};
use shex_compact::{ShExFormatter, ShExParser, ShapeMapParser, ShapemapFormatter};
use shex_validation::{Validator, ValidatorConfig};
use sparql_service::{ServiceConfig, ServiceDescription};
use srdf::srdf_graph::SRDFGraph;
use srdf::{RDFFormat, RdfDataConfig, SRDFBuilder, SRDFSparql, SRDF};
use std::fs::{File, OpenOptions};
Expand Down Expand Up @@ -86,6 +87,23 @@ fn main() -> Result<()> {
let cli = Cli::parse();

match &cli.command {
Some(Command::Service {
service,
service_format,
output,
result_service_format,
config,
reader_mode,
force_overwrite,
}) => run_service(
service,
service_format,
reader_mode,
output,
result_service_format,
config,
*force_overwrite,
),
Some(Command::Shex {
schema,
schema_format,
Expand Down Expand Up @@ -371,6 +389,38 @@ fn main() -> Result<()> {
}
}

fn run_service(
input: &InputSpec,
data_format: &DataFormat,
reader_mode: &RDFReaderMode,
output: &Option<PathBuf>,
result_format: &ResultServiceFormat,
config: &Option<PathBuf>,
force_overwrite: bool,
) -> Result<()> {
let reader = input.open_read()?;
let (mut writer, _color) = get_writer(output, force_overwrite)?;
let config = if let Some(path) = config {
ServiceConfig::from_path(path)?
} else {
ServiceConfig::new()
};
let rdf_format = data_format2rdf_format(data_format);
let base = config
.base
.as_ref()
.map(|iri_s| Iri::parse_unchecked(iri_s.as_str().to_string()));

let service_description =
ServiceDescription::from_reader(reader, &rdf_format, base, &(*reader_mode).into())?;
match result_format {
ResultServiceFormat::Internal => {
writeln!(writer, "{service_description}")?;
}
}
Ok(())
}

#[allow(clippy::too_many_arguments)]
fn run_shex(
input: &InputSpec,
Expand Down Expand Up @@ -1325,21 +1375,25 @@ fn shacl_format_to_data_format(shacl_format: &ShaclFormat) -> Result<DataFormat>
}
}

fn data_format2rdf_format(data_format: &DataFormat) -> RDFFormat {
match data_format {
DataFormat::N3 => RDFFormat::N3,
DataFormat::NQuads => RDFFormat::NQuads,
DataFormat::NTriples => RDFFormat::NTriples,
DataFormat::RDFXML => RDFFormat::RDFXML,
DataFormat::TriG => RDFFormat::TriG,
DataFormat::Turtle => RDFFormat::Turtle,
}
}

fn parse_data(
data: &Vec<InputSpec>,
data_format: &DataFormat,
reader_mode: &RDFReaderMode,
config: &RdfDataConfig,
) -> Result<SRDFGraph> {
let mut graph = SRDFGraph::new();
let rdf_format = match data_format {
DataFormat::N3 => RDFFormat::N3,
DataFormat::NQuads => RDFFormat::NQuads,
DataFormat::NTriples => RDFFormat::NTriples,
DataFormat::RDFXML => RDFFormat::RDFXML,
DataFormat::TriG => RDFFormat::TriG,
DataFormat::Turtle => RDFFormat::Turtle,
};
let rdf_format = data_format2rdf_format(data_format);
for d in data {
use std::convert::Into;
let reader = d.open_read()?;
Expand Down
33 changes: 33 additions & 0 deletions sparql_service/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
[package]
name = "sparql_service"
version = "0.1.15"
authors.workspace = true
description.workspace = true
edition.workspace = true
license.workspace = true
documentation = "https://docs.rs/sparql_service"
homepage.workspace = true
repository.workspace = true

[features]
rdf-star = [
"sparesults/rdf-star",
]

[dependencies]
const_format = "0.2"
thiserror = "1"
hashbag = { version = "0.1.11"}
lazy_static = "1"
serde = "1.0"
serde_derive = "1.0"
serde_json = { workspace = true }
serde_yml = "0.0.11"
itertools = "0.13"
indexmap = { version = "2"}
iri_s = { workspace = true }
srdf = { workspace = true }
tracing = { workspace = true }
oxiri = "0.2.3-alpha.1"
sparesults = { version = "0.2.0-alpha.5" }

Loading

0 comments on commit c2b87bd

Please sign in to comment.