📄 transitiveengine.java
字号:
return subClassCache;
} else {
return continuation;
}
}
/**
* Add one triple to caches if it is relevant.
* @return true if the triple affected the caches
*/
public synchronized boolean add(Triple t) {
int triageClass = triage(t);
switch (triageClass & UPDATE_MASK) {
case SUBCLASS:
subClassCache.addRelation(t);
break;
case SUBPROPERTY:
subPropertyCache.addRelation(t);
break;
case NOT_RELEVANT:
return false;
}
// If we get here we might need to some cache rebuilding
if ((triageClass & REBUILD_SUBPROPERTY) != 0) {
TransitiveEngine.cacheSubPropUtility(data, subPropertyCache);
isPrepared = false;
}
if ((triageClass & REBUILD_SUBCLASS) != 0) {
TransitiveEngine.cacheSubClassUtility(data, subPropertyCache, subClassCache);
isPrepared = false;
}
return true;
}
/**
* Removes the triple t (if relevant) from the caches.
* @return true if the triple affected the caches
*/
public synchronized boolean delete(Triple t) {
int triageClass = triage(t);
switch (triageClass & UPDATE_MASK) {
case SUBCLASS:
subClassCache.removeRelation(t);
break;
case SUBPROPERTY:
subPropertyCache.removeRelation(t);
break;
case NOT_RELEVANT:
return false;
}
// If we get here we might need to some cache rebuilding
if ((triageClass & REBUILD_SUBPROPERTY) != 0) {
subPropertyCache.clear();
TransitiveEngine.cacheSubPropUtility(data, subPropertyCache);
isPrepared = false;
}
if ((triageClass & REBUILD_SUBCLASS) != 0) {
subClassCache.clear();
TransitiveEngine.cacheSubClassUtility(data, subPropertyCache, subClassCache);
isPrepared = false;
}
return true;
}
/**
* Test if there are any usages of prop within the given graph.
* This includes indirect usages incurred by subProperties of prop.
*
* @param prop the property to be checked for
* @param graph the graph to be check
* @return true if there is a triple using prop or one of its sub properties
*/
public boolean checkOccurance(Node prop, Graph graph) {
return checkOccuranceUtility(prop, graph, subPropertyCache);
}
// ----------------------------------------------------------------------------
// Global helper routines, maintined in this for just to suppor the (now deprecated)
// rdfs1 reasoner.
/**
* Caches all subClass declarations, including those that
* are defined via subProperties of subClassOf. Public to allow other reasoners
* to use it but not of interest to end users.
*
* @param graph a graph whose declarations are to be cached
* @param spCache the existing state of the subPropertyOf cache
* @param scCache the existing state of the subClassOf cache, will be updated
* @return true if there were new metalevel declarations discovered.
*/
public static boolean cacheSubClassUtility(Finder graph, TransitiveGraphCache spCache, TransitiveGraphCache scCache) {
if (graph == null) return false;
scCache.cacheAll(graph, TransitiveReasoner.subClassOf);
// Check for any properties which are subProperties of subClassOf
boolean foundAny = false;
ExtendedIterator subClasses
= spCache.find(new TriplePattern(null, TransitiveReasoner.subPropertyOf, TransitiveReasoner.subClassOf));
while (subClasses.hasNext()) {
foundAny = true;
Triple t = (Triple)subClasses.next();
Node subClass = t.getSubject();
if (!subClass.equals(TransitiveReasoner.subClassOf)) {
scCache.cacheAll(graph, subClass);
}
}
return foundAny;
}
/**
* Test if there are any usages of prop within the given graph.
* This includes indirect usages incurred by subProperties of prop.
* Public to allow other reasoners
* to use it but not of interest to end users.
*
* @param prop the property to be checked for
* @param graph the graph to be check
* @param spCache the subPropertyOf cache to use
* @return true if there is a triple using prop or one of its sub properties
*/
public static boolean checkOccuranceUtility(Node prop, Graph graph, TransitiveGraphCache spCache) {
boolean foundOne = false;
ExtendedIterator uses = graph.find( null, prop, null );
foundOne = uses.hasNext();
uses.close();
if (foundOne) return foundOne;
ExtendedIterator propVariants
= spCache.find(new TriplePattern(null, TransitiveReasoner.subPropertyOf, prop));
while (propVariants.hasNext() && !foundOne) {
Triple t = (Triple)propVariants.next();
Node propVariant = t.getSubject();
uses = graph.find( null, propVariant, null );
foundOne = uses.hasNext();
uses.close();
}
propVariants.close();
return foundOne;
}
/**
* Caches all subPropertyOf declarations, including any meta level
* ones (subPropertyOf subPropertyOf). Public to allow other reasoners
* to use it but not of interest to end users.
*
* @param graph a graph whose declarations are to be cached
* @param spCache the existing state of the subPropertyOf cache, will be updated
* @return true if there were new metalevel declarations discovered.
*/
public static boolean cacheSubPropUtility(Finder graph, TransitiveGraphCache spCache) {
if (graph == null) return false;
spCache.cacheAll(graph, TransitiveReasoner.subPropertyOf);
// Check for any properties which are subProperties of subProperty
// and so introduce additional subProperty relations.
// Each one discovered might reveal indirect subPropertyOf subPropertyOf
// declarations - hence the double iteration
boolean foundAny = false;
boolean foundMore = false;
HashSet cached = new HashSet();
do {
ExtendedIterator subProps
= spCache.find(new TriplePattern(null, TransitiveReasoner.subPropertyOf, TransitiveReasoner.subPropertyOf));
while (subProps.hasNext()) {
foundMore = false;
Triple t = (Triple)subProps.next();
Node subProp = t.getSubject();
if (!subProp.equals(TransitiveReasoner.subPropertyOf) && !cached.contains(subProp)) {
foundAny = true;
cached.add(subProp);
spCache.cacheAll(graph, subProp);
foundMore = true;
}
}
} while (foundMore);
return foundAny;
}
}
/*
(c) Copyright 2003, 2004, 2005, 2006, 2007 Hewlett-Packard Development Company, LP
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. The name of the author may not be used to endorse or promote products
derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -