Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove forked code from processor, monkey patch instead #288

Draft
wants to merge 16 commits into
base: master
Choose a base branch
from

Conversation

white-gecko
Copy link
Member

@white-gecko white-gecko commented Aug 19, 2021

In the module quit.tools most of the code is forked from the rdflib. This code was updated in the rdflib and in the quit store code and is in a diverged state now. So we should remove that code and use some technique like monkey patching instead.

For processor.py the monkey patch was straightforward.

To reconstruct a diff and find out what to patch I went to the initial commit of the files algebra.py (776aa20), evaluate.py (776aa20), and update.py (201ad1f) and compared it with the following commands to the history of the respective files in the rdflib repo.

# the difference is in the grep "^>" resp. grep "^<"
for i in $( git log --oneline rdflib/plugins/sparql/update.py | awk '{print $1}' ); do git show --raw $i | grep rdflib/plugins/sparql/update.py | awk '{print $4}' | xargs git show --raw | diff -w …/QuitStore/quit/update.py - | grep "^<" | wc -l ; echo $i ; done
for i in $( git log --oneline rdflib/plugins/sparql/update.py | awk '{print $1}' ); do git show --raw $i | grep rdflib/plugins/sparql/update.py | awk '{print $4}' | xargs git show --raw | diff -w …/QuitStore/quit/update.py - | grep "^>" | wc -l ; echo $i ; done

I came to the conclusion, that the minimal diff is at:

TODO:

  • compare the content
  • reconstruct the changes on our side
  • also include changes in recent commit
  • monkey patch the current version of the respective modules

Files completed:

  • processor.py
  • algebra.py
  • evaluate.py
  • update.py

Fix #160 .

@white-gecko
Copy link
Member Author

The effective diff of the update.py appears to be:

*** update-rdflib-6c026d09_changed.py	2022-02-16 15:42:35.668776881 +0100
--- update-quit-201ad1fe_changed.py	2022-02-16 15:46:36.622912921 +0100
*************** def evalInsertData(ctx, u):
*** 77,107 ****
--- 77,128 ----
      """
      http://www.w3.org/TR/sparql11-update/#insertData
      """
+     insertedtriples = ()
+ 
      # add triples
      g = ctx.graph
+     for triple in u.triples:
+         if g.triples(triple):
+             insertedtriples += ({'insert': (triple, 'default')}),
      g += u.triples
  
      # add quads
      # u.quads is a dict of graphURI=>[triples]
      for g in u.quads:
          cg = ctx.dataset.get_context(g)
+         for triple in u.quads[g]:
+             if cg.triples(triple):
+                 insertedtriples += ({'insert': (triple, cg.identifier)}),
          cg += u.quads[g]
  
+     return insertedtriples
+ 
  
  def evalDeleteData(ctx, u):
      """
      http://www.w3.org/TR/sparql11-update/#deleteData
      """
+     deletedtriples = ()
+ 
      # remove triples
      g = ctx.graph
+     deletedriples = ()
+     for triple in u.triples:
+         if g.triples(triple):
+             deletedriples += ({'delete': (triple, 'default')}),
      g -= u.triples
  
      # remove quads
      # u.quads is a dict of graphURI=>[triples]
      for g in u.quads:
          cg = ctx.dataset.get_context(g)
+         for triple in u.quads[g]:
+             if cg.triples(triple):
+                 deletedriples += ({'delete': (triple, cg.identifier)}),
          cg -= u.quads[g]
  
+     return deletedriples
+ 
  
  def evalDeleteWhere(ctx, u):
      """
*************** def evalDeleteWhere(ctx, u):
*** 114,126 ****
          c = ctx.pushGraph(cg)
          res = _join(res, list(evalBGP(c, u.quads[g])))
  
      for c in res:
          g = ctx.graph
!         g -= _fillTemplate(u.triples, c)
  
          for g in u.quads:
              cg = ctx.dataset.get_context(c.get(g))
!             cg -= _fillTemplate(u.quads[g], c)
  
  
  def evalModify(ctx, u):
--- 135,159 ----
          c = ctx.pushGraph(cg)
          res = _join(res, list(evalBGP(c, u.quads[g])))
  
+     deletedtriples = ()
+ 
      for c in res:
          g = ctx.graph
!         filled = _fillTemplate(u.triples, c)
!         for item in filled:
!             triple = (item[0], item[1], item[2])
!             deletedtriples += ({'delete': (triple, 'default')},)
!         g -= filled
  
          for g in u.quads:
              cg = ctx.dataset.get_context(c.get(g))
!             filledq = _fillTemplate(u.triples, c)
!             for item in filledq:
!                 triple = (item[0], item[1], item[2])
!                 deletedtriples += ({'delete': (triple, cg.identifier)},)
!             cg -= filledq
! 
!     return deletedtriples
  
  
  def evalModify(ctx, u):
*************** def evalModify(ctx, u):
*** 159,164 ****
--- 192,198 ----
          ctx = ctx.pushGraph(g)
  
      res = evalPart(ctx, u.where)
+     modifiedtriples = ()
  
      if u.using:
          if otherDefault:
*************** def evalModify(ctx, u):
*** 170,187 ****
      for c in res:
          dg = ctx.graph
          if u.delete:
!             dg -= _fillTemplate(u.delete.triples, c)
  
              for g, q in u.delete.quads.items():
                  cg = ctx.dataset.get_context(c.get(g))
!                 cg -= _fillTemplate(q, c)
  
          if u.insert:
!             dg += _fillTemplate(u.insert.triples, c)
  
              for g, q in u.insert.quads.items():
                  cg = ctx.dataset.get_context(c.get(g))
!                 cg += _fillTemplate(q, c)
  
  
  def evalAdd(ctx, u):
--- 204,237 ----
      for c in res:
          dg = ctx.graph
          if u.delete:
!             filled = _fillTemplate(u.delete.triples, c)
!             for item in filled:
!                 triple = (item[0], item[1], item[2])
!                 modifiedtriples += ({'delete': (triple, dg.identifier)}),
!             dg -= filled
  
              for g, q in u.delete.quads.items():
                  cg = ctx.dataset.get_context(c.get(g))
!                 filledq = _fillTemplate(q, c)
!                 for itemq in filledq:
!                     triple = (itemq[0], itemq[1], itemq[2])
!                     modifiedtriples += ({'delete': (triple, cg.identifier)}),
!                 cg -= filledq
  
          if u.insert:
!             filled = _fillTemplate(u.insert.triples, c)
!             for item in filled:
!                 modifiedtriples += ({'insert': (item, dg.identifier)}),
!             dg += filled
  
              for g, q in u.insert.quads.items():
                  cg = ctx.dataset.get_context(c.get(g))
!                 filledq = _fillTemplate(q, c)
!                 for itemq in filledq:
!                     modifiedtriples += ({'insert': (triple, cg.identifier)}),
!                 cg += filledq
! 
!     return modifiedtriples
  
  
  def evalAdd(ctx, u):
*************** def evalCopy(ctx, u):
*** 252,258 ****
      dstg += srcg
  
  
! def evalUpdate(graph, update, initBindings=None):
      """
  
      http://www.w3.org/TR/sparql11-update/#updateLanguage
--- 302,308 ----
      dstg += srcg
  
  
! def evalUpdate(graph, update, initBindings=None, actionLog=False):
      """
  
      http://www.w3.org/TR/sparql11-update/#updateLanguage
*************** def evalUpdate(graph, update, initBindin
*** 271,276 ****
--- 321,327 ----
      This will return None on success and raise Exceptions on error
  
      """
+     changes = ()
  
      for u in update:
  
*************** def evalUpdate(graph, update, initBindin
*** 300,314 ****
              elif u.name == 'Copy':
                  evalCopy(ctx, u)
              elif u.name == 'InsertData':
!                 evalInsertData(ctx, u)
              elif u.name == 'DeleteData':
!                 evalDeleteData(ctx, u)
              elif u.name == 'DeleteWhere':
!                 evalDeleteWhere(ctx, u)
              elif u.name == 'Modify':
!                 evalModify(ctx, u)
              else:
                  raise Exception('Unknown update operation: %s' % (u,))
          except:
              if not u.silent:
                  raise
--- 351,366 ----
              elif u.name == 'Copy':
                  evalCopy(ctx, u)
              elif u.name == 'InsertData':
!                 changes += evalInsertData(ctx, u)
              elif u.name == 'DeleteData':
!                 changes += evalDeleteData(ctx, u)
              elif u.name == 'DeleteWhere':
!                 changes += evalDeleteWhere(ctx, u)
              elif u.name == 'Modify':
!                 changes += evalModify(ctx, u)
              else:
                  raise Exception('Unknown update operation: %s' % (u,))
          except:
              if not u.silent:
                  raise
+     return changes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Usage of translateQuery and translateUpdate
1 participant