1 10 package com.hp.hpl.jena.reasoner.rulesys; 11 12 import com.hp.hpl.jena.graph.*; 13 import com.hp.hpl.jena.graph.impl.*; 14 import com.hp.hpl.jena.rdf.model.*; 15 import com.hp.hpl.jena.reasoner.Finder; 16 import com.hp.hpl.jena.reasoner.IllegalParameterException; 17 import com.hp.hpl.jena.reasoner.TriplePattern; 18 import com.hp.hpl.jena.util.FileUtils; 19 import com.hp.hpl.jena.util.iterator.ClosableIterator; 20 import com.hp.hpl.jena.vocabulary.RDF; 21 import com.hp.hpl.jena.datatypes.xsd.XSDDateTime; 22 23 import java.io.*; 24 import java.util.*; 25 26 29 35 public class Util { 36 37 40 public static boolean isNumeric(Node n) { 41 if (n.isLiteral()) { 42 Object o = n.getLiteral().getValue(); 43 return (o instanceof Number ); 44 } else { 45 return false; 46 } 47 } 48 49 52 public static int getIntValue(Node n) { 53 return ((Number )n.getLiteral().getValue()).intValue(); 54 } 55 56 63 public static int compareNumbers(Node n1, Node n2) { 64 if (n1.isLiteral() && n2.isLiteral()) { 65 Object v1 = n1.getLiteral().getValue(); 66 Object v2 = n2.getLiteral().getValue(); 67 if (v1 instanceof Number && v2 instanceof Number ) { 68 if (v1 instanceof Float || v1 instanceof Double 69 || v1 instanceof Float || v2 instanceof Double ) { 70 double d1 = ((Number )v1).doubleValue(); 71 double d2 = ((Number )v2).doubleValue(); 72 return (d1 < d2) ? -1 : ( (d1 == d2) ? 0 : +1 ); 73 } else { 74 long l1 = ((Number )v1).longValue(); 75 long l2 = ((Number )v2).longValue(); 76 return (l1 < l2) ? -1 : ( (l1 == l2) ? 0 : +1 ); 77 } 78 } 79 } 80 throw new ClassCastException ("Non-numeric literal in compareNumbers"); 81 } 82 83 86 public static boolean isInstant(Node n) { 87 if (n.isLiteral()) { 88 Object o = n.getLiteral().getValue(); 89 return (o instanceof XSDDateTime); 90 } else { 91 return false; 92 } 93 } 94 95 102 public static int compareInstants(Node n1, Node n2) { 103 if (n1.isLiteral() && n2.isLiteral()) { 104 Object v1 = n1.getLiteral().getValue(); 105 Object v2 = n2.getLiteral().getValue(); 106 if (v1 instanceof XSDDateTime && v2 instanceof XSDDateTime) { 107 XSDDateTime a = (XSDDateTime) v1; 108 XSDDateTime b = (XSDDateTime) v2; 109 return a.compare(b); 110 } 111 } 112 throw new ClassCastException ("Non-numeric literal in compareNumbers"); 113 } 114 115 119 public static Node getPropValue(Node root, Node prop, Finder context) { 120 return doGetPropValue(context.find(new TriplePattern(root, prop, null))); 121 } 122 123 127 public static Node getPropValue(Node root, Node prop, Graph context) { 128 return doGetPropValue(context.find(root, prop, null)); 129 } 130 131 135 public static Node getPropValue(Node root, Node prop, RuleContext context) { 136 return doGetPropValue(context.find(root, prop, null)); 137 } 138 139 142 private static Node doGetPropValue(ClosableIterator it) { 143 Node result = null; 144 if (it.hasNext()) { 145 result = ((Triple)it.next()).getObject(); 146 } 147 it.close(); 148 return result; 149 } 150 151 156 public static List convertList(Node root, RuleContext context) { 157 return convertList(root, context, new LinkedList()); 158 } 159 160 163 private static List convertList(Node node, RuleContext context, List sofar) { 164 if (node == null || node.equals(RDF.nil.asNode())) return sofar; 165 Node next = getPropValue(node, RDF.first.asNode(), context); 166 if (next != null) { 167 sofar.add(next); 168 return convertList(getPropValue(node, RDF.rest.asNode(), context), context, sofar); 169 } else { 170 return sofar; 171 } 172 } 173 174 177 public static Node makeIntNode(int value) { 178 return Node.createLiteral(new LiteralLabel(new Integer (value))); 179 } 180 181 184 public static Node makeLongNode(long value) { 185 if (value > Integer.MAX_VALUE) { 186 return Node.createLiteral(new LiteralLabel(new Long (value))); 187 } else { 188 return Node.createLiteral(new LiteralLabel(new Integer ((int)value))); 189 } 190 } 191 192 195 public static Node makeDoubleNode(double value) { 196 return Node.createLiteral(new LiteralLabel(new Double (value))); 197 } 198 199 203 public static Node makeList(Node[] nodes, Graph graph) { 204 return doMakeList(nodes, 0, graph); 205 } 206 207 210 private static Node doMakeList(Node[] nodes, int next, Graph graph) { 211 if (next < nodes.length) { 212 Node listNode = Node.createAnon(); 213 graph.add(new Triple(listNode, RDF.Nodes.first, nodes[next])); 214 graph.add(new Triple(listNode, RDF.Nodes.rest, doMakeList(nodes, next+1, graph))); 215 return listNode; 216 } else { 217 return RDF.Nodes.nil; 218 } 219 } 220 221 226 public static String loadResourceFile( String filename ) { 227 return Rule.rulesStringFromReader( FileUtils.openResourceFile( filename ) ); 228 } 229 230 234 public static Rule.Parser loadRuleParserFromResourceFile( String filename ) { 235 return Rule.rulesParserFromReader( FileUtils.openResourceFile( filename ) ); 236 } 237 238 242 public static String loadURLFile(String urlStr) throws IOException { 243 BufferedReader dataReader = FileUtils.readerFromURL( urlStr ); 244 StringWriter sw = new StringWriter(1024); 245 char buff[] = new char[1024]; 246 while (dataReader.ready()) { 247 int l = dataReader.read(buff); 248 if (l <= 0) 249 break; 250 sw.write(buff, 0, l); 251 } 252 dataReader.close(); 253 sw.close(); 254 return sw.toString(); 255 } 256 257 264 public static Boolean checkBinaryPredicate(Property predicate, Resource configuration) { 265 StmtIterator i = configuration.listProperties(predicate); 266 if (i.hasNext()) { 267 return new Boolean (i.nextStatement().getObject().toString().equalsIgnoreCase("true")); 268 } else { 269 return null; 270 } 271 } 272 273 280 public static Integer getIntegerPredicate(Property predicate, Resource configuration) { 281 StmtIterator i = configuration.listProperties(predicate); 282 if (i.hasNext()) { 283 RDFNode lit = i.nextStatement().getObject(); 284 if (lit instanceof Literal) { 285 return new Integer (((Literal)lit).getInt()); 286 } 287 } 288 return null; 289 } 290 291 299 public static boolean convertBooleanPredicateArg(Property parameter, Object value) { 300 if (value instanceof Boolean ) { 301 return ((Boolean )value).booleanValue(); 302 } else if (value instanceof String ) { 303 return ((String )value).equalsIgnoreCase("true"); 304 } else { 305 throw new IllegalParameterException("Illegal type for " + parameter + " setting - use a Boolean"); 306 } 307 308 } 309 310 318 public static int convertIntegerPredicateArg(Property parameter, Object value) { 319 if (value instanceof Number ) { 320 return ((Number )value).intValue(); 321 } else if (value instanceof String ) { 322 try { 323 return Integer.parseInt((String )value); 324 } catch (NumberFormatException e) { 325 throw new IllegalParameterException("Illegal type for " + parameter + " setting - use an integer"); 326 } 327 } else { 328 throw new IllegalParameterException("Illegal type for " + parameter + " setting - use an integer"); 329 } 330 } 331 332 338 public static void updateParameter(Resource config, Property parameter, Object value) { 339 for (StmtIterator i = config.listProperties(parameter); i.hasNext(); ) { 340 i.next(); 341 i.remove(); 342 } 343 config.addProperty(parameter, value); 344 } 345 } 346 347 373 374 | Popular Tags |