Esempio n. 1
0
class Plugin (plugin.TransformPlugin):
   """Rewrite to perform imports
   """

   id=__name__
   options = [ ]

   def __init__(self, **kwargs):
      pass

   def transform(self, instance):
      self.ast = NodeFactory()
      self.ast.nsmap.bind("", "http://www.w3.org/2007/rif#")
      self.graph = ConjunctiveGraph()
      self.profiles = set()
      self.bmap = {}

      doc = self.ast.deepcopy(instance)

      did_import = False
      for d in doc.directive.values:
         if d.has_type("Import"):
            print "IMPORT"
            print "   location: ", d.location.the.as_string
            print "   profile: ", d.profile.the.as_string
            try:
               self.graph.parse(d.location.the.as_string, format="n3")
            except Exception, e:
               raise RDFLibParseError(e)
            self.profiles.add(d.profile.the.as_string)
            did_import = True
         
      if not did_import:
         return instance

      print "  imported %d triples" % len(self.graph)

      # assume zero or one payloads
      try:
         topgroup = doc.payload.the
      except:
         topgroup = self.ast.Instance("Group")
         doc.payload = topgroup

      # add more 'sentence' properties to topgroup
      for s, p, o in self.graph:
         for ss in self.rif_terms(s):
            for pp in self.rif_terms(p):
               for oo in self.rif_terms(o):
                  f = self.ast.Instance("Frame")
                  f.object=ss
                  f.slot = self.ast.Sequence( [pp, oo] )
                  topgroup._addpv("sentence", f)

      #for profile in profiles:
      #   ...

      return doc
Esempio n. 2
0
   def transform(self, instance):
      self.ast = NodeFactory()
      self.ast.nsmap.bind("", "http://www.w3.org/2007/rif#")
      self.graph = ConjunctiveGraph()
      self.profiles = set()
      self.bmap = {}

      doc = self.ast.deepcopy(instance)

      did_import = False
      for d in doc.directive.values:
         if d.has_type("Import"):
            print "IMPORT"
            print "   location: ", d.location.the.as_string
            print "   profile: ", d.profile.the.as_string
            try:
               self.graph.parse(d.location.the.as_string, format="n3")
            except Exception, e:
               raise RDFLibParseError(e)
            self.profiles.add(d.profile.the.as_string)
            did_import = True