📄 rdfschemarepository.java
字号:
/* Sesame - Storage and Querying architecture for RDF and RDF Schema * Copyright (C) 2001-2005 Aduna * * Contact: * Aduna * Prinses Julianaplein 14 b * 3817 CS Amersfoort * The Netherlands * tel. +33 (0)33 465 99 87 * fax. +33 (0)33 465 99 87 * * http://aduna.biz/ * http://www.openrdf.org/ * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */package org.openrdf.sesame.sailimpl.rdbms;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.util.Map;import org.openrdf.util.log.ThreadLog;import org.openrdf.vocabulary.RDF;import org.openrdf.vocabulary.RDFS;import org.openrdf.model.Resource;import org.openrdf.model.URI;import org.openrdf.model.Value;import org.openrdf.model.impl.URIImpl;import org.openrdf.sesame.sail.LiteralIterator;import org.openrdf.sesame.sail.SailInitializationException;import org.openrdf.sesame.sail.SailInternalException;import org.openrdf.sesame.sail.SailUpdateException;import org.openrdf.sesame.sail.StatementIterator;import org.openrdf.sesame.sail.query.DirectSubClassOf;import org.openrdf.sesame.sail.query.DirectSubPropertyOf;import org.openrdf.sesame.sail.query.DirectType;import org.openrdf.sesame.sail.query.PathExpression;import org.openrdf.sesame.sail.query.TriplePattern;import org.openrdf.sesame.sail.util.EmptyLiteralIterator;import org.openrdf.sesame.sail.util.EmptyStatementIterator;import org.openrdf.sesame.sailimpl.rdbms.iterators.RdbmsLiteralIterator;import org.openrdf.sesame.sailimpl.rdbms.iterators.RdbmsStatementIterator;/** * A portable implementation of the RdfSchemaRepository interface for * relational databases. This class defines the RDF Schema specific * methods and takes care of RDF Model Theory inferencing. The superclasses * define the basic RDF functionality. * * @author Arjohn Kampman * @version $Revision: 1.15.4.1 $ */public class RdfSchemaRepository extends RdfRepository implements org.openrdf.sesame.sail.RdfSchemaRepository, TableNames{/*-----------+| Variables |+-----------*/ /** * Key used to store a fully qualified inferencer class name in the * initialization parameters. **/ public static final String INFERENCER_KEY = "use-inferencer"; public static final String USE_DEPENDENCY_INFERENCER_KEY = "dependency-inferencing"; protected InferenceServices _inferencer = null; protected boolean _useDependencyInferencer = true; // Id 's of standard RDF primitives. public int rdfTypeId; public int rdfPropertyId; public int rdfXMLLiteralId; public int rdfStatementId; public int rdfSubjectId; public int rdfPredicateId; public int rdfObjectId; public int rdfAltId; public int rdfBagId; public int rdfSeqId; public int rdfListId; public int rdfFirstId; public int rdfRestId; public int rdfNilId; public int rdfValueId; // Id 's of standard RDF Schema primitives. public int rdfsResourceId; public int rdfsLiteralId; public int rdfsClassId; public int rdfsSubClassOfId; public int rdfsSubPropertyOfId; public int rdfsDomainId; public int rdfsRangeId; public int rdfsCommentId; public int rdfsLabelId; public int rdfsDatatypeId; public int rdfsContainerId; public int rdfsMemberId; public int rdfsContainerMembershipPropertyId; public int rdfsIsDefinedById; public int rdfsSeeAlsoId;/*----------------+| Constructors |+----------------*/ public RdfSchemaRepository() { super(); }/*--------------------------+| Database initialization |+--------------------------*/ // overrides RdfSource.initialize(Map) public void initialize(Map configParams) throws SailInitializationException { ThreadLog.trace("Initializing RdfSchemaRepository..."); String inferencerClass = (String)configParams.get(INFERENCER_KEY); // check if the dependency inferencer is supposed to be used. Default // is that it _is_ used. String useDependencyInferencer = (String)configParams.get(USE_DEPENDENCY_INFERENCER_KEY); _useDependencyInferencer = !("no".equalsIgnoreCase(useDependencyInferencer)); ThreadLog.trace("Dependency inferencer used: " + _useDependencyInferencer); if (inferencerClass != null) { try { _inferencer = (InferenceServices)Class.forName(inferencerClass).newInstance(); _inferencer.setDependencyInferencer(_useDependencyInferencer); ThreadLog.trace("Using inferencer: " + inferencerClass); } catch (InstantiationException e) { throw new SailInternalException(e); } catch (IllegalAccessException e) { throw new SailInternalException(e); } catch (ClassNotFoundException e) { throw new SailInternalException(e); } catch (ClassCastException e) { throw new SailInternalException( "The provided class '" + inferencerClass + "' does not implement the '" + InferenceServices.class.getName()+ "' interface", e); } } if (_inferencer == null) { ThreadLog.trace("No inferencer specified, using default RDF MT inferencer"); _inferencer = new RdbmsInferenceServices(_useDependencyInferencer); } // initialize the inferencer _inferencer.initialize(this, configParams); super.initialize(configParams); _inferencer.afterInitialize(); try { _initRdfSchema(); } catch (SQLException e) { throw new SailInitializationException(e); } ThreadLog.trace("RdfSchemaRepository initialized."); } protected void _initRdfSchema() throws SQLException { _initRdfPrimitives(); _inferencer.initRdfSchema(); // ALL_NEW_TRIPLES_TABLE now contains all axioms and statements // that can be derived from the axioms. _rdbms.optimizeTable(ALL_NEW_TRIPLES_TABLE); // Mark these triples as axioms _inferencer.markAxioms(); // Update the auxiliary tables _updateAuxiliaryTables(); _rdbms.clearTable(ALL_NEW_TRIPLES_TABLE); // Set the export status to 'dirty' _setExportStatusUpToDate(false); } protected void _initRdfPrimitives() { // Get the IDs of RDF primitives rdfTypeId = _insertURI(URIImpl.RDF_TYPE); rdfPropertyId = _insertURI(URIImpl.RDF_PROPERTY); rdfXMLLiteralId = _insertURI(URIImpl.RDF_XMLLITERAL); rdfSubjectId = _insertURI(URIImpl.RDF_SUBJECT); rdfPredicateId = _insertURI(URIImpl.RDF_PREDICATE); rdfObjectId = _insertURI(URIImpl.RDF_OBJECT); rdfStatementId = _insertURI(URIImpl.RDF_STATEMENT); rdfAltId = _insertURI(URIImpl.RDF_ALT); rdfBagId = _insertURI(URIImpl.RDF_BAG); rdfSeqId = _insertURI(URIImpl.RDF_SEQ); rdfListId = _insertURI(URIImpl.RDF_LIST); rdfFirstId = _insertURI(URIImpl.RDF_FIRST); rdfRestId = _insertURI(URIImpl.RDF_REST); rdfNilId = _insertURI(URIImpl.RDF_NIL); rdfValueId = _insertURI(URIImpl.RDF_VALUE); // Get the IDs of RDF Schema primitives rdfsResourceId = _insertURI(URIImpl.RDFS_RESOURCE); rdfsClassId = _insertURI(URIImpl.RDFS_CLASS); rdfsLiteralId = _insertURI(URIImpl.RDFS_LITERAL); rdfsSubClassOfId = _insertURI(URIImpl.RDFS_SUBCLASSOF); rdfsSubPropertyOfId = _insertURI(URIImpl.RDFS_SUBPROPERTYOF); rdfsDomainId = _insertURI(URIImpl.RDFS_DOMAIN); rdfsRangeId = _insertURI(URIImpl.RDFS_RANGE); rdfsCommentId = _insertURI(URIImpl.RDFS_COMMENT); rdfsLabelId = _insertURI(URIImpl.RDFS_LABEL); rdfsIsDefinedById = _insertURI(URIImpl.RDFS_ISDEFINEDBY); rdfsSeeAlsoId = _insertURI(URIImpl.RDFS_SEEALSO); rdfsDatatypeId = _insertURI(URIImpl.RDFS_DATATYPE); rdfsContainerId = _insertURI(URIImpl.RDFS_CONTAINER); rdfsMemberId = _insertURI(URIImpl.RDFS_MEMBER); rdfsContainerMembershipPropertyId = _insertURI(URIImpl.RDFS_CONTAINERMEMBERSHIPPROPERTY); // Try setting the namespace prefixes try { changeNamespacePrefix(RDF.NAMESPACE, "rdf"); } catch (SailUpdateException e) { // FIXME /* ignore */ } try { changeNamespacePrefix(RDFS.NAMESPACE, "rdfs"); } catch (SailUpdateException e) { // FIXME /* ignore */ } } protected int _insertURI(URI r) { // Check if the resource is already in RESOURCES_TABLE int id = _getURIId(r); if (id == 0) { // resource not yet in the table. id = _getNextResourceId(); int nsId = _createIdForNamespace(r.getNamespace()); String lname = _rdbms.escapeString(r.getLocalName()); try { _rdbms.executeUpdate( "INSERT INTO " + RESOURCES_TABLE + " VALUES(" + id + ", " + nsId + ", '" + lname + "')"); } catch (SQLException e) { throw new SailInternalException(e); } } return id; } protected void _createDbSchema() throws SQLException { super._createDbSchema(); // Tables for specific RDF primitives if (_schemaVersion == -1) { _createOneIdColumnTable(CLASS_TABLE, "id"); _createOneIdColumnTable(PROPERTY_TABLE, "id"); _createTwoIdColumnsTable(SUBCLASSOF_TABLE, "sub", "super"); _createTwoIdColumnsTable(DIRECT_SUBCLASSOF_TABLE, "sub", "super"); _createTwoIdColumnsTable(SUBPROPERTYOF_TABLE, "sub", "super"); _createTwoIdColumnsTable(DIRECT_SUBPROPERTYOF_TABLE, "sub", "super"); _createTwoIdColumnsTable(INSTANCEOF_TABLE, "inst", "class"); _createTwoIdColumnsTable(PROPER_INSTANCEOF_TABLE, "inst", "class"); _createTwoIdColumnsTable(DOMAIN_TABLE, "property", "class"); _createTwoIdColumnsTable(RANGE_TABLE, "property", "class"); } // Tables used during inferencing _createAllNewTriplesTable(); _createInferredTriplesTable(); _createAllInferredTriplesTable(); if (_useDependencyInferencer) { _createDependenciesTable(); // Tables used for the Truth Maintenance System _createGroundedTriplesTable(); _createNewGroundedTriplesTable(); } } /** * Creates a table with the specified name and having one column of type * ID_INT with name <tt>colName</tt>. The column is part of a primary key. * This method is used to create the CLASS_TABLE and PROPERTY_TABLE. * * @param tableName the name of the table. * @param colName the name of the column. **/ protected void _createOneIdColumnTable(String tableName, String colName) throws SQLException { _rdbms.executeUpdate( "CREATE TABLE " + tableName + " (" + colName + " " + _rdbms.ID_INT + " NOT NULL PRIMARY KEY)"); } /** * Creates a table with the specified name and having two columns of type * ID_INT: <tt>col1</tt> and <tt>col2</tt>. Both columns are part of the * primary key, and an additional index is created for the second column. * This method is used to create the SUBCLASSOF_TABLE, DIRECT_SUBCLASSOF_TABLE, * SUBPROPERTYOF_TABLE, DIRECT_SUBPROPERTYOF_TABLE, INSTANCEOF_TABLE, * PROPER_INSTANCEOF_TABLE, DOMAIN_TABLE and RANGE_TABLE. * * @param tableName the name of the table. * @param col1 the name of the first column. * @param col2 the name of the second column. **/ protected void _createTwoIdColumnsTable( String tableName, String col1, String col2) throws SQLException { _rdbms.executeUpdate( "CREATE TABLE " + tableName + " (" + col1 + " " + _rdbms.ID_INT + " NOT NULL, " + col2 + " " + _rdbms.ID_INT + " NOT NULL, " + "PRIMARY KEY(" + col1 + ", " + col2 + "))"); // Create an index over col2 _rdbms.createIndex(tableName, col2); } protected void _createAllNewTriplesTable() throws SQLException { if (_schemaVersion == -1) { _createTriplesTable(ALL_NEW_TRIPLES_TABLE, true); _rdbms.createIndex(ALL_NEW_TRIPLES_TABLE, "subj"); _rdbms.createIndex(ALL_NEW_TRIPLES_TABLE, "pred"); _rdbms.createIndex(ALL_NEW_TRIPLES_TABLE, "obj"); } else if (_schemaVersion < 4) { _rdbms.renameTableColumn(ALL_NEW_TRIPLES_TABLE, "subject" , "subj", _rdbms.ID_INT+" NOT NULL"); _rdbms.renameTableColumn(ALL_NEW_TRIPLES_TABLE, "predicate", "pred", _rdbms.ID_INT+" NOT NULL"); _rdbms.renameTableColumn(ALL_NEW_TRIPLES_TABLE, "object" , "obj" , _rdbms.ID_INT+" NOT NULL"); } } protected void _createInferredTriplesTable() throws SQLException { if (_schemaVersion == -1) { _createTriplesTable(INFERRED_TABLE, false); } else if (_schemaVersion < 4) { _rdbms.renameTableColumn(INFERRED_TABLE, "subject" , "subj", _rdbms.ID_INT+" NOT NULL"); _rdbms.renameTableColumn(INFERRED_TABLE, "predicate", "pred", _rdbms.ID_INT+" NOT NULL"); _rdbms.renameTableColumn(INFERRED_TABLE, "object" , "obj" , _rdbms.ID_INT+" NOT NULL"); } } protected void _createAllInferredTriplesTable() throws SQLException { if (_schemaVersion == -1) { _createTriplesTable(ALL_INFERRED_TABLE, false); } else if (_schemaVersion < 4) { _rdbms.renameTableColumn(ALL_INFERRED_TABLE, "subject" , "subj", _rdbms.ID_INT+" NOT NULL"); _rdbms.renameTableColumn(ALL_INFERRED_TABLE, "predicate", "pred", _rdbms.ID_INT+" NOT NULL"); _rdbms.renameTableColumn(ALL_INFERRED_TABLE, "object" , "obj" , _rdbms.ID_INT+" NOT NULL"); } } protected void _createDependenciesTable() throws SQLException { if (_schemaVersion == -1) { _inferencer.createDependenciesTable(); } } protected void _createGroundedTriplesTable() throws SQLException { if (_schemaVersion == -1) { _rdbms.executeUpdate( "CREATE TABLE " + GROUNDED_TRIPLES_TABLE + " (id " + _rdbms.ID_INT + " NOT NULL PRIMARY KEY)");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -