Vanilla RDF API

# Vanilla version of Aaron's http://blogspace.com/rdf/rdfapi.txt

import urllib, string

def literalToURI(value):
   return 'data:,' + urllib.quote(value)
 
def URIToLiteral(uri):
   if uri[0:6] != 'data:,': raise 'Only parses URIs like: data:,somedata'
   return urllib.unquote(uri[6:])

class Namespace:
   def __init__(self, prefix): self.prefix = prefix
   def __getattr__(self,name): return self.prefix + name

class Node: 
   nodeList = {}
   def __init__(self, uri=None):
      if uri is not None: self.uri = uri

def node(value=None):
   if value == None: return Node()
   if type(value) is type(u''): value = literalToURI(value)
   if not Node.nodeList.has_key(value): Node.nodeList[value] = Node(value)
   return Node.nodeList[value]

class Triple(Node): 
   def __init__(self, store, s, p, o):
      if not hasattr(s, '__class__'): s = node(s)
      if not hasattr(p, '__class__'): p = node(p)
      if not hasattr(o, '__class__'): o = node(o)
      self.subject, self.predicate, self.object = s, p, o

class Store:
   def __init__(self): self.tripleList = []
  
   def triple(self, s, p, o):
      result = Triple(self, s, p, o)
      self.tripleList.append(result); return result
  
   def query(self, s, p, o):
      if not hasattr(s, '__class__') and s is not None: s = node(s)
      if not hasattr(p, '__class__') and p is not None: p = node(p)
      if not hasattr(o, '__class__') and o is not None: o = node(o)
      results = []
      for t in self.tripleList:
         if ( (s is None or t.subject is s)
           and (p is None or t.predicate is p)
           and (o is None or t.object is o) ): results.append(t)
      return results

Cheers,

-- 
Kindest Regards,
Sean B. Palmer
@prefix : <http://webns.net/roughterms/> .
:Sean :hasHomepage <http://purl.org/net/sbp/> .

Received on Saturday, 20 October 2001 22:41:43 UTC