#| -*-Scheme-*- $Id: trivial-store.scm 4174 2007-10-09 20:01:42Z cph $ Copyright (C) 2007 Massachusetts Institute of Technology This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. |# ;;;; Trivial RDF store (declare (usual-integrations)) (define (make-tstore graph) (guarantee-rdf-graph graph 'make-tstore) (%make-tstore (rdf-graph-triples graph))) (define-record-type (%make-tstore contents) tstore? (contents tstore-contents)) (define (find-triple subject predicate object tstore) (find-matching-item (tstore-contents tstore) (lambda (triple) (and (eq? (rdf-triple-subject triple) subject) (eq? (rdf-triple-predicate triple) predicate) (eq? (rdf-triple-object triple) object))))) (define (find-multiple-values subject predicate tstore) (map rdf-triple-object (keep-matching-items (tstore-contents tstore) (lambda (triple) (and (eq? (rdf-triple-subject triple) subject) (eq? (rdf-triple-predicate triple) predicate)))))) (define (find-multiple-subjects predicate object tstore) (map rdf-triple-subject (keep-matching-items (tstore-contents tstore) (lambda (triple) (and (eq? (rdf-triple-predicate triple) predicate) (eq? (rdf-triple-object triple) object)))))) (define (find-resources-of-type type tstore) (find-multiple-subjects rdf:type type tstore)) (define (find-optional-value subject predicate tstore) (let ((ts (find-multiple-values subject predicate tstore))) (if (pair? ts) (begin (if (not (null? (cdr ts))) (error "Too many values:" subject predicate)) (car ts)) #f))) (define (find-required-value subject predicate tstore) (let ((value (find-optional-value subject predicate tstore))) (if (not value) (error "Missing required value:" subject predicate)) value)) (define (find-optional-list-value subject predicate tstore) (let ((value (find-optional-value subject predicate tstore))) (and value (rdf-list->list value tstore)))) (define (find-required-list-value subject predicate tstore) (rdf-list->list (find-required-value subject predicate tstore) tstore)) (define (rdf-list? resource tstore) (find-triple resource rdf:type tstore)) (define-guarantee rdf-list "RDF list") (define (rdf-list->list resource tstore) (guarantee-rdf-list resource 'rdf-list->list) (if (eq? resource rdf:nil) '() (cons (find-required-value resource rdf:first tstore) (find-required-list-value resource rdf:rest tstore))))