📄 custominferenceservices.java
字号:
/* Sesame - Storage and Querying architecture for RDF and RDF Schema * Copyright (C) 2003 OntoText Lab, Sirma AI OOD * * Contact: * Sirma AI OOD, OntoText Lab. * 38A, Christo Botev Blvd. * 1000 Sofia, Bulgaria * tel. +359(2)981 00 18 * fax. +359(2)981 90 58 * info@ontotext.com * * http://www.ontotext.com/ * * 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.io.IOException;import java.sql.Connection;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.ArrayList;import java.util.Iterator;import java.util.Map;import org.xml.sax.SAXException;import org.openrdf.util.log.ThreadLog;import org.openrdf.model.impl.URIImpl;import org.openrdf.sesame.sail.SailInternalException;import org.openrdf.sesame.sail.SailUpdateException;import org.openrdf.sesame.sailimpl.rdbms.rules.Rule;import org.openrdf.sesame.sailimpl.rdbms.rules.RuleParser;import org.openrdf.sesame.sailimpl.rdbms.rules.TripleTemplate;/** * <p>Title: Custom Inference Rules</p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2003</p> * <p>Company: Ontotext Lab. Sirma AI</p> * @author Damyan Ognyanoff * @version 1.0 */public class CustomInferenceServices implements InferenceServices { public static final String PARAM_RULE_FILE = "rule-file"; static final String DEFAULT_RULE_FILE = "org/openrdf/sesame/sailimpl/rdbms/entailment-rdfmt-REC.xml"; private static int maxTemplateCountsPerRule = 2; private boolean _useDependencyInferencer = true; public static int getMaxTemplateCountsPerRule() { return maxTemplateCountsPerRule; } ArrayList rules = null; ArrayList axioms = null; RdfSchemaRepository _sail = null; protected Connection _insertCon; protected PreparedStatement _insertSt; protected int iteration; protected int _nofInferred = 0; // Variables for optimization statistics protected int _totalInferred = 0; // number of inferred statements per rule. protected int[] _ruleCount = null; // total processing time per rule. protected long[] _ruleTime = null; ArrayList inferenceSQLs = null; ArrayList dependenceSQLs = null; protected boolean[][] _ruleTrigers = null; protected boolean[] _ruleIsApplied = null; public CustomInferenceServices() { } public void setDependencyInferencer(boolean useDependencyInferencer) { _useDependencyInferencer = useDependencyInferencer; } public void initialize(RdfSchemaRepository sail, Map configParams) { _sail = sail; String ruleFileName = (String)configParams.get(PARAM_RULE_FILE); if (ruleFileName == null) { ThreadLog.warning("No rule file defined. using default."); ruleFileName = DEFAULT_RULE_FILE; } ThreadLog.trace("Using rule file: " + ruleFileName); RuleParser parser = new RuleParser(); try { parser.load(ruleFileName); } catch (IOException e) { ThreadLog.error("Rule parser i/o error: " + e.getMessage()); throw new SailInternalException(e); } catch (SAXException e) { ThreadLog.error("Error parsing entailment rules: " + e.getMessage()); throw new SailInternalException(e); } rules = parser.getRules(); axioms = parser.getAxioms(); Iterator iter = rules.iterator(); int max = 0; while (iter.hasNext()) { Rule r = (Rule)iter.next(); if (max < r.getPremiseCount()) { max = r.getPremiseCount(); } } maxTemplateCountsPerRule = max; } /** * invoked to initilize the basic RDFS schema for the repository */ public void initRdfSchema() { // preSet the resource IDs used by axioms and rules // they are collected and stored in Rule.idMap; Iterator iter = Rule.constantsIter(); while (iter.hasNext()) { String uri = (String)iter.next(); int id = _sail._insertURI(new URIImpl(uri)); Rule.setId(uri, id); } _initialize(); } protected void _buildSQLs() { inferenceSQLs = new ArrayList(); dependenceSQLs = new ArrayList(); Iterator iter = rules.iterator(); while (iter.hasNext()) { Rule r = (Rule)iter.next(); ArrayList list = r.getAllSQLs(); inferenceSQLs.addAll(list); list = r.getDependSQL(); dependenceSQLs.addAll(list); } _ruleCount = new int[inferenceSQLs.size()]; _ruleTime = new long[inferenceSQLs.size()]; _ruleIsApplied = new boolean[inferenceSQLs.size()]; _ruleTrigers = buildTriggers(rules); } void _initialize() { try { // As defined in the RDF MT: add a number of default statements // concerning the definition of RDF and RDF Schema primitives _addAxioms(); _buildSQLs(); // ...and apply the inference rules to them. doInferencing(); } catch (SailUpdateException e) { throw new SailInternalException(e); } } /** * the entry point for statement inferencing */ public void doInferencing() { iteration = 1; // initialize some vars _totalInferred = 0; for (int i = 0; i < inferenceSQLs.size(); i++) { _ruleCount[i] = 0; _ruleTime[i] = 0; } try { _nofInferred = 1; ThreadLog.trace("starting inferencing"); // at first iteration we should apply all the rules(rule variants) boolean rulesToApply[] = new boolean[_ruleIsApplied.length]; java.util.Arrays.fill(rulesToApply, true); while (_nofInferred > 0) { // Apply inferencing to all statements in newTriples. // Each rule will insert any newly inferred statements into // INFERRED_TABLE and ALL_INFERRED_TABLE. _nofInferred = 0; // the line bellow means that there is no rule that infers new statements java.util.Arrays.fill(_ruleIsApplied, false); // Batch-oriented processing: for (int ruleNo = 0; ruleNo < inferenceSQLs.size(); ruleNo++) { // should we apply the rule? if (false == rulesToApply[ruleNo]) { continue; } // how much new statements are infered by the rule int newTriplesInferred = _applyRule(ruleNo, (String)inferenceSQLs.get(ruleNo)); // mark that this rule triggers some other rules at next iteration _ruleIsApplied[ruleNo] = (newTriplesInferred > 0); _nofInferred += newTriplesInferred; } _totalInferred += _nofInferred; // let clear all the rule activation flags java.util.Arrays.fill(rulesToApply, false); if (_nofInferred > 0) { // iterate over the flags thet indicate thet some rule has inferred new statements for (int riRow = 0; riRow < rulesToApply.length; ++riRow) { if (false == _ruleIsApplied[riRow]) { continue; } // get the list of triggered rules by thet rule boolean[] ruleRowFromTrigers = _ruleTrigers[riRow]; for (int riColumn = 0; riColumn < rulesToApply.length; ++riColumn) { // merge it with the current state rulesToApply[riColumn] = (rulesToApply[riColumn] || ruleRowFromTrigers[riColumn]); } } // computing the list of active rules during the next iteration } // if there are new statements inferred in the last iteration // The triples from NEW_TRIPLES_TABLE have been processed, // continue with the newly inferred triples from ALL_INFERRED_TABLE _sail._rdbms.clearTable(NEW_TRIPLES_TABLE); if (_nofInferred > 0) { _sail._rdbms.copyRows(ALL_INFERRED_TABLE, NEW_TRIPLES_TABLE); _sail._rdbms.copyRows(ALL_INFERRED_TABLE, ALL_NEW_TRIPLES_TABLE); _sail._rdbms.clearTable(ALL_INFERRED_TABLE); _sail._rdbms.optimizeTable(NEW_TRIPLES_TABLE); _sail._rdbms.optimizeTable(TRIPLES_TABLE); } ThreadLog.trace("iteration " + iteration + " done; " + "inferred " + _nofInferred + " new statements"); iteration++; } _sail._rdbms.optimizeTable(ALL_NEW_TRIPLES_TABLE); } catch (SQLException e) { throw new SailInternalException(e); } catch (SailUpdateException e) { throw new SailInternalException(e); } // Print some statistics ThreadLog.trace("---RdfMTInferencer statistics:---"); ThreadLog.trace("total statements inferred = " + _totalInferred); for (int i = 0; i < inferenceSQLs.size(); i++) { ThreadLog.trace("rule " + i + ": time=" + _ruleTime[i] + ";\t#inferred=" + _ruleCount[i]); } ThreadLog.trace("---end of statistics:---"); } /** * invoked when some statements are being removed * @throws SQLException */ public void removeExpiredStatements() throws SQLException { if (_useDependencyInferencer) { _makeExpiredStatementsInferred(); _determineGroundedStatements(); // Get the IDs of statements that are no longer grounded. Connection con = _sail._rdbms.getConnection(); java.sql.Statement st = con.createStatement(); ResultSet rs = st.executeQuery( "SELECT t.id FROM " + TRIPLES_TABLE + " t " + "LEFT JOIN " + GROUNDED_TRIPLES_TABLE + " g " + "ON t.id = g.id " + "WHERE g.id IS NULL"); String[] idChunks = _sail._chunkIdSet(rs, 3500); rs.close(); st.close(); // Delete these statements from TRIPLES_TABLE and DEPEND_TABLE. con.setAutoCommit(false); st = con.createStatement(); for (int i = 0; i < idChunks.length; i++) { st.executeUpdate( "DELETE FROM " + TRIPLES_TABLE + " WHERE id IN " + idChunks[i]); st.executeUpdate( "DELETE FROM " + DEPEND_TABLE + " WHERE id IN " + idChunks[i]); for (int j = 1; j <= maxTemplateCountsPerRule; j++) { st.executeUpdate( "DELETE FROM " + DEPEND_TABLE + " WHERE dep" + j + " IN " + idChunks[i]); } _sail._processChunkFromRemoveExpiredStatements(idChunks[i]); } con.commit(); st.close(); con.close(); _sail._rdbms.clearTable(GROUNDED_TRIPLES_TABLE); _sail._rdbms.clearTable(NEW_GROUNDED_TRIPLES_TABLE); } else { // don't use dependency inferencer // mark all expired statements as inferred _makeExpiredStatementsInferred(); // remove all inferred statements _removeAllInferred(); // copy all TRIPLES to the NEW_TRIPLES table _sail._rdbms.copyRows(TRIPLES_TABLE, NEW_TRIPLES_TABLE); // clean the TRIPLES table. _sail._rdbms.clearTable(TRIPLES_TABLE); // re-compute the closure _initialize(); } } /** * Set the 'explicit' flag to 'false' for all statements whose ID * is in the EXPIRED_TRIPLES_TABLE. **/ protected void _makeExpiredStatementsInferred() throws SQLException { Connection con = _sail._rdbms.getConnection(); java.sql.Statement st = con.createStatement(); ResultSet rs = st.executeQuery("SELECT DISTINCT id FROM " + EXPIRED_TRIPLES_TABLE); String[] idChunks = _sail._chunkIdSet(rs, 3500); rs.close(); st.close(); // Mark all expired statements to be inferred. The statements // cannot just be removed, as it is possible that they can also be // inferred from other statements. con.setAutoCommit(false); st = con.createStatement(); for (int i = 0; i < idChunks.length; i++) { st.executeUpdate( "UPDATE " + TRIPLES_TABLE + " SET explicit = " + _sail._rdbms.FALSE + " WHERE id IN " + idChunks[i]); } con.commit(); st.close(); con.close(); } protected void _determineGroundedStatements() throws SQLException { // Statement with ID '0' is grounded. '0' is used in dependencies
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -