match.js (4106B)
1 // Matching a statement against a formula 2 // 3 // 4 // W3C open source licence 2005. 5 // 6 // We retpresent a set as an associative array whose value for 7 // each member is set to true. 8 $rdf.Symbol.prototype.sameTerm = function (other) { 9 if(!other) { 10 return false 11 } 12 return((this.termType == other.termType) && (this.uri == other.uri)) 13 } 14 15 $rdf.BlankNode.prototype.sameTerm = function (other) { 16 if(!other) { 17 return false 18 } 19 return((this.termType == other.termType) && (this.id == other.id)) 20 } 21 22 $rdf.Literal.prototype.sameTerm = function (other) { 23 if(!other) { 24 return false 25 } 26 return((this.termType == other.termType) 27 && (this.value == other.value) 28 && (this.lang == other.lang) 29 && ((!this.datatype && !other.datatype) 30 || (this.datatype && this.datatype.sameTerm(other.datatype)))) 31 } 32 33 $rdf.Variable.prototype.sameTerm = function (other) { 34 if(!other) { 35 return false 36 } 37 return((this.termType == other.termType) && (this.uri == other.uri)) 38 } 39 40 $rdf.Collection.prototype.sameTerm = $rdf.BlankNode.prototype.sameTerm 41 42 $rdf.Formula.prototype.sameTerm = function (other) { 43 return this.hashString() == other.hashString(); 44 } 45 // Comparison for ordering 46 // 47 // These compare with ANY term 48 // 49 // 50 // When we smush nodes we take the lowest value. This is not 51 // arbitrary: we want the value actually used to be the literal 52 // (or list or formula). 53 $rdf.Literal.prototype.classOrder = 1 54 $rdf.Collection.prototype.classOrder = 3 55 $rdf.Formula.prototype.classOrder = 4 56 $rdf.Symbol.prototype.classOrder = 5 57 $rdf.BlankNode.prototype.classOrder = 6 58 59 // Compaisons return sign(self - other) 60 // Literals must come out before terms for smushing 61 $rdf.Literal.prototype.compareTerm = function (other) { 62 if(this.classOrder < other.classOrder) return -1 63 if(this.classOrder > other.classOrder) return +1 64 if(this.value < other.value) return -1 65 if(this.value > other.value) return +1 66 return 0 67 } 68 69 $rdf.Symbol.prototype.compareTerm = function (other) { 70 if(this.classOrder < other.classOrder) return -1 71 if(this.classOrder > other.classOrder) return +1 72 if(this.uri < other.uri) return -1 73 if(this.uri > other.uri) return +1 74 return 0 75 } 76 77 $rdf.BlankNode.prototype.compareTerm = function (other) { 78 if(this.classOrder < other.classOrder) return -1 79 if(this.classOrder > other.classOrder) return +1 80 if(this.id < other.id) return -1 81 if(this.id > other.id) return +1 82 return 0 83 } 84 85 $rdf.Collection.prototype.compareTerm = $rdf.BlankNode.prototype.compareTerm 86 87 // Convenience routines 88 // Only one of s p o can be undefined, and w is optional. 89 $rdf.Formula.prototype.each = function (s, p, o, w) { 90 var results = [] 91 var st, sts = this.statementsMatching(s, p, o, w, false) 92 var i, n = sts.length 93 if(typeof s == 'undefined') { 94 for(i = 0; i < n; i++) { 95 st = sts[i]; 96 results.push(st.subject) 97 } 98 } else if(typeof p == 'undefined') { 99 for(i = 0; i < n; i++) { 100 st = sts[i]; 101 results.push(st.predicate) 102 } 103 } else if(typeof o == 'undefined') { 104 for(i = 0; i < n; i++) { 105 st = sts[i]; 106 results.push(st.object) 107 } 108 } else if(typeof w == 'undefined') { 109 for(i = 0; i < n; i++) { 110 st = sts[i]; 111 results.push(st.why) 112 } 113 } 114 return results 115 } 116 117 $rdf.Formula.prototype.any = function (s, p, o, w) { 118 var st = this.anyStatementMatching(s, p, o, w) 119 if(typeof st == 'undefined') return undefined; 120 121 if(typeof s == 'undefined') return st.subject; 122 if(typeof p == 'undefined') return st.predicate; 123 if(typeof o == 'undefined') return st.object; 124 125 return undefined 126 } 127 128 $rdf.Formula.prototype.holds = function (s, p, o, w) { 129 var st = this.anyStatementMatching(s, p, o, w) 130 if(typeof st == 'undefined') return false; 131 return true; 132 } 133 134 $rdf.Formula.prototype.the = function (s, p, o, w) { 135 // the() should contain a check there is only one 136 var x = this.any(s, p, o, w) 137 if(typeof x == 'undefined') 138 $rdf.log.error("No value found for the(){" + s + " " + p + " " + o + "}.") 139 return x 140 } 141 142 $rdf.Formula.prototype.whether = function (s, p, o, w) { 143 return this.statementsMatching(s, p, o, w, false).length; 144 }