Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id C7E49200BAC for ; Wed, 26 Oct 2016 17:58:15 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id C67CF160AE1; Wed, 26 Oct 2016 15:58:15 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id 981B9160AFD for ; Wed, 26 Oct 2016 17:58:13 +0200 (CEST) Received: (qmail 34129 invoked by uid 500); 26 Oct 2016 15:58:12 -0000 Mailing-List: contact commits-help@commonsrdf.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commonsrdf.incubator.apache.org Delivered-To: mailing list commits@commonsrdf.incubator.apache.org Received: (qmail 34120 invoked by uid 99); 26 Oct 2016 15:58:12 -0000 Received: from pnap-us-west-generic-nat.apache.org (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 26 Oct 2016 15:58:12 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id 4DB4218397F for ; Wed, 26 Oct 2016 15:58:12 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: -0.799 X-Spam-Level: X-Spam-Status: No, score=-0.799 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, KAM_LINEPADDING=1.2, RP_MATCHES_RCVD=-2.999] autolearn=disabled Received: from mx1-lw-us.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id AX933qepky15 for ; Wed, 26 Oct 2016 15:58:01 +0000 (UTC) Received: from mailrelay1-us-west.apache.org (mailrelay1-us-west.apache.org [209.188.14.139]) by mx1-lw-us.apache.org (ASF Mail Server at mx1-lw-us.apache.org) with ESMTP id E0F385FC5F for ; Wed, 26 Oct 2016 15:58:00 +0000 (UTC) Received: from svn01-us-west.apache.org (svn.apache.org [10.41.0.6]) by mailrelay1-us-west.apache.org (ASF Mail Server at mailrelay1-us-west.apache.org) with ESMTP id C2561E85E7 for ; Wed, 26 Oct 2016 15:57:57 +0000 (UTC) Received: from svn01-us-west.apache.org (localhost [127.0.0.1]) by svn01-us-west.apache.org (ASF Mail Server at svn01-us-west.apache.org) with ESMTP id 10E863A3A46 for ; Wed, 26 Oct 2016 15:57:57 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r999954 [15/27] - in /websites/production/commonsrdf/content: ./ apidocs/ apidocs/org/apache/commons/rdf/api/ apidocs/org/apache/commons/rdf/api/class-use/ apidocs/org/apache/commons/rdf/jena/ apidocs/org/apache/commons/rdf/jena/class-use/ ... Date: Wed, 26 Oct 2016 15:57:54 -0000 To: commits@commonsrdf.incubator.apache.org From: stain@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20161026155757.10E863A3A46@svn01-us-west.apache.org> archived-at: Wed, 26 Oct 2016 15:58:15 -0000 Added: websites/production/commonsrdf/content/apidocs/src-html/org/apache/commons/rdf/rdf4j/RDF4JFactory.html ============================================================================== --- websites/production/commonsrdf/content/apidocs/src-html/org/apache/commons/rdf/rdf4j/RDF4JFactory.html (added) +++ websites/production/commonsrdf/content/apidocs/src-html/org/apache/commons/rdf/rdf4j/RDF4JFactory.html Wed Oct 26 15:57:51 2016 @@ -0,0 +1,673 @@ + + + +Source code + + + +
+
001/**
+002 * Licensed to the Apache Software Foundation (ASF) under one
+003 * or more contributor license agreements. See the NOTICE file
+004 * distributed with this work for additional information
+005 * regarding copyright ownership. The ASF licenses this file
+006 * to you under the Apache License, Version 2.0 (the
+007 * "License"); you may not use this file except in compliance
+008 * with the License.  You may obtain a copy of the License at
+009 *
+010 *     http://www.apache.org/licenses/LICENSE-2.0
+011 *
+012 * Unless required by applicable law or agreed to in writing, software
+013 * distributed under the License is distributed on an "AS IS" BASIS,
+014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+015 * See the License for the specific language governing permissions and
+016 * limitations under the License.
+017 */
+018package org.apache.commons.rdf.rdf4j;
+019
+020import java.util.Arrays;
+021import java.util.EnumSet;
+022import java.util.Objects;
+023import java.util.Set;
+024import java.util.UUID;
+025
+026// To avoid confusion, avoid importing
+027// classes that are in both
+028// commons.rdf and openrdf.model (e.g. IRI, Literal)
+029import org.apache.commons.rdf.api.BlankNode;
+030import org.apache.commons.rdf.api.BlankNodeOrIRI;
+031import org.apache.commons.rdf.api.Dataset;
+032import org.apache.commons.rdf.api.Graph;
+033import org.apache.commons.rdf.api.Quad;
+034import org.apache.commons.rdf.api.RDFTerm;
+035import org.apache.commons.rdf.api.RDFTermFactory;
+036import org.apache.commons.rdf.api.Triple;
+037import org.apache.commons.rdf.api.TripleLike;
+038import org.apache.commons.rdf.rdf4j.impl.InternalRDF4JFactory;
+039import org.eclipse.rdf4j.model.BNode;
+040import org.eclipse.rdf4j.model.Model;
+041import org.eclipse.rdf4j.model.Resource;
+042import org.eclipse.rdf4j.model.Statement;
+043import org.eclipse.rdf4j.model.Value;
+044import org.eclipse.rdf4j.model.ValueFactory;
+045import org.eclipse.rdf4j.model.impl.LinkedHashModel;
+046import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
+047import org.eclipse.rdf4j.repository.Repository;
+048import org.eclipse.rdf4j.repository.RepositoryConnection;
+049import org.eclipse.rdf4j.repository.sail.SailRepository;
+050import org.eclipse.rdf4j.sail.Sail;
+051import org.eclipse.rdf4j.sail.memory.MemoryStore;
+052
+053/**
+054 * RDF4J implementation of RDFTermFactory.
+055 * <p>
+056 * The {@link #RDF4JFactory()} constructor uses a {@link SimpleValueFactory}
+057 * to create corresponding RDF4J {@link Value} instances. Alternatively, this
+058 * factory can be constructed with a different {@link ValueFactory} using
+059 * {@link #RDF4JFactory(ValueFactory)}.
+060 * <p>
+061 * {@link #asRDFTerm(Value)} can be used to convert any RDF4J {@link Value} to
+062 * an RDFTerm. Note that adapted {@link BNode}s are considered equal if they are
+063 * converted with the same {@link RDF4JFactory} instance and have the same
+064 * {@link BNode#getID()}.
+065 * <p>
+066 * {@link #createGraph()} creates a new Graph backed by {@link LinkedHashModel}.
+067 * To use other models, see {@link #asGraph(Model)}.
+068 * <p>
+069 * To adapt a RDF4J {@link Repository} as a {@link Dataset} or {@link Graph},
+070 * use {@link #asDataset(Repository, Option...)} 
+071 * or 
+072 * {@link #asGraph(Repository, Option...)}.
+073 * <p>
+074 * {@link #asTriple(Statement)} can be used to convert a RDF4J {@link Statement}
+075 * to a Commons RDF {@link Triple}, and equivalent {@link #asQuad(Statement)} to
+076 * convert a {@link Quad}.
+077 * <p>
+078 * To convert any {@link Triple} or {@link Quad} to to RDF4J {@link Statement},
+079 * use {@link #asStatement(TripleLike)}. This recognises previously converted
+080 * {@link RDF4JTriple}s and {@link RDF4JQuad}s without re-converting their
+081 * {@link RDF4JTripleLike#asStatement()}.
+082 * <p>
+083 * Likewise, {@link #asValue(RDFTerm)} can be used to convert any Commons RDF
+084 * {@link RDFTerm} to a corresponding RDF4J {@link Value}. This recognises
+085 * previously converted {@link RDF4JTerm}s without re-converting their
+086 * {@link RDF4JTerm#asValue()}.
+087 * <p>
+088 * For the purpose of {@link BlankNode} equivalence, this factory contains an
+089 * internal {@link UUID} salt that is used by adapter methods like
+090 * {@link #asQuad(Statement)}, {@link #asTriple(Statement)},
+091 * {@link #asRDFTerm(Value)} as well as {@link #createBlankNode(String)}. As
+092 * RDF4J {@link BNode} instances from multiple repositories or models may have
+093 * the same {@link BNode#getID()}, converting them with the above methods might
+094 * cause accidental {@link BlankNode} equivalence. Note that the {@link Graph}
+095 * and {@link Dataset} adapter methods like
+096 * {@link #asDataset(Repository, Option...)}
+097 * and 
+098 * {@link #asGraph(Repository, Option...)}
+099 * therefore uses a unique {@link RDF4JFactory} internally.
+100 *
+101 */
+102public final class RDF4JFactory implements RDFTermFactory {
+103
+104        /**
+105         * InternalRDF4JFactory is deliberately abstract
+106         */
+107        private static InternalRDF4JFactory rdf4j = new InternalRDF4JFactory() {
+108        };
+109
+110        public enum Option { 
+111                /** 
+112                 * The Graph/Dataset should include any inferred statements 
+113                 */
+114                includeInferred,
+115                /**
+116                 * The graph/dataset should handle {@link Repository#initialize()} (if
+117                 * needed) and {@link Repository#shutDown()} on {@link Graph#close()} /
+118                 * {@link Dataset#close()}.
+119                 */
+120                handleInitAndShutdown
+121        }
+122
+123        private final UUID salt;
+124
+125        private final ValueFactory valueFactory;
+126
+127        /**
+128         * Construct an {@link RDF4JFactory}.
+129         * 
+130         */
+131        public RDF4JFactory() {
+132                this(SimpleValueFactory.getInstance(), UUID.randomUUID());
+133        }
+134
+135        /**
+136         * Construct an {@link RDF4JFactory}.
+137         * <p>
+138         * This constructor is intended for use with the value factory from
+139         * {@link Repository#getValueFactory()} when using 
+140         * Repository-based graphs and datasets.
+141         * 
+142         * @param valueFactory
+143         *            The RDF4J {@link ValueFactory} to use
+144         */
+145        public RDF4JFactory(ValueFactory valueFactory) {
+146                this(valueFactory, UUID.randomUUID());
+147        }
+148
+149        /**
+150         * Construct an {@link RDF4JFactory}.
+151         * <p>
+152         * This constructor may be used if reproducible
+153         * {@link BlankNode#uniqueReference()} in {@link BlankNode} is desirable.
+154         * 
+155         * @param salt
+156         *            An {@link UUID} salt to be used by any created
+157         *            {@link BlankNode}s for the purpose of
+158         *            {@link BlankNode#uniqueReference()}
+159         */     
+160        public RDF4JFactory(UUID salt) {
+161                this(SimpleValueFactory.getInstance(), salt);
+162        }
+163        /**
+164         * Construct an {@link RDF4JFactory}.
+165         * <p>
+166         * This constructor may be used if reproducible
+167         * {@link BlankNode#uniqueReference()} in {@link BlankNode} is desirable.
+168         * 
+169         * @param valueFactory
+170         *            The RDF4J {@link ValueFactory} to use
+171         * @param salt
+172         *            An {@link UUID} salt to be used by any created
+173         *            {@link BlankNode}s for the purpose of
+174         *            {@link BlankNode#uniqueReference()}
+175         */     
+176        public RDF4JFactory(ValueFactory valueFactory, UUID salt) {
+177                this.valueFactory = valueFactory;
+178                this.salt = salt;
+179        }
+180
+181        /**
+182         * Adapt a RDF4J {@link Statement} as a Commons RDF {@link Quad}.
+183         * <p>
+184         * For the purpose of {@link BlankNode} equivalence, this method will use an
+185         * internal salt UUID that is unique per instance of
+186         * {@link RDF4JFactory}.
+187         * <p>
+188         * <strong>NOTE:</strong> If combining RDF4J {@link Statement}s multiple
+189         * repositories or models, then their {@link BNode}s may have the same
+190         * {@link BNode#getID()}, which with this method would become equivalent
+191         * according to {@link BlankNode#equals(Object)} and
+192         * {@link BlankNode#uniqueReference()}, unless a separate
+193         * {@link RDF4JFactory} instance is used per RDF4J repository/model.
+194         *
+195         * @param statement
+196         *            The statement to convert
+197         * @return A {@link RDF4JQuad} that is equivalent to the statement
+198         */
+199        public RDF4JQuad asQuad(final Statement statement) {
+200                return rdf4j.createQuadImpl(statement, salt);
+201        }
+202
+203        /**
+204         *
+205         * Adapt a RDF4J {@link Value} as a Commons RDF {@link RDFTerm}.
+206         * <p>
+207         * The value will be of the same kind as the term, e.g. a
+208         * {@link org.eclipse.rdf4j.model.BNode} is converted to a
+209         * {@link org.apache.commons.rdf.api.BlankNode}, a
+210         * {@link org.eclipse.rdf4j.model.IRI} is converted to a
+211         * {@link org.apache.commons.rdf.api.IRI} and a
+212         * {@link org.eclipse.rdf4j.model.Literal}. is converted to a
+213         * {@link org.apache.commons.rdf.api.Literal}
+214         * <p>
+215         * For the purpose of {@link BlankNode} equivalence, this method will use an
+216         * internal salt UUID that is unique per instance of
+217         * {@link RDF4JFactory}.
+218         * <p>
+219         * <strong>NOTE:</strong> If combining RDF4J values from multiple
+220         * repositories or models, then their {@link BNode}s may have the same
+221         * {@link BNode#getID()}, which with this method would become equivalent
+222         * according to {@link BlankNode#equals(Object)} and
+223         * {@link BlankNode#uniqueReference()}, unless a separate
+224         * {@link RDF4JFactory} instance is used per RDF4J repository/model.
+225         *
+226         * @param value
+227         *            The RDF4J {@link Value} to convert.
+228         * @return A {@link RDFTerm} that corresponds to the RDF4J value
+229         * @throws IllegalArgumentException
+230         *             if the value is not a BNode, Literal or IRI
+231         */
+232        public RDF4JTerm asRDFTerm(Value value) {
+233                return asRDFTerm(value, salt);
+234        }
+235        
+236        /**
+237         * Adapt a RDF4J {@link Value} as a Commons RDF {@link RDFTerm}.
+238         * <p>
+239         * The value will be of the same kind as the term, e.g. a
+240         * {@link org.eclipse.rdf4j.model.BNode} is converted to a
+241         * {@link org.apache.commons.rdf.api.BlankNode}, a
+242         * {@link org.eclipse.rdf4j.model.IRI} is converted to a
+243         * {@link org.apache.commons.rdf.api.IRI} and a
+244         * {@link org.eclipse.rdf4j.model.Literal}. is converted to a
+245         * {@link org.apache.commons.rdf.api.Literal}
+246         *
+247         * @param value
+248         *            The RDF4J {@link Value} to convert.
+249         * @param salt
+250         *            A {@link UUID} salt to use for uniquely mapping any
+251         *            {@link BNode}s. The salt should typically be the same for
+252         *            multiple statements in the same {@link Repository} or
+253         *            {@link Model} to ensure {@link BlankNode#equals(Object)} and
+254         *            {@link BlankNode#uniqueReference()} works as intended.
+255         * @return A {@link RDFTerm} that corresponds to the RDF4J value
+256         * @throws IllegalArgumentException
+257         *             if the value is not a BNode, Literal or IRI
+258         */
+259        public static RDF4JTerm asRDFTerm(final Value value, UUID salt) {
+260                if (value instanceof BNode) {
+261                        return rdf4j.createBlankNodeImpl((BNode) value, salt);
+262                }
+263                if (value instanceof org.eclipse.rdf4j.model.Literal) {
+264                        return rdf4j.createLiteralImpl((org.eclipse.rdf4j.model.Literal) value);
+265                }
+266                if (value instanceof org.eclipse.rdf4j.model.IRI) {
+267                        return rdf4j.createIRIImpl((org.eclipse.rdf4j.model.IRI) value);
+268                }
+269                throw new IllegalArgumentException("Value is not a BNode, Literal or IRI: " + value.getClass());
+270        }       
+271
+272        /**
+273         * Adapt an RDF4J {@link Repository} as a Commons RDF {@link Dataset}.
+274         * <p>
+275         * Changes to the dataset are reflected in the repository, and vice versa.
+276         * <p>
+277         * <strong>Note:</strong> Some operations on the {@link RDF4JDataset}
+278         * requires the use of try-with-resources to close underlying
+279         * {@link RepositoryConnection}s, including
+280         * {@link RDF4JDataset#iterate()}, 
+281         * {@link RDF4JDataset#stream()} and {@link RDF4JDataset#getGraphNames()}.
+282         *
+283         * @param repository
+284         *            RDF4J {@link Repository} to connect to.
+285         * @param options
+286         *            Zero or more {@link Option}
+287         * @return A {@link Dataset} backed by the RDF4J repository.
+288         */
+289        public RDF4JDataset asDataset(Repository repository, Option... options) {
+290                EnumSet<Option> opts = optionSet(options);
+291                return rdf4j.createRepositoryDatasetImpl(repository, 
+292                                opts.contains(Option.handleInitAndShutdown), 
+293                                opts.contains(Option.includeInferred));
+294        }
+295
+296        /**
+297         * Adapt an RDF4J {@link Model} as a Commons RDF {@link Graph}.
+298         * <p>
+299         * Changes to the graph are reflected in the model, and vice versa.
+300         *
+301         * @param model
+302         *            RDF4J {@link Model} to adapt.
+303         * @return Adapted {@link Graph}.
+304         */
+305        public RDF4JGraph asGraph(Model model) {
+306                return rdf4j.createModelGraphImpl(model, this);
+307        }
+308
+309        /**
+310         * Adapt an RDF4J {@link Repository} as a Commons RDF {@link Graph}.
+311         * <p>
+312         * The graph will only include triples in the default graph (equivalent to
+313         * context <code>new Resource[0]{null})</code> in RDF4J).
+314         * <p>
+315         * Changes to the graph are reflected in the repository, and vice versa.
+316         * <p>
+317         * <strong>Note:</strong> Some operations on the {@link RDF4JGraph}
+318         * requires the use of try-with-resources to close underlying
+319         * {@link RepositoryConnection}s, including
+320         * {@link RDF4JGraph#iterate()} and 
+321         * {@link RDF4JGraph#stream()}.
+322         *
+323         * @param repository
+324         *            RDF4J {@link Repository} to connect to.
+325         * @param options
+326         *            Zero or more {@link Option}
+327         * @return A {@link Graph} backed by the RDF4J repository.
+328         */
+329        public RDF4JGraph asGraph(Repository repository, Option... options) {
+330                EnumSet<Option> opts = optionSet(options);
+331                return rdf4j.createRepositoryGraphImpl(repository, 
+332                                opts.contains(Option.handleInitAndShutdown), 
+333                                opts.contains(Option.includeInferred), 
+334                                new Resource[]{null}); // default graph
+335        }
+336
+337        /**
+338         * Adapt an RDF4J {@link Repository} as a Commons RDF {@link Graph}.
+339         * <p>
+340         * The graph will include triples in any contexts (e.g. the union graph).
+341         * <p>
+342         * Changes to the graph are reflected in the repository, and vice versa.
+343         *
+344         * @param repository
+345         *            RDF4J {@link Repository} to connect to.
+346         * @param options
+347         *            Zero or more {@link Option}
+348         * @return A union {@link Graph} backed by the RDF4J repository.
+349         */
+350        public RDF4JGraph asGraphUnion(Repository repository, Option... options) {
+351                EnumSet<Option> opts = optionSet(options);
+352                return rdf4j.createRepositoryGraphImpl(repository, 
+353                                opts.contains(Option.handleInitAndShutdown), 
+354                                opts.contains(Option.includeInferred),
+355                                new Resource[]{}); // union graph 
+356                
+357        }
+358
+359        /**
+360         * Adapt an RDF4J {@link Repository} as a Commons RDF {@link Graph}.
+361         * <p>
+362         * The graph will include triples in the specified contexts.
+363         * <p>
+364         * Changes to the graph are reflected in the repository, and vice versa.
+365         * Triples added/removed to the graph are reflected in all the specified
+366         * contexts.
+367         * <p>
+368         * <strong>Note:</strong> Some operations on the {@link RDF4JGraph}
+369         * requires the use of try-with-resources to close underlying
+370         * {@link RepositoryConnection}s, including
+371         * {@link RDF4JGraph#iterate()} and 
+372         * {@link RDF4JGraph#stream()}.
+373         *
+374         * @param repository
+375         *            RDF4J {@link Repository} to connect to.
+376         * @param contexts
+377         *            A {@link Set} of {@link BlankNodeOrIRI} specifying the graph
+378         *            names to use as a context. The set may include the value
+379         *            <code>null</code> to indicate the default graph. The empty set
+380         *            indicates any context, e.g. the <em>union graph</em>.
+381         * @param option
+382         *            Zero or more {@link Option}s
+383         * @return A {@link Graph} backed by the RDF4J repository.
+384         */
+385        public RDF4JGraph asGraph(Repository repository, Set<? extends BlankNodeOrIRI> contexts,
+386                        Option... option) {
+387                EnumSet<Option> opts = optionSet(option);
+388                /** NOTE: asValue() deliberately CAN handle <code>null</code> */
+389                Resource[] resources = contexts.stream().map(g -> (Resource) asValue(g)).toArray(Resource[]::new);
+390                return rdf4j.createRepositoryGraphImpl(Objects.requireNonNull(repository), 
+391                                opts.contains(Option.handleInitAndShutdown),
+392                                opts.contains(Option.includeInferred),
+393                                resources);
+394        }
+395
+396        /**
+397         * Adapt a Commons RDF {@link Triple} or {@link Quad} as a RDF4J
+398         * {@link Statement}.
+399         * <p>
+400         * If the <code>tripleLike</code> argument is an {@link RDF4JTriple} or a
+401         * {@link RDF4JQuad}, then its {@link RDF4JTripleLike#asStatement()} is
+402         * returned as-is. Note that this means that a {@link RDF4JTriple} would
+403         * preserve its {@link Statement#getContext()}, and that any
+404         * {@link BlankNode}s would be deemed equivalent in RDF4J if they have the
+405         * same {@link BNode#getID()}.
+406         *
+407         * @param tripleLike
+408         *            A {@link Triple} or {@link Quad} to adapt
+409         * @return A corresponding {@link Statement}
+410         */
+411        public Statement asStatement(TripleLike tripleLike) {
+412                if (tripleLike instanceof RDF4JTripleLike) {
+413                        // Return original statement - this covers both RDF4JQuad and
+414                        // RDF4JTriple
+415                        return ((RDF4JTripleLike) tripleLike).asStatement();
+416                }
+417
+418                org.eclipse.rdf4j.model.Resource subject = (org.eclipse.rdf4j.model.Resource) asValue(tripleLike.getSubject());
+419                org.eclipse.rdf4j.model.IRI predicate = (org.eclipse.rdf4j.model.IRI) asValue(tripleLike.getPredicate());
+420                Value object = asValue(tripleLike.getObject());
+421
+422                org.eclipse.rdf4j.model.Resource context = null;
+423                if (tripleLike instanceof Quad) {
+424                        Quad quad = (Quad) tripleLike;
+425                        context = (org.eclipse.rdf4j.model.Resource) asValue(quad.getGraphName().orElse(null));
+426                }
+427
+428                return getValueFactory().createStatement(subject, predicate, object, context);
+429        }
+430
+431        /**
+432         * Adapt a RDF4J {@link Statement} as a Commons RDF {@link Triple}.
+433         * <p>
+434         * For the purpose of {@link BlankNode} equivalence, this method will use an
+435         * internal salt UUID that is unique per instance of
+436         * {@link RDF4JFactory}.
+437         * <p>
+438         * <strong>NOTE:</strong> If combining RDF4J statements from multiple
+439         * repositories or models, then their {@link BNode}s may have the same
+440         * {@link BNode#getID()}, which with this method would become equivalent
+441         * according to {@link BlankNode#equals(Object)} and
+442         * {@link BlankNode#uniqueReference()}, unless a separate
+443         * {@link RDF4JFactory} instance is used per RDF4J repository/model.
+444         *
+445         * @param statement
+446         *            The RDF4J {@link Statement} to adapt.
+447         * @return A {@link RDF4JTriple} that is equivalent to the statement
+448         */
+449        public RDF4JTriple asTriple(final Statement statement) {
+450                return rdf4j.createTripleImpl(statement, salt);
+451        }
+452
+453        /**
+454         * Adapt a Commons RDF {@link RDFTerm} as a RDF4J {@link Value}.
+455         * <p>
+456         * The value will be of the same kind as the term, e.g. a
+457         * {@link org.apache.commons.rdf.api.BlankNode} is converted to a
+458         * {@link org.eclipse.rdf4j.model.BNode}, a
+459         * {@link org.apache.commons.rdf.api.IRI} is converted to a
+460         * {@link org.eclipse.rdf4j.model.IRI} and a
+461         * {@link org.apache.commons.rdf.api.Literal} is converted to a
+462         * {@link org.eclipse.rdf4j.model.Literal}.
+463         * <p>
+464         * If the provided {@link RDFTerm} is <code>null</code>, then the returned
+465         * value is <code>null</code>.
+466         * <p>
+467         * If the provided term is an instance of {@link RDF4JTerm}, then the
+468         * {@link RDF4JTerm#asValue()} is returned without any conversion. Note that
+469         * this could mean that a {@link Value} from a different kind of
+470         * {@link ValueFactory} could be returned.
+471         *
+472         * @param term
+473         *            RDFTerm to adapt to RDF4J Value
+474         * @return Adapted RDF4J {@link Value}
+475         */
+476        public Value asValue(RDFTerm term) {
+477                if (term == null) {
+478                        return null;
+479                }
+480                if (term instanceof RDF4JTerm) {
+481                        // One of our own - avoid converting again.
+482                        // (This is crucial to avoid double-escaping in BlankNode)
+483                        return ((RDF4JTerm) term).asValue();
+484                }
+485                if (term instanceof org.apache.commons.rdf.api.IRI) {
+486                        org.apache.commons.rdf.api.IRI iri = (org.apache.commons.rdf.api.IRI) term;
+487                        return getValueFactory().createIRI(iri.getIRIString());
+488                }
+489                if (term instanceof org.apache.commons.rdf.api.Literal) {
+490                        org.apache.commons.rdf.api.Literal literal = (org.apache.commons.rdf.api.Literal) term;
+491                        String label = literal.getLexicalForm();
+492                        if (literal.getLanguageTag().isPresent()) {
+493                                String lang = literal.getLanguageTag().get();
+494                                return getValueFactory().createLiteral(label, lang);
+495                        }
+496                        org.eclipse.rdf4j.model.IRI dataType = (org.eclipse.rdf4j.model.IRI) asValue(literal.getDatatype());
+497                        return getValueFactory().createLiteral(label, dataType);
+498                }
+499                if (term instanceof BlankNode) {
+500                        // This is where it gets tricky to support round trips!
+501                        BlankNode blankNode = (BlankNode) term;
+502                        // FIXME: The uniqueReference might not be a valid BlankNode
+503                        // identifier..
+504                        // does it have to be in RDF4J?
+505                        return getValueFactory().createBNode(blankNode.uniqueReference());
+506                }
+507                throw new IllegalArgumentException("RDFTerm was not an IRI, Literal or BlankNode: " + term.getClass());
+508        }
+509
+510        @Override
+511        public RDF4JBlankNode createBlankNode() {
+512                BNode bnode = getValueFactory().createBNode();
+513                return (RDF4JBlankNode) asRDFTerm(bnode);
+514        }
+515
+516        @Override
+517        public RDF4JBlankNode createBlankNode(String name) {
+518                BNode bnode = getValueFactory().createBNode(name);
+519                return (RDF4JBlankNode) asRDFTerm(bnode);
+520        }
+521        
+522        /**
+523         * {@inheritDoc}
+524         * <p>
+525         * <strong>Note:</strong> Some operations on the {@link RDF4JDataset}
+526         * requires the use of try-with-resources to close underlying
+527         * {@link RepositoryConnection}s, including
+528         * {@link RDF4JDataset#iterate()}, 
+529         * {@link RDF4JDataset#stream()} and {@link RDF4JDataset#getGraphNames()}.
+530         * 
+531         */
+532        @Override
+533        public RDF4JDataset createDataset() {
+534                Sail sail = new MemoryStore();
+535                Repository repository = new SailRepository(sail);
+536                return rdf4j.createRepositoryDatasetImpl(repository, true, false);
+537        }
+538
+539        @Override
+540        public RDF4JGraph createGraph() {
+541                return asGraph(new LinkedHashModel());
+542        }
+543
+544        @Override
+545        public RDF4JIRI createIRI(String iri) throws IllegalArgumentException {
+546                return (RDF4JIRI) asRDFTerm(getValueFactory().createIRI(iri));
+547        }
+548
+549        @Override
+550        public RDF4JLiteral createLiteral(String lexicalForm)
+551                        throws IllegalArgumentException {
+552                org.eclipse.rdf4j.model.Literal lit = getValueFactory().createLiteral(lexicalForm);
+553                return (RDF4JLiteral) asRDFTerm(lit);
+554        }
+555
+556        @Override
+557        public org.apache.commons.rdf.api.Literal createLiteral(String lexicalForm, org.apache.commons.rdf.api.IRI dataType)
+558                        throws IllegalArgumentException {
+559                org.eclipse.rdf4j.model.IRI iri = getValueFactory().createIRI(dataType.getIRIString());
+560                org.eclipse.rdf4j.model.Literal lit = getValueFactory().createLiteral(lexicalForm, iri);
+561                return (org.apache.commons.rdf.api.Literal) asRDFTerm(lit);
+562        }
+563
+564        @Override
+565        public org.apache.commons.rdf.api.Literal createLiteral(String lexicalForm, String languageTag)
+566                        throws IllegalArgumentException {
+567                org.eclipse.rdf4j.model.Literal lit = getValueFactory().createLiteral(lexicalForm, languageTag);
+568                return (org.apache.commons.rdf.api.Literal) asRDFTerm(lit);
+569        }
+570
+571        @Override
+572        public RDF4JTriple createTriple(BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate, RDFTerm object)
+573                        throws IllegalArgumentException {
+574                final Statement statement = getValueFactory().createStatement(
+575                                (org.eclipse.rdf4j.model.Resource) asValue(subject), (org.eclipse.rdf4j.model.IRI) asValue(predicate),
+576                                asValue(object));
+577                return asTriple(statement);
+578        }
+579
+580        @Override
+581        public Quad createQuad(BlankNodeOrIRI graphName, BlankNodeOrIRI subject, org.apache.commons.rdf.api.IRI predicate,
+582                        RDFTerm object) throws IllegalArgumentException {
+583                final Statement statement = getValueFactory().createStatement(
+584                                (org.eclipse.rdf4j.model.Resource) asValue(subject), (org.eclipse.rdf4j.model.IRI) asValue(predicate),
+585                                asValue(object), (org.eclipse.rdf4j.model.Resource) asValue(graphName));
+586                return asQuad(statement);
+587        }
+588
+589        public ValueFactory getValueFactory() {
+590                return valueFactory;
+591        }
+592
+593        private EnumSet<Option> optionSet(Option... options) {
+594                EnumSet<Option> opts = EnumSet.noneOf(Option.class);
+595                opts.addAll(Arrays.asList(options));
+596                return opts;
+597        }
+598        
+599        
+600        
+601}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ + \ No newline at end of file