1 10 package com.hp.hpl.jena.reasoner.rdfsReasoner1; 11 12 import com.hp.hpl.jena.reasoner.*; 13 import com.hp.hpl.jena.reasoner.transitiveReasoner.TransitiveReasoner; 14 import com.hp.hpl.jena.graph.*; 15 16 import java.util.*; 17 18 27 public class BaseFRule { 28 29 30 protected TriplePattern head; 31 32 33 protected TriplePattern[] body; 34 35 38 public BaseFRule(TriplePattern head, TriplePattern[] body) { 39 this.head = head; 40 this.body = body; 41 } 42 43 46 public BaseFRule(String spec) { 47 List patterns = parseTripleSequence(spec); 48 head = (TriplePattern) patterns.get(0); 49 body = new TriplePattern[patterns.size() - 1]; 50 for (int i = 1; i < patterns.size(); i++) { 51 body[i-1] = (TriplePattern) patterns.get(i); 52 } 53 } 54 55 59 public void bindAndFire(Triple value, RDFSInfGraph reasoner) { 60 if ((value.getPredicate().equals(TransitiveReasoner.subPropertyOf) || 63 value.getPredicate().equals(TransitiveReasoner.subClassOf)) 64 && value.getSubject().equals(value.getObject())) { 65 return; 67 } 68 Map bindings = new HashMap(); 69 matchNode(value.getSubject(), head.getSubject(), bindings); 70 matchNode(value.getPredicate(), head.getPredicate(), bindings); 71 matchNode(value.getObject(), head.getObject(), bindings); 72 TriplePattern[] newBody = new TriplePattern[body.length]; 74 for (int i = 0; i < body.length; i++) { 75 newBody[i] = new TriplePattern( 76 instantiate(body[i].getSubject(), bindings), 77 instantiate(body[i].getPredicate(), bindings), 78 instantiate(body[i].getObject(), bindings)); 79 } 80 fire(newBody, reasoner); 81 } 82 83 87 void fire(TriplePattern[] body, RDFSInfGraph reasoner) { 88 } 89 90 93 static void matchNode(Node valueNode, Node patternNode, Map bindings) { 94 if (patternNode.isVariable()) { 95 bindings.put(patternNode.getName(), valueNode); 96 } 97 } 98 99 102 static Node instantiate(Node elt, Map bindings) { 103 if (elt.isVariable()) { 104 Node result = (Node) bindings.get(elt.getName()); 105 if (result != null) return result; 106 } 107 return elt; 108 } 109 110 113 public static Triple parseTriple(String spec) { 114 StringTokenizer tokenizer = new StringTokenizer(spec); 115 try { 116 Node s = BRWRule.parseNode(tokenizer.nextToken()); 117 Node p = BRWRule.parseNode(tokenizer.nextToken()); 118 Node o = BRWRule.parseNode(tokenizer.nextToken()); 119 return new Triple(s, p, o); 120 } catch (NoSuchElementException e) { 121 throw new ReasonerException("Illegal triple: " + spec); 122 } 123 } 124 125 128 private static TriplePattern parseTriplePattern(StringTokenizer tokenizer) { 129 try { 130 Node s = BRWRule.parseNode(tokenizer.nextToken()); 131 Node p = BRWRule.parseNode(tokenizer.nextToken()); 132 Node o = BRWRule.parseNode(tokenizer.nextToken()); 133 return new TriplePattern(s, p, o); 134 } catch (NoSuchElementException e) { 135 throw new ReasonerException("Illegal triple in rule"); 136 } 137 } 138 139 144 public static List parseTripleSequence(String spec) { 145 StringTokenizer tokenizer = new StringTokenizer(spec); 146 List triples = new ArrayList(); 147 while (tokenizer.hasMoreElements()) { 148 triples.add(parseTriplePattern(tokenizer)); 149 if (tokenizer.hasMoreElements()) { 150 String sep = tokenizer.nextToken(); 151 if (!sep.equals("|") && !sep.equals("->") && !sep.equals("<-")) { 152 throw new ReasonerException("Illegal FRUle spec: " + spec); 153 } 154 } 155 } 156 return triples; 157 } 158 162 public TriplePattern getHead() { 163 return head; 164 } 165 166 } 167 168 197 | Popular Tags |