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

@ -473,8 +473,7 @@ fn copy_graph(
Variable::new("o"), Variable::new("o"),
) )
.into()]); .into()]);
GraphUpdateOperation::OpDeleteInsert { GraphUpdateOperation::DeleteInsert {
with: None,
delete: None, delete: None,
insert: Some(vec![QuadPattern::new( insert: Some(vec![QuadPattern::new(
Variable::new("s"), Variable::new("s"),
@ -953,23 +952,23 @@ parser! {
//[31] //[31]
rule Load() -> Vec<GraphUpdateOperation> = i("LOAD") _ silent:Update1_silent() _ from:iri() _ to:Load_to()? { 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 } rule Load_to() -> NamedNode = i("INTO") _ g: GraphRef() { g }
//[32] //[32]
rule Clear() -> Vec<GraphUpdateOperation> = i("CLEAR") _ silent:Update1_silent() _ graph:GraphRefAll() { rule Clear() -> Vec<GraphUpdateOperation> = i("CLEAR") _ silent:Update1_silent() _ graph:GraphRefAll() {
vec![GraphUpdateOperation::OpClear { silent, graph }] vec![GraphUpdateOperation::Clear { silent, graph }]
} }
//[33] //[33]
rule Drop() -> Vec<GraphUpdateOperation> = i("DROP") _ silent:Update1_silent() _ graph:GraphRefAll() { rule Drop() -> Vec<GraphUpdateOperation> = i("DROP") _ silent:Update1_silent() _ graph:GraphRefAll() {
vec![GraphUpdateOperation::OpDrop { silent, graph }] vec![GraphUpdateOperation::Drop { silent, graph }]
} }
//[34] //[34]
rule Create() -> Vec<GraphUpdateOperation> = i("CREATE") _ silent:Update1_silent() _ graph:GraphRef() { rule Create() -> Vec<GraphUpdateOperation> = i("CREATE") _ silent:Update1_silent() _ graph:GraphRef() {
vec![GraphUpdateOperation::OpCreate { silent, graph }] vec![GraphUpdateOperation::Create { silent, graph }]
} }
//[35] //[35]
@ -983,24 +982,24 @@ parser! {
rule Move() -> Vec<GraphUpdateOperation> = i("MOVE") _ silent:Update1_silent() _ from:GraphOrDefault() _ i("TO") _ to:GraphOrDefault() { 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 // 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()]); 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] //[37]
rule Copy() -> Vec<GraphUpdateOperation> = i("COPY") _ silent:Update1_silent() _ from:GraphOrDefault() _ i("TO") _ to:GraphOrDefault() { 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 // 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()]); 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] //[38]
rule InsertData() -> Vec<GraphUpdateOperation> = i("INSERT") _ i("DATA") _ data:QuadData() { rule InsertData() -> Vec<GraphUpdateOperation> = i("INSERT") _ i("DATA") _ data:QuadData() {
vec![GraphUpdateOperation::OpInsertData { data }] vec![GraphUpdateOperation::InsertData { data }]
} }
//[39] //[39]
rule DeleteData() -> Vec<GraphUpdateOperation> = i("DELETE") _ i("DATA") _ data:QuadData() { rule DeleteData() -> Vec<GraphUpdateOperation> = i("DELETE") _ i("DATA") _ data:QuadData() {
vec![GraphUpdateOperation::OpDeleteData { data }] vec![GraphUpdateOperation::DeleteData { data }]
} }
//[40] //[40]
@ -1013,8 +1012,7 @@ parser! {
bgp bgp
} }
}).fold(GraphPattern::BGP(Vec::new()), new_join); }).fold(GraphPattern::BGP(Vec::new()), new_join);
vec![GraphUpdateOperation::OpDeleteInsert { vec![GraphUpdateOperation::DeleteInsert {
with: None,
delete: Some(d), delete: Some(d),
insert: None, insert: None,
using: Rc::new(DatasetSpec::default()), using: Rc::new(DatasetSpec::default()),
@ -1024,9 +1022,25 @@ parser! {
//[41] //[41]
rule Modify() -> Vec<GraphUpdateOperation> = with:Modify_with() _ c:Modify_clauses() _ using:(UsingClause() ** (_)) _ i("WHERE") _ algebra:GroupGraphPattern() { rule Modify() -> Vec<GraphUpdateOperation> = with:Modify_with() _ c:Modify_clauses() _ using:(UsingClause() ** (_)) _ i("WHERE") _ algebra:GroupGraphPattern() {
let (delete, insert) = c; let (mut delete, mut insert) = c;
vec![GraphUpdateOperation::OpDeleteInsert { let mut algebra = algebra;
with,
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, delete,
insert, insert,
using: Rc::new(using.into_iter().fold(DatasetSpec::default(), |mut a, b| a + b)), using: Rc::new(using.into_iter().fold(DatasetSpec::default(), |mut a, b| a + b)),

Loading…
Cancel
Save