| 1 | #!/usr/bin/perl |
| 2 | # |
| 3 | # example.pl - Redland eaxmple Perl program |
| 4 | # |
| 5 | # Copyright (C) 2000-2005 David Beckett - http://www.dajobe.org/ |
| 6 | # Copyright (C) 2000-2005 University of Bristol - http://www.bristol.ac.uk/ |
| 7 | # |
| 8 | # This package is Free Software and part of Redland http://librdf.org/ |
| 9 | # |
| 10 | # It is licensed under the following three licenses as alternatives: |
| 11 | # 1. GNU Lesser General Public License (LGPL) V2.1 or any newer version |
| 12 | # 2. GNU General Public License (GPL) V2 or any newer version |
| 13 | # 3. Apache License, V2.0 or any newer version |
| 14 | # |
| 15 | # You may not use this file except in compliance with at least one of |
| 16 | # the above three licenses. |
| 17 | # |
| 18 | # See LICENSE.html or LICENSE.txt at the top of this package for the |
| 19 | # full license terms. |
| 20 | # |
| 21 | # |
| 22 | # |
| 23 | |
| 24 | use RDF::Redland; |
| 25 | |
| 26 | $test_file="../data/dc.rdf"; |
| 27 | |
| 28 | warn "Creating storage\n"; |
| 29 | my $storage=new RDF::Redland::Storage("hashes", "test", |
| 30 | "new='yes',hash-type='bdb',dir='.'"); |
| 31 | die "Failed to create RDF::Redland::Storage\n" unless $storage; |
| 32 | |
| 33 | warn "\nCreating model\n"; |
| 34 | my $model=new RDF::Redland::Model($storage, ""); |
| 35 | die "Failed to create RDF::Redland::Model for storage\n" unless $model; |
| 36 | |
| 37 | |
| 38 | my $creator_uri=new RDF::Redland::URI("http://purl.org/dc/elements/1.1/creator"); |
| 39 | |
| 40 | warn "\nCreating statement\n"; |
| 41 | my $statement=new RDF::Redland::Statement(RDF::Redland::Node->new_from_uri("http://www.dajobe.org/"), |
| 42 | $creator_uri, |
| 43 | new RDF::Redland::Node("Dave Beckett")); |
| 44 | die "Failed to create RDF::Redland::Statement\n" unless $statement; |
| 45 | |
| 46 | warn "\nAdding statement to model\n"; |
| 47 | $model->add_statement($statement); |
| 48 | $statement=undef; |
| 49 | |
| 50 | my $n=new RDF::Redland::URI('http://example.org/foo'); |
| 51 | my $statementn=new RDF::Redland::Statement($n, $n, $n); |
| 52 | |
| 53 | warn "\nAdding statement (new Statement(n, n, n)) to model\n"; |
| 54 | $model->add_statement($statementn); |
| 55 | $statement=undef; |
| 56 | |
| 57 | warn "\nAdding statement (n,n,n) to model\n"; |
| 58 | $model->add_statement($n, $n, $n); |
| 59 | $statement=undef; |
| 60 | |
| 61 | die "Data file $test_file not found - cannot parse it.\n Try editing \$test_file variable to point to a file.\n" |
| 62 | unless -r $test_file; |
| 63 | |
| 64 | warn "\nParsing URI (file) $test_file\n"; |
| 65 | my $uri=new RDF::Redland::URI("file:$test_file"); |
| 66 | |
| 67 | # Use any rdf/xml parser that is available |
| 68 | my $parser=new RDF::Redland::Parser("rdfxml", "application/rdf+xml"); |
| 69 | die "Failed to find parser\n" if !$parser; |
| 70 | |
| 71 | $stream=$parser->parse_as_stream($uri,$uri); |
| 72 | my $count=0; |
| 73 | while(!$stream->end) { |
| 74 | $model->add_statement($stream->current); |
| 75 | $count++; |
| 76 | $stream->next; |
| 77 | } |
| 78 | $stream=undef; |
| 79 | warn "Parsing added $count statements\n"; |
| 80 | |
| 81 | |
| 82 | warn "\nNamespace declarations seen during parsing:\n"; |
| 83 | my(%namespaces)=$parser->namespaces_seen(); |
| 84 | while(my($prefix,$uri)=each %namespaces) { |
| 85 | warn " prefix '$prefix' URI ".$uri->as_string."\n"; |
| 86 | } |
| 87 | |
| 88 | warn "\nPrinting all statements\n"; |
| 89 | $stream=$model->as_stream; |
| 90 | while(!$stream->end) { |
| 91 | print "Statement: ",$stream->current->as_string,"\n"; |
| 92 | $stream->next; |
| 93 | } |
| 94 | $stream=undef; |
| 95 | |
| 96 | warn "\nSearching model for statements matching predicate http://purl.org/dc/elements/1.1/creator\n"; |
| 97 | $statement=new RDF::Redland::Statement(undef, $creator_uri, undef); |
| 98 | my $stream=$model->find_statements($statement); |
| 99 | while(!$stream->end) { |
| 100 | my $statement2=$stream->current; |
| 101 | print "Matching Statement: ",$statement2->as_string,"\n"; |
| 102 | my $subject=$statement2->subject; |
| 103 | print " Subject: ",$subject->as_string,"\n"; |
| 104 | print " Predicate: ",$statement2->predicate->as_string,"\n"; |
| 105 | print " Object: ",$statement2->object->as_string,"\n"; |
| 106 | $stream->next; |
| 107 | } |
| 108 | $stream=undef; |
| 109 | |
| 110 | $statement=undef; |
| 111 | |
| 112 | |
| 113 | my $home=RDF::Redland::Node->new_from_uri("http://www.dajobe.org/"); |
| 114 | warn "\nSearching model for targets of subject ",$home->uri->as_string," predicate ", $creator_uri->as_string, "\n"; |
| 115 | my(@nodes)=$model->targets($home, new RDF::Redland::Node($creator_uri)); |
| 116 | die "Failed to find any targets matching\n" |
| 117 | unless @nodes; |
| 118 | for my $node (@nodes) { |
| 119 | print "Matching Node: ",$node->as_string,"\n"; |
| 120 | } |
| 121 | $iterator=undef; |
| 122 | |
| 123 | |
| 124 | my $q = new RDF::Redland::Query("PREFIX dc: <http://purl.org/dc/elements/1.1/>\nSELECT ?a ?c WHERE { ?a dc:title ?c }", undef, undef, "sparql"); |
| 125 | print "Querying for dc:titles:\n"; |
| 126 | my $results=$model->query_execute($q); |
| 127 | my $count=1; |
| 128 | while(!$results->finished) { |
| 129 | print "result $count: {\n"; |
| 130 | for(my $i=0; $i < $results->bindings_count; $i++) { |
| 131 | my $val=$results->binding_value($i); |
| 132 | next unless defined $val; # optionals |
| 133 | print " ",$results->binding_name($i),"=",$val->as_string,"\n"; |
| 134 | } |
| 135 | print "}\n"; |
| 136 | $results->next_result; |
| 137 | $count++; |
| 138 | } |
| 139 | $results=undef; |
| 140 | warn "Returned $count results\n"; |
| 141 | |
| 142 | |
| 143 | print "\nExecuting query again\n"; |
| 144 | my $results=$model->query_execute($q); |
| 145 | my $str=$results->to_string; |
| 146 | print "Query results serialized to an XML string size ".length($str)." bytes\n"; |
| 147 | |
| 148 | print "\nExecuting SPARQL construct query\n"; |
| 149 | my $q2 = new RDF::Redland::Query("CONSTRUCT { ?a ?b ?c } WHERE { ?a ?b ?c }", undef, undef, "sparql"); |
| 150 | my $results=$model->query_execute($q2); |
| 151 | $stream=$results->as_stream; |
| 152 | $count=0; |
| 153 | while(!$stream->end) { |
| 154 | print "Statement: ",$stream->current->as_string,"\n"; |
| 155 | $stream->next; |
| 156 | $count++; |
| 157 | } |
| 158 | $stream=undef; |
| 159 | warn "Returned $count triples\n"; |
| 160 | |
| 161 | warn "\nWriting model to test-out.rdf as rdf/xml\n"; |
| 162 | |
| 163 | # Use any rdf/xml parser that is available |
| 164 | my $serializer=new RDF::Redland::Serializer("rdfxml"); |
| 165 | die "Failed to find serializer\n" if !$serializer; |
| 166 | |
| 167 | $serializer->set_namespace("dc", new RDF::Redland::URI("http://purl.org/dc/elements/1.1/")); |
| 168 | $serializer->serialize_model_to_file("test-out.rdf", $uri, $model); |
| 169 | |
| 170 | my $str1=$serializer->serialize_model_to_string($uri, $model); |
| 171 | warn "\nSerialized to RDF/XML as a string size ",length($str1)," bytes\n"; |
| 172 | |
| 173 | $serializer=undef; |
| 174 | |
| 175 | my $str=$model->to_string(new RDF::Redland::URI("http://example.org/base#"), "ntriples"); |
| 176 | warn "\nSerialized to ntriples as a string size ",length($str)," bytes\n"; |
| 177 | |
| 178 | warn "\nDone\n"; |
| 179 | |
| 180 | # Required in order to ensure storage is correctly flushed to disk |
| 181 | $storage=undef; |
| 182 | $model=undef; |