1 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 28 public class RDFSRuleInfGraph extends FBRuleInfGraph { 29 30 31 protected HashMap dtRange = null; 32 33 39 public RDFSRuleInfGraph(Reasoner reasoner, List rules, Graph schema) { 40 super(reasoner, rules, schema); 41 } 42 43 50 public RDFSRuleInfGraph(Reasoner reasoner, List rules, Graph schema, Graph data) { 51 super(reasoner, rules, schema, data); 52 } 53 54 60 public ValidityReport validate() { 61 StandardValidityReport report = (StandardValidityReport)super.validate(); 63 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 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 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 | Popular Tags |