| 1 | from rdfalchemy import rdfSubject, rdfSingle, rdfMultiple |
|---|
| 2 | from rdflib import ConjunctiveGraph, Namespace, Literal, BNode, URIRef |
|---|
| 3 | |
|---|
| 4 | from StringIO import StringIO |
|---|
| 5 | |
|---|
| 6 | n3data = """ |
|---|
| 7 | @prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>. |
|---|
| 8 | @prefix ov: <http://owl.openvest.org/2005/10/Portfolio#>. |
|---|
| 9 | @prefix vcard: <http://www.w3.org/2006/vcard/ns#>. |
|---|
| 10 | @prefix iso3166: <http://www.daml.org/2001/09/countries/iso-3166-ont#>. |
|---|
| 11 | |
|---|
| 12 | ov:C_US_SUNW a ov:Company; |
|---|
| 13 | ov:GICS_2 ov:GICS_2_45; |
|---|
| 14 | ov:companyName "Sun Microsystems"; |
|---|
| 15 | ov:country iso3166:US; |
|---|
| 16 | ov:nasdaqSymbol "SUNW"; |
|---|
| 17 | ov:secCik "0000709519"; |
|---|
| 18 | ov:symbol "JAVA". |
|---|
| 19 | |
|---|
| 20 | ov:C_US_IBM a ov:Company; |
|---|
| 21 | ov:GICS_2 ov:GICS_2_45; |
|---|
| 22 | ov:companyName "International Business Machines Corp."; |
|---|
| 23 | ov:country iso3166:US; |
|---|
| 24 | ov:nyseSymbol "IBM"; |
|---|
| 25 | ov:secCik "0000051143"; |
|---|
| 26 | ov:symbol "IBM"; |
|---|
| 27 | ov:yindustry "Diversified Computer Systems"; |
|---|
| 28 | ov:ysector "Technology"; |
|---|
| 29 | vcard:tel "914-499-1900"; |
|---|
| 30 | vcard:url "http://www.ibm.com"^^<http://www.w3.org/2001/XMLSchema#anyURI>; |
|---|
| 31 | ov:stockDescription "International Business Machines Corporation (IBM) operates as an information technology (IT) company worldwide. It has .....". |
|---|
| 32 | """ |
|---|
| 33 | |
|---|
| 34 | OV = Namespace('http://owl.openvest.org/2005/10/Portfolio#') |
|---|
| 35 | VCARD = Namespace("http://www.w3.org/2006/vcard/ns#") |
|---|
| 36 | |
|---|
| 37 | rdfSubject.db = ConjunctiveGraph() |
|---|
| 38 | rdfSubject.db.parse(StringIO(n3data), format='n3') |
|---|
| 39 | |
|---|
| 40 | class Company(rdfSubject): |
|---|
| 41 | rdf_type = OV.Company |
|---|
| 42 | symbol = rdfSingle(OV.symbol,'symbol') |
|---|
| 43 | cik = rdfSingle(OV.secCik,'cik') |
|---|
| 44 | companyName = rdfSingle(OV.companyName) |
|---|
| 45 | address = rdfSingle(VCARD.adr) |
|---|
| 46 | |
|---|
| 47 | |
|---|
| 48 | # Above here would typically go in a model.py file and be imported |
|---|
| 49 | ########################################################################## |
|---|
| 50 | # Below here goes in the file with business logic agnostic of persistance |
|---|
| 51 | |
|---|
| 52 | c = Company.get_by(symbol='IBM') |
|---|
| 53 | ## this will enable us to see that the reads are cached |
|---|
| 54 | import logging |
|---|
| 55 | log=logging.getLogger('rdfAlchemy') |
|---|
| 56 | ## comment out to quite debug messages |
|---|
| 57 | log.setLevel(logging.DEBUG) |
|---|
| 58 | |
|---|
| 59 | def test_1(): |
|---|
| 60 | c=Company.get_by(cik="0000051143") |
|---|
| 61 | assert c.symbol == "IBM" |
|---|
| 62 | |
|---|
| 63 | ## list Companies |
|---|
| 64 | for c in Company.ClassInstances(): |
|---|
| 65 | print "%s has an SEC symbol of %s" % (c.companyName, c.cik) |
|---|
| 66 | print '' |
|---|
| 67 | |
|---|
| 68 | |
|---|
| 69 | def test_2(): |
|---|
| 70 | ## Add a descriptor on the fly |
|---|
| 71 | Company.stockDescription = rdfSingle(OV.stockDescription,'stockDescription') |
|---|
| 72 | |
|---|
| 73 | assert c.companyName == c[OV.companyName] |
|---|
| 74 | |
|---|
| 75 | |
|---|
| 76 | ## add another descriptor on the fly |
|---|
| 77 | Company.industry = rdfSingle(OV.yindustry,'industry') |
|---|
| 78 | |
|---|
| 79 | ## add an attribute (from the database) |
|---|
| 80 | c = Company.get_by(symbol = 'JAVA') |
|---|
| 81 | c.industry = 'Computer stuff' |
|---|
| 82 | |
|---|
| 83 | def test_3(): |
|---|
| 84 | ## delete an attribute (from the database) |
|---|
| 85 | c = Company.get_by(symbol = 'IBM') |
|---|
| 86 | assert c.industry == "Diversified Computer Systems" |
|---|
| 87 | del c.industry |
|---|
| 88 | assert c.industry <> "Diversified Computer Systems" |
|---|
| 89 | c = Company.get_by(symbol = 'IBM') |
|---|
| 90 | assert not c.industry |
|---|
| 91 | |
|---|
| 92 | def test_creating(): |
|---|
| 93 | c1=Company() |
|---|
| 94 | c2=Company(OV.A) |
|---|
| 95 | c3=Company('<http://owl.openvest.org/2005/10/Portfolio#A>') |
|---|
| 96 | c4=Company('_:xyz123') |
|---|
| 97 | #assert c2==c3 |
|---|
| 98 | assert c2.resUri == c3.resUri |
|---|
| 99 | assert c4.resUri == BNode('xyz123') |
|---|
| 100 | |
|---|
| 101 | # write out the new n3 file to see the changes |
|---|
| 102 | c.db.serialize('example-out.n3',format='n3') |
|---|