001: /******************************************************************
002: * File: RDFSRuleInfGraph.java
003: * Created by: Dave Reynolds
004: * Created on: 22-Jun-2003
005: *
006: * (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
007: * [See end of file]
008: * $Id: RDFSRuleInfGraph.java,v 1.9 2008/01/02 12:07:47 andy_seaborne Exp $
009: *****************************************************************/package com.hp.hpl.jena.reasoner.rulesys;
010:
011: import com.hp.hpl.jena.reasoner.*;
012: import com.hp.hpl.jena.graph.*;
013: import com.hp.hpl.jena.graph.impl.LiteralLabel;
014: import com.hp.hpl.jena.datatypes.*;
015: import com.hp.hpl.jena.vocabulary.*;
016:
017: import java.util.*;
018:
019: /**
020: * Customization of the generic rule inference graph for RDFS inference.
021: * In fact all the rule processing is unchanged, the only extenstion is
022: * the validation support.
023: *
024: * @author <a href="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
025: * @version $Revision: 1.9 $ on $Date: 2008/01/02 12:07:47 $
026: */
027: public class RDFSRuleInfGraph extends FBRuleInfGraph {
028:
029: /** Optional map of property node to datatype ranges */
030: protected HashMap dtRange = null;
031:
032: /**
033: * Constructor.
034: * @param reasoner the reasoner which created this inf graph instance
035: * @param rules the rules to process
036: * @param schema the (optional) schema graph to be included
037: */
038: public RDFSRuleInfGraph(Reasoner reasoner, List rules, Graph schema) {
039: super (reasoner, rules, schema);
040: }
041:
042: /**
043: * Constructor.
044: * @param reasoner the reasoner which created this inf graph instance
045: * @param rules the rules to process
046: * @param schema the (optional) schema graph to be included
047: * @param data the data graph to be processed
048: */
049: public RDFSRuleInfGraph(Reasoner reasoner, List rules,
050: Graph schema, Graph data) {
051: super (reasoner, rules, schema, data);
052: }
053:
054: /**
055: * Test the consistency of the bound data. For RDFS this checks that all
056: * instances of datatype-ranged properties have correct data values.
057: *
058: * @return a ValidityReport structure
059: */
060: public ValidityReport validate() {
061: // The full configuration uses validation rules so check for these
062: StandardValidityReport report = (StandardValidityReport) super
063: .validate();
064: // Also do a hardwired check to handle the simpler configurations
065: HashMap dtRange = getDTRange();
066: for (Iterator props = dtRange.keySet().iterator(); props
067: .hasNext();) {
068: Node prop = (Node) props.next();
069: for (Iterator i = find(null, prop, null); i.hasNext();) {
070: Triple triple = (Triple) i.next();
071: report.add(checkLiteral(prop, triple.getObject()));
072: }
073: }
074: return report;
075: }
076:
077: /**
078: * Check a given literal value for a property against the set of
079: * known range constraints for it.
080: * @param prop the property node whose range is under scrutiny
081: * @param value the literal node whose value is to be checked
082: * @return null if the range is legal, otherwise a ValidityReport.Report
083: * which describes the problem.
084: */
085: public ValidityReport.Report checkLiteral(Node prop, Node value) {
086: List range = (List) getDTRange().get(prop);
087: if (range != null) {
088: if (value.isBlank())
089: return null;
090: if (!value.isLiteral()) {
091: return new ValidityReport.Report(
092: true,
093: "dtRange",
094: "Property "
095: + prop
096: + " has a typed range but was given a non literal value "
097: + value);
098: }
099: LiteralLabel ll = value.getLiteral();
100: for (Iterator i = range.iterator(); i.hasNext();) {
101: RDFDatatype dt = (RDFDatatype) i.next();
102: if (!dt.isValidLiteral(ll)) {
103: return new ValidityReport.Report(true, "dtRange",
104: "Property " + prop + " has a typed range "
105: + dt
106: + "that is not compatible with "
107: + value);
108: }
109: }
110: }
111: return null;
112: }
113:
114: /**
115: * Return a map from property nodes to a list of RDFDatatype objects
116: * which have been declared as the range of that property.
117: */
118: private HashMap getDTRange() {
119: if (dtRange == null) {
120: dtRange = new HashMap();
121: for (Iterator i = find(null, RDFS.range.asNode(), null); i
122: .hasNext();) {
123: Triple triple = (Triple) i.next();
124: Node prop = triple.getSubject();
125: Node rangeValue = triple.getObject();
126: if (rangeValue.isURI()) {
127: RDFDatatype dt = TypeMapper.getInstance()
128: .getTypeByName(rangeValue.getURI());
129: if (dt != null) {
130: List range = (ArrayList) dtRange.get(prop);
131: if (range == null) {
132: range = new ArrayList();
133: dtRange.put(prop, range);
134: }
135: range.add(dt);
136: }
137: }
138: }
139: }
140: return dtRange;
141: }
142:
143: }
144:
145: /*
146: (c) Copyright 2003, 2004, 2005, 2006, 2007, 2008 Hewlett-Packard Development Company, LP
147: All rights reserved.
148:
149: Redistribution and use in source and binary forms, with or without
150: modification, are permitted provided that the following conditions
151: are met:
152:
153: 1. Redistributions of source code must retain the above copyright
154: notice, this list of conditions and the following disclaimer.
155:
156: 2. Redistributions in binary form must reproduce the above copyright
157: notice, this list of conditions and the following disclaimer in the
158: documentation and/or other materials provided with the distribution.
159:
160: 3. The name of the author may not be used to endorse or promote products
161: derived from this software without specific prior written permission.
162:
163: THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
164: IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
165: OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
166: IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
167: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
168: NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
169: DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
170: THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
171: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
172: THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
173: */
|