Simplifies SPARQL update operations representation

pull/51/head
Tpt 4 years ago
parent 7f06b938a5
commit b762991917
  1. 33
      lib/src/sparql/algebra.rs
  2. 44
      lib/src/sparql/parser.rs

@ -1462,58 +1462,53 @@ impl fmt::Display for QueryVariants {
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub enum GraphUpdateOperation {
/// [insert data](https://www.w3.org/TR/sparql11-update/#def_insertdataoperation)
OpInsertData { data: Vec<QuadPattern> },
InsertData { data: Vec<QuadPattern> },
/// [delete data](https://www.w3.org/TR/sparql11-update/#def_deletedataoperation)
OpDeleteData { data: Vec<QuadPattern> },
DeleteData { data: Vec<QuadPattern> },
/// [delete insert](https://www.w3.org/TR/sparql11-update/#def_deleteinsertoperation)
OpDeleteInsert {
with: Option<NamedNode>,
DeleteInsert {
delete: Option<Vec<QuadPattern>>,
insert: Option<Vec<QuadPattern>>,
using: Rc<DatasetSpec>,
algebra: Rc<GraphPattern>,
},
/// [load](https://www.w3.org/TR/sparql11-update/#def_loadoperation)
OpLoad {
Load {
silent: bool,
from: NamedNode,
to: Option<NamedNode>,
},
/// [clear](https://www.w3.org/TR/sparql11-update/#def_clearoperation)
OpClear { silent: bool, graph: GraphTarget },
Clear { silent: bool, graph: GraphTarget },
/// [create](https://www.w3.org/TR/sparql11-update/#def_createoperation)
OpCreate { silent: bool, graph: NamedNode },
Create { silent: bool, graph: NamedNode },
/// [drop](https://www.w3.org/TR/sparql11-update/#def_dropoperation)
OpDrop { silent: bool, graph: GraphTarget },
Drop { silent: bool, graph: GraphTarget },
}
impl fmt::Display for GraphUpdateOperation {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
GraphUpdateOperation::OpInsertData { data } => {
GraphUpdateOperation::InsertData { data } => {
writeln!(f, "INSERT DATA {{")?;
for quad in data {
writeln!(f, "\t{}", quad)?;
}
write!(f, "}}")
}
GraphUpdateOperation::OpDeleteData { data } => {
GraphUpdateOperation::DeleteData { data } => {
writeln!(f, "DELETE DATA {{")?;
for quad in data {
writeln!(f, "\t{}", quad)?;
}
write!(f, "}}")
}
GraphUpdateOperation::OpDeleteInsert {
with,
GraphUpdateOperation::DeleteInsert {
delete,
insert,
using,
algebra,
} => {
if let Some(with) = with {
writeln!(f, "WITH {}", with)?;
}
if let Some(delete) = delete {
writeln!(f, "DELETE {{")?;
for quad in delete {
@ -1543,7 +1538,7 @@ impl fmt::Display for GraphUpdateOperation {
}
)
}
GraphUpdateOperation::OpLoad { silent, from, to } => {
GraphUpdateOperation::Load { silent, from, to } => {
write!(f, "LOAD ")?;
if *silent {
write!(f, "SILENT ")?;
@ -1554,21 +1549,21 @@ impl fmt::Display for GraphUpdateOperation {
}
Ok(())
}
GraphUpdateOperation::OpClear { silent, graph } => {
GraphUpdateOperation::Clear { silent, graph } => {
write!(f, "CLEAR ")?;
if *silent {
write!(f, "SILENT ")?;
}
write!(f, "{}", graph)
}
GraphUpdateOperation::OpCreate { silent, graph } => {
GraphUpdateOperation::Create { silent, graph } => {
write!(f, "CREATE ")?;
if *silent {
write!(f, "SILENT ")?;
}
write!(f, "GRAPH {}", graph)
}
GraphUpdateOperation::OpDrop { silent, graph } => {
GraphUpdateOperation::Drop { silent, graph } => {
write!(f, "DROP ")?;
if *silent {
write!(f, "SILENT ")?;

@ -473,8 +473,7 @@ fn copy_graph(
Variable::new("o"),
)
.into()]);
GraphUpdateOperation::OpDeleteInsert {
with: None,
GraphUpdateOperation::DeleteInsert {
delete: None,
insert: Some(vec![QuadPattern::new(
Variable::new("s"),
@ -953,23 +952,23 @@ parser! {
//[31]
rule Load() -> Vec<GraphUpdateOperation> = i("LOAD") _ silent:Update1_silent() _ from:iri() _ to:Load_to()? {
vec![GraphUpdateOperation::OpLoad { silent, from, to }]
vec![GraphUpdateOperation::Load { silent, from, to }]
}
rule Load_to() -> NamedNode = i("INTO") _ g: GraphRef() { g }
//[32]
rule Clear() -> Vec<GraphUpdateOperation> = i("CLEAR") _ silent:Update1_silent() _ graph:GraphRefAll() {
vec![GraphUpdateOperation::OpClear { silent, graph }]
vec![GraphUpdateOperation::Clear { silent, graph }]
}
//[33]
rule Drop() -> Vec<GraphUpdateOperation> = i("DROP") _ silent:Update1_silent() _ graph:GraphRefAll() {
vec![GraphUpdateOperation::OpDrop { silent, graph }]
vec![GraphUpdateOperation::Drop { silent, graph }]
}
//[34]
rule Create() -> Vec<GraphUpdateOperation> = i("CREATE") _ silent:Update1_silent() _ graph:GraphRef() {
vec![GraphUpdateOperation::OpCreate { silent, graph }]
vec![GraphUpdateOperation::Create { silent, graph }]
}
//[35]
@ -983,24 +982,24 @@ parser! {
rule Move() -> Vec<GraphUpdateOperation> = i("MOVE") _ silent:Update1_silent() _ from:GraphOrDefault() _ i("TO") _ to:GraphOrDefault() {
// Rewriting defined by https://www.w3.org/TR/sparql11-update/#move
let bgp = GraphPattern::BGP(vec![TriplePattern::new(Variable::new("s"), Variable::new("p"), Variable::new("o")).into()]);
vec![GraphUpdateOperation::OpDrop { silent, graph: to.clone().into() }, copy_graph(from.clone(), to), GraphUpdateOperation::OpDrop { silent, graph: from.into() }]
vec![GraphUpdateOperation::Drop { silent, graph: to.clone().into() }, copy_graph(from.clone(), to), GraphUpdateOperation::Drop { silent, graph: from.into() }]
}
//[37]
rule Copy() -> Vec<GraphUpdateOperation> = i("COPY") _ silent:Update1_silent() _ from:GraphOrDefault() _ i("TO") _ to:GraphOrDefault() {
// Rewriting defined by https://www.w3.org/TR/sparql11-update/#copy
let bgp = GraphPattern::BGP(vec![TriplePattern::new(Variable::new("s"), Variable::new("p"), Variable::new("o")).into()]);
vec![GraphUpdateOperation::OpDrop { silent, graph: to.clone().into() }, copy_graph(from, to)]
vec![GraphUpdateOperation::Drop { silent, graph: to.clone().into() }, copy_graph(from, to)]
}
//[38]
rule InsertData() -> Vec<GraphUpdateOperation> = i("INSERT") _ i("DATA") _ data:QuadData() {
vec![GraphUpdateOperation::OpInsertData { data }]
vec![GraphUpdateOperation::InsertData { data }]
}
//[39]
rule DeleteData() -> Vec<GraphUpdateOperation> = i("DELETE") _ i("DATA") _ data:QuadData() {
vec![GraphUpdateOperation::OpDeleteData { data }]
vec![GraphUpdateOperation::DeleteData { data }]
}
//[40]
@ -1013,8 +1012,7 @@ parser! {
bgp
}
}).fold(GraphPattern::BGP(Vec::new()), new_join);
vec![GraphUpdateOperation::OpDeleteInsert {
with: None,
vec![GraphUpdateOperation::DeleteInsert {
delete: Some(d),
insert: None,
using: Rc::new(DatasetSpec::default()),
@ -1024,9 +1022,25 @@ parser! {
//[41]
rule Modify() -> Vec<GraphUpdateOperation> = with:Modify_with() _ c:Modify_clauses() _ using:(UsingClause() ** (_)) _ i("WHERE") _ algebra:GroupGraphPattern() {
let (delete, insert) = c;
vec![GraphUpdateOperation::OpDeleteInsert {
with,
let (mut delete, mut insert) = c;
let mut algebra = algebra;
if let Some(with) = with {
// We inject WITH everywhere
delete = delete.map(|quads| quads.into_iter().map(|q| if q.graph_name.is_none() {
QuadPattern::new(q.subject, q.predicate, q.object, Some(with.clone().into()))
} else {
q
}).collect());
insert = insert.map(|quads| quads.into_iter().map(|q| if q.graph_name.is_none() {
QuadPattern::new(q.subject, q.predicate, q.object, Some(with.clone().into()))
} else {
q
}).collect());
algebra = GraphPattern::Graph(with.into(), Box::new(algebra));
}
vec![GraphUpdateOperation::DeleteInsert {
delete,
insert,
using: Rc::new(using.into_iter().fold(DatasetSpec::default(), |mut a, b| a + b)),

Loading…
Cancel
Save