📄 rdfmtinferencer.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.memory;import org.openrdf.util.log.ThreadLog;import org.openrdf.vocabulary.RDF;import org.openrdf.model.Statement;import org.openrdf.sesame.sail.StatementIterator;/** * RDF(S) Inferencer for in-memory repositories using the rules from the * RDF Semantics Recommendation (10 February 2004). * See http://www.w3.org/TR/2004/REC-rdf-mt-20040210/ * * @author Jeen Broekstra * @version $Revision: 1.11 $ */public class RdfMTInferencer {/*-------------------------------------+| Constants |+-------------------------------------*/ private static final int Rdf1 = 0; private static final int Rdfs2_1 = 1; private static final int Rdfs2_2 = 2; private static final int Rdfs3_1 = 3; private static final int Rdfs3_2 = 4; private static final int Rdfs4a = 5; private static final int Rdfs4b = 6; private static final int Rdfs5_1 = 7; private static final int Rdfs5_2 = 8; private static final int Rdfs6 = 9; private static final int Rdfs7_1 = 10; private static final int Rdfs7_2 = 11; private static final int Rdfs8 = 12; private static final int Rdfs9_1 = 13; private static final int Rdfs9_2 = 14; private static final int Rdfs10 = 15; private static final int Rdfs11_1 = 16; private static final int Rdfs11_2 = 17; private static final int Rdfs12 = 18; private static final int Rdfs13 = 19; private static final int RX1 = 20; private static final int RULECOUNT = 21; private static final String[] RULENAMES = { " Rdf1", " Rdfs2_1", " Rdfs2_2", " Rdfs3_1", " Rdfs3_2", " Rdfs4a", " Rdfs4b", "Rdfs5_1", "Rdfs5_2", " Rdfs6", " Rdfs7_1", " Rdfs7_2", " Rdfs8", " Rdfs9_1", " Rdfs9_2", " Rdfs10", " Rdfs11_1", " Rdfs11_2", " Rdfs12", " Rdfs13", " RX1"}; private static final boolean _ = false; private static final boolean X = true; protected static final boolean[][] TRIGGERS = {// 1 2_2 3_2 4b 5_2 7_1 8 9_2 11_1 12 X1// 2_1 3_1 4a 5_1 6 7_2 9_1 10 11_2 13 { _, X, _, X, _, X, _, _, _, X, X, _, _, _, X, _, _, _, _, _, _},// 1 { _, X, _, X, _, _, _, _, _, X, X, _, X, _, X, X, _, _, X, X, _},// 2_1 { _, X, _, X, _, _, _, _, _, X, X, _, X, _, X, X, _, _, X, X, _},// 2_2 { _, X, _, X, _, _, _, _, _, X, X, _, X, _, X, X, _, _, X, X, _},// 3_1 { _, X, _, X, _, _, _, _, _, X, X, _, X, _, X, X, _, _, X, X, _},// 3_2 { _, X, _, X, _, _, _, _, _, _, X, _, _, _, X, _, _, _, _, _, _},// 4a { _, X, _, X, _, _, _, _, _, _, X, _, _, _, X, _, _, _, _, _, _},// 4b { _, _, _, _, _, _, _, X, X, _, X, X, _, _, _, _, _, _, _, _, _},// 51 { _, _, _, _, _, _, _, X, X, _, X, X, _, _, _, _, _, _, _, _, _},// 52 { _, X, _, X, _, _, _, _, _, _, X, _, _, _, _, _, _, _, _, _, _},// 6 { _, X, X, X, X, _, _, X, X, X, X, X, X, X, X, X, X, X, X, X, X},// 7_1 { _, X, X, X, X, _, _, X, X, X, X, X, X, X, X, X, X, X, X, X, X},// 7_2 { _, X, _, X, _, _, _, _, _, _, X, _, _, X, _, _, X, X, _, _, _},// 8 { _, _, _, X, _, _, _, _, _, X, X, _, X, _, X, X, _, _, X, X, _},// 9_1 { _, _, _, X, _, _, _, _, _, X, X, _, X, _, X, X, _, _, X, X, _},// 9_2 { _, X, _, X, _, _, _, _, _, _, X, _, _, _, _, _, _, _, _, _, _},// 10 { _, _, _, _, _, _, _, _, _, _, X, _, _, X, _, _, X, X, _, _, _},// 11_1 { _, _, _, _, _, _, _, _, _, _, X, _, _, X, _, _, X, X, _, _, _},// 11_2 { _, X, _, X, _, _, X, X, X, _, X, X, _, _, _, _, _, _, _, _, _},// 12 { _, X, _, X, _, _, _, _, _, _, X, _, _, X, _, _, X, X, _, _, _},// 13 { _, X, _, X, _, _, _, _, _, _, X, _, _, _, X, _, _, _, X, _, _},// X1 };/*-------------------------------------+| Variables |+-------------------------------------*/ private RdfSchemaRepository _repository; private StatementList _newStatements; private StatementList _newThisIteration; /** Flags indicating which rules should be evaluated. **/ private boolean[] _checkRule = new boolean[RULECOUNT]; /** Flags indicating which rules should be evaluated next iteration. **/ private boolean[] _checkRuleNextIter = new boolean[RULECOUNT]; private int _totalInferred = 0; // number of inferred statements per rule. private int[] _ruleCount = new int[RULECOUNT];/*-------------------------------------+| Constructors |+-------------------------------------*/ protected RdfMTInferencer(RdfSchemaRepository repository) { _repository = repository; }/*-------------------------------------+| Methods |+-------------------------------------*/ protected void initialize() { _addAxiomStatements(); doInferencing(); } protected void notifyNewStatement(Statement newStatement) { if (_newStatements == null) { _newStatements = new StatementList(512); } _newStatements.add(newStatement); } protected void notifyNewStatements(StatementList newStatements) { if (_newStatements == null) { _newStatements = new StatementList(newStatements); } else { _newStatements.addAll(newStatements); } } protected void doInferencing() { if (_newStatements == null) { // There's nothing to do return; } // initialize some vars _totalInferred = 0; int iteration = 0; int nofInferred = 1; // All rules needs to be checked: for (int i = 0; i < RULECOUNT; i++) { _ruleCount[i] = 0; _checkRuleNextIter[i] = true; } while (nofInferred > 0) { iteration++; _prepareIteration(); nofInferred = 0; nofInferred += _applyRuleRdf1(); nofInferred += _applyRuleRdfs2_1(); nofInferred += _applyRuleRdfs2_2(); nofInferred += _applyRuleRdfs3_1(); nofInferred += _applyRuleRdfs3_2(); nofInferred += _applyRuleRdfs4a(); nofInferred += _applyRuleRdfs4b(); nofInferred += _applyRuleRdfs5_1(); nofInferred += _applyRuleRdfs5_2(); nofInferred += _applyRuleRdfs6(); nofInferred += _applyRuleRdfs7_1(); nofInferred += _applyRuleRdfs7_2(); nofInferred += _applyRuleRdfs8(); nofInferred += _applyRuleRdfs9_1(); nofInferred += _applyRuleRdfs9_2(); nofInferred += _applyRuleRdfs10(); nofInferred += _applyRuleRdfs11_1(); nofInferred += _applyRuleRdfs11_2(); nofInferred += _applyRuleRdfs12(); nofInferred += _applyRuleRdfs13(); nofInferred += _applyRuleX1(); ThreadLog.trace("iteration " + iteration + " done; " + "inferred " + nofInferred + " new statements"); _totalInferred += nofInferred; } // Print some statistics ThreadLog.trace("---RdfMTInferencer statistics:---"); ThreadLog.trace("total statements inferred = " + _totalInferred); for (int i = 0; i < RULECOUNT; i++) { ThreadLog.trace( "rule " + RULENAMES[i] + ":\t#inferred=" + _ruleCount[i]); } ThreadLog.trace("---end of statistics:---"); // clean up _newStatements = null; _newThisIteration = null; } private void _prepareIteration() { _newThisIteration = _newStatements; _newStatements = new StatementList(); for (int i = 0; i < RULECOUNT; i++) { _checkRule[i] = _checkRuleNextIter[i]; // reset for next iteration: _checkRuleNextIter[i] = false; } } private void _updateTriggers(int ruleNo, int nofInferred) { if (nofInferred > 0) { _ruleCount[ruleNo] += nofInferred; // Check which rules are triggered by this one. boolean[] triggers = TRIGGERS[ruleNo]; for (int i = 0; i < RULECOUNT; i++) { if (triggers[i] == true) { _checkRuleNextIter[i] = true; } } } } /* rdf1. * xxx aaa yyy --> aaa rdf:type rdf:Property */ private int _applyRuleRdf1() { if (! _checkRule[Rdf1]) { return 0; } int nofInferred = 0; StatementIterator iter = new MemStatementIterator(_newThisIteration); while (iter.hasNext()) { Statement st = iter.next(); boolean added = _addInferredStatement( (URINode)st.getPredicate(), _repository.RDF_TYPE_NODE, _repository.RDF_PROPERTY_NODE); if (added) { nofInferred++; } } iter.close(); _updateTriggers(Rdf1, nofInferred); return nofInferred; } /* rdfs2. * 2_1. xxx aaa yyy && (nt) * aaa rdfs:domain zzz --> (t1) * xxx rdf:type zzz (t2) */ private int _applyRuleRdfs2_1() { if (! _checkRule[Rdfs2_1]) { return 0; } int nofInferred = 0; StatementIterator ntIter = new MemStatementIterator(_newThisIteration); while (ntIter.hasNext()) { Statement nt = ntIter.next(); ResourceNode xxx = (ResourceNode)nt.getSubject(); URINode aaa = (URINode)nt.getPredicate(); StatementIterator t1Iter = _repository.getStatements(aaa, _repository.RDFS_DOMAIN_NODE, null); while (t1Iter.hasNext()) { Statement t1 = t1Iter.next(); ValueNode zzz = (ValueNode)t1.getObject(); if (zzz instanceof ResourceNode) { boolean added = _addInferredStatement(xxx, _repository.RDF_TYPE_NODE, zzz); if (added) { nofInferred++; } } } t1Iter.close(); } ntIter.close(); _updateTriggers(Rdfs2_1, nofInferred); return nofInferred; } /* rdfs2. * 2_2. aaa rdfs:domain zzz && (nt) * xxx aaa yyy --> (t1) * xxx rdf:type zzz (t2) */ private int _applyRuleRdfs2_2() { if (! _checkRule[Rdfs2_2]) { return 0; } int nofInferred = 0; StatementIterator ntIter = new MemStatementIterator(_newThisIteration, null, _repository.RDFS_DOMAIN_NODE, null); while (ntIter.hasNext()) { Statement nt = ntIter.next(); ResourceNode aaa = (ResourceNode)nt.getSubject(); ValueNode zzz = (ValueNode)nt.getObject(); if (aaa instanceof URINode && zzz instanceof ResourceNode) { StatementIterator t1Iter = _repository.getStatements(null, (URINode)aaa, null); while (t1Iter.hasNext()) { Statement t1 = t1Iter.next(); ResourceNode xxx = (ResourceNode)t1.getSubject(); boolean added = _addInferredStatement(xxx, _repository.RDF_TYPE_NODE, zzz); if (added) { nofInferred++; } } t1Iter.close(); } } ntIter.close(); _updateTriggers(Rdfs2_2, nofInferred); return nofInferred; } /* rdfs3. * 3_1. xxx aaa uuu && (nt) * aaa rdfs:range zzz --> (t1) * uuu rdf:type zzz (t2) */ private int _applyRuleRdfs3_1() { if (! _checkRule[Rdfs3_1]) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -