From 7b36941e5803391899bffe2c344fbd217d4bdb2d Mon Sep 17 00:00:00 2001 From: Mikhail Katychev Date: Fri, 26 Jul 2024 13:12:40 -0500 Subject: [PATCH 1/4] Not matcher --- api/src/term/matcher/_graph_name_matcher.rs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/api/src/term/matcher/_graph_name_matcher.rs b/api/src/term/matcher/_graph_name_matcher.rs index 9f755159..d08f508d 100644 --- a/api/src/term/matcher/_graph_name_matcher.rs +++ b/api/src/term/matcher/_graph_name_matcher.rs @@ -135,3 +135,22 @@ impl GraphNameMatcher for Any { true } } + +/// Matches on the inverse of the inner [`GraphNameMatcher`] +pub struct Not(pub M); + +impl GraphNameMatcher for Not { + type Term = SimpleTerm<'static>; // not actually used + + fn matches(&self, graph_name: GraphName<&T2>) -> bool { + !self.0.matches(graph_name) + } +} + +impl GraphNameMatcher for Option> { + type Term = SimpleTerm<'static>; // not actually used + + fn matches(&self, graph_name: GraphName<&T2>) -> bool { + graph_name_eq(self.as_ref(), graph_name.map(Term::as_simple)) + } +} From 7dd61a5a1571ccaea8ddd02fa5d6f5bbcd317a20 Mon Sep 17 00:00:00 2001 From: Mikhail Katychev Date: Fri, 26 Jul 2024 15:55:35 -0500 Subject: [PATCH 2/4] introduce Not term --- api/src/term/matcher/_any.rs | 11 +++++++++++ api/src/term/matcher/_graph_name_matcher.rs | 5 +---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/api/src/term/matcher/_any.rs b/api/src/term/matcher/_any.rs index e5968dda..062be0cd 100644 --- a/api/src/term/matcher/_any.rs +++ b/api/src/term/matcher/_any.rs @@ -11,3 +11,14 @@ impl TermMatcher for Any { true } } + +/// Matches on the inverse of the inner [`Term`] or [`GraphName`] +pub struct Not(pub M); + +impl TermMatcher for Not { + type Term = SimpleTerm<'static>; // not actually used + + fn matches(&self, term: &T2) -> bool { + !self.0.matches(term) + } +} diff --git a/api/src/term/matcher/_graph_name_matcher.rs b/api/src/term/matcher/_graph_name_matcher.rs index d08f508d..7eaf5a84 100644 --- a/api/src/term/matcher/_graph_name_matcher.rs +++ b/api/src/term/matcher/_graph_name_matcher.rs @@ -136,9 +136,6 @@ impl GraphNameMatcher for Any { } } -/// Matches on the inverse of the inner [`GraphNameMatcher`] -pub struct Not(pub M); - impl GraphNameMatcher for Not { type Term = SimpleTerm<'static>; // not actually used @@ -148,7 +145,7 @@ impl GraphNameMatcher for Not { } impl GraphNameMatcher for Option> { - type Term = SimpleTerm<'static>; // not actually used + type Term = SimpleTerm<'static>; fn matches(&self, graph_name: GraphName<&T2>) -> bool { graph_name_eq(self.as_ref(), graph_name.map(Term::as_simple)) From bff2d7efb2c6dde666f7dd482bf05b43aeb55746 Mon Sep 17 00:00:00 2001 From: Mikhail Katychev Date: Mon, 29 Jul 2024 12:08:18 -0500 Subject: [PATCH 3/4] updated with unit tests --- api/src/term/matcher.rs | 15 +++++++++++++++ api/src/term/matcher/_graph_name_matcher.rs | 8 -------- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/api/src/term/matcher.rs b/api/src/term/matcher.rs index a44dfa03..2bd7b3d7 100644 --- a/api/src/term/matcher.rs +++ b/api/src/term/matcher.rs @@ -66,10 +66,12 @@ mod test { is_graph_name_matcher([Some(T1), Some(T2), None]); is_graph_name_matcher(&[Some(T1), Some(T2), None][..]); is_graph_name_matcher(|t: Option| t.is_some()); + is_graph_name_matcher(Not(|t: Option| t.is_some())); is_graph_name_matcher([T1, T2].gn()); is_graph_name_matcher(Some(TermKind::Iri)); is_graph_name_matcher(Some(([T1], [T2], [T3]))); is_graph_name_matcher([Some(T1)].matcher_ref()); + is_graph_name_matcher(Not([Some(T1)].matcher_ref())); } #[test] @@ -177,6 +179,12 @@ mod test { assert!(TermMatcher::constant(&Any).is_none()); } + #[test] + fn not() { + assert!(Not(TermKind::BlankNode).matches(&T1)); + assert!(Not([T1, T2]).matches(&T3)); + } + #[test] fn datatype_matcher() { let m1 = Any * xsd::string; // testing Mul @@ -332,6 +340,13 @@ mod test { assert!(GraphNameMatcher::constant(&Any).is_none()); } + #[test] + fn graph_name_not() { + assert!(GraphNameMatcher::matches(&Not([DEFAULT]), Some(&T1))); + assert!(!GraphNameMatcher::matches(&Not([T1, T2].gn()), Some(&T2))); + assert!(GraphNameMatcher::matches(&Not([T1, T2].gn()), Some(&T3))); + } + #[test] fn graph_name_term_matcher_gn() { let a1 = [T1].gn(); diff --git a/api/src/term/matcher/_graph_name_matcher.rs b/api/src/term/matcher/_graph_name_matcher.rs index 7eaf5a84..48df2ce7 100644 --- a/api/src/term/matcher/_graph_name_matcher.rs +++ b/api/src/term/matcher/_graph_name_matcher.rs @@ -143,11 +143,3 @@ impl GraphNameMatcher for Not { !self.0.matches(graph_name) } } - -impl GraphNameMatcher for Option> { - type Term = SimpleTerm<'static>; - - fn matches(&self, graph_name: GraphName<&T2>) -> bool { - graph_name_eq(self.as_ref(), graph_name.map(Term::as_simple)) - } -} From 6d09187f85a19384db08bed38faf20e10d871c9f Mon Sep 17 00:00:00 2001 From: Mikhail Katychev Date: Mon, 29 Jul 2024 12:11:30 -0500 Subject: [PATCH 4/4] move Not into _not.rs --- api/src/term/matcher.rs | 4 +++- api/src/term/matcher/_any.rs | 11 ----------- api/src/term/matcher/_not.rs | 12 ++++++++++++ 3 files changed, 15 insertions(+), 12 deletions(-) create mode 100644 api/src/term/matcher/_not.rs diff --git a/api/src/term/matcher.rs b/api/src/term/matcher.rs index 2bd7b3d7..f93f44be 100644 --- a/api/src/term/matcher.rs +++ b/api/src/term/matcher.rs @@ -21,14 +21,16 @@ mod _datatype_matcher; mod _graph_name_matcher; mod _language_tag_matcher; mod _matcher_ref; +mod _not; mod _term_matcher_gn; mod _trait; -pub use _any::*; +pub use _any::Any; pub use _datatype_matcher::*; pub use _graph_name_matcher::*; pub use _language_tag_matcher::*; pub use _matcher_ref::*; +pub use _not::Not; pub use _term_matcher_gn::*; pub use _trait::*; diff --git a/api/src/term/matcher/_any.rs b/api/src/term/matcher/_any.rs index 062be0cd..e5968dda 100644 --- a/api/src/term/matcher/_any.rs +++ b/api/src/term/matcher/_any.rs @@ -11,14 +11,3 @@ impl TermMatcher for Any { true } } - -/// Matches on the inverse of the inner [`Term`] or [`GraphName`] -pub struct Not(pub M); - -impl TermMatcher for Not { - type Term = SimpleTerm<'static>; // not actually used - - fn matches(&self, term: &T2) -> bool { - !self.0.matches(term) - } -} diff --git a/api/src/term/matcher/_not.rs b/api/src/term/matcher/_not.rs new file mode 100644 index 00000000..21d03987 --- /dev/null +++ b/api/src/term/matcher/_not.rs @@ -0,0 +1,12 @@ +use super::*; + +/// Matches on the inverse of the inner [`Term`] or [`GraphName`] +pub struct Not(pub M); + +impl TermMatcher for Not { + type Term = SimpleTerm<'static>; // not actually used + + fn matches(&self, term: &T2) -> bool { + !self.0.matches(term) + } +}