一套用来处理RDF语言文档的工具。
原文地址:
这个套件有以下几个部分:
目前我只是进行了合理的使用和配置,并简单跑了一下DEMO,不过好像有些问题。
下载安装:
redland-1.0.8.tar.gz tar ... ./configure make sudo make install
redland-bindings-1.0.8.1.tar.gz tar ... ./configure --with-ruby make sudo make install
不过我直接运行例子是有问题的。
目录结构:
../data/dc.rdf
../ruby/example.rb
#!/usr/bin/env ruby
#
# example.rb - Redland example Ruby program
#
# Copyright (C) 2002-2004 David Beckett - http://www.dajobe.org/
# Copyright (C) 2002-2004 University of Bristol - http://www.bristol.ac.uk/
#
# This package is Free Software or Open Source available under the
# following licenses (these are alternatives):
# 1. GNU Lesser General Public License (LGPL)
# 2. GNU General Public License (GPL)
# 3. Mozilla Public License (MPL)
#
# See LICENSE.html or LICENSE.txt at the top of this package for the
# full license terms.
#
#
# USAGE: ruby example.rb file:../data/dc.rdf raptor
#
#
require 'rdf/redland'
uri_string=ARGV[0]
parser_name=ARGV[1]
#storage=Redland::TripleStore.new("hashes", "test", "new='no',hash-type='bdb',dir='.'")
#源代码在这里被我注释掉了,调试时有问题,改成了下列代码
storage=Redland::MemoryStore.new("test", "new='yes',hash-type='bdb',dir='.'")
raise "Failed to create RDF storage" if !storage
model=Redland::Model.new(storage)
if !model then
raise "Failed to create RDF model"
end
parser=Redland::Parser.new(parser_name, "", nil)
if !parser then
raise "Failed to create RDF parser"
end
uri=Redland::Uri.new(uri_string)
stream=parser.parse_as_stream(uri, uri)
count=0
while !stream.end?()
statement=stream.current()
model.add_statement(statement)
puts "found statement: #{statement}"
count=count+1
stream.next()
end
puts "Parsing added #{count} statements"
puts "Printing all statements"
stream=model.as_stream()
while !stream.end?()
statement=stream.current()
puts "Statement: #{statement}"
stream.next()
end
q = Redland::Query.new(" PREFIX dc: <http://purl.org/dc/elements/1.1/> SELECT ?a ?c WHERE { ?a dc:title ?c } ")
puts "Querying for dc:titles:"
results=q.execute(model)
while !results.finished?()
puts "{"
for k in 0..results.bindings_count()-1
puts " #{k}= #{results.binding_value(k)}"
end
puts "}"
results.next()
end
results=q.execute(model)
size=results.to_string(Redland::Uri.new("http://www.w3.org/2001/sw/DataAccess/json-sparql/")).length()
puts "Serialized query results to JSON as a string size #{size} bytes"
puts "Writing model to test-out.rdf as rdf/xml"
# Use any rdf/xml parser that is available
serializer=Redland::Serializer.new()
serializer.set_namespace("dc", Redland::Uri.new("http://purl.org/dc/elements/1.1/"))
serializer.set_namespace("rdf", Redland::Uri.new("http://www.w3.org/1999/02/22-rdf-syntax-ns#"))
serializer.to_file("test-out.rdf", model)
size=model.to_string(name="ntriples", base_uri=Redland::Uri.new("http://example.org/base#")).length()
puts "Serialized to ntriples as a string size #{size} bytes"
puts "Done"
其实刚刚开始运行是不行的。我做了以下处理:
将/usr/local/lib 中的关于 librdf,librasqal,libraptor相关的link文件,转移到/usr/lib中。由于在运行中需要调用这些主件,所以需要放在一个可以看到的位置。要不然会报错。