1 5 6 package com.hp.hpl.jena.rdql; 7 8 import java.util.* ; 9 10 import com.hp.hpl.jena.graph.*; 11 import com.hp.hpl.jena.rdf.model.* ; 12 13 14 19 20 21 public class ResultBindingImpl implements ResultBinding 22 { 23 26 ResultBindingImpl parent = null ; 27 28 List varNames = new ArrayList() ; 29 List values = new ArrayList() ; List causalTriples = null ; 31 Query query = null ; 32 33 public ResultBindingImpl(ResultBindingImpl parent) { this.parent = parent ; } 34 public ResultBindingImpl() { this(null) ; } 35 36 public int add(String varName, Value value) 37 { 38 varNames.add(varName) ; 39 values.add(value) ; 40 check() ; 41 return varNames.size()-1 ; 42 } 43 44 public int add(String varName, RDFNode node) 45 { 46 varNames.add(varName) ; 47 values.add(node) ; 48 check() ; 49 return varNames.size()-1 ; 50 } 51 52 public void setQuery(Query q) 53 { 54 query = q ; 55 } 56 57 58 61 public void addTriple(Statement s) 63 { 64 if ( causalTriples == null ) 65 causalTriples = new ArrayList() ; 66 causalTriples.add(s) ; 67 } 68 69 74 public Set getTriples() 75 { 76 Set set = new HashSet() ; 77 getTriples(set) ; 78 return set ; 79 } 80 81 private void getTriples(Collection acc) 83 { 84 if ( causalTriples == null ) 85 { 86 causalTriples = new ArrayList() ; 87 88 if ( query != null ) 89 { 90 Model model = query.getSource() ; 91 try { 92 for ( Iterator iter = query.getTriplePatterns().iterator() ; iter.hasNext() ; ) 93 { 94 Triple t1 = (Triple)iter.next() ; 95 Triple t2 = QueryEngine.substituteIntoTriple(t1, this) ; 96 RDFNode s = QueryEngine.convertGraphNodeToRDFNode(t2.getSubject(), model) ; 97 RDFNode p = QueryEngine.convertGraphNodeToRDFNode(t2.getPredicate(), model) ; 98 if ( p instanceof Resource ) 99 p = model.createProperty(((Resource)p).getURI()) ; 100 RDFNode o = QueryEngine.convertGraphNodeToRDFNode(t2.getObject(), model) ; 101 Statement stmt = model.createStatement((Resource)s, (Property)p, o) ; 102 causalTriples.add(stmt) ; 103 } 104 } catch (Exception ex) 105 { 106 System.err.println("ResultBindingImpl.getTriples: Substitution error: "+ex) ; 107 } 108 } 109 } 110 111 if ( causalTriples == null ) 112 return ; 113 114 acc.addAll(causalTriples) ; 115 } 116 117 120 public Model mergeTriples(Model model) 121 { 122 Set s = getTriples() ; 123 for ( Iterator iter = s.iterator() ; iter.hasNext() ; ) 124 { 125 model.add((Statement)iter.next()) ; 126 } 127 return model ; 128 } 129 130 public Iterator names() { return new ResultBindingIterator(this) ; } 131 132 public ResultBindingIterator iterator() 133 { 134 return new ResultBindingIterator(this) ; 135 } 136 137 public Object get(String varName) 138 { 139 return lookup(varName, 0) ; 140 } 141 142 private Object lookup(String varName, int localOffset) 143 { 144 for ( int i = localOffset ; i < varNames.size() ; i++ ) 145 { 146 if ( varName.equals((String )varNames.get(i)) ) 147 { 148 Object obj = values.get(i) ; 150 if ( obj instanceof RDFNode ) 151 return obj ; 152 if ( obj instanceof Node ) 153 return QueryEngine.convertGraphNodeToRDFNode((Node)obj, query.getSource()) ; 154 } 155 } 156 157 if ( parent == null ) 158 return null ; 159 160 return parent.lookup(varName,0) ; 162 } 163 164 165 203 204 205 206 void setParent(ResultBindingImpl p) 207 { 208 parent = p ; 210 } 211 212 public int size() 213 { 214 int size = varNames.size() ; 215 216 if ( parent != null ) 217 size += parent.size() ; 218 219 return size ; 220 } 221 222 public void check() 224 { 225 for (int i = 0 ; i < varNames.size() ; i++ ) 226 { 227 String varName = (String )varNames.get(i) ; 228 Object tmp = lookup(varName, i+1) ; 229 } 231 } 232 233 234 public String toString() 235 { 236 StringBuffer sbuff = new StringBuffer ("") ; 237 for (int i = 0 ; i < varNames.size() ; i++ ) 238 { 239 if ( i != 0 ) 240 sbuff.append(" ") ; 241 sbuff.append("("+varNames.get(i)+", "+values.get(i)+")") ; 242 } 243 244 if ( parent != null ) 245 { 246 String tmp = parent.toString() ; 247 if ( tmp != null && (tmp.length() != 0 ) ) 248 { 249 sbuff.append(" ") ; 250 sbuff.append(tmp) ; 251 } 252 } 253 return sbuff.toString() ; 254 } 255 } 256 257 283 | Popular Tags |