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
23import RDF
24
25storage=RDF.Storage(storage_name="memory",
26                    name="test",
27                    options_string="")
28#                    options_string="new='yes',hash-type='bdb',dir='.'")
29if storage is None:
30  raise "new RDF.storage failed"
31
32model=RDF.Model(storage)
33if model is None:
34  raise "new RDF.model failed"
35
36statement=RDF.Statement(RDF.Uri("http://www.dajobe.org/"),
37                        RDF.Uri("http://purl.org/dc/elements/1.1/creator"),
38                        RDF.Node("Dave Beckett"))
39if statement is None:
40  raise "new RDF.statement failed"
41
42model.add_statement(statement)
43
44print("printing all model statements")
45# Match against an empty statement - find everything
46statement=RDF.Statement(subject=None, predicate=None, object=None);
47
48for s in model.find_statements(statement):
49  print("  found statement: %s" % (s,))
50
51# Use any rdf/xml parser that is available
52parser=RDF.Parser(name="rdfxml",mime_type="application/rdf+xml")
53if parser is None:
54  raise "Could not find any rdf/xml parser"
55
56uri=RDF.Uri(string="file:../data/dc.rdf")
57print("made uri %s" %(uri,))
58
59for s in parser.parse_as_stream(uri, uri):
60  print("found parsed statement: %s" % (s,))
61  model.add_statement(s)
62
63
64rdfxml_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
71for 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
75print(parser.namespaces_seen())
76
77# add it again just to get some more statements
78print("adding statements again with model.load")
79model.load(uri)
80
81
82
83print("printing model")
84for s in model.as_stream():
85  print("found statement: %s" %(s,))
86
87print("searching model by statement")
88
89for 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
93print("searching model for node targets")
94n1=RDF.Uri("http://www.dajobe.org/")
95n2=RDF.Uri("http://purl.org/dc/elements/1.1/title")
96for node in model.targets(n1,n2):
97  print("  found node: %s" % (node,))
98
99print("matching statements")
100if not RDF.Statement(None,None,None).matches(RDF.Statement(n1,n2,"Title")):
101  print("Failed")
102
103print("Testing for None")
104print(RDF.Statement(None,None,None).subject)
105
106print("Adding datatyped literal statement to model")
107model.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
113print("writing model as RDF/XML to test-out.rdf")
114ser=RDF.RDFXMLSerializer()
115ser.serialize_model_to_file("test-out.rdf", model)
116
117print("done")
118