KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > hp > hpl > jena > reasoner > rulesys > RDFSRuleInfGraph


1 /******************************************************************
2  * File: RDFSRuleInfGraph.java
3  * Created by: Dave Reynolds
4  * Created on: 22-Jun-2003
5  *
6  * (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
7  * [See end of file]
8  * $Id: RDFSRuleInfGraph.java,v 1.6 2005/02/21 12:17:04 andy_seaborne Exp $
9  *****************************************************************/

10 package com.hp.hpl.jena.reasoner.rulesys;
11
12 import com.hp.hpl.jena.reasoner.*;
13 import com.hp.hpl.jena.graph.*;
14 import com.hp.hpl.jena.graph.impl.LiteralLabel;
15 import com.hp.hpl.jena.datatypes.*;
16 import com.hp.hpl.jena.vocabulary.*;
17
18 import java.util.*;
19
20 /**
21  * Customization of the generic rule inference graph for RDFS inference.
22  * In fact all the rule processing is unchanged, the only extenstion is
23  * the validation support.
24  *
25  * @author <a HREF="mailto:der@hplb.hpl.hp.com">Dave Reynolds</a>
26  * @version $Revision: 1.6 $ on $Date: 2005/02/21 12:17:04 $
27  */

28 public class RDFSRuleInfGraph extends FBRuleInfGraph {
29
30     /** Optional map of property node to datatype ranges */
31     protected HashMap dtRange = null;
32
33     /**
34      * Constructor.
35      * @param reasoner the reasoner which created this inf graph instance
36      * @param rules the rules to process
37      * @param schema the (optional) schema graph to be included
38      */

39     public RDFSRuleInfGraph(Reasoner reasoner, List rules, Graph schema) {
40         super(reasoner, rules, schema);
41     }
42
43     /**
44      * Constructor.
45      * @param reasoner the reasoner which created this inf graph instance
46      * @param rules the rules to process
47      * @param schema the (optional) schema graph to be included
48      * @param data the data graph to be processed
49      */

50     public RDFSRuleInfGraph(Reasoner reasoner, List rules, Graph schema, Graph data) {
51         super(reasoner, rules, schema, data);
52     }
53     
54     /**
55      * Test the consistency of the bound data. For RDFS this checks that all
56      * instances of datatype-ranged properties have correct data values.
57      *
58      * @return a ValidityReport structure
59      */

60     public ValidityReport validate() {
61         // The full configuration uses validation rules so check for these
62
StandardValidityReport report = (StandardValidityReport)super.validate();
63         // Also do a hardwired check to handle the simpler configurations
64
HashMap dtRange = getDTRange();
65         for (Iterator props = dtRange.keySet().iterator(); props.hasNext(); ) {
66             Node prop = (Node)props.next();
67             for (Iterator i = find(null, prop, null); i.hasNext(); ) {
68                 Triple triple = (Triple)i.next();
69                 report.add(checkLiteral(prop, triple.getObject()));
70             }
71         }
72         return report;
73     }
74     
75     /**
76      * Check a given literal value for a property against the set of
77      * known range constraints for it.
78      * @param prop the property node whose range is under scrutiny
79      * @param value the literal node whose value is to be checked
80      * @return null if the range is legal, otherwise a ValidityReport.Report
81      * which describes the problem.
82      */

83     public ValidityReport.Report checkLiteral(Node prop, Node value) {
84         List range = (List) getDTRange().get(prop);
85         if (range != null) {
86             if (value.isBlank()) return null;
87             if (!value.isLiteral()) {
88                 return new ValidityReport.Report(true, "dtRange",
89                     "Property " + prop + " has a typed range but was given a non literal value " + value);
90             }
91             LiteralLabel ll = value.getLiteral();
92             for (Iterator i = range.iterator(); i.hasNext(); ) {
93                 RDFDatatype dt = (RDFDatatype)i.next();
94                 if (!dt.isValidLiteral(ll)) {
95                     return new ValidityReport.Report(true, "dtRange",
96                         "Property " + prop + " has a typed range " + dt +
97                         "that is not compatible with " + value);
98                 }
99             }
100         }
101         return null;
102     }
103
104     /**
105      * Return a map from property nodes to a list of RDFDatatype objects
106      * which have been declared as the range of that property.
107      */

108     private HashMap getDTRange() {
109         if (dtRange == null) {
110             dtRange = new HashMap();
111             for (Iterator i = find(null, RDFS.range.asNode(), null); i.hasNext(); ) {
112                 Triple triple = (Triple)i.next();
113                 Node prop = triple.getSubject();
114                 Node rangeValue = triple.getObject();
115                 if (rangeValue.isURI()) {
116                     RDFDatatype dt = TypeMapper.getInstance().getTypeByName(rangeValue.getURI());
117                     if (dt != null) {
118                         List range = (ArrayList) dtRange.get(prop);
119                         if (range == null) {
120                             range = new ArrayList();
121                             dtRange.put(prop, range);
122                         }
123                         range.add(dt);
124                     }
125                 }
126             }
127         }
128         return dtRange;
129     }
130
131 }
132
133
134
135 /*
136     (c) Copyright 2003, 2004, 2005 Hewlett-Packard Development Company, LP
137     All rights reserved.
138
139     Redistribution and use in source and binary forms, with or without
140     modification, are permitted provided that the following conditions
141     are met:
142
143     1. Redistributions of source code must retain the above copyright
144        notice, this list of conditions and the following disclaimer.
145
146     2. Redistributions in binary form must reproduce the above copyright
147        notice, this list of conditions and the following disclaimer in the
148        documentation and/or other materials provided with the distribution.
149
150     3. The name of the author may not be used to endorse or promote products
151        derived from this software without specific prior written permission.
152
153     THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
154     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
155     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
156     IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
157     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
158     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
159     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
160     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
161     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
162     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
163 */
Popular Tags