#!/usr/bin/env ruby
#
# Calling Redland for Ruby (so we can glue RubyRDF code into Redland facilities
# such as parsers, storage systems.
# 
# this file shows some sample Ruby calls to RDF parser via Redland
# $Id: redrubytests.rb,v 1.1 2002/04/25 18:04:12 danbri Exp $ -- danbri@w3.org
#
# This script expects the Redland RDF toolkit to be compiled and installed,
# and for the Redland.so file to be somewhere where Ruby can find it.
# See http://redland.opensource.ac.uk/ for details on Redland.
#
# In brief:
#
# - build yourself a Redland installation (or acquire via RPM etc???)
# - you'll need SWIG (see http://www.swig.org/, consider grabbing latest via CVS)
#   ...this tool builds the C wrappers for Ruby, Perl, Python, Java etc.
# - in Redland's redland/librdf/ruby/ directory, 'make build-ruby'
# - copy the resulting Redland.so file into the right place, which
#  will be somewhere along lines of /usr/local/lib/ruby/1.6/i686-linux
#
#   we can find out using:
#	 ruby -rrbconfig -e "puts \"copy Redland.so into \"+ Config::CONFIG['archdir']"
#
# thanks to dave.beckett@bristol.ac.uk for help getting this running. 
#
# Note that we could use mkmf and the extconf.rb system, but the interactions with 
# Redland's cross-platform autoconf-based configure/build/install machinery are unclear.
#
# Also note that if you want to use BerkeleyDB based storage, there is some minor 
# hoop-jumping to do at the Redland end. For me, using 
#	 ./configure --with-bdb=/usr/local/BerkeleyDB.3.1 
# seems to work OK.

#
# Redland overview - random notes
# everything should be pretty obvious from the examples below.
# one distinction not found in some RDF APIs is between 'storage' and 'graph' interfaces.
# note that an in-memory graph is considered a form of (albeit transient) storage.
# Ruby/SWIG issues: passing in empty strings seems to confuse SWIG.
# as does using an old version of SWIG (nil / NULL issues)

## TODO:
#
#  - I want to switch to using this for RubyRDF access to parser, instead of parsing ntriples 
#    acquired via commandline
#
#  - I want to try pointing the squish query engine at these interfaces, esp the BDB store.
#
#  - I might try hooking my Ruby classes up to this implementation, though that sounds a bit hairy


require 'Redland'
file = 'file:webwho.xrdf'


myworld = Redland::librdf_new_world
Redland::librdf_world_open myworld 


### testing parser
uri = Redland::librdf_new_uri myworld, file
parser = Redland::librdf_new_parser myworld, 'repat', "application/rdf+xml", nil
raise "Failed to find parser" if !parser

stream = Redland::librdf_parser_parse_as_stream parser, uri, uri
raise "No data stream" if !stream

# print a node
def pn (node)
  return Redland::librdf_node_to_string node
end

while  Redland::librdf_stream_end (stream) == 0
   s = Redland::librdf_stream_next stream 
   puts "Subject: #{pn Redland::librdf_statement_get_subject s}"
   puts "predicate: #{pn Redland::librdf_statement_get_predicate s}"
   puts "Object: #{pn Redland::librdf_statement_get_object s} \n\n"
end
stream=nil

### testing model

puts "\n\n\n\nTesting parser-to-memgraph"

memdb = Redland::librdf_new_storage  myworld, 'hashes',  'test', "new='yes',hash-type='bdb','dir='.'"

raise "No data stream" if !memdb

memgraph = Redland::librdf_new_model myworld, memdb, 'yadda'

raise "no model" if !memgraph

retcode = Redland::librdf_parser_parse_into_model parser, uri, uri, memgraph
stream = Redland::librdf_model_serialise memgraph

while  Redland::librdf_stream_end (stream) == 0
   s = Redland::librdf_stream_next stream 
   puts "Subject: #{pn Redland::librdf_statement_get_subject s}"
   puts "predicate: #{pn Redland::librdf_statement_get_predicate s}"
   puts "Object: #{pn Redland::librdf_statement_get_object s} \n\n"
end

# to query, create URIs and nodes

stream=nil

puts "\n\n####################### QUERY RESULTS #### \n\n\n"

rss_title='http://purl.org/rss/1.0/title'

t=  Redland::librdf_new_node_from_uri_string( myworld, rss_title)


st1 = Redland::librdf_new_statement_from_nodes ( myworld,nil,t, nil )

raise "no query statement" if !st1

res=Redland::librdf_model_find_statements memgraph, st1
raise "No result stream" if !res

stream=res
while  Redland::librdf_stream_end (stream) == 0
   s = Redland::librdf_stream_next stream 
   puts "Subject: #{pn Redland::librdf_statement_get_subject s}"
   puts "predicate: #{pn Redland::librdf_statement_get_predicate s}"
   puts "Object: #{pn Redland::librdf_statement_get_object s} \n\n"
end

stream = nil

Redland::librdf_free_world myworld
  

#  my
#$statement=&RDF::Redland::CORE::librdf_new_statement_from_nodes($RDF::Redland::World->{WORLD}, 
#
#&RDF::Redland::CORE::librdf_new_node_from_uri_string($RDF::Redland::World->{WORLD}, 
#"http://purl.org/net/dajobe/"),
#
#&RDF::Redland::CORE::librdf_new_node_from_uri_string($RDF::Redland::World->{WORLD}, 
#"http://purl.org/dc/elements/1.1/creator"),
#
#&RDF::Redland::CORE::librdf_new_node_from_literal($RDF::Redland::World->{WORLD}, 
#"Dave Beckett", "", 0, 0)

#  my $stream=&RDF::Redland::CORE::librdf_model_find_statements($model,$statement);
#  my $source_node=&RDF::Redland::CORE::librdf_new_node_from_uri_string($RDF::Redland::World->{WORLD}, "http://purl.org/net/dajobe/");
#  my $target_node=&RDF::Redland::CORE::librdf_new_node_from_uri_string($RDF::Redland::World->{WORLD}, "http://purl.org/dc/elements/1.1/creator");
#  &RDF::Redland::CORE::librdf_free_iterator($iterator);
#  &RDF::Redland::CORE::librdf_free_model($model);
#  &RDF::Redland::CORE::librdf_free_storage($storage);
 # my $storage=new RDF::Redland::Storage("hashes", "test", "new='yes',hash-type='memory',dir='.'");
 # my $model=new RDF::Redland::Model($storage, "");

 #$statement=RDF::Redland::Statement->new_from_nodes(undef,undef,undef);
 # my $stream=$model->find_statements($statement);


