#!/usr/local/bin/perl -I ../lib
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License.  You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

use RDFStore::NodeFactory;
use RDFStore::Model;
use File::Path qw(rmtree);

unless($#ARGV>=2) {
	print STDERR "\nUsage: <subject> <predicate> (<object> | 'object')\n\n";
	exit;
};

my $factory= new RDFStore::NodeFactory();
my $statement = $factory->createStatement(
			$factory->createResource($ARGV[0]),
			$factory->createResource($ARGV[1]),
			($#ARGV>2) ? $factory->createLiteral($ARGV[2]) : $factory->createResource($ARGV[2])
					);
my $statement0 = $factory->createStatement(
			$factory->createResource($ARGV[0]),
			$factory->createResource($ARGV[0]),
			($#ARGV>2) ? $factory->createLiteral($ARGV[2]) : $factory->createResource($ARGV[2])
					);
my $statement1 = $factory->createStatement($factory->createResource("http://www.altavista.com"),$factory->createResource("http://pen.jrc.it/schema/1.0/#author"),$factory->createLiteral("Alberto Reggiori"));

my $statement2 = $factory->createStatement($factory->createUniqueResource(),$factory->createUniqueResource(),$factory->createLiteral(""));

my $model = new RDFStore::Model(Name => 'test', Style => "BerkeleyDB", Split => 3);

$model->add($statement);
$model->add($statement0);
$model->add($statement1);
$model->add($statement2);

my $found = $model->find($statement->subject,undef,$statement->object);
my($found_elements) = $found->elements;
for ( my $st = $found_elements->first; $found_elements->hasnext; $st= $found_elements->next ) {
	print $st->getLabel(),"\n";
};

my $model1 = $model->duplicate();
#print $model1->getDigest->equals($model1->getDigest),"\n";
#print $model1->getDigest->hashCode,"\n";

print "The following should give the same results due that is a duplicate model :-)\n";

$found = $model1->find($statement->subject,undef,$statement->object);
($found_elements) = $found->elements;
for ( my $st = $found_elements->first; $found_elements->hasnext; $st= $found_elements->next ) {
	print $st->getLabel(),"\n";
};
eval{
rmtree('test');
};
