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

renaming a node in a graph or pointed graph #325

Open
bblfish opened this issue Jul 28, 2017 · 1 comment
Open

renaming a node in a graph or pointed graph #325

bblfish opened this issue Jul 28, 2017 · 1 comment

Comments

@bblfish
Copy link
Member

bblfish commented Jul 28, 2017

It does not seem easy to rename a node in a graph. This seems to be an operation that should be on a PointedGraph actually (or a PointedGraphW)

class PGwrapper[Rdf<:RDF](val pg: PointedGraph[Rdf]) extends AnyVal {
                 def rename(to: Rdf#Node): PointedGraph[Rdf] = ???
}

so for example say I create a PointedGraph from a PublicKey using the Cert PGBinder from the examples.

val keyPg = pub.toPG

this returns a PointedGraph, but with an anonymous node, where we would like a named node. So we'de like to rename that with

keyPg.rename(URI("#key"))

There are many other use cases where one may want to rename a node, eg: when one discoveres that people in one's foaf-profile no longer have a profile page, or if people who did not start having one.

@bblfish
Copy link
Member Author

bblfish commented Jul 28, 2017

This works but is certainly not optimally efficient, as it has to iterate
through the whole graph.

implicit class PGwrapper(val pg: PointedGraph[Rdf]) extends AnyVal {
  def rename(to: Rdf#Node)(implicit ops: RDFOps[Rdf]): PointedGraph[Rdf] = {
    val oldNode = pg.pointer
    PointedGraph[Rdf](
      to,
      ops.makeGraph(pg.graph.triples.map{ triple =>
        ops.fromTriple(triple) match {
          case (oldNode,rel,obj) => ops.makeTriple(to,rel,obj)
          case (subj,rel,oldNode) => ops.makeTriple(subj,rel,to)
          case _ => triple
        }
      })
    )
  }
}

It could probably be optimised with RDFOps.find

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

No branches or pull requests

1 participant