def run(): passed = 0 u1 = '1' u2 = unicode('2') u3 = '12' a = objects.resource(u1+u2) b = objects.resource(u3) if a==b: passed = passed + 1 if id(a)==id(b): passed = passed + 1 a = objects.literal(u1+u2) b = objects.literal(u3) if a==b: passed = passed + 1 # TODO: we may want to intern the keys for resource and literal # dictionaries. As I think there is a performance gain. But have # also read that intern-ed string are immortal and never get # garbage collected -- maybe this will change in a future version # of python. return (passed==3, '')
def disconnect_from(self, uri): for store in [store for store in self.neighbours.stores if store.uri==uri]: self.neighbours.remove_store(store) self.remove(resource(uri), CONNECTED, None) self.add(resource(uri), CONNECTED, NO) # Do we want to remember our neighbour? if not self.local.exists(resource(uri), TYPE, NEIGHBOUR): self.add(resource(uri), TYPE, NEIGHBOUR)
def add_statement(self, subject, predicate, object, literal_object=None, anonymous_subject=None, anonymous_object=None): s = resource(subject, anonymous_subject) p = resource(predicate) if literal_object: o = literal(object) else: o = resource(object, anonymous_object) self.add(s, p, o)
def connect_to(self, location, uri=None): uri = uri or location storeIO = TripleStoreIO() storeIO.load(location, uri, 0) self.neighbours.add_store(storeIO) self.remove(resource(location), TYPE, NEIGHBOUR) self.add(resource(location), TYPE, NEIGHBOUR) self.remove(resource(location), CONNECTED, None) self.add(resource(location), CONNECTED, YES)
def run(): store = TestStore() store.add(resource("john"), resource("age"), literal("37")) store.add(resource("paul"), resource("age"), literal("35")) store.add(resource("paul"), resource("foo"), literal("bar")) store.add(resource("peter"), resource("age"), literal("35")) import sys store.output(sys.stdout) return (2, '')
def __init__(self, host, username, password): self.store = TripleStore() M = poplib.POP3(host) M.user(username) M.pass_(password) numMessages = len(M.list()[1]) self.messages = {} for i in range(numMessages): uidl = string.split(M.uidl(i+1))[2] msg = M.retr(i+1)[1] lineFile = LineFile(msg) message = rfc822.Message(lineFile) bodyStartLine = lineFile.count headers = message.dict body = string.join(msg[bodyStartLine:], "\n") subject = resource(POP_URI + uidl) for header in headers.keys(): predicate = resource(RFC_822_URI + header) object = literal(headers[header]) self.store.add(subject, predicate, object) self.store.add(subject, BODY, literal(body)) self.store.add(subject, TYPE, MESSAGE) M.quit()
import sys sys.path.extend(("../../redfoot-core", "../../redfoot-components")) from redfoot.rdf.store.autosave import AutoSave from redfoot.rdf.store.storeio import LoadSave from redfoot.rdf.store.triple import TripleStore class AutoSaveStoreIO(AutoSave, LoadSave, TripleStore, object): def __init__(self): super(AutoSaveStoreIO, self).__init__() from redfoot.rdf.objects import resource, literal from redfoot.rdf.const import LABEL, TYPE store = AutoSaveStoreIO() store.add(resource("http://eikeon.com/"), LABEL, literal("Daniel 'eikeon' Krech")) def print_triple(s, p, o): print s, p, o store.visit(print_triple, (resource("http://eikeon.com/"), None, None)) store.save('test.rdf')
def run(): passed = 0 store = QueryStore() store.add(resource("john"), resource("age"), literal("37")) store.add(resource("paul"), resource("age"), literal("35")) store.add(resource("peter"), resource("age"), literal("35")) b = ItemBuilder() store.visit(triple2statement(b.accept), (None, resource("age"), None)) if b.item.subject == resource("john"): print "passed 1" passed = passed + 1 else: print "failed 1" b = ListBuilder() l = [] store.visit(triple2statement(b.accept), (None, resource("age"), None)) for st in b.list: l.append((str(st.subject), str(st.predicate), str(st.object))) correct = [('peter', 'age', '35'), ('paul', 'age', '35'), ('john', 'age', '37')] if compare_lists(l, correct): print "passed 2" passed = passed + 1 else: print "failed 2. got", l b = ListBuilder() l = [] store.visit(triple2statement(b.accept), (None, resource("age"), literal("35"))) for st in b.list: l.append((str(st.subject), str(st.predicate), str(st.object))) correct = [('peter', 'age', '35'), ('paul', 'age', '35')] if compare_lists(l, correct): print "passed 3" passed = passed + 1 else: print "failed 3. got", l class TestResult: def __init__(self): self.result = [] def add_list(self, *args): self.result.append(map(str, args)) def add_list2(self, *args): a = map(lambda x: "-" + str(x), args) self.result.append(a) t = TestResult() store.visit(t.add_list, (None, resource("age"), None)) correct = [['peter', 'age', '35'], ['paul', 'age', '35'], ['john', 'age', '37']] if compare_lists(t.result, correct): print "passed 4" passed = passed + 1 else: print "failed 4" t = TestResult() store.visit(so(t.add_list), (None, resource("age"), None)) correct = [['peter', '35'], ['paul', '35'], ['john', '37']] if compare_lists(t.result, correct): print "passed 5" passed = passed + 1 else: print "failed 5" t = TestResult() store.visit(both(t.add_list, t.add_list2), (None, resource("age"), None)) correct = [['peter', 'age', '35'], ['-peter', '-age', '-35'], ['paul', 'age', '35'], ['-paul', '-age', '-35'], ['john', 'age', '37'], ['-john', '-age', '-37']] if compare_lists(t.result, correct): print "passed 6" passed = passed + 1 else: print "failed 6" t = TestResult() store.visit(first(t.add_list), (None, resource("age"), None)) correct = [['peter', 'age', '35']] if compare_lists(t.result, correct): print "passed 7" passed = passed + 1 else: print "failed 7" if store.exists(resource("john"), resource("age"), literal("37")): print "passed 8" passed = passed + 1 else: print "failed 8" if not store.exists(resource("john"), resource("age"), literal("36")): print "passed 9" passed = passed + 1 else: print "failed 9" def filter1(s, p, o): return s == resource("john") t = TestResult() store.visit(filter(t.add_list, filter1), (None, None, None)) correct = [['john', 'age', '37']] if compare_lists(t.result, correct): print "passed 10" passed = passed + 1 else: print "failed 10" if passed == 10: return (1, "ALL TESTED PASSED") elif passed > 1: return (0, "ONLY %s TESTS PASSED" % passed) elif passed == 1: return (0, "ONLY 1 TEST PASSED") else: return (0, "NO TESTS PASSED")
def filter1(s, p, o): return s == resource("john")
import poplib, rfc822, string from redfoot.rdf.store.triple import TripleStore from redfoot.rdf.objects import resource, literal from redfoot.rdf.const import TYPE POP_URI = "http://redfoot.sourceforge.net/2001/08/POP/" RFC_822_URI = "http://redfoot.sourceforge.net/2001/08/RFC822/" MESSAGE = resource(POP_URI+"Message") SUBJECT = resource(RFC_822_URI+"subject") FROM = resource(RFC_822_URI+"from") DATE = resource(RFC_822_URI+"date") BODY = resource(POP_URI+"BODY") class POP_Connector: def __init__(self, host, username, password): self.store = TripleStore() M = poplib.POP3(host) M.user(username) M.pass_(password) numMessages = len(M.list()[1]) self.messages = {} for i in range(numMessages): uidl = string.split(M.uidl(i+1))[2] msg = M.retr(i+1)[1] lineFile = LineFile(msg) message = rfc822.Message(lineFile) bodyStartLine = lineFile.count
def run(): passed = 0 store = triple.TripleStore() store.add(resource("john"), resource("age"), literal("37")) store.add(resource("paul"), resource("age"), literal("35")) store.add(resource("peter"), resource("age"), literal("35")) b = ItemBuilder() store.visit(triple2statement(b.accept), (None, resource("age"), literal("37"))) if b.item.subject == resource("john"): print "passed 1" passed = passed + 1 else: print "failed 1" b = ListBuilder() store.visit(o(b.accept), (None, None, None)) def my_comparator(a, b): if a.value < b.value: return -1 elif a.value == b.value: return 0 else: return 1 b.sort(my_comparator) if b.list == [literal("35"), literal("35"), literal("37")]: print "passed 2" passed = passed + 1 else: print "failed 2" def my_filter(a): if int(a.value) > 36: return 1 else: return 0 b = ListBuilder() store.visit(o(b.accept), (None, None, None)) b.filter(my_filter) if b.list == [literal("37")]: print "passed 3" passed = passed + 1 else: print "failed 3: get", b.list b = SetBuilder() store.visit(o(b.accept), (None, None, None)) b.sort(my_comparator) if b.set == [literal("35"), literal("37")]: print "passed 4" passed = passed + 1 else: print "failed 4" b = SetBuilder() store.visit(o(b.accept), (None, None, None)) b.filter(my_filter) if b.set == [literal("37")]: print "passed 5" passed = passed + 1 else: print "failed 5: get", b.list if passed == 5: return (1, "ALL TESTED PASSED") elif passed > 1: return (0, "ONLY %s TESTS PASSED" % passed) elif passed == 1: return (0, "ONLY 1 TEST PASSED") else: return (0, "NO TESTS PASSED")
def run(): rednode = RedNode() rednode.local.add(resource("john"), resource("age"), literal("37")) rednode.local.add(resource("paul"), resource("age"), literal("35")) rednode.local.add(resource("peter"), resource("age"), literal("35")) rednode.local.add(resource("john"), TYPE, resource("person")) rednode.local.add(resource("peter"), TYPE, resource("fish")) rednode.local.add(resource("john"), LABEL, literal("John")) rednode.local.add(resource("paul"), LABEL, literal("Paul")) rednode.local.add(resource("peter"), LABEL, literal("Peter")) rednode.local.add(resource("john"), resource("knows"), resource("paul")) rednode.local.add(resource("paul"), resource("knows"), resource("peter")) rednode.local.add(resource("peter"), resource("knows"), resource("mark")) rednode.local.add(resource("design_patterns"), TYPE, resource("book")) rednode.local.add(resource("person"), TYPE, CLASS) rednode.local.add(resource("fish"), TYPE, CLASS) rednode.local.add(resource("book"), TYPE, CLASS) rednode.local.add(resource("animate"), TYPE, CLASS) rednode.local.add(resource("person"), SUBCLASSOF, resource("animate")) rednode.local.add(resource("person"), SUBCLASSOF, resource("some_superclass")) rednode.local.add(resource("fish"), SUBCLASSOF, resource("animate")) rednode.local.add(resource("age"), DOMAIN, resource("person")) rednode.local.add(resource("knows"), DOMAIN, resource("person")) rednode.local.add(resource("age"), RANGE, LITERAL) rednode.local.add(resource("knows"), RANGE, resource("animate")) rednode.local.add(resource("age"), TYPE, PROPERTY) rednode.local.add(resource("knows"), TYPE, PROPERTY) passed = 0 t = TestResult() rednode.visit_resources_by_type(t.add_class, t.add_instance) correct = {'http://www.w3.org/2000/01/rdf-schema#Class': {'http://www.w3.org/2000/01/rdf-schema#Class': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Property': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Alt': 1, 'http://www.w3.org/2000/01/rdf-schema#Resource': 1, 'http://www.w3.org/2000/01/rdf-schema#ConstraintProperty': 1, 'animate': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq': 1, 'http://www.w3.org/2000/01/rdf-schema#Literal': 1, 'http://redfoot.sourceforge.net/2000/10/06/builtin#UIType': 1, 'http://www.w3.org/2000/01/rdf-schema#ConstraintResource': 1, 'person': 1, 'http://www.w3.org/2000/01/rdf-schema#ContainerMembershipProperty': 1, 'fish': 1, 'book': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement': 1, 'http://www.w3.org/2000/01/rdf-schema#Container': 1, 'http://redfoot.sourceforge.net/2000/10/06/builtin#YesNo': 1}, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Property': {'http://www.w3.org/2000/01/rdf-schema#label': 1, 'http://redfoot.sourceforge.net/2000/10/06/builtin#uiType': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#value': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate': 1, 'http://www.w3.org/2000/01/rdf-schema#subClassOf': 1, 'http://www.w3.org/2000/01/rdf-schema#isDefinedBy': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#subject': 1, 'http://www.w3.org/2000/01/rdf-schema#subPropertyOf': 1, 'age': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#object': 1, 'http://www.w3.org/2000/01/rdf-schema#comment': 1, 'knows': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type': 1, 'http://redfoot.sourceforge.net/2000/10/06/builtin#requiredProperty': 1, 'http://www.w3.org/2000/01/rdf-schema#seeAlso': 1}, 'fish': {'peter': 1}, 'http://www.w3.org/2000/01/rdf-schema#ConstraintProperty': {'http://www.w3.org/2000/01/rdf-schema#domain': 1, 'http://www.w3.org/2000/01/rdf-schema#range': 1}, 'http://www.w3.org/TR/WD-rdf-schema#Class': {'http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Property': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Alt': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag': 1}, 'book': {'design_patterns': 1}, 'http://redfoot.sourceforge.net/2000/10/06/builtin#UIType': {'http://redfoot.sourceforge.net/2000/10/06/builtin#TEXTAREA': 1, 'http://redfoot.sourceforge.net/2000/10/06/builtin#TEXTINPUT': 1}, 'http://redfoot.sourceforge.net/2000/10/06/builtin#YesNo': {'http://redfoot.sourceforge.net/2000/10/06/builtin#NO': 1, 'http://redfoot.sourceforge.net/2000/10/06/builtin#YES': 1}, 'person': {'john': 1}} if t.classes == correct: passed = passed + 1 else: print "TEST 1 FAILED", t.classes t = TestResult() rednode.local.visit_resources_by_type(t.add_class, t.add_instance) correct = {'http://www.w3.org/2000/01/rdf-schema#Class': {'fish': 1, 'book': 1, 'animate': 1, 'person': 1}, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Property': {'age': 1, 'knows': 1}, 'book': {'design_patterns': 1}, 'fish': {'peter': 1}, 'person': {'john': 1}} if t.classes == correct: passed = passed + 1 else: print "TEST 2 FAILED", t.classes t = TestResult() rednode.neighbours.visit_resources_by_type(t.add_class, t.add_instance) correct = {'http://www.w3.org/2000/01/rdf-schema#ConstraintProperty': {'http://www.w3.org/2000/01/rdf-schema#domain': 1, 'http://www.w3.org/2000/01/rdf-schema#range': 1}, 'http://www.w3.org/2000/01/rdf-schema#Class': {'http://www.w3.org/2000/01/rdf-schema#Class': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Property': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Alt': 1, 'http://www.w3.org/2000/01/rdf-schema#Resource': 1, 'http://www.w3.org/2000/01/rdf-schema#ConstraintProperty': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq': 1, 'http://www.w3.org/2000/01/rdf-schema#Literal': 1, 'http://redfoot.sourceforge.net/2000/10/06/builtin#UIType': 1, 'http://www.w3.org/2000/01/rdf-schema#ConstraintResource': 1, 'http://www.w3.org/2000/01/rdf-schema#ContainerMembershipProperty': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement': 1, 'http://www.w3.org/2000/01/rdf-schema#Container': 1, 'http://redfoot.sourceforge.net/2000/10/06/builtin#YesNo': 1}, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Property': {'http://www.w3.org/2000/01/rdf-schema#label': 1, 'http://redfoot.sourceforge.net/2000/10/06/builtin#uiType': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#value': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate': 1, 'http://www.w3.org/2000/01/rdf-schema#subClassOf': 1, 'http://www.w3.org/2000/01/rdf-schema#isDefinedBy': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#subject': 1, 'http://www.w3.org/2000/01/rdf-schema#subPropertyOf': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#object': 1, 'http://www.w3.org/2000/01/rdf-schema#comment': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type': 1, 'http://redfoot.sourceforge.net/2000/10/06/builtin#requiredProperty': 1, 'http://www.w3.org/2000/01/rdf-schema#seeAlso': 1}, 'http://www.w3.org/TR/WD-rdf-schema#Class': {'http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Property': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Alt': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag': 1}, 'http://redfoot.sourceforge.net/2000/10/06/builtin#UIType': {'http://redfoot.sourceforge.net/2000/10/06/builtin#TEXTAREA': 1, 'http://redfoot.sourceforge.net/2000/10/06/builtin#TEXTINPUT': 1}, 'http://redfoot.sourceforge.net/2000/10/06/builtin#YesNo': {'http://redfoot.sourceforge.net/2000/10/06/builtin#NO': 1, 'http://redfoot.sourceforge.net/2000/10/06/builtin#YES': 1}} if t.classes == correct: passed = passed + 1 else: print "TEST 3 FAILED", t.classes t = TestResult() rednode.neighbourhood.visit_resources_by_type(t.add_class, t.add_instance) correct = {'http://www.w3.org/2000/01/rdf-schema#Class': {'http://www.w3.org/2000/01/rdf-schema#Class': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Property': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Alt': 1, 'http://www.w3.org/2000/01/rdf-schema#Resource': 1, 'http://www.w3.org/2000/01/rdf-schema#ConstraintProperty': 1, 'animate': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq': 1, 'http://www.w3.org/2000/01/rdf-schema#Literal': 1, 'http://redfoot.sourceforge.net/2000/10/06/builtin#UIType': 1, 'http://www.w3.org/2000/01/rdf-schema#ConstraintResource': 1, 'person': 1, 'http://www.w3.org/2000/01/rdf-schema#ContainerMembershipProperty': 1, 'fish': 1, 'book': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement': 1, 'http://www.w3.org/2000/01/rdf-schema#Container': 1, 'http://redfoot.sourceforge.net/2000/10/06/builtin#YesNo': 1}, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Property': {'http://www.w3.org/2000/01/rdf-schema#label': 1, 'http://redfoot.sourceforge.net/2000/10/06/builtin#uiType': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#value': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#predicate': 1, 'http://www.w3.org/2000/01/rdf-schema#subClassOf': 1, 'http://www.w3.org/2000/01/rdf-schema#isDefinedBy': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#subject': 1, 'http://www.w3.org/2000/01/rdf-schema#subPropertyOf': 1, 'age': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#object': 1, 'http://www.w3.org/2000/01/rdf-schema#comment': 1, 'knows': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#type': 1, 'http://redfoot.sourceforge.net/2000/10/06/builtin#requiredProperty': 1, 'http://www.w3.org/2000/01/rdf-schema#seeAlso': 1}, 'fish': {'peter': 1}, 'http://www.w3.org/2000/01/rdf-schema#ConstraintProperty': {'http://www.w3.org/2000/01/rdf-schema#domain': 1, 'http://www.w3.org/2000/01/rdf-schema#range': 1}, 'http://www.w3.org/TR/WD-rdf-schema#Class': {'http://www.w3.org/1999/02/22-rdf-syntax-ns#Seq': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Property': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Alt': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Statement': 1, 'http://www.w3.org/1999/02/22-rdf-syntax-ns#Bag': 1}, 'book': {'design_patterns': 1}, 'http://redfoot.sourceforge.net/2000/10/06/builtin#UIType': {'http://redfoot.sourceforge.net/2000/10/06/builtin#TEXTAREA': 1, 'http://redfoot.sourceforge.net/2000/10/06/builtin#TEXTINPUT': 1}, 'http://redfoot.sourceforge.net/2000/10/06/builtin#YesNo': {'http://redfoot.sourceforge.net/2000/10/06/builtin#NO': 1, 'http://redfoot.sourceforge.net/2000/10/06/builtin#YES': 1}, 'person': {'john': 1}} if t.classes == correct: passed = passed + 1 else: print "TEST 4 FAILED", t.classes t = TestResult2() val = rednode.visit(t.add_list, (None, None, None)) if val==1 and len(t.result)==1: passed = passed + 1 else: print "TEST 4 FAILED", val, len(t.result) if passed == 5: return (1, "ALL TESTS PASSED") elif passed > 1: return (0, "ONLY %s TESTS PASSED" % passed) elif passed == 1: return (0, "ONLY 1 TEST PASSED") else: return (0, "ALL TESTS FAILED")
# TODO: seems like rednode should live under redfoot.rdf... move there? from redfoot.rdf.query.schema import SchemaQuery from redfoot.rdf.store.storeio import StoreIO, TripleStoreIO from redfoot.rdf.store.autosave import AutoSaveStoreIO from redfoot.rdf.query.functors import * from redfoot.rdf.const import * from redfoot.rdf.objects import resource, literal NEIGHBOUR = resource("http://redfoot.sourceforge.net/2001/04/neighbour#Neighbour") CONNECTED = resource("http://redfoot.sourceforge.net/2001/04/neighbour#Connected") YES = resource("http://redfoot.sourceforge.net/2000/10/06/builtin#YES") NO = resource("http://redfoot.sourceforge.net/2000/10/06/builtin#NO") class Local(SchemaQuery, TripleStoreIO): """A read/write store of RDF statements - a mixin of Query and TripleStoreIO.""" pass class AutoSaveLocal(SchemaQuery, AutoSaveStoreIO): """Like Local but for auto-saving stores.""" pass class MultiStore(SchemaQuery, StoreIO): """An ordered collection of stores with a 'visit' facade that visits all stores."""
def run(): passed = 0 store = QueryStore() store.add(resource("john"), resource("age"), literal("37")) store.add(resource("paul"), resource("age"), literal("35")) store.add(resource("peter"), resource("age"), literal("35")) store.add(resource("john"), TYPE, resource("person")) store.add(resource("peter"), TYPE, resource("fish")) store.add(resource("john"), LABEL, literal("John")) store.add(resource("paul"), LABEL, literal("Paul")) store.add(resource("peter"), LABEL, literal("Peter")) store.add(resource("john"), resource("knows"), resource("paul")) store.add(resource("paul"), resource("knows"), resource("peter")) store.add(resource("peter"), resource("knows"), resource("mark")) store.add(resource("design_patterns"), TYPE, resource("book")) store.add(resource("person"), TYPE, CLASS) store.add(resource("fish"), TYPE, CLASS) store.add(resource("book"), TYPE, CLASS) store.add(resource("animate"), TYPE, CLASS) store.add(resource("person"), SUBCLASSOF, resource("animate")) store.add(resource("person"), SUBCLASSOF, resource("some_superclass")) store.add(resource("fish"), SUBCLASSOF, resource("animate")) store.add(resource("age"), DOMAIN, resource("person")) store.add(resource("knows"), DOMAIN, resource("person")) store.add(resource("age"), RANGE, LITERAL) store.add(resource("knows"), RANGE, resource("animate")) if store.is_of_type(resource("john"), resource("person")): print "passed 1" passed = passed + 1 else: print "failed 1" if not store.is_of_type(resource("peter"), resource("person")): print "passed 2" passed = passed + 1 else: print "failed 2" if not store.is_of_type(resource("fred"), resource("person")): print "passed 3" passed = passed + 1 else: print "failed 3" class TestResult: def __init__(self): self.result = [] def add_list(self, *args): self.result.append(map(str, args)) t = TestResult() store.visit_typeless_resources(t.add_list) correct = [["age"], ["paul"], ["knows"]] if compare_lists(t.result, correct): print "passed 4" passed = passed + 1 else: print "failed 4" t = TestResult() store.visit_by_type(t.add_list, resource("person"), resource("age"), literal("35")) if t.result == []: print "passed 5" passed = passed + 1 else: print "failed 5" t = TestResult() store.visit_by_type(t.add_list, resource("person"), resource("age"), literal("37")) correct = [["john", "age", "37"]] if compare_lists(t.result, correct): print "passed 6" passed = passed + 1 else: print "failed 6" if str(store.label(resource("john"))) == "John": print "passed 7" passed = passed + 1 else: print "failed 7" t = TestResult() store.visit_transitive(t.add_list, resource("john"), resource("knows")) correct = [["john", "knows", "paul"], ["paul", "knows", "peter"], ["peter", "knows", "mark"]] if compare_lists(t.result, correct): print "passed 8" passed = passed + 1 else: print "failed 8" t = TestResult() store.visit_transitive_reverse(t.add_list, resource("mark"), resource("knows")) correct = [["peter", "knows", "mark"], ["paul", "knows", "peter"], ["john", "knows", "paul"]] if compare_lists(t.result, correct): print "passed 9" passed = passed + 1 else: print "failed 9" if store.exists(resource("person"), SUBCLASSOF, resource("animate")): print "passed 10" passed = passed + 1 else: print "failed 10" if not store.exists(resource("book"), SUBCLASSOF, resource("animate")): print "passed 11" passed = passed + 1 else: print "failed 11" if store.exists(resource("person"), SUBCLASSOF, None): print "passed 12" passed = passed + 1 else: print "failed 12" if not store.exists(resource("book"), SUBCLASSOF, None): print "passed 13" passed = passed + 1 else: print "failed 13" t = TestResult() store.visit_root_classes(s(t.add_list)) correct = [["animate"], ["book"]] if compare_lists(t.result, correct): print "passed 14" passed = passed + 1 else: print "failed 14" t = TestResult() store.visit_parent_types(o(t.add_list), resource("person")) correct = [["some_superclass"], ["animate"]] if compare_lists(t.result, correct): print "passed 15" passed = passed + 1 else: print "failed 15" t = TestResult() store.visit_possible_properties(t.add_list, resource("person")) correct = [["knows"], ["age"]] if compare_lists(t.result, correct): print "passed 16" passed = passed + 1 else: print "failed 16" t = TestResult() store.visit_possible_properties_for_subject(t.add_list, resource("john")) correct = [["knows"], ["age"]] if compare_lists(t.result, correct): print "passed 17" passed = passed + 1 else: print "failed 17" t = TestResult() store.visit_ranges(t.add_list, resource("age")) correct = [["http://www.w3.org/2000/01/rdf-schema#Literal"]] if compare_lists(t.result, correct): print "passed 18" passed = passed + 1 else: print "failed 18" t = TestResult() store.visit_ranges(t.add_list, resource("knows")) correct = [["animate"]] if compare_lists(t.result, correct): print "passed 19" passed = passed + 1 else: print "failed 19" t = TestResult() store.visit_possible_values(t.add_list, resource("knows")) correct = [["peter"], ["john"]] if compare_lists(t.result, correct): print "passed 20" passed = passed + 1 else: print "failed 20" class TestResult2: def __init__(self): self.classes = {} self.current_class = None def add_class(self, c): self.classes[str(c)] = {} self.current_class = str(c) def add_instance(self, i): self.classes[self.current_class][str(i)] = 1 t = TestResult2() store.visit_resources_by_type(t.add_class, t.add_instance) correct = { "http://www.w3.org/2000/01/rdf-schema#Class": {"fish": 1, "book": 1, "animate": 1, "person": 1}, "book": {"design_patterns": 1}, "fish": {"peter": 1}, "person": {"john": 1}, } if t.classes == correct: print "passed 21" passed = passed + 1 else: print "failed 21" class TestResult3: def __init__(self): self.classes = {} self.current_class = None def add_class(self, c, depth): self.classes[str(c) + str(depth)] = {} self.current_class = str(c) + str(depth) def add_instance(self, i, depth): self.classes[self.current_class][str(i) + str(depth)] = 1 def nop(*args): pass t = TestResult3() store.visit_subclasses(t.add_class, nop, t.add_instance, resource("animate"), 1) correct = {"animate0": {}, "fish1": {"peter1": 1}, "person1": {"john1": 1}} if t.classes == correct: print "passed 22" passed = passed + 1 else: print "failed 22" t = TestResult3() store.visit_subclasses(t.add_class, nop, t.add_instance, resource("animate"), 0) correct = {"animate0": {}, "fish1": {}, "person1": {}} if t.classes == correct: print "passed 23" passed = passed + 1 else: print "failed 23" if passed == 23: return (1, "ALL TESTS PASSED") elif passed > 1: return (0, "ONLY %s TESTS PASSED" % passed) elif passed == 1: return (0, "ONLY 1 TEST PASSED") else: return (0, "ALL TESTS FAILED")
import new from redfoot.xml.handler import HandlerBase from redfoot.rdf.objects import resource, literal NS = "http://redfoot.sourceforge.net/2001/09/" SUBMODULE = resource(NS+"SUBMODULE") INSTANCE_NAME = resource(NS+"INSTANCE_NAME") CLASS_NAME = resource(NS+"CLASS_NAME") FROM = resource(NS+"FROM") from redfoot.rdf.syntax.parser import RDF_ELEMENT, RDFHandler class RDFRedcodeRootHandler(HandlerBase): def __init__(self, parser, name): HandlerBase.__init__(self, parser, None) self.name = name from redfoot.rednode import RedNode uri = 'tmp' self.rednode = RedNode(uri) self.rednode.local.location = "TODOtmplocation" self.rednode.local.uri = uri self.globals = {'adder': self.rednode.local.add_statement} def child(self, name, atts): if name == RDF_ELEMENT: RDFHandler(self.parser, self, self.globals) app_class = RDFApp
from redfoot.rdf.objects import resource # Useful RDF constants # SYNTAX RDFNS = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" TYPE = resource(RDFNS + "type") PROPERTY = resource(RDFNS + "Property") STATEMENT = resource(RDFNS + "Statement") SUBJECT = resource(RDFNS + "subject") PREDICATE = resource(RDFNS + "predicate") OBJECT = resource(RDFNS + "object") # SCHEMA RDFSNS = "http://www.w3.org/2000/01/rdf-schema#" CLASS = resource(RDFSNS + "Class") RESOURCE = resource(RDFSNS + "Resource") SUBCLASSOF = resource(RDFSNS + "subClassOf") LABEL = resource(RDFSNS + "label") COMMENT = resource(RDFSNS + "comment") RANGE = resource(RDFSNS + "range") DOMAIN = resource(RDFSNS + "domain") LITERAL = resource(RDFSNS + "Literal") CONTAINER = resource(RDFSNS + "Container")