home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / python-support / python-rdflib / rdflib / sparql / sparqlGraph.py < prev    next >
Encoding:
Python Source  |  2007-04-04  |  3.8 KB  |  115 lines

  1. from rdflib.Graph import Graph
  2.  
  3.  
  4. class SPARQLGraph(Graph):
  5.     """
  6.     A subclass of Graph with a few extra SPARQL bits.
  7.     """
  8.     def __init__(self, graph, graphVariable = None):
  9.         self.graphVariable = graphVariable
  10.         self.graph = graph # TODO
  11.         store = graph.store
  12.         identifier = graph.identifier
  13.         super(SPARQLGraph, self).__init__(store, identifier)
  14.  
  15.     ##############################################################################################################
  16.     # Clustering methods
  17.     def _clusterForward(self,seed,Cluster) :
  18.         """Cluster the triple store: from a seed, transitively get all
  19.         properties and objects in direction of the arcs.
  20.  
  21.         @param seed: RDFLib Resource
  22.  
  23.         @param Cluster: a L{sparqlGraph} instance, that has to be
  24.         expanded with the new arcs
  25.         """
  26.         try :
  27.             # get all predicate and object pairs for the seed.
  28.             # *If not yet in the new cluster, then go with a recursive round with those*
  29.             for (p,o) in self.graph.predicate_objects(seed) :
  30.                 if not (seed,p,o) in Cluster.graph :
  31.                     Cluster.add((seed,p,o))
  32.                     self._clusterForward(p,Cluster)
  33.                     self._clusterForward(o,Cluster)
  34.         except :
  35.             pass
  36.  
  37.  
  38.     def clusterForward(self,seed,Cluster=None) :
  39.         """
  40.         Cluster the triple store: from a seed, transitively get all
  41.         properties and objects in direction of the arcs.
  42.  
  43.         @param seed: RDFLib Resource
  44.  
  45.         @param Cluster: another sparqlGraph instance; if None, a new
  46.         one will be created. The subgraph will be added to this graph.
  47.  
  48.         @returns: The triple store containing the cluster
  49.  
  50.         @rtype: L{sparqlGraph}
  51.         """
  52.         if Cluster == None :
  53.             Cluster = SPARQLGraph()
  54.  
  55.         # This will raise an exception if not kosher...
  56.         check_subject(seed) #print "Wrong type for clustering (probably a literal): %s" % seed
  57.         self._clusterForward(seed,Cluster)
  58.         return Cluster
  59.  
  60.  
  61.     def _clusterBackward(self,seed,Cluster) :
  62.         """Cluster the triple store: from a seed, transitively get all
  63.         properties and objects in backward direction of the arcs.
  64.  
  65.         @param seed: RDFLib Resource
  66.  
  67.         @param Cluster: a L{sparqlGraph} instance, that has to be
  68.         expanded with the new arcs
  69.         """
  70.         try :
  71.             for (s,p) in self.graph.subject_predicates(seed) :
  72.                 if not (s,p,seed) in Cluster.graph :
  73.                     Cluster.add((s,p,seed))
  74.                     self._clusterBackward(s,Cluster)
  75.                     self._clusterBackward(p,Cluster)
  76.         except :
  77.             pass
  78.  
  79.     def clusterBackward(self,seed,Cluster=None) :
  80.         """
  81.         Cluster the triple store: from a seed, transitively get all
  82.         properties and objects 'backward', ie, following the link back
  83.         in the graph.
  84.  
  85.         @param seed: RDFLib Resource
  86.  
  87.         @param Cluster: another sparqlGraph instance; if None, a new
  88.         one will be created. The subgraph will be added to this graph.
  89.  
  90.         @returns: The triple store containing the cluster
  91.  
  92.         @rtype: L{sparqlGraph}
  93.         """
  94.         if Cluster == None :
  95.             Cluster = SPARQLGraph()
  96.  
  97.         # This will raise an exception if not kosher...
  98.         check_object(seed) # print "Wrong type for clustering: %s" % seed
  99.         self._clusterBackward(seed,Cluster)
  100.         return Cluster
  101.  
  102.     def cluster(self,seed) :
  103.         """
  104.         Cluster up and down, by summing up the forward and backward
  105.         clustering
  106.  
  107.         @param seed: RDFLib Resource
  108.  
  109.         @returns: The triple store containing the cluster
  110.  
  111.         @rtype: L{sparqlGraph}
  112.         """
  113.         raise "Am I getting here?"
  114.         return self.clusterBackward(seed) + self.clusterForward(seed)
  115.