Forbids variables in property path

Allows BGP to contain TriplePattern
pull/10/head
Tpt 6 years ago
parent 028910b77c
commit 257cd08846
  1. 212
      src/sparql/algebra.rs
  2. 31
      src/sparql/parser.rs
  3. 67
      src/sparql/sparql_grammar.rustpeg

@ -7,7 +7,7 @@ use utils::Escaper;
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
pub enum PropertyPath {
PredicatePath(NamedNodeOrVariable), //TODO: restrict to NamedNode
PredicatePath(NamedNode),
InversePath(Box<PropertyPath>),
SequencePath(Box<PropertyPath>, Box<PropertyPath>),
AlternativePath(Box<PropertyPath>, Box<PropertyPath>),
@ -20,7 +20,7 @@ pub enum PropertyPath {
impl fmt::Display for PropertyPath {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
PropertyPath::PredicatePath(p) => write!(f, "{}", p),
PropertyPath::PredicatePath(p) => write!(f, "link({})", p),
PropertyPath::InversePath(p) => write!(f, "inv({})", p),
PropertyPath::AlternativePath(a, b) => write!(f, "alt({}, {})", a, b),
PropertyPath::SequencePath(a, b) => write!(f, "seq({}, {})", a, b),
@ -73,38 +73,26 @@ impl<'a> fmt::Display for SparqlPropertyPath<'a> {
}
}
impl From<NamedNodeOrVariable> for PropertyPath {
fn from(p: NamedNodeOrVariable) -> Self {
PropertyPath::PredicatePath(p)
}
}
impl From<NamedNode> for PropertyPath {
fn from(p: NamedNode) -> Self {
PropertyPath::PredicatePath(p.into())
}
}
impl From<Variable> for PropertyPath {
fn from(p: Variable) -> Self {
PropertyPath::PredicatePath(p.into())
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
pub struct PropertyPathPattern {
pub struct PathPattern {
pub subject: TermOrVariable,
pub path: PropertyPath,
pub object: TermOrVariable,
}
impl fmt::Display for PropertyPathPattern {
impl fmt::Display for PathPattern {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{} {} {}", self.subject, self.path, self.object)
write!(f, "Path({} {} {})", self.subject, self.path, self.object)
}
}
impl PropertyPathPattern {
impl PathPattern {
pub fn new(
subject: impl Into<TermOrVariable>,
path: impl Into<PropertyPath>,
@ -118,19 +106,9 @@ impl PropertyPathPattern {
}
}
impl From<TriplePattern> for PropertyPathPattern {
fn from(p: TriplePattern) -> Self {
Self {
subject: p.subject,
path: p.predicate.into(),
object: p.object,
}
}
}
struct SparqlPropertyPathPattern<'a>(&'a PropertyPathPattern);
struct SparqlPathPattern<'a>(&'a PathPattern);
impl<'a> fmt::Display for SparqlPropertyPathPattern<'a> {
impl<'a> fmt::Display for SparqlPathPattern<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(
f,
@ -142,6 +120,44 @@ impl<'a> fmt::Display for SparqlPropertyPathPattern<'a> {
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
pub enum TripleOrPathPattern {
Triple(TriplePattern),
Path(PathPattern),
}
impl<'a> fmt::Display for TripleOrPathPattern {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
TripleOrPathPattern::Triple(tp) => write!(f, "{}", tp),
TripleOrPathPattern::Path(ppp) => write!(f, "{}", ppp),
}
}
}
impl From<TriplePattern> for TripleOrPathPattern {
fn from(tp: TriplePattern) -> Self {
TripleOrPathPattern::Triple(tp)
}
}
impl From<PathPattern> for TripleOrPathPattern {
fn from(ppp: PathPattern) -> Self {
TripleOrPathPattern::Path(ppp)
}
}
struct SparqlTripleOrPathPattern<'a>(&'a TripleOrPathPattern);
impl<'a> fmt::Display for SparqlTripleOrPathPattern<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.0 {
TripleOrPathPattern::Triple(tp) => write!(f, "{}", tp),
TripleOrPathPattern::Path(ppp) => write!(f, "{}", SparqlPathPattern(&ppp)),
}
}
}
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
pub enum Expression {
ConstantExpression(TermOrVariable),
@ -750,7 +766,7 @@ impl<'a> fmt::Display for SparqlExpression<'a> {
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Hash)]
pub enum MultiSetPattern {
BGP(Vec<PropertyPathPattern>),
BGP(Vec<TripleOrPathPattern>),
Join(Box<MultiSetPattern>, Box<MultiSetPattern>),
LeftJoin(Box<MultiSetPattern>, Box<MultiSetPattern>, Expression),
Filter(Expression, Box<MultiSetPattern>),
@ -792,8 +808,8 @@ impl Default for MultiSetPattern {
}
}
impl From<PropertyPathPattern> for MultiSetPattern {
fn from(p: PropertyPathPattern) -> Self {
impl From<TripleOrPathPattern> for MultiSetPattern {
fn from(p: TripleOrPathPattern) -> Self {
MultiSetPattern::BGP(vec![p])
}
}
@ -818,14 +834,28 @@ impl MultiSetPattern {
match self {
MultiSetPattern::BGP(p) => {
for pattern in p {
if let TermOrVariable::Variable(ref s) = pattern.subject {
match pattern {
TripleOrPathPattern::Triple(tp) => {
if let TermOrVariable::Variable(ref s) = tp.subject {
vars.insert(s);
}
//TODO: pred
if let TermOrVariable::Variable(ref o) = pattern.object {
if let NamedNodeOrVariable::Variable(ref p) = tp.predicate {
vars.insert(p);
}
if let TermOrVariable::Variable(ref o) = tp.object {
vars.insert(o);
}
}
TripleOrPathPattern::Path(ppp) => {
if let TermOrVariable::Variable(ref s) = ppp.subject {
vars.insert(s);
}
if let TermOrVariable::Variable(ref o) = ppp.object {
vars.insert(o);
}
}
}
}
}
MultiSetPattern::Join(a, b) => {
a.add_visible_variables(vars);
@ -865,7 +895,7 @@ impl<'a> fmt::Display for SparqlMultiSetPattern<'a> {
f,
"{}",
p.iter()
.map(|v| SparqlPropertyPathPattern(v).to_string())
.map(|v| SparqlTripleOrPathPattern(v).to_string())
.collect::<Vec<String>>()
.join(" . ")
)
@ -1318,12 +1348,12 @@ impl fmt::Display for Query {
#[test]
fn test_sparql_algebra_examples() {
assert_eq!(
ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new(
ast::GraphPattern::PathPattern(ast::PathPattern::new(
Variable::new("s"),
Variable::new("p"),
Variable::new("o")
)).try_into(),
Ok(GraphPattern::BGP(vec![PropertyPathPattern::new(
Ok(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p"),
Variable::new("o"),
@ -1332,43 +1362,43 @@ fn test_sparql_algebra_examples() {
assert_eq!(
ast::GraphPattern::GroupPattern(vec![
ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new(
ast::GraphPattern::PathPattern(ast::PathPattern::new(
Variable::new("s"),
Variable::new("p1"),
Variable::new("v1"),
)),
ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new(
ast::GraphPattern::PathPattern(ast::PathPattern::new(
Variable::new("s"),
Variable::new("p2"),
Variable::new("v2"),
)),
]).try_into(),
Ok(GraphPattern::BGP(vec![
PropertyPathPattern::new(Variable::new("s"), Variable::new("p1"), Variable::new("v1")),
PropertyPathPattern::new(Variable::new("s"), Variable::new("p2"), Variable::new("v2")),
PathPattern::new(Variable::new("s"), Variable::new("p1"), Variable::new("v1")),
PathPattern::new(Variable::new("s"), Variable::new("p2"), Variable::new("v2")),
]))
);
assert_eq!(
ast::GraphPattern::UnionPattern(vec![
ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new(
ast::GraphPattern::PathPattern(ast::PathPattern::new(
Variable::new("s"),
Variable::new("p1"),
Variable::new("v1"),
)),
ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new(
ast::GraphPattern::PathPattern(ast::PathPattern::new(
Variable::new("s"),
Variable::new("p2"),
Variable::new("v2"),
)),
]).try_into(),
Ok(GraphPattern::Union(
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p1"),
Variable::new("v1"),
)])),
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p2"),
Variable::new("v2"),
@ -1378,17 +1408,17 @@ fn test_sparql_algebra_examples() {
assert_eq!(
ast::GraphPattern::UnionPattern(vec![
ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new(
ast::GraphPattern::PathPattern(ast::PathPattern::new(
Variable::new("s"),
Variable::new("p1"),
Variable::new("v1"),
)),
ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new(
ast::GraphPattern::PathPattern(ast::PathPattern::new(
Variable::new("s"),
Variable::new("p2"),
Variable::new("v2"),
)),
ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new(
ast::GraphPattern::PathPattern(ast::PathPattern::new(
Variable::new("s"),
Variable::new("p3"),
Variable::new("v3"),
@ -1396,18 +1426,18 @@ fn test_sparql_algebra_examples() {
]).try_into(),
Ok(GraphPattern::Union(
Box::new(GraphPattern::Union(
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p1"),
Variable::new("v1"),
)])),
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p2"),
Variable::new("v2"),
)])),
)),
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p3"),
Variable::new("v3"),
@ -1417,13 +1447,13 @@ fn test_sparql_algebra_examples() {
assert_eq!(
ast::GraphPattern::GroupPattern(vec![
ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new(
ast::GraphPattern::PathPattern(ast::PathPattern::new(
Variable::new("s"),
Variable::new("p1"),
Variable::new("v1"),
)),
ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PropertyPathPattern(
ast::PropertyPathPattern::new(
ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PathPattern(
ast::PathPattern::new(
Variable::new("s"),
Variable::new("p2"),
Variable::new("v2"),
@ -1431,12 +1461,12 @@ fn test_sparql_algebra_examples() {
))),
]).try_into(),
Ok(GraphPattern::LeftJoin(
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p1"),
Variable::new("v1"),
)])),
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p2"),
Variable::new("v2"),
@ -1447,20 +1477,20 @@ fn test_sparql_algebra_examples() {
assert_eq!(
ast::GraphPattern::GroupPattern(vec![
ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new(
ast::GraphPattern::PathPattern(ast::PathPattern::new(
Variable::new("s"),
Variable::new("p1"),
Variable::new("v1"),
)),
ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PropertyPathPattern(
ast::PropertyPathPattern::new(
ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PathPattern(
ast::PathPattern::new(
Variable::new("s"),
Variable::new("p2"),
Variable::new("v2"),
),
))),
ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PropertyPathPattern(
ast::PropertyPathPattern::new(
ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PathPattern(
ast::PathPattern::new(
Variable::new("s"),
Variable::new("p3"),
Variable::new("v3"),
@ -1469,19 +1499,19 @@ fn test_sparql_algebra_examples() {
]).try_into(),
Ok(GraphPattern::LeftJoin(
Box::new(GraphPattern::LeftJoin(
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p1"),
Variable::new("v1"),
)])),
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p2"),
Variable::new("v2"),
)])),
ast::Expression::ConstantExpression(Literal::from(true).into()),
)),
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p3"),
Variable::new("v3"),
@ -1492,13 +1522,13 @@ fn test_sparql_algebra_examples() {
assert_eq!(
ast::GraphPattern::GroupPattern(vec![
ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new(
ast::GraphPattern::PathPattern(ast::PathPattern::new(
Variable::new("s"),
Variable::new("p1"),
Variable::new("v1"),
)),
ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::GroupPattern(vec![
ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new(
ast::GraphPattern::PathPattern(ast::PathPattern::new(
Variable::new("s"),
Variable::new("p2"),
Variable::new("v2"),
@ -1512,12 +1542,12 @@ fn test_sparql_algebra_examples() {
]))),
]).try_into(),
Ok(GraphPattern::LeftJoin(
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p1"),
Variable::new("v1"),
)])),
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p2"),
Variable::new("v2"),
@ -1534,19 +1564,19 @@ fn test_sparql_algebra_examples() {
assert_eq!(
ast::GraphPattern::GroupPattern(vec![
ast::GraphPattern::UnionPattern(vec![
ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new(
ast::GraphPattern::PathPattern(ast::PathPattern::new(
Variable::new("s"),
Variable::new("p1"),
Variable::new("v1"),
)),
ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new(
ast::GraphPattern::PathPattern(ast::PathPattern::new(
Variable::new("s"),
Variable::new("p2"),
Variable::new("v2"),
)),
]),
ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PropertyPathPattern(
ast::PropertyPathPattern::new(
ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PathPattern(
ast::PathPattern::new(
Variable::new("s"),
Variable::new("p3"),
Variable::new("v3"),
@ -1555,18 +1585,18 @@ fn test_sparql_algebra_examples() {
]).try_into(),
Ok(GraphPattern::LeftJoin(
Box::new(GraphPattern::Union(
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p1"),
Variable::new("v1"),
)])),
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p2"),
Variable::new("v2"),
)])),
)),
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p3"),
Variable::new("v3"),
@ -1577,7 +1607,7 @@ fn test_sparql_algebra_examples() {
assert_eq!(
ast::GraphPattern::GroupPattern(vec![
ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new(
ast::GraphPattern::PathPattern(ast::PathPattern::new(
Variable::new("s"),
Variable::new("p1"),
Variable::new("v1"),
@ -1588,8 +1618,8 @@ fn test_sparql_algebra_examples() {
)),
Box::new(ast::Expression::ConstantExpression(Literal::from(3).into())),
)),
ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PropertyPathPattern(
ast::PropertyPathPattern::new(
ast::GraphPattern::OptionalPattern(Box::new(ast::GraphPattern::PathPattern(
ast::PathPattern::new(
Variable::new("s"),
Variable::new("p2"),
Variable::new("v2"),
@ -1604,12 +1634,12 @@ fn test_sparql_algebra_examples() {
Box::new(ast::Expression::ConstantExpression(Literal::from(3).into())),
),
Box::new(GraphPattern::LeftJoin(
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p1"),
Variable::new("v1"),
)])),
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p2"),
Variable::new("v2"),
@ -1621,7 +1651,7 @@ fn test_sparql_algebra_examples() {
assert_eq!(
ast::GraphPattern::GroupPattern(vec![
ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new(
ast::GraphPattern::PathPattern(ast::PathPattern::new(
Variable::new("s"),
Variable::new("p"),
Variable::new("v"),
@ -1635,7 +1665,7 @@ fn test_sparql_algebra_examples() {
),
Variable::new("v2"),
),
ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new(
ast::GraphPattern::PathPattern(ast::PathPattern::new(
Variable::new("s"),
Variable::new("p1"),
Variable::new("v2"),
@ -1643,7 +1673,7 @@ fn test_sparql_algebra_examples() {
]).try_into(),
Ok(GraphPattern::Join(
Box::new(GraphPattern::Extend(
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p"),
Variable::new("v"),
@ -1656,7 +1686,7 @@ fn test_sparql_algebra_examples() {
)),
),
)),
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p1"),
Variable::new("v2"),
@ -1668,13 +1698,13 @@ fn test_sparql_algebra_examples() {
assert_eq!(
ast::GraphPattern::GroupPattern(vec![
ast::GraphPattern::PropertyPathPattern(ast::PropertyPathPattern::new(
ast::GraphPattern::PathPattern(ast::PathPattern::new(
Variable::new("s"),
Variable::new("p"),
Variable::new("v"),
)),
ast::GraphPattern::MinusPattern(Box::new(ast::GraphPattern::PropertyPathPattern(
ast::PropertyPathPattern::new(
ast::GraphPattern::MinusPattern(Box::new(ast::GraphPattern::PathPattern(
ast::PathPattern::new(
Variable::new("s"),
Variable::new("p1"),
Variable::new("v2"),
@ -1682,12 +1712,12 @@ fn test_sparql_algebra_examples() {
))),
]).try_into(),
Ok(GraphPattern::Minus(
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p"),
Variable::new("v"),
)])),
Box::new(GraphPattern::BGP(vec![PropertyPathPattern::new(
Box::new(GraphPattern::BGP(vec![PathPattern::new(
Variable::new("s"),
Variable::new("p1"),
Variable::new("v2"),

@ -49,12 +49,29 @@ mod grammar {
}
}
struct FocusedPropertyPathPattern<F> {
#[derive(Clone)]
enum VariableOrPropertyPath {
Variable(Variable),
PropertyPath(PropertyPath),
}
fn to_triple_or_path_pattern(
s: TermOrVariable,
p: VariableOrPropertyPath,
o: TermOrVariable,
) -> TripleOrPathPattern {
match p {
VariableOrPropertyPath::Variable(p) => TriplePattern::new(s, p, o).into(),
VariableOrPropertyPath::PropertyPath(p) => PathPattern::new(s, p, o).into(),
}
}
struct FocusedTripleOrPathPattern<F> {
focus: F,
patterns: Vec<PropertyPathPattern>,
patterns: Vec<TripleOrPathPattern>,
}
impl<F> FocusedPropertyPathPattern<F> {
impl<F> FocusedTripleOrPathPattern<F> {
fn new(focus: F) -> Self {
Self {
focus,
@ -63,7 +80,7 @@ mod grammar {
}
}
impl<F: Default> Default for FocusedPropertyPathPattern<F> {
impl<F: Default> Default for FocusedTripleOrPathPattern<F> {
fn default() -> Self {
Self {
focus: F::default(),
@ -72,8 +89,8 @@ mod grammar {
}
}
impl<F> From<FocusedPropertyPathPattern<F>> for FocusedPropertyPathPattern<Vec<F>> {
fn from(input: FocusedPropertyPathPattern<F>) -> Self {
impl<F> From<FocusedTripleOrPathPattern<F>> for FocusedTripleOrPathPattern<Vec<F>> {
fn from(input: FocusedTripleOrPathPattern<F>) -> Self {
Self {
focus: vec![input.focus],
patterns: input.patterns,
@ -81,7 +98,7 @@ mod grammar {
}
}
impl<F, T: From<F>> From<FocusedTriplePattern<F>> for FocusedPropertyPathPattern<T> {
impl<F, T: From<F>> From<FocusedTriplePattern<F>> for FocusedTripleOrPathPattern<T> {
fn from(input: FocusedTriplePattern<F>) -> Self {
Self {
focus: input.focus.into(),

@ -87,7 +87,7 @@ ConstructQuery -> Query =
algebra: build_select(Selection {
option: SelectionOption::Reduced,
variables: None
}, MultiSetPattern::BGP(c.into_iter().map(|p| PropertyPathPattern::from(p)).collect()).into(),
}, MultiSetPattern::BGP(c.into_iter().map(|p| TripleOrPathPattern::from(p)).collect()).into(),
h, o, l, v)
}
}
@ -253,14 +253,14 @@ GroupGraphPatternSub_item -> Vec<PartialGraphPattern> = a:GraphPatternNotTriples
}
//[55]
TriplesBlock -> Vec<PropertyPathPattern> = h:TriplesSameSubjectPath _ t:TriplesBlock_tail? {
TriplesBlock -> Vec<TripleOrPathPattern> = h:TriplesSameSubjectPath _ t:TriplesBlock_tail? {
let mut triples = h;
if let Some(l) = t {
triples.extend_from_slice(&l)
}
triples
}
TriplesBlock_tail -> Vec<PropertyPathPattern> = '.' _ t:TriplesBlock? _ {
TriplesBlock_tail -> Vec<TripleOrPathPattern> = '.' _ t:TriplesBlock? _ {
t.unwrap_or_else(|| Vec::default())
}
@ -433,12 +433,12 @@ ObjectList_item -> FocusedTriplePattern<TermOrVariable> = o:Object _ { o }
Object -> FocusedTriplePattern<TermOrVariable> = GraphNode
//[81]
TriplesSameSubjectPath -> Vec<PropertyPathPattern> =
TriplesSameSubjectPath -> Vec<TripleOrPathPattern> =
s:VarOrTerm _ po:PropertyListPathNotEmpty {
let mut patterns = po.patterns;
for (p, os) in po.focus {
for o in os {
patterns.push(PropertyPathPattern::new(s.clone(), p.clone(), o))
patterns.push(to_triple_or_path_pattern(s.clone(), p.clone(), o))
}
}
patterns
@ -448,20 +448,20 @@ TriplesSameSubjectPath -> Vec<PropertyPathPattern> =
patterns.extend_from_slice(&po.patterns);
for (p, os) in po.focus {
for o in os {
patterns.push(PropertyPathPattern::new(s.focus.clone(), p.clone(), o))
patterns.push(to_triple_or_path_pattern(s.focus.clone(), p.clone(), o))
}
}
patterns
}
//[82]
PropertyListPath -> FocusedPropertyPathPattern<Vec<(PropertyPath,Vec<TermOrVariable>)>> =
PropertyListPath -> FocusedTripleOrPathPattern<Vec<(VariableOrPropertyPath,Vec<TermOrVariable>)>> =
PropertyListPathNotEmpty /
{ FocusedPropertyPathPattern::default() }
{ FocusedTripleOrPathPattern::default() }
//[83]
PropertyListPathNotEmpty -> FocusedPropertyPathPattern<Vec<(PropertyPath,Vec<TermOrVariable>)>> = hp:(VerbPath / VerbSimple) _ ho:ObjectListPath _ t:PropertyListPathNotEmpty_item* {
t.into_iter().fold(FocusedPropertyPathPattern {
PropertyListPathNotEmpty -> FocusedTripleOrPathPattern<Vec<(VariableOrPropertyPath,Vec<TermOrVariable>)>> = hp:(VerbPath / VerbSimple) _ ho:ObjectListPath _ t:PropertyListPathNotEmpty_item* {
t.into_iter().fold(FocusedTripleOrPathPattern {
focus: vec![(hp, ho.focus)],
patterns: ho.patterns
}, |mut a, b| {
@ -470,7 +470,7 @@ PropertyListPathNotEmpty -> FocusedPropertyPathPattern<Vec<(PropertyPath,Vec<Ter
a
})
}
PropertyListPathNotEmpty_item -> FocusedTriplePattern<(PropertyPath,Vec<TermOrVariable>)> = ';' _ p:(VerbPath / VerbSimple) _ o:ObjectList _ { //TODO: make values after ';' optional
PropertyListPathNotEmpty_item -> FocusedTriplePattern<(VariableOrPropertyPath,Vec<TermOrVariable>)> = ';' _ p:(VerbPath / VerbSimple) _ o:ObjectList _ { //TODO: make values after ';' optional
FocusedTriplePattern {
focus: (p, o.focus),
patterns: o.patterns
@ -478,25 +478,27 @@ PropertyListPathNotEmpty_item -> FocusedTriplePattern<(PropertyPath,Vec<TermOrVa
}
//[84]
VerbPath -> PropertyPath = Path
VerbPath -> VariableOrPropertyPath = p:Path {
VariableOrPropertyPath::PropertyPath(p)
}
//[85]
VerbSimple -> PropertyPath = v:Var {
v.into()
VerbSimple -> VariableOrPropertyPath = v:Var {
VariableOrPropertyPath::Variable(v)
}
//[86]
ObjectListPath -> FocusedPropertyPathPattern<Vec<TermOrVariable>> = o:ObjectPath_item **<1,> (',' _) {
o.into_iter().fold(FocusedPropertyPathPattern::<Vec<TermOrVariable>>::default(), |mut a, b| {
ObjectListPath -> FocusedTripleOrPathPattern<Vec<TermOrVariable>> = o:ObjectPath_item **<1,> (',' _) {
o.into_iter().fold(FocusedTripleOrPathPattern::<Vec<TermOrVariable>>::default(), |mut a, b| {
a.focus.push(b.focus);
a.patterns.extend_from_slice(&b.patterns);
a
})
}
ObjectPath_item -> FocusedPropertyPathPattern<TermOrVariable> = o:ObjectPath _ { o }
ObjectPath_item -> FocusedTripleOrPathPattern<TermOrVariable> = o:ObjectPath _ { o }
//[87]
ObjectPath -> FocusedPropertyPathPattern<TermOrVariable> = GraphNodePath
ObjectPath -> FocusedTripleOrPathPattern<TermOrVariable> = GraphNodePath
//[88]
Path -> PropertyPath = PathAlternative
@ -531,7 +533,8 @@ PathEltOrInverse -> PropertyPath =
//[94]
PathPrimary -> PropertyPath =
v:Verb { v.into() } /
'a' { rdf::TYPE.clone().into() } /
v:iri { v.into() } /
'!' _ p:PathNegatedPropertySet { p } /
'(' _ p:Path _ ')' { p }
@ -591,18 +594,18 @@ BlankNodePropertyList -> FocusedTriplePattern<TermOrVariable> = '[' _ po:Propert
}
//[100]
TriplesNodePath -> FocusedPropertyPathPattern<TermOrVariable> = CollectionPath / BlankNodePropertyListPath
TriplesNodePath -> FocusedTripleOrPathPattern<TermOrVariable> = CollectionPath / BlankNodePropertyListPath
//[101]
BlankNodePropertyListPath -> FocusedPropertyPathPattern<TermOrVariable> = '[' _ po:PropertyListPathNotEmpty _ ']' {
let mut patterns: Vec<PropertyPathPattern> = Vec::default();
BlankNodePropertyListPath -> FocusedTripleOrPathPattern<TermOrVariable> = '[' _ po:PropertyListPathNotEmpty _ ']' {
let mut patterns: Vec<TripleOrPathPattern> = Vec::default();
let mut bnode = TermOrVariable::from(BlankNode::default());
for (p, os) in po.focus {
for o in os {
patterns.push(PropertyPathPattern::new(bnode.clone(), p.clone(), o));
patterns.push(to_triple_or_path_pattern(bnode.clone(), p.clone(), o));
}
}
FocusedPropertyPathPattern {
FocusedTripleOrPathPattern {
focus: bnode,
patterns
}
@ -627,22 +630,22 @@ Collection -> FocusedTriplePattern<TermOrVariable> = '(' _ o:Collection_item+ ')
Collection_item -> FocusedTriplePattern<TermOrVariable> = o:GraphNode _ { o }
//[103]
CollectionPath -> FocusedPropertyPathPattern<TermOrVariable> = '(' _ o:CollectionPath_item+ _ ')' {
let mut patterns: Vec<PropertyPathPattern> = Vec::default();
CollectionPath -> FocusedTripleOrPathPattern<TermOrVariable> = '(' _ o:CollectionPath_item+ _ ')' {
let mut patterns: Vec<TripleOrPathPattern> = Vec::default();
let mut current_list_node = TermOrVariable::from(rdf::NIL.clone());
for objWithPatterns in o.into_iter().rev() {
let new_blank_node = TermOrVariable::from(BlankNode::default());
patterns.push(PropertyPathPattern::new(new_blank_node.clone(), rdf::FIRST.clone(), objWithPatterns.focus.clone()));
patterns.push(PropertyPathPattern::new(new_blank_node.clone(), rdf::REST.clone(), current_list_node));
patterns.push(TriplePattern::new(new_blank_node.clone(), rdf::FIRST.clone(), objWithPatterns.focus.clone()).into());
patterns.push(TriplePattern::new(new_blank_node.clone(), rdf::REST.clone(), current_list_node).into());
current_list_node = new_blank_node;
patterns.extend_from_slice(&objWithPatterns.patterns);
}
FocusedPropertyPathPattern {
FocusedTripleOrPathPattern {
focus: current_list_node,
patterns
}
}
CollectionPath_item -> FocusedPropertyPathPattern<TermOrVariable> = p:GraphNodePath _ { p }
CollectionPath_item -> FocusedTripleOrPathPattern<TermOrVariable> = p:GraphNodePath _ { p }
//[104]
GraphNode -> FocusedTriplePattern<TermOrVariable> =
@ -650,8 +653,8 @@ GraphNode -> FocusedTriplePattern<TermOrVariable> =
TriplesNode
//[105]
GraphNodePath -> FocusedPropertyPathPattern<TermOrVariable> =
t:VarOrTerm { FocusedPropertyPathPattern::new(t.into()) } /
GraphNodePath -> FocusedTripleOrPathPattern<TermOrVariable> =
t:VarOrTerm { FocusedTripleOrPathPattern::new(t.into()) } /
TriplesNodePath
//[106]

Loading…
Cancel
Save