Debian Codegraph @

python/test/test.py

raw symbols SCIP
1 #
2 # test.py - Redland Python 2.0 test code
3 #
4 # Copyright (C) 2000-2004 David Beckett - http://www.dajobe.org/
5 # Copyright (C) 2000-2004 University of Bristol - http://www.bristol.ac.uk/
6 #
7 # This package is Free Software and part of Redland http://librdf.org/
8 #
9 # It is licensed under the following three licenses as alternatives:
10 # 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version
11 # 2. GNU General Public License (GPL) V2 or any newer version
12 # 3. Apache License, V2.0 or any newer version
13 #
14 # You may not use this file except in compliance with at least one of
15 # the above three licenses.
16 #
17 # See LICENSE.html or LICENSE.txt at the top of this package for the
18 # full license terms.
19 #
20 #
21 #
22
23 import RDF
24
25 storage=RDF.Storage(storage_name="memory",
26 name="test",
27 options_string="")
28 # options_string="new='yes',hash-type='bdb',dir='.'")
29 if storage is None:
30 raise "new RDF.storage failed"
31
32 model=RDF.Model(storage)
33 if model is None:
34 raise "new RDF.model failed"
35
36 statement=RDF.Statement(RDF.Uri("http://www.dajobe.org/"),
37 RDF.Uri("http://purl.org/dc/elements/1.1/creator"),
38 RDF.Node("Dave Beckett"))
39 if statement is None:
40 raise "new RDF.statement failed"
41
42 model.add_statement(statement)
43
44 print("printing all model statements")
45 # Match against an empty statement - find everything
46 statement=RDF.Statement(subject=None, predicate=None, object=None);
47
48 for s in model.find_statements(statement):
49 print(" found statement: %s" % (s,))
50
51 # Use any rdf/xml parser that is available
52 parser=RDF.Parser(name="rdfxml",mime_type="application/rdf+xml")
53 if parser is None:
54 raise "Could not find any rdf/xml parser"
55
56 uri=RDF.Uri(string="file:../data/dc.rdf")
57 print("made uri %s" %(uri,))
58
59 for s in parser.parse_as_stream(uri, uri):
60 print("found parsed statement: %s" % (s,))
61 model.add_statement(s)
62
63
64 rdfxml_string="""<?xml version='1.0'?>
65 <rdf:RDF xmlns:rdf='http://www.w3.org/1999/02/22-rdf-syntax-ns#'
66 xmlns:dc='http://purl.org/dc/elements/1.1/'>
67 <rdf:Description rdf:about='http://www.dajobe.org/'
68 dc:title='Home Page of David Beckett' />
69 </rdf:RDF>"""
70
71 for s in parser.parse_string_as_stream(rdfxml_string, uri):
72 print("found parsed statement from string: %s" % (s,))
73 model.add_statement(s)
74
75 print(parser.namespaces_seen())
76
77 # add it again just to get some more statements
78 print("adding statements again with model.load")
79 model.load(uri)
80
81
82
83 print("printing model")
84 for s in model.as_stream():
85 print("found statement: %s" %(s,))
86
87 print("searching model by statement")
88
89 for s in model.find_statements(RDF.Statement(None, RDF.Uri("http://purl.org/dc/elements/1.1/title"), None)):
90 print(" found statement: %s" % (s,))
91
92
93 print("searching model for node targets")
94 n1=RDF.Uri("http://www.dajobe.org/")
95 n2=RDF.Uri("http://purl.org/dc/elements/1.1/title")
96 for node in model.targets(n1,n2):
97 print(" found node: %s" % (node,))
98
99 print("matching statements")
100 if not RDF.Statement(None,None,None).matches(RDF.Statement(n1,n2,"Title")):
101 print("Failed")
102
103 print("Testing for None")
104 print(RDF.Statement(None,None,None).subject)
105
106 print("Adding datatyped literal statement to model")
107 model.add_typed_literal_statement(subject=RDF.Node(uri_string="http://example.org/subject"),
108 predicate=RDF.Node(uri_string="http://example.org/predicate"),
109 string="Literal content",
110 xml_language="en-GB",
111 datatype=RDF.Uri(string="http://example.org/datatype"))
112
113 print("writing model as RDF/XML to test-out.rdf")
114 ser=RDF.RDFXMLSerializer()
115 ser.serialize_model_to_file("test-out.rdf", model)
116
117 print("done")