📄 rdfsinfgraph.java
字号:
}
}
}
// set up the router which connect queries to the appropriate processing element
router = new PatternRouter();
router.register(subPropertyCache);
router.register(subClassCache);
// Run the forward rules to preload the tripleCache and build the backward rules
checkAllForwardRules();
// Add fixed backward rules
for (int i = 0; i < brules.length; i++) {
addBRule(brules[i]);
}
isPrepared = true;
}
/**
* Extended find interface used in situations where the implementator
* may or may not be able to answer the complete query. It will
* attempt to answer the pattern but if its answers are not known
* to be complete then it will also pass the request on to the nested
* Finder to append more results.
* @param pattern a TriplePattern to be matched against the data
* @param continuation either a Finder or a normal Graph which
* will be asked for additional match results if the implementor
* may not have completely satisfied the query.
*/
public ExtendedIterator findWithContinuation(TriplePattern pattern, Finder continuation) {
checkOpen();
if (!isPrepared) prepare();
return new UniqueExtendedIterator(router.find(pattern, tripleCache, continuation,this));
}
/**
* Variant on find called by backward rules, additional
* argument used to pass set of instantiated rules to prevent
* run-away rule firing.
*/
public ExtendedIterator findNested(TriplePattern pattern, Finder continuation, HashSet firedRules) {
return router.find(pattern, tripleCache, continuation,this, firedRules);
}
/**
* Variant on find called by special backward rules that only
* access the raw data and axioms and bypass further rules
*/
public ExtendedIterator findRawWithContinuation(TriplePattern pattern, Finder continuation) {
return tripleCache.findWithContinuation(pattern, continuation);
}
/**
* Variant on find called by special backward rules that need
* to list all pre-registered properties. The iterator returns Nodes
* not triples.
*/
public ExtendedIterator findProperties() {
return subPropertyCache.listAllSubjects();
}
/**
* Variant on find called by special backward rules that need
* to list check for a specific preregistered property.
*/
public boolean isProperty(Node prop) {
return subPropertyCache.isSubject(prop);
}
/**
* Test the consistency of the bound data. This normally tests
* the validity of the bound instance data against the bound
* schema data.
* @return a ValidityReport structure
*/
public ValidityReport validate() {
StandardValidityReport report = new StandardValidityReport();
HashMap dtRange = getDTRange();
for (Iterator props = dtRange.keySet().iterator(); props.hasNext(); ) {
Node prop = (Node)props.next();
for (Iterator i = find(null, prop, null); i.hasNext(); ) {
Triple triple = (Triple)i.next();
report.add(checkLiteral(prop, triple.getObject()));
}
}
return report;
}
//=======================================================================
// helper methods
/**
* Return a map from property nodes to a list of RDFDatatype objects
* which have been declared as the range of that property.
*/
private HashMap getDTRange() {
if (dtRange == null) {
dtRange = new HashMap();
for (Iterator i = find(null, RDFS.range.asNode(), null); i.hasNext(); ) {
Triple triple = (Triple)i.next();
Node prop = triple.getSubject();
Node rangeValue = triple.getObject();
if (rangeValue.isURI()) {
RDFDatatype dt = TypeMapper.getInstance().getTypeByName(rangeValue.getURI());
if (dt != null) {
List range = (ArrayList) dtRange.get(prop);
if (range == null) {
range = new ArrayList();
dtRange.put(prop, range);
}
range.add(dt);
}
}
}
}
return dtRange;
}
/**
* Check a given literal value for a property against the set of
* known range constraints for it.
* @param prop the property node whose range is under scrutiny
* @param value the literal node whose value is to be checked
* @return null if the range is legal, otherwise a ValidityReport.Report
* which describes the problem.
*/
private ValidityReport.Report checkLiteral(Node prop, Node value) {
List range = (List) getDTRange().get(prop);
if (range != null) {
if (!value.isLiteral()) {
return new ValidityReport.Report(true, "dtRange",
"Property " + prop + " has a typed range but was given a non literal value " + value);
}
LiteralLabel ll = value.getLiteral();
for (Iterator i = range.iterator(); i.hasNext(); ) {
RDFDatatype dt = (RDFDatatype)i.next();
if (!dt.isValidLiteral(ll)) {
return new ValidityReport.Report(true, "dtRange",
"Property " + prop + " has a typed range " + dt +
"that is not compatible with " + value);
}
}
}
return null;
}
/**
* Run all the builtin forward rules, on all the elements in the tbox and data
* graphs. Checkes for all subProperties of the properties mentioned in the
* rules themselves.
*/
private void checkAllForwardRules() {
// Build a search path for the rules
Finder caches = FinderUtil.cascade(subPropertyCache, subClassCache, tripleCache);
// Check all rules sequentially
for (int i = 0; i < rules.length; i++) {
BaseFRule rule = rules[i];
TriplePattern head = rule.getHead();
Node pPattern = head.getPredicate();
if (pPattern.isVariable()) {
checkRule(head, rule, caches);
} else {
// Check out all subProperties of the given predicate
TriplePattern spPatt = new TriplePattern(null, TransitiveReasoner.subPropertyOf, pPattern);
ExtendedIterator sps = subPropertyCache.find(spPatt);
while (sps.hasNext()) {
TriplePattern altHead = new TriplePattern(
head.getSubject(),
((Triple)sps.next()).getSubject(),
head.getObject());
checkRule(altHead, rule, caches);
}
}
}
}
/**
* Run a single rule, with the rewritten head, against the data
*/
private void checkRule(TriplePattern altHead, BaseFRule rule, Finder caches) {
Iterator it = caches.findWithContinuation(altHead, fdata);
while (it.hasNext()) {
Triple t = (Triple)it.next();
rule.bindAndFire(t, this);
}
}
/**
* Assert a triple into the triple cache.
* Called by FRules when they fire
*/
public void assertTriple(Triple t) {
axioms.getGraph().add(t);
}
/**
* Add a new backchaining rule into the rule set.
* Called by FRules when they fire
*/
public void addBRule(BRWRule rule) {
router.register(rule);
}
/**
* Separate the cache of subClassOf relations from the parent reasoner
* because new added data has changed the class lattice
*/
private void splitSubClassCache() {
if (!haveSplitSubClassCache) {
subClassCache = subClassCache.deepCopy();
haveSplitSubClassCache = true;
}
}
/**
* Printable version of the whole reasoner state.
* Used during debugging
*/
public String toString() {
StringBuffer state = new StringBuffer();
TriplePattern all = new TriplePattern(null, null, null);
if (tripleCache != null) {
state.append("axioms + tbox\n");
for (Iterator i = tripleCache.find(all); i.hasNext(); ) {
state.append(TriplePattern.simplePrintString((Triple)i.next()));
state.append("\n");
}
}
if (fdata != null) {
state.append("Bound raw data\n");
for (Iterator i = fdata.find(all); i.hasNext(); ) {
state.append(TriplePattern.simplePrintString((Triple)i.next()));
state.append("\n");
}
}
if (router != null) {
state.append("Rule set\n");
state.append(router.toString());
}
return state.toString();
}
}
/*
(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 + -