Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
bbc807b
feat: introducing the triples and triples_matching methods
angelip2303 Feb 26, 2025
3d655da
fix: refactorig the `triples_with_predicate` method using the `triple…
angelip2303 Feb 26, 2025
e3226e7
fix: improving how the tests are placed in srdfgraph
angelip2303 Feb 26, 2025
d86a834
feat: including some simple tests for the triples_matching methods
angelip2303 Feb 26, 2025
ff8847a
fix: fixing a bug in the shacl parser during the refactoring
angelip2303 Feb 26, 2025
d16d6bf
feat: implementation of the triples_with_subject method
angelip2303 Feb 26, 2025
5784839
feat: implementation of the triples_with_object method
angelip2303 Feb 26, 2025
74cb147
fix: replacing the predicates_with_subject method with its appropiate…
angelip2303 Feb 26, 2025
49ff806
fix: replacing the objects_for_subject_predicate with its appropiate …
angelip2303 Feb 26, 2025
3a90b3e
fix: removing the subjects_for_predicate_object method and replaced b…
angelip2303 Feb 26, 2025
0ba6e61
fix: minor refactoring of the neighs method
angelip2303 Feb 26, 2025
dd64907
fix: writing the default implementation of the incoming_arcs method
angelip2303 Feb 26, 2025
8c43e94
fix: replacing the outgoing_arcs method by its default implementation
angelip2303 Feb 26, 2025
2c84352
fix: replacing the outgoing_arcs_from_list method by its default impl…
angelip2303 Feb 26, 2025
14a3973
fix: fixing some minor clippy-related issues
angelip2303 Feb 26, 2025
8b72173
fix: minor fixes of the srdfgraph thing
angelip2303 Feb 26, 2025
666195c
fix: fixing the method for obtaining the triples in SRDF Sparql
angelip2303 Feb 26, 2025
127fde2
fix: fixing the implementation of SRDFSparql
angelip2303 Feb 26, 2025
651a853
fix: improving the way we express the triples method in SRDFSparql
angelip2303 Feb 26, 2025
ae46758
fix: changing the return types of the triples methods to Result<..>
angelip2303 Feb 26, 2025
0769a92
fix: trying to fix the clippy warnings
angelip2303 Feb 26, 2025
1470803
fix: removing an unnecessary todo
angelip2303 Feb 26, 2025
d3db04d
fix: removing the unnecessary neighs method
angelip2303 Feb 26, 2025
6d0c04e
fix: remove a todo
angelip2303 Feb 26, 2025
222d486
fix: more improvements when it comes to clippy
angelip2303 Feb 26, 2025
f9567c8
fix: writing more tests
angelip2303 Feb 26, 2025
044dc6e
fix: removing the unnecessary reference in the incoming_arcs method
angelip2303 Feb 26, 2025
a860319
fix: removing the unnecessary reference in the outgoing_arcs method
angelip2303 Feb 26, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ members = [

exclude = ["shex_compact_winnow"]

default-members = ["rudof_cli", "shacl_validation"]
default-members = ["rudof_cli", "shacl_validation", "srdf"]

[workspace.package]
edition = "2021"
Expand Down
6 changes: 3 additions & 3 deletions rudof_cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1183,7 +1183,7 @@ where
ShowNodeMode::Outgoing | ShowNodeMode::Both => {
writeln!(writer, "Outgoing arcs")?;
let map = if predicates.is_empty() {
match rdf.outgoing_arcs(&subject) {
match rdf.outgoing_arcs(subject.clone()) {
Result::Ok(rs) => rs,
Err(e) => bail!("Error obtaining outgoing arcs of {subject}: {e}"),
}
Expand Down Expand Up @@ -1215,8 +1215,8 @@ where
match show_node_mode {
ShowNodeMode::Incoming | ShowNodeMode::Both => {
writeln!(writer, "Incoming arcs")?;
let object = subject.clone().into();
let map = match rdf.incoming_arcs(&object) {
let object: S::Term = subject.clone().into();
let map = match rdf.incoming_arcs(object.clone()) {
Result::Ok(m) => m,
Err(e) => bail!("Can't get outgoing arcs of node {subject}: {e}"),
};
Expand Down
41 changes: 14 additions & 27 deletions shacl_ast/src/converter/rdf_to_shacl/shacl_parser.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use iri_s::IriS;
use prefixmap::{IriRef, PrefixMap};
use srdf::{
combine_parsers, combine_vec, has_type, not, ok, optional, parse_nodes, property_bool,
property_value, property_values, property_values_int, property_values_iri,
property_values_non_empty, rdf_list, term, FocusRDF, Iri as _, Literal, Object, PResult,
RDFNode, RDFNodeParse, RDFParseError, RDFParser, Rdf, SHACLPath, Term, Triple as _, RDF_TYPE,
combine_parsers, combine_vec, has_type, matcher::Any, not, ok, optional, parse_nodes,
property_bool, property_value, property_values, property_values_int, property_values_iri,
property_values_non_empty, rdf_list, term, FocusRDF, Iri as _, Literal, PResult, RDFNode,
RDFNodeParse, RDFParseError, RDFParser, Rdf, SHACLPath, Term, Triple, RDF_TYPE,
};
use std::collections::{HashMap, HashSet};

Expand Down Expand Up @@ -72,13 +72,13 @@ where

fn shapes_candidates(&mut self) -> Result<Vec<RDFNode>> {
// subjects with type `sh:NodeShape`
let node_shape_instances = self
let node_shape_instances: HashSet<_> = self
.rdf_parser
.rdf
.subjects_with_predicate_object(&Self::rdf_type(), &Self::sh_node_shape())
.map_err(|e| ShaclParserError::Custom {
msg: format!("Error obtaining values with type sh:NodeShape: {e}"),
})?;
.triples_matching(Any, Self::rdf_type(), Self::sh_node_shape())
.map_err(|e| ShaclParserError::Custom { msg: e.to_string() })?
.map(Triple::into_subject)
.collect();

// subjects with property `sh:property`
let subjects_property = self.objects_with_predicate(Self::sh_property())?;
Expand Down Expand Up @@ -197,16 +197,13 @@ where
}

fn objects_with_predicate(&self, pred: RDF::IRI) -> Result<HashSet<RDF::Subject>> {
let triples = self
let values_as_subjects = self
.rdf_parser
.rdf
.triples_with_predicate(&pred)
.map_err(|e| ShaclParserError::Custom {
msg: format!("Error obtaining values with predicate sh:property: {e}"),
})?;
let values_as_subjects = triples
.iter()
.flat_map(Self::triple_object_as_subject)
.triples_with_predicate(pred)
.map_err(|e| ShaclParserError::Custom { msg: e.to_string() })?
.map(Triple::into_object)
.flat_map(TryInto::try_into)
.collect();
Ok(values_as_subjects)
}
Expand Down Expand Up @@ -249,16 +246,6 @@ where
SH_NODE.clone().into()
}

fn triple_object_as_subject(triple: &RDF::Triple) -> Result<RDF::Subject> {
let subj: RDF::Subject = triple
.obj()
.try_into()
.map_err(|_| ShaclParserError::Custom {
msg: format!("Expected triple object value to act as a subject: {triple}"),
})?;
Ok(subj)
}

fn shape<'a>(state: &'a mut State) -> impl RDFNodeParse<RDF, Output = Shape> + 'a
where
RDF: FocusRDF + 'a,
Expand Down
38 changes: 19 additions & 19 deletions shacl_validation/src/engine/native.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use shacl_ast::compiled::component::CompiledComponent;
use shacl_ast::compiled::property_shape::CompiledPropertyShape;
use shacl_ast::compiled::shape::CompiledShape;
use srdf::matcher::Any;
use srdf::Query;
use srdf::SHACLPath;
use srdf::Term;
Expand Down Expand Up @@ -48,12 +49,13 @@ impl<S: Query + Debug + 'static> Engine<S> for NativeEngine {
return Err(ValidateError::TargetClassNotIri);
}

let subjects = match store.subjects_with_predicate_object(&RDF_TYPE.clone().into(), class) {
Ok(subjects) => subjects,
Err(_) => return Err(ValidateError::SRDF),
};
let rdf_type: S::IRI = RDF_TYPE.clone().into();

let focus_nodes = subjects.iter().map(|subject| subject.clone().into());
let focus_nodes = store
.triples_matching(Any, rdf_type, class.clone())
.map_err(|_| ValidateError::SRDF)?
.map(Triple::into_subject)
.map(Into::into);

Ok(FocusNodes::new(focus_nodes))
}
Expand All @@ -63,27 +65,25 @@ impl<S: Query + Debug + 'static> Engine<S> for NativeEngine {
store: &S,
predicate: &S::IRI,
) -> Result<FocusNodes<S>, ValidateError> {
let triples = match store.triples_with_predicate(predicate) {
Ok(triples) => triples,
Err(_) => return Err(ValidateError::SRDF),
};

Ok(FocusNodes::new(
triples.iter().map(|triple| triple.subj().into()),
))
let subjects = store
.triples_with_predicate(predicate.clone())
.map_err(|_| ValidateError::SRDF)?
.map(Triple::into_subject)
.map(Into::into);
let focus_nodes = FocusNodes::new(subjects);
Ok(focus_nodes)
}

fn target_object_of(
&self,
store: &S,
predicate: &S::IRI,
) -> Result<FocusNodes<S>, ValidateError> {
let triples = match store.triples_with_predicate(predicate) {
Ok(triples) => triples,
Err(_) => return Err(ValidateError::SRDF),
};

Ok(FocusNodes::new(triples.iter().map(Triple::obj)))
let objects = store
.triples_with_predicate(predicate.clone())
.map_err(|_| ValidateError::SRDF)?
.map(Triple::into_object);
Ok(FocusNodes::new(objects))
}

fn implicit_target_class(
Expand Down
37 changes: 20 additions & 17 deletions shacl_validation/src/helpers/srdf.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::collections::HashSet;

use srdf::{Query, RDFNode};
use srdf::{matcher::Any, Query, RDFNode, Triple};

use super::helper_error::SRDFError;

Expand All @@ -23,7 +23,7 @@ pub(crate) fn get_objects_for<S: Query>(
subject: &S::Term,
predicate: &S::IRI,
) -> Result<HashSet<S::Term>, SRDFError> {
let subject = match subject.clone().try_into() {
let subject: S::Subject = match subject.clone().try_into() {
Ok(subject) => subject,
Err(_) => {
return Err(SRDFError::SRDFTermAsSubject {
Expand All @@ -32,26 +32,29 @@ pub(crate) fn get_objects_for<S: Query>(
}
};

store
.objects_for_subject_predicate(&subject, predicate)
.map_err(|e| SRDFError::ObjectsWithSubjectPredicate {
predicate: format!("{predicate}"),
subject: format!("{subject}"),
error: format!("{e}"),
})
let triples = store
.triples_matching(subject, predicate.clone(), Any)
.map_err(|e| SRDFError::Srdf {
error: e.to_string(),
})?
.map(Triple::into_object)
.collect();

Ok(triples)
}

pub(crate) fn get_subjects_for<S: Query>(
store: &S,
predicate: &S::IRI,
object: &S::Term,
) -> Result<HashSet<S::Term>, SRDFError> {
match store.subjects_with_predicate_object(predicate, object) {
Ok(ans) => Ok(ans.into_iter().map(Into::into).collect()),
Err(e) => Err(SRDFError::SubjectsWithPredicateObject {
predicate: format!("{predicate}"),
object: format!("{object}"),
error: format!("{e}"),
}),
}
let values = store
.triples_matching(Any, predicate.clone(), object.clone())
.map_err(|e| SRDFError::Srdf {
error: e.to_string(),
})?
.map(Triple::into_subject)
.map(Into::into)
.collect();
Ok(values)
}
70 changes: 30 additions & 40 deletions shacl_validation/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ use shacl_validation::validation_report::report::ValidationReport;
use shacl_validation::validation_report::validation_report_error::ReportError;
use sparql_service::RdfData;
use sparql_service::RdfDataError;
use srdf::matcher::Any;
use srdf::Query;
use srdf::RDFFormat;
use srdf::Rdf;
use srdf::Triple;
use thiserror::Error;

mod core;
Expand Down Expand Up @@ -83,37 +85,32 @@ impl Manifest {
) -> Result<HashSet<OxTerm>, TestSuiteError> {
let mut entry_terms = HashSet::new();

let mf_entries: NamedNode = shacl_validation_vocab::MF_ENTRIES.clone().into();
let entry_subject = store
.objects_for_subject_predicate(
&subject,
&shacl_validation_vocab::MF_ENTRIES.clone().into(),
)?
.into_iter()
.triples_matching(subject, mf_entries, Any)?
.map(Triple::into_object)
.next();

if let Some(mut subject) = entry_subject {
loop {
let inner_subject: OxSubject = subject.clone().try_into().unwrap();
let rdf_first: NamedNode = srdf::RDF_FIRST.clone().into();
match store
.objects_for_subject_predicate(
&subject.clone().try_into().unwrap(),
&srdf::RDF_FIRST.clone().into(),
)?
.into_iter()
.triples_matching(inner_subject.clone(), rdf_first, Any)?
.map(Triple::into_object)
.next()
{
Some(terms) => entry_terms.insert(terms),
None => break,
};

let rdf_rest: NamedNode = srdf::RDF_REST.clone().into();
subject = match store
.objects_for_subject_predicate(
&subject.clone().try_into().unwrap(),
&srdf::RDF_REST.clone().into(),
)?
.into_iter()
.triples_matching(inner_subject, rdf_rest, Any)?
.map(Triple::into_object)
.next()
{
Some(subject) => subject,
Some(term) => term,
None => break,
};
}
Expand All @@ -125,48 +122,41 @@ impl Manifest {
fn collect_tests(&self) -> Result<Vec<ShaclTest<RdfData>>, TestSuiteError> {
let mut entries = Vec::new();
for entry in &self.entries {
let entry = entry.clone().try_into()?;
let entry: OxSubject = entry.clone().try_into()?;

let action = self
let mf_action: NamedNode = shacl_validation_vocab::MF_ACTION.clone().into();
let action: OxSubject = self
.store
.objects_for_subject_predicate(
&entry,
&shacl_validation_vocab::MF_ACTION.clone().into(),
)?
.into_iter()
.triples_matching(entry.clone(), mf_action, Any)?
.map(Triple::into_object)
.next()
.unwrap();
let action = action.try_into()?;
.unwrap()
.try_into()?;

let mf_result: NamedNode = shacl_validation_vocab::MF_RESULT.clone().into();
let results = self
.store
.objects_for_subject_predicate(
&entry,
&shacl_validation_vocab::MF_RESULT.clone().into(),
)?
.into_iter()
.triples_matching(entry, mf_result, Any)?
.map(Triple::into_object)
.next()
.unwrap();

let report = ValidationReport::parse(&self.store, results)?;

let sht_data_graph: NamedNode = shacl_validation_vocab::SHT_DATA_GRAPH.clone().into();
let data_graph_iri = self
.store
.objects_for_subject_predicate(
&action,
&shacl_validation_vocab::SHT_DATA_GRAPH.clone().into(),
)?
.into_iter()
.triples_matching(action.clone(), sht_data_graph, Any)?
.map(Triple::into_object)
.next()
.unwrap();

let sht_shapes_graph: NamedNode =
shacl_validation_vocab::SHT_SHAPES_GRAPH.clone().into();
let shapes_graph_iri = self
.store
.objects_for_subject_predicate(
&action,
&shacl_validation_vocab::SHT_SHAPES_GRAPH.clone().into(),
)?
.into_iter()
.triples_matching(action, sht_shapes_graph, Any)?
.map(Triple::into_object)
.next()
.unwrap();

Expand Down
2 changes: 1 addition & 1 deletion shex_ast/src/shexr/shexr_error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use iri_s::IriS;
use srdf::{literal::Literal, RDFParseError};
use srdf::RDFParseError;
use std::fmt::Display;
use thiserror::Error;

Expand Down
4 changes: 0 additions & 4 deletions shex_ast/src/shexr/shexr_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@ use crate::{
BNode, NodeConstraint, NodeKind, ObjectValue, Schema, Shape, ShapeDecl, ShapeExpr,
ShapeExprLabel, ValueSetValue,
};
use iri_s::iri;
use iri_s::IriS;
use prefixmap::IriRef;
use srdf::rdf_parser;
use srdf::srdf_parser::*;
use srdf::BlankNode as _;
use srdf::FocusRDF;
use srdf::Iri as _;
use srdf::RDFParseError;
use srdf::Term;
use srdf::{Object, RDFParser};

type Result<A> = std::result::Result<A, ShExRError>;
Expand Down
Loading
Loading