Debian Codegraph @

lua/test.lua

raw symbols SCIP
1 --[[
2 * test.lua - Redland Lua Interface test program
3 *
4 * Joe Presbrey - http://presbrey.mit.edu/
5 * 2010-09-20
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 print("Testing Redland...");
23 require("redland");
24
25 world = redland.librdf_new_world();
26 print("Redland world opened");
27
28 storage = redland.librdf_new_storage(world,'hashes','dummy',"new=yes,hash-type='memory'");
29 print("Redland storage created");
30
31 model = redland.librdf_new_model(world,storage,'');
32 print("Redland model created");
33
34 parser = redland.librdf_new_parser(world,'rdfxml','application/rdf+xml',null);
35 print("Redland parser created");
36
37 uri = redland.librdf_new_uri(world,'file:../data/dc.rdf');
38
39 print("Parsing...");
40 redland.librdf_parser_parse_into_model(parser,uri,uri,model);
41 print("Done...");
42
43 redland.librdf_free_uri(uri);
44
45 redland.librdf_free_parser(parser);
46
47 query = redland.librdf_new_query(world, 'sparql', null, "PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?a ?c ?d WHERE { ?a dc:title ?c . OPTIONAL { ?a dc:related ?d } }", null);
48 print("Querying for dc:titles:");
49 results = redland.librdf_model_query_execute(model, query);
50 count=0;
51 while not(results == nil) and redland.librdf_query_results_finished(results) == 0 do
52 print("result "..count..": {");
53 for i = 0, redland.librdf_query_results_get_bindings_count(results) - 1, 1 do
54 val = redland.librdf_query_results_get_binding_value(results, i);
55 if val then
56 nval = redland.librdf_node_to_string(val);
57 else
58 nval = "(unbound)";
59 end
60 print(" "..redland.librdf_query_results_get_binding_name(results, i).."="..(nval or "(nil)"));
61 end
62 print("}");
63 redland.librdf_query_results_next(results);
64 count = count + 1;
65 end
66 if not(results == nil) then
67 print("Returned "..count.." results");
68 end
69
70 results = nil;
71
72 print "\nExecuting query again";
73 results = redland.librdf_model_query_execute(model, query);
74 if not(results == nil) then
75 str = redland.librdf_query_results_to_string(results, nil, nil);
76 print("Query results serialized to an XML string size "..string.len(str).." bytes");
77 else
78 print("Query results couldn't be serialized to an XML string");
79 end
80
81 serializer = redland.librdf_new_serializer(world, "rdfxml", "", nil);
82 print("Redland serializer created");
83
84 base = redland.librdf_new_uri(world, "http://example.org/base.rdf");
85
86 print("Serializing...");
87 redland.librdf_serializer_serialize_model_to_file(serializer,'./test-out.rdf',base,model);
88 print("Done...");
89
90 redland.librdf_free_serializer(serializer);
91
92 redland.librdf_free_uri(base);
93
94 redland.librdf_free_model(model);
95
96 redland.librdf_free_storage(storage);
97
98 print("Done");