📄 rdfschemarepository.java
字号:
} } protected void _createNewGroundedTriplesTable() throws SQLException { if (_schemaVersion == -1) { _rdbms.executeUpdate( "CREATE TABLE " + NEW_GROUNDED_TRIPLES_TABLE + " (id " + _rdbms.ID_INT + " NOT NULL PRIMARY KEY)"); } } /*-----------------------------------------+ | Overridden methods from RdfRepository | +-----------------------------------------*/ public void clearRepository() throws SailUpdateException { super.clearRepository(); try { _rdbms.clearTable(CLASS_TABLE); _rdbms.clearTable(PROPERTY_TABLE); _rdbms.clearTable(SUBCLASSOF_TABLE); _rdbms.clearTable(DIRECT_SUBCLASSOF_TABLE); _rdbms.clearTable(SUBPROPERTYOF_TABLE); _rdbms.clearTable(DIRECT_SUBPROPERTYOF_TABLE); _rdbms.clearTable(INSTANCEOF_TABLE); _rdbms.clearTable(PROPER_INSTANCEOF_TABLE); _rdbms.clearTable(DOMAIN_TABLE); _rdbms.clearTable(RANGE_TABLE); _rdbms.clearTable(ALL_NEW_TRIPLES_TABLE); _rdbms.clearTable(INFERRED_TABLE); _rdbms.clearTable(ALL_INFERRED_TABLE); if (_useDependencyInferencer) { _rdbms.clearTable(DEPEND_TABLE); _rdbms.clearTable(GROUNDED_TRIPLES_TABLE); _rdbms.clearTable(NEW_GROUNDED_TRIPLES_TABLE); } } catch (SQLException e) { throw new SailInternalException(e); } try { _initRdfSchema(); } catch (SQLException e) { throw new SailInternalException(e); } } /** * Processes the triples from ADDED_TRIPLES_TABLE. This method first * makes any implicit statements from TRIPLES_TABLE that occur in * ADDED_TRIPLES_TABLE explicit. It then copies all triples from * ADDED_TRIPLES_TABLE that are not yet in TRIPLES_TABLE to * NEW_TRIPLES_TABLE. **/ protected void _processAddedTriples() throws SQLException { // Find any implicit statements that should be made explicit now. Connection con = _rdbms.getConnection(); java.sql.Statement st = con.createStatement(); ResultSet rs = st.executeQuery( "SELECT t.id FROM " + TRIPLES_TABLE + " t, " + ADDED_TRIPLES_TABLE + " at " + "WHERE " + "t.subj = at.subj AND " + "t.pred = at.pred AND " + "t.obj = at.obj AND " + "t.explicit = " + _rdbms.FALSE); String[] idChunks = _chunkIdSet(rs, 3500); rs.close(); st.close(); con.setAutoCommit(false); st = con.createStatement(); for (int i = 0; i < idChunks.length; i++) { st.executeUpdate( "UPDATE " + TRIPLES_TABLE + " SET explicit = " + _rdbms.TRUE + " WHERE id IN " + idChunks[i]); } con.commit(); st.close(); con.close(); super._processAddedTriples(); } /** * Updates the inferred statements. **/ protected void _processChangedTriples() throws SQLException { // Collect all new triples in ALL_NEW_TRIPLES_TABLE int count = _rdbms.copyRows(NEW_TRIPLES_TABLE, ALL_NEW_TRIPLES_TABLE); if (count > 0) { // Infer new statements from the added ones. _inferencer.doInferencing(); // Register new dependencies _inferencer.processNewStatements(); } // ALL_NEW_TRIPLES_TABLE now contains all statements, including the // ones that were added by the inferencer. _rdbms.optimizeTable(ALL_NEW_TRIPLES_TABLE); _updateAuxiliaryTables(); _rdbms.clearTable(ALL_NEW_TRIPLES_TABLE); } // Overrides RdfRepository._removeExpiredStatements() protected void _removeExpiredStatements() throws SQLException { _inferencer.removeExpiredStatements(); } protected void _updateAuxiliaryTables() throws SQLException { ThreadLog.trace("Updating auxiliary tables"); _updateClasses(); _updateProperties(); _updateDomains(); _updateRanges(); int addedSubClassesCount = _updateSubClasses(); _updateSubProperties(); _updateInstanceOf( (addedSubClassesCount > 0) ); } // Updates classes. private void _updateClasses() throws SQLException { _updateClassOrProperties(CLASS_TABLE, rdfsClassId); } private void _updateProperties() throws SQLException { _updateClassOrProperties(PROPERTY_TABLE, rdfPropertyId); } private void _updateClassOrProperties(String tableName, int objectId) throws SQLException { // Use ALL_NEW_TRIPLES_TABLE for incremental updates and TRIPLES_TABLE // for complete updates when statements were removed. String sourceTable = ALL_NEW_TRIPLES_TABLE; if (_statementsRemoved) { sourceTable = TRIPLES_TABLE; _rdbms.clearTable(tableName); } int count = _rdbms.executeUpdate( "INSERT INTO " + tableName + " SELECT subj FROM " + sourceTable + " WHERE pred = " + rdfTypeId + " AND obj = " + objectId); if (count > 0 || _statementsRemoved) { _rdbms.optimizeTable(tableName); } } private void _updateDomains() throws SQLException { _updateDomainsOrRanges(DOMAIN_TABLE, rdfsDomainId); } private void _updateRanges() throws SQLException { _updateDomainsOrRanges(RANGE_TABLE, rdfsRangeId); } private void _updateDomainsOrRanges(String tableName, int predicateId) throws SQLException { // Use ALL_NEW_TRIPLES_TABLE for incremental updates and TRIPLES_TABLE // for complete updates when statements were removed. String sourceTable = ALL_NEW_TRIPLES_TABLE; if (_statementsRemoved) { sourceTable = TRIPLES_TABLE; _rdbms.clearTable(tableName); } int count = _rdbms.executeUpdate( "INSERT INTO " + tableName + " SELECT subj, obj FROM " + sourceTable + " WHERE pred = " + predicateId); if (count > 0 || _statementsRemoved) { _rdbms.optimizeTable(tableName); } } private int _updateSubClasses() throws SQLException { return _updateSubClassesOrProperties( SUBCLASSOF_TABLE, DIRECT_SUBCLASSOF_TABLE, rdfsSubClassOfId); } private int _updateSubProperties() throws SQLException { return _updateSubClassesOrProperties( SUBPROPERTYOF_TABLE, DIRECT_SUBPROPERTYOF_TABLE, rdfsSubPropertyOfId); } /* Updates subClassOf or subPropertyOf relations. Strings allTable and * directTable indicates which tables are updated. Decides whether a * subClassOf or subPropertyOf relation is a direct or indirect one. */ private int _updateSubClassesOrProperties( String allTable, String directTable, int propId) throws SQLException { // Use ALL_NEW_TRIPLES_TABLE for incremental updates and TRIPLES_TABLE // for complete updates when statements were removed. String sourceTable = ALL_NEW_TRIPLES_TABLE; if (_statementsRemoved) { sourceTable = TRIPLES_TABLE; _rdbms.clearTable(allTable); } // Add any new relations to allTable. int count = _rdbms.executeUpdate( "INSERT INTO " + allTable + " SELECT subj, obj FROM " + sourceTable + " WHERE pred = " + propId); // The directTable only needs to be updated if new rows were added // to allTale. if (count > 0 || _statementsRemoved) { _rdbms.optimizeTable(allTable); _rdbms.clearTable(directTable); // Create extra, temporary table for storing non-cycles. _rdbms.executeUpdate( "CREATE TABLE noncycles (" + "sub " + _rdbms.ID_INT + " NOT NULL, " + "super " + _rdbms.ID_INT + " NOT NULL, " + "UNIQUE(sub, super))"); // Copy all relations that are not self-references or part of cycles _rdbms.executeUpdate( "INSERT INTO noncycles " + "SELECT s1.* " + "FROM " + allTable + " s1 " + "LEFT JOIN " + allTable + " s2 " + "ON s1.sub = s2.super AND s1.super = s2.sub " + "WHERE s2.sub IS NULL"); _rdbms.optimizeTable("noncycles"); // Copy all direct relations from noncycles to directTable. All // direct relations are all relations A --> C for which no // relations can be found such that A --> B --> C. _rdbms.executeUpdate( "INSERT INTO " + directTable + " SELECT nc1.*" + " FROM noncycles nc1" + " LEFT JOIN noncycles nc2" + " ON nc1.sub = nc2.sub" + " LEFT JOIN noncycles nc3" + " ON nc3.sub = nc2.super AND nc3.super = nc1.super" + " GROUP BY nc1.sub, nc1.super" + " HAVING count(nc3.sub) = 0"); // NULL values don't add to count _rdbms.optimizeTable(directTable); // Drop temporary table. _rdbms.dropTable("noncycles"); } return count; } protected void _updateInstanceOf(boolean newSubClassesAdded) throws SQLException { // Use ALL_NEW_TRIPLES_TABLE for incremental updates and TRIPLES_TABLE // for complete updates when statements were removed. String sourceTable = ALL_NEW_TRIPLES_TABLE; if (_statementsRemoved) { sourceTable = TRIPLES_TABLE; _rdbms.clearTable(INSTANCEOF_TABLE); } // Copy all new rdf:type relations to instanceOf table. int count = _rdbms.executeUpdate( "INSERT INTO " + INSTANCEOF_TABLE + " SELECT subj, obj FROM " + sourceTable + " WHERE pred = " + rdfTypeId); // The proper_instanceof table only needs to be updated if new rows // were added to the instanceof table, or if new subclasses have been // added. if (count > 0 || newSubClassesAdded || _statementsRemoved) { _rdbms.optimizeTable(INSTANCEOF_TABLE); // Clear proper_instanceof table. _rdbms.clearTable(PROPER_INSTANCEOF_TABLE); // Drop index to speed up the proces. // FIXME: do we need to do this? // _rdbms.dropIndex(DIRECT_SUBCLASSOF_TABLE, "super"); // Copy all proper instanceOf relations to proper instanceof table. // Proper instanceOf relations are those relations (a type b) for // which no proper subclass of b can be found of which a is also an // instance. _rdbms.executeUpdate( "INSERT INTO " + PROPER_INSTANCEOF_TABLE + " SELECT type.inst, type.class" + " FROM " + INSTANCEOF_TABLE + " type" + " LEFT JOIN " + INSTANCEOF_TABLE + " subtype" + " ON subtype.inst = type.inst" + " LEFT JOIN " + DIRECT_SUBCLASSOF_TABLE + " dsco" + " ON type.class = dsco.super AND subtype.class = dsco.sub" + " GROUP BY type.inst, type.class" + " HAVING count(dsco.sub) = 0"); _rdbms.optimizeTable(PROPER_INSTANCEOF_TABLE); // Re-create index. // _rdbms.createIndex(DIRECT_SUBCLASSOF_TABLE, "super"); } } /** * A more efficient implementation is possible now that the full RDF * Schema is stored. **/ protected void _updateExportedNamespaces() { ThreadLog.trace("Updating exported namespaces information."); try { // First 'reset' all namespaces that are not user-defined to // non-exported. This eliminates exporting of namespaces // belonging to removed predicates. _rdbms.executeUpdate( "UPDATE " + NAMESPACES_TABLE + " SET export = " + _rdbms.FALSE + " WHERE userDefined = " + _rdbms.FALSE); // Query for all namespaces that need to be exported: those that // are the in subject of statements where the predicate is // rdf:type and the object is rdf:Property. Connection con = _rdbms.getConnection(); java.sql.Statement st = con.createStatement(); ResultSet rs = st.executeQuery( "SELECT DISTINCT r.namespace " + "FROM " + RESOURCES_TABLE + " r, " + TRIPLES_TABLE + " t "+ "WHERE t.subj = r.id " + "AND t.pred = " + rdfTypeId + " " + "AND t.obj = " + rdfPropertyId); String[] idChunks = _chunkIdSet(rs, 3500); rs.close(); st.close(); con.setAutoCommit(false); st = con.createStatement(); for (int i = 0; i < idChunks.length; i++) { st.executeUpdate( "UPDATE " + NAMESPACES_TABLE + " SET export = " + _rdbms.TRUE + " WHERE id IN " + idChunks[i]); } con.commit(); st.close(); con.close(); // Set the export status to 'up-to-date' _setExportStatusUpToDate(true); // re-initialize the namespaces to update the cache. _initNamespaceCache(); } catch (SQLException e) { throw new SailInternalException(e); } }/*-----------------------------+| Methods from RdfSchemaSource |+-----------------------------*/ public StatementIterator getExplicitStatements(Resource subj, URI pred, Value obj) { return super.getStatements(subj, pred, obj, true); } public boolean hasExplicitStatement(Resource subj, URI pred, Value obj) { return super.hasStatement(subj, pred, obj, true); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -