View | Details | Raw Unified | Return to bug 18713
Collapse All | Expand All

(-)a/Koha/RDF.pm (+54 lines)
Lines 47-50 sub mint_uri { Link Here
47
    return $new_uri;
47
    return $new_uri;
48
}
48
}
49
49
50
sub DelNamedGraph {
51
    my ($self, $args) = @_;
52
    my $model = $args->{model};
53
    my $context = $args->{context};
54
    if ($model && $context){
55
        #Create variables to match all nodes with a given context (ie within a named graph),
56
        # so that we can delete all triples in the named graph and thus delete the graph.
57
        my $s = RDF::Trine::Node::Variable->new("s");
58
        my $p = RDF::Trine::Node::Variable->new("p");
59
        my $o = RDF::Trine::Node::Variable->new("o");
60
61
        #Perform operations
62
        #FIXME: Workaround for differences in RDF::Trine::Store::* API interfaces...
63
        #See https://github.com/kasei/perlrdf/issues/149 and https://github.com/kasei/perlrdf/pull/150
64
        my $store = $model->_store;
65
        if ($store){
66
            if ( $store->isa("RDF::Trine::Store::SPARQL") ){
67
                my $quad = RDF::Trine::Statement::Quad->new($s, $p, $o, $context);
68
                $model->remove_statements($quad);
69
            }
70
            else {
71
                $model->remove_statements($s,$p,$o,$context);
72
            }
73
        }
74
    }
75
}
76
77
sub AddNamedGraph {
78
    my ($self, $args) = @_;
79
    my $model = $args->{model};
80
    my $context = $args->{context};
81
    my $iterator = $args->{iterator};
82
83
    while (my $st = $iterator->next){
84
        #Set the context (in order to populate the named graph)
85
        $st->context($context);
86
87
        $model->add_statement($st);
88
        #NOTE: This method returns undef on success.
89
    }
90
}
91
92
sub AddSeeAlso {
93
    my ($self, $args) = @_;
94
    my $model = $args->{model};
95
    my $subject = $args->{subject};
96
    my $predicate = RDF::Trine::Node::Resource->new("http://www.w3.org/2000/01/rdf-schema#seeAlso");
97
    my $object = $args->{object};
98
    if ($model && $subject && $predicate && $object){
99
        my $statement = RDF::Trine::Statement->new($subject,$predicate,$object);
100
        $model->add_statement($statement);
101
    }
102
}
103
50
1;
104
1;
(-)a/t/Koha/RDF.t (-2 / +67 lines)
Lines 16-22 Link Here
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
17
18
use Modern::Perl;
18
use Modern::Perl;
19
use Test::More tests => 3;
19
use Test::More tests => 5;
20
use RDF::Trine;
21
use JSON;
20
22
21
use t::lib::Mocks;
23
use t::lib::Mocks;
22
24
Lines 31-33 is($well_formed_uri,'http://koha-community.org/bib/1','Successfully minted a RDF Link Here
31
t::lib::Mocks::mock_preference('OPACBaseURL', 'koha-community.org');
33
t::lib::Mocks::mock_preference('OPACBaseURL', 'koha-community.org');
32
my $malformed_uri = $rdf->mint_uri('biblio',2);
34
my $malformed_uri = $rdf->mint_uri('biblio',2);
33
is($malformed_uri,undef,"Didn't mint URI due to missing URI scheme");
35
is($malformed_uri,undef,"Didn't mint URI due to missing URI scheme");
34
- 
36
37
38
subtest 'Create and delete named graphs' => sub {
39
    plan tests => 3;
40
    my $store = RDF::Trine::Store::Memory->new();
41
    my $model = RDF::Trine::Model->new($store);
42
43
    my $context = RDF::Trine::Node::Resource->new("http://koha-community/graph/1");
44
    my $subject = RDF::Trine::Node::Resource->new("http://koha-community/resource/1");
45
    my $predicate = RDF::Trine::Node::Resource->new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
46
    my $object = RDF::Trine::Node::Resource->new("http://koha-community/vocab/record");
47
    my $triple = RDF::Trine::Statement->new($subject,$predicate,$object);
48
    my $quad = RDF::Trine::Statement::Quad->new($subject,$predicate,$object,undef);
49
    my $stream = [
50
        $quad,
51
    ];
52
    my $iterator = RDF::Trine::Iterator::Graph->new($stream);
53
54
    #NOTE: Add a triple to the default graph
55
    $model->add_statement($triple);
56
57
    #NOTE: Add a triple to a named graph (ie add a quad to the triplestore) separate from the
58
    #named graph we're going to make with the Koha::RDF method
59
    if (my $clone = $quad->clone){
60
        my $separate_context = RDF::Trine::Node::Resource->new("http://koha-community/graph/keep");
61
        $clone->context($separate_context);
62
        $model->add_statement($clone);
63
    }
64
65
    is($model->size,2,"2 statements in model (in default graph and a named graph)");
66
    $rdf->AddNamedGraph({
67
        model => $model,
68
        context => $context,
69
        iterator => $iterator,
70
    });
71
    is($model->size,3,"3 statements in model (1 in default graph and 2 in separate named graphs");
72
73
    $rdf->DelNamedGraph({
74
        model => $model,
75
        context => $context,
76
    });
77
    is($model->size,2,"2 statements in model (1 in default graph and 1 in a named graph) after 1 of the named graphs was deleted");
78
};
79
80
subtest 'Add triple using Koha::RDF::AddSeeAlso' => sub {
81
    plan tests => 2;
82
    my $store = RDF::Trine::Store::Memory->new();
83
    my $model = RDF::Trine::Model->new($store);
84
85
    my $resource = RDF::Trine::Node::Resource->new("http://koha-community/resource/1");
86
    my $linked_resource = RDF::Trine::Node::Resource->new("http://upstream/resource/1000");
87
    
88
    $rdf->AddSeeAlso({
89
        model => $model,
90
        subject => $resource,
91
        object => $linked_resource,
92
    });
93
    my $iterator = $model->as_stream;
94
    my @statements = $iterator->get_all;
95
    is(scalar @statements,1,"Added one statement");
96
    my $statement = shift @statements;
97
    my $string = $statement->as_string;
98
    is($string,'(quad <http://koha-community/resource/1> <http://www.w3.org/2000/01/rdf-schema#seeAlso> <http://upstream/resource/1000> (nil))',"Statement correctly added");
99
};

Return to bug 18713