mirror of
https://github.com/zotero/zotero.git
synced 2026-10-05 02:43:38 +00:00
Passed through jsbeutifier
This commit is contained in:
parent
87e77593c3
commit
805d3ed6a6
8 changed files with 2869 additions and 2937 deletions
|
|
@ -12,505 +12,489 @@
|
|||
// 2007 Changed so as not to munge statements from documents when smushing
|
||||
//
|
||||
//
|
||||
/*jsl:option explicit*/
|
||||
// Turn on JavaScriptLint variable declaration checking
|
||||
$rdf.IndexedFormula = function () {
|
||||
|
||||
/*jsl:option explicit*/ // Turn on JavaScriptLint variable declaration checking
|
||||
|
||||
$rdf.IndexedFormula = function() {
|
||||
|
||||
var owl_ns = "http://www.w3.org/2002/07/owl#";
|
||||
// var link_ns = "http://www.w3.org/2007/ont/link#";
|
||||
|
||||
/* hashString functions are used as array indeces. This is done to avoid
|
||||
** conflict with existing properties of arrays such as length and map.
|
||||
** See issue 139.
|
||||
*/
|
||||
$rdf.Literal.prototype.hashString = $rdf.Literal.prototype.toNT;
|
||||
$rdf.Symbol.prototype.hashString = $rdf.Symbol.prototype.toNT;
|
||||
$rdf.BlankNode.prototype.hashString = $rdf.BlankNode.prototype.toNT;
|
||||
$rdf.Collection.prototype.hashString = $rdf.Collection.prototype.toNT;
|
||||
var owl_ns = "http://www.w3.org/2002/07/owl#";
|
||||
// var link_ns = "http://www.w3.org/2007/ont/link#";
|
||||
/* hashString functions are used as array indeces. This is done to avoid
|
||||
** conflict with existing properties of arrays such as length and map.
|
||||
** See issue 139.
|
||||
*/
|
||||
$rdf.Literal.prototype.hashString = $rdf.Literal.prototype.toNT;
|
||||
$rdf.Symbol.prototype.hashString = $rdf.Symbol.prototype.toNT;
|
||||
$rdf.BlankNode.prototype.hashString = $rdf.BlankNode.prototype.toNT;
|
||||
$rdf.Collection.prototype.hashString = $rdf.Collection.prototype.toNT;
|
||||
|
||||
|
||||
//Stores an associative array that maps URIs to functions
|
||||
$rdf.IndexedFormula = function(features) {
|
||||
this.statements = []; // As in Formula
|
||||
//Stores an associative array that maps URIs to functions
|
||||
$rdf.IndexedFormula = function (features) {
|
||||
this.statements = []; // As in Formula
|
||||
this.optional = [];
|
||||
this.propertyActions = []; // Array of functions to call when getting statement with {s X o}
|
||||
//maps <uri> to [f(F,s,p,o),...]
|
||||
this.classActions = []; // Array of functions to call when adding { s type X }
|
||||
this.redirections = []; // redirect to lexically smaller equivalent symbol
|
||||
this.aliases = []; // reverse mapping to redirection: aliases for this
|
||||
this.classActions = []; // Array of functions to call when adding { s type X }
|
||||
this.redirections = []; // redirect to lexically smaller equivalent symbol
|
||||
this.aliases = []; // reverse mapping to redirection: aliases for this
|
||||
this.HTTPRedirects = []; // redirections we got from HTTP
|
||||
this.subjectIndex = []; // Array of statements with this X as subject
|
||||
this.predicateIndex = []; // Array of statements with this X as subject
|
||||
this.objectIndex = []; // Array of statements with this X as object
|
||||
this.whyIndex = []; // Array of statements with X as provenance
|
||||
this.index = [ this.subjectIndex, this.predicateIndex, this.objectIndex, this.whyIndex ];
|
||||
this.subjectIndex = []; // Array of statements with this X as subject
|
||||
this.predicateIndex = []; // Array of statements with this X as subject
|
||||
this.objectIndex = []; // Array of statements with this X as object
|
||||
this.whyIndex = []; // Array of statements with X as provenance
|
||||
this.index = [this.subjectIndex, this.predicateIndex, this.objectIndex, this.whyIndex];
|
||||
this.namespaces = {} // Dictionary of namespace prefixes
|
||||
if (features === undefined) features = ["sameAs",
|
||||
"InverseFunctionalProperty", "FunctionalProperty"];
|
||||
// this.features = features
|
||||
if(features === undefined) features = ["sameAs",
|
||||
"InverseFunctionalProperty", "FunctionalProperty"];
|
||||
// this.features = features
|
||||
// Callbackify?
|
||||
function handleRDFType(formula, subj, pred, obj, why) {
|
||||
if (formula.typeCallback != undefined)
|
||||
formula.typeCallback(formula, obj, why);
|
||||
if(formula.typeCallback != undefined)
|
||||
formula.typeCallback(formula, obj, why);
|
||||
|
||||
var x = formula.classActions[obj.hashString()];
|
||||
var done = false;
|
||||
if (x) {
|
||||
for (var i=0; i<x.length; i++) {
|
||||
done = done || x[i](formula, subj, pred, obj, why);
|
||||
}
|
||||
var x = formula.classActions[obj.hashString()];
|
||||
var done = false;
|
||||
if(x) {
|
||||
for(var i = 0; i < x.length; i++) {
|
||||
done = done || x[i](formula, subj, pred, obj, why);
|
||||
}
|
||||
return done; // statement given is not needed if true
|
||||
}
|
||||
return done; // statement given is not needed if true
|
||||
} //handleRDFType
|
||||
|
||||
//If the predicate is #type, use handleRDFType to create a typeCallback on the object
|
||||
this.propertyActions[
|
||||
'<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>'] = [ handleRDFType ];
|
||||
this.propertyActions['<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>'] = [handleRDFType];
|
||||
|
||||
// Assumption: these terms are not redirected @@fixme
|
||||
if ($rdf.Util.ArrayIndexOf(features,"sameAs") >= 0)
|
||||
this.propertyActions['<http://www.w3.org/2002/07/owl#sameAs>'] = [
|
||||
function(formula, subj, pred, obj, why) {
|
||||
// tabulator.log.warn("Equating "+subj.uri+" sameAs "+obj.uri); //@@
|
||||
formula.equate(subj,obj);
|
||||
return true; // true if statement given is NOT needed in the store
|
||||
}]; //sameAs -> equate & don't add to index
|
||||
|
||||
if ($rdf.Util.ArrayIndexOf(features,"InverseFunctionalProperty") >= 0)
|
||||
this.classActions["<"+owl_ns+"InverseFunctionalProperty>"] = [
|
||||
function(formula, subj, pred, obj, addFn) {
|
||||
return formula.newPropertyAction(subj, handle_IFP); // yes subj not pred!
|
||||
}]; //IFP -> handle_IFP, do add to index
|
||||
|
||||
if ($rdf.Util.ArrayIndexOf(features,"FunctionalProperty") >= 0)
|
||||
this.classActions["<"+owl_ns+"FunctionalProperty>"] = [
|
||||
function(formula, subj, proj, obj, addFn) {
|
||||
return formula.newPropertyAction(subj, handle_FP);
|
||||
}]; //FP => handleFP, do add to index
|
||||
|
||||
function handle_IFP(formula, subj, pred, obj) {
|
||||
var s1 = formula.any(undefined, pred, obj);
|
||||
if (s1 == undefined) return false; // First time with this value
|
||||
// tabulator.log.warn("Equating "+s1.uri+" and "+subj.uri + " because IFP "+pred.uri); //@@
|
||||
formula.equate(s1, subj);
|
||||
return true;
|
||||
if($rdf.Util.ArrayIndexOf(features, "sameAs") >= 0)
|
||||
this.propertyActions['<http://www.w3.org/2002/07/owl#sameAs>'] = [
|
||||
function (formula, subj, pred, obj, why) {
|
||||
// tabulator.log.warn("Equating "+subj.uri+" sameAs "+obj.uri); //@@
|
||||
formula.equate(subj, obj);
|
||||
return true; // true if statement given is NOT needed in the store
|
||||
}]; //sameAs -> equate & don't add to index
|
||||
if($rdf.Util.ArrayIndexOf(features, "InverseFunctionalProperty") >= 0)
|
||||
this.classActions["<" + owl_ns + "InverseFunctionalProperty>"] = [
|
||||
function (formula, subj, pred, obj, addFn) {
|
||||
return formula.newPropertyAction(subj, handle_IFP); // yes subj not pred!
|
||||
}]; //IFP -> handle_IFP, do add to index
|
||||
if($rdf.Util.ArrayIndexOf(features, "FunctionalProperty") >= 0)
|
||||
this.classActions["<" + owl_ns + "FunctionalProperty>"] = [
|
||||
function (formula, subj, proj, obj, addFn) {
|
||||
return formula.newPropertyAction(subj, handle_FP);
|
||||
}
|
||||
]; //FP => handleFP, do add to index
|
||||
function handle_IFP(formula, subj, pred, obj) {
|
||||
var s1 = formula.any(undefined, pred, obj);
|
||||
if(s1 == undefined) return false; // First time with this value
|
||||
// tabulator.log.warn("Equating "+s1.uri+" and "+subj.uri + " because IFP "+pred.uri); //@@
|
||||
formula.equate(s1, subj);
|
||||
return true;
|
||||
} //handle_IFP
|
||||
|
||||
function handle_FP(formula, subj, pred, obj) {
|
||||
var o1 = formula.any(subj, pred, undefined);
|
||||
if (o1 == undefined) return false; // First time with this value
|
||||
// tabulator.log.warn("Equating "+o1.uri+" and "+obj.uri + " because FP "+pred.uri); //@@
|
||||
formula.equate(o1, obj);
|
||||
return true ;
|
||||
function handle_FP(formula, subj, pred, obj) {
|
||||
var o1 = formula.any(subj, pred, undefined);
|
||||
if(o1 == undefined) return false; // First time with this value
|
||||
// tabulator.log.warn("Equating "+o1.uri+" and "+obj.uri + " because FP "+pred.uri); //@@
|
||||
formula.equate(o1, obj);
|
||||
return true;
|
||||
} //handle_FP
|
||||
} /* end IndexedFormula */
|
||||
|
||||
} /* end IndexedFormula */
|
||||
$rdf.IndexedFormula.prototype = new $rdf.Formula();
|
||||
$rdf.IndexedFormula.prototype.constructor = $rdf.IndexedFormula;
|
||||
$rdf.IndexedFormula.SuperClass = $rdf.Formula;
|
||||
|
||||
$rdf.IndexedFormula.prototype = new $rdf.Formula();
|
||||
$rdf.IndexedFormula.prototype.constructor = $rdf.IndexedFormula;
|
||||
$rdf.IndexedFormula.SuperClass = $rdf.Formula;
|
||||
|
||||
$rdf.IndexedFormula.prototype.newPropertyAction = function newPropertyAction(pred, action) {
|
||||
$rdf.IndexedFormula.prototype.newPropertyAction = function newPropertyAction(pred, action) {
|
||||
//$rdf.log.debug("newPropertyAction: "+pred);
|
||||
var hash = pred.hashString();
|
||||
if (this.propertyActions[hash] == undefined)
|
||||
this.propertyActions[hash] = [];
|
||||
if(this.propertyActions[hash] == undefined)
|
||||
this.propertyActions[hash] = [];
|
||||
this.propertyActions[hash].push(action);
|
||||
// Now apply the function to to statements already in the store
|
||||
var toBeFixed = this.statementsMatching(undefined, pred, undefined);
|
||||
var done = false;
|
||||
for (var i=0; i<toBeFixed.length; i++) { // NOT optimized - sort toBeFixed etc
|
||||
done = done || action(this, toBeFixed[i].subject, pred, toBeFixed[i].object);
|
||||
for(var i = 0; i < toBeFixed.length; i++) { // NOT optimized - sort toBeFixed etc
|
||||
done = done || action(this, toBeFixed[i].subject, pred, toBeFixed[i].object);
|
||||
}
|
||||
return done;
|
||||
}
|
||||
}
|
||||
|
||||
$rdf.IndexedFormula.prototype.setPrefixForURI = function(prefix, nsuri) {
|
||||
$rdf.IndexedFormula.prototype.setPrefixForURI = function (prefix, nsuri) {
|
||||
//TODO:This is a hack for our own issues, which ought to be fixed post-release
|
||||
//See http://dig.csail.mit.edu/cgi-bin/roundup.cgi/$rdf/issue227
|
||||
if(prefix=="tab" && this.namespaces["tab"]) {
|
||||
return;
|
||||
if(prefix == "tab" && this.namespaces["tab"]) {
|
||||
return;
|
||||
}
|
||||
this.namespaces[prefix] = nsuri
|
||||
}
|
||||
}
|
||||
|
||||
// Deprocated ... name too generic
|
||||
$rdf.IndexedFormula.prototype.register = function(prefix, nsuri) {
|
||||
// Deprocated ... name too generic
|
||||
$rdf.IndexedFormula.prototype.register = function (prefix, nsuri) {
|
||||
this.namespaces[prefix] = nsuri
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/** simplify graph in store when we realize two identifiers are equivalent
|
||||
/** simplify graph in store when we realize two identifiers are equivalent
|
||||
|
||||
We replace the bigger with the smaller.
|
||||
|
||||
*/
|
||||
$rdf.IndexedFormula.prototype.equate = function(u1, u2) {
|
||||
$rdf.IndexedFormula.prototype.equate = function (u1, u2) {
|
||||
// tabulator.log.warn("Equating "+u1+" and "+u2); // @@
|
||||
//@@JAMBO Must canonicalize the uris to prevent errors from a=b=c
|
||||
//03-21-2010
|
||||
u1 = this.canon( u1 );
|
||||
u2 = this.canon( u2 );
|
||||
u1 = this.canon(u1);
|
||||
u2 = this.canon(u2);
|
||||
var d = u1.compareTerm(u2);
|
||||
if (!d) return true; // No information in {a = a}
|
||||
if(!d) return true; // No information in {a = a}
|
||||
var big, small;
|
||||
if (d < 0) { // u1 less than u2
|
||||
return this.replaceWith(u2, u1);
|
||||
if(d < 0) { // u1 less than u2
|
||||
return this.replaceWith(u2, u1);
|
||||
} else {
|
||||
return this.replaceWith(u1, u2);
|
||||
return this.replaceWith(u1, u2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Replace big with small, obsoleted with obsoleting.
|
||||
//
|
||||
$rdf.IndexedFormula.prototype.replaceWith = function(big, small) {
|
||||
// Replace big with small, obsoleted with obsoleting.
|
||||
//
|
||||
$rdf.IndexedFormula.prototype.replaceWith = function (big, small) {
|
||||
//$rdf.log.debug("Replacing "+big+" with "+small) // @@
|
||||
var oldhash = big.hashString();
|
||||
var newhash = small.hashString();
|
||||
|
||||
var moveIndex = function(ix) {
|
||||
var moveIndex = function (ix) {
|
||||
var oldlist = ix[oldhash];
|
||||
if (oldlist == undefined) return; // none to move
|
||||
if(oldlist == undefined) return; // none to move
|
||||
var newlist = ix[newhash];
|
||||
if (newlist == undefined) {
|
||||
ix[newhash] = oldlist;
|
||||
if(newlist == undefined) {
|
||||
ix[newhash] = oldlist;
|
||||
} else {
|
||||
ix[newhash] = oldlist.concat(newlist);
|
||||
ix[newhash] = oldlist.concat(newlist);
|
||||
}
|
||||
delete ix[oldhash];
|
||||
}
|
||||
|
||||
// the canonical one carries all the indexes
|
||||
for (var i=0; i<4; i++) {
|
||||
moveIndex(this.index[i]);
|
||||
delete ix[oldhash];
|
||||
}
|
||||
|
||||
// the canonical one carries all the indexes
|
||||
for(var i = 0; i < 4; i++) {
|
||||
moveIndex(this.index[i]);
|
||||
}
|
||||
|
||||
this.redirections[oldhash] = small;
|
||||
if (big.uri) {
|
||||
//@@JAMBO: must update redirections,aliases from sub-items, too.
|
||||
if (this.aliases[newhash] == undefined)
|
||||
this.aliases[newhash] = [];
|
||||
this.aliases[newhash].push(big); // Back link
|
||||
|
||||
if( this.aliases[oldhash] ) {
|
||||
for( var i = 0; i < this.aliases[oldhash].length; i++ ) {
|
||||
this.redirections[this.aliases[oldhash][i].hashString()] = small;
|
||||
this.aliases[newhash].push(this.aliases[oldhash][i]);
|
||||
}
|
||||
if(big.uri) {
|
||||
//@@JAMBO: must update redirections,aliases from sub-items, too.
|
||||
if(this.aliases[newhash] == undefined)
|
||||
this.aliases[newhash] = [];
|
||||
this.aliases[newhash].push(big); // Back link
|
||||
if(this.aliases[oldhash]) {
|
||||
for(var i = 0; i < this.aliases[oldhash].length; i++) {
|
||||
this.redirections[this.aliases[oldhash][i].hashString()] = small;
|
||||
this.aliases[newhash].push(this.aliases[oldhash][i]);
|
||||
}
|
||||
|
||||
this.add(small, this.sym('http://www.w3.org/2007/ont/link#uri'), big.uri)
|
||||
|
||||
// If two things are equal, and one is requested, we should request the other.
|
||||
if (this.sf) {
|
||||
this.sf.nowKnownAs(big, small)
|
||||
}
|
||||
}
|
||||
|
||||
this.add(small, this.sym('http://www.w3.org/2007/ont/link#uri'), big.uri)
|
||||
|
||||
// If two things are equal, and one is requested, we should request the other.
|
||||
if(this.sf) {
|
||||
this.sf.nowKnownAs(big, small)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
moveIndex(this.classActions);
|
||||
moveIndex(this.propertyActions);
|
||||
|
||||
//$rdf.log.debug("Equate done. "+big+" to be known as "+small)
|
||||
return true; // true means the statement does not need to be put in
|
||||
};
|
||||
return true; // true means the statement does not need to be put in
|
||||
};
|
||||
|
||||
// Return the symbol with canonical URI as smushed
|
||||
$rdf.IndexedFormula.prototype.canon = function(term) {
|
||||
if (term == undefined) return term;
|
||||
// Return the symbol with canonical URI as smushed
|
||||
$rdf.IndexedFormula.prototype.canon = function (term) {
|
||||
if(term == undefined) return term;
|
||||
var y = this.redirections[term.hashString()];
|
||||
if (y == undefined) return term;
|
||||
if(y == undefined) return term;
|
||||
return y;
|
||||
}
|
||||
}
|
||||
|
||||
// Compare by canonical URI as smushed
|
||||
$rdf.IndexedFormula.prototype.sameThings = function(x, y) {
|
||||
if (x.sameTerm(y)) return true;
|
||||
// Compare by canonical URI as smushed
|
||||
$rdf.IndexedFormula.prototype.sameThings = function (x, y) {
|
||||
if(x.sameTerm(y)) return true;
|
||||
var x1 = this.canon(x);
|
||||
// alert('x1='+x1);
|
||||
if (x1 == undefined) return false;
|
||||
// alert('x1='+x1);
|
||||
if(x1 == undefined) return false;
|
||||
var y1 = this.canon(y);
|
||||
// alert('y1='+y1); //@@
|
||||
if (y1 == undefined) return false;
|
||||
return (x1.uri == y1.uri);
|
||||
}
|
||||
// alert('y1='+y1); //@@
|
||||
if(y1 == undefined) return false;
|
||||
return(x1.uri == y1.uri);
|
||||
}
|
||||
|
||||
// A list of all the URIs by which this thing is known
|
||||
$rdf.IndexedFormula.prototype.uris = function(term) {
|
||||
// A list of all the URIs by which this thing is known
|
||||
$rdf.IndexedFormula.prototype.uris = function (term) {
|
||||
var cterm = this.canon(term)
|
||||
var terms = this.aliases[cterm.hashString()];
|
||||
if (!cterm.uri) return []
|
||||
var res = [ cterm.uri ]
|
||||
if (terms != undefined) {
|
||||
for (var i=0; i<terms.length; i++) {
|
||||
res.push(terms[i].uri)
|
||||
}
|
||||
if(!cterm.uri) return []
|
||||
var res = [cterm.uri]
|
||||
if(terms != undefined) {
|
||||
for(var i = 0; i < terms.length; i++) {
|
||||
res.push(terms[i].uri)
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
}
|
||||
|
||||
// On input parameters, convert constants to terms
|
||||
//
|
||||
function RDFMakeTerm(formula,val, canonicalize) {
|
||||
if (typeof val != 'object') {
|
||||
if (typeof val == 'string')
|
||||
return new $rdf.Literal(val);
|
||||
if (typeof val == 'number')
|
||||
return new $rdf.Literal(val); // @@ differet types
|
||||
if (typeof val == 'boolean')
|
||||
return new $rdf.Literal(val?"1":"0", undefined,
|
||||
$rdf.Symbol.prototype.XSDboolean);
|
||||
else if (typeof val == 'number')
|
||||
return new $rdf.Literal(''+val); // @@ datatypes
|
||||
else if (typeof val == 'undefined')
|
||||
return undefined;
|
||||
else // @@ add converting of dates and numbers
|
||||
throw "Can't make Term from " + val + " of type " + typeof val;
|
||||
// On input parameters, convert constants to terms
|
||||
//
|
||||
function RDFMakeTerm(formula, val, canonicalize) {
|
||||
if(typeof val != 'object') {
|
||||
if(typeof val == 'string')
|
||||
return new $rdf.Literal(val);
|
||||
if(typeof val == 'number')
|
||||
return new $rdf.Literal(val); // @@ differet types
|
||||
if(typeof val == 'boolean')
|
||||
return new $rdf.Literal(val ? "1" : "0", undefined, $rdf.Symbol.prototype.XSDboolean);
|
||||
else if(typeof val == 'number')
|
||||
return new $rdf.Literal('' + val); // @@ datatypes
|
||||
else if(typeof val == 'undefined')
|
||||
return undefined;
|
||||
else // @@ add converting of dates and numbers
|
||||
throw "Can't make Term from " + val + " of type " + typeof val;
|
||||
}
|
||||
return val;
|
||||
}
|
||||
}
|
||||
|
||||
// Add a triple to the store
|
||||
//
|
||||
// Returns the statement added
|
||||
// (would it be better to return the original formula for chaining?)
|
||||
//
|
||||
$rdf.IndexedFormula.prototype.add = function(subj, pred, obj, why) {
|
||||
// Add a triple to the store
|
||||
//
|
||||
// Returns the statement added
|
||||
// (would it be better to return the original formula for chaining?)
|
||||
//
|
||||
$rdf.IndexedFormula.prototype.add = function (subj, pred, obj, why) {
|
||||
var actions, st;
|
||||
if (why == undefined) why = this.fetcher ? this.fetcher.appNode: this.sym("chrome:theSession"); //system generated
|
||||
//defined in source.js, is this OK with identity.js only user?
|
||||
if(why == undefined) why = this.fetcher ? this.fetcher.appNode : this.sym("chrome:theSession"); //system generated
|
||||
//defined in source.js, is this OK with identity.js only user?
|
||||
subj = RDFMakeTerm(this, subj);
|
||||
pred = RDFMakeTerm(this, pred);
|
||||
obj = RDFMakeTerm(this, obj);
|
||||
why = RDFMakeTerm(this, why);
|
||||
|
||||
var hash = [ this.canon(subj).hashString(), this.canon(pred).hashString(),
|
||||
this.canon(obj).hashString(), this.canon(why).hashString()];
|
||||
|
||||
var hash = [this.canon(subj).hashString(), this.canon(pred).hashString(),
|
||||
this.canon(obj).hashString(), this.canon(why).hashString()];
|
||||
|
||||
|
||||
if (this.predicateCallback != undefined)
|
||||
this.predicateCallback(this, pred, why);
|
||||
|
||||
if(this.predicateCallback != undefined)
|
||||
this.predicateCallback(this, pred, why);
|
||||
|
||||
// Action return true if the statement does not need to be added
|
||||
var actions = this.propertyActions[hash[1]]; // Predicate hash
|
||||
var done = false;
|
||||
if (actions) {
|
||||
// alert('type: '+typeof actions +' @@ actions='+actions);
|
||||
for (var i=0; i<actions.length; i++) {
|
||||
done = done || actions[i](this, subj, pred, obj, why);
|
||||
}
|
||||
if(actions) {
|
||||
// alert('type: '+typeof actions +' @@ actions='+actions);
|
||||
for(var i = 0; i < actions.length; i++) {
|
||||
done = done || actions[i](this, subj, pred, obj, why);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//If we are tracking provenanance, every thing should be loaded into the store
|
||||
//if (done) return new Statement(subj, pred, obj, why); // Don't put it in the store
|
||||
// still return this statement for owl:sameAs input
|
||||
// still return this statement for owl:sameAs input
|
||||
var st = new $rdf.Statement(subj, pred, obj, why);
|
||||
for (var i=0; i<4; i++) {
|
||||
var ix = this.index[i];
|
||||
var h = hash[i];
|
||||
if (ix[h] == undefined) ix[h] = [];
|
||||
ix[h].push(st); // Set of things with this as subject, etc
|
||||
for(var i = 0; i < 4; i++) {
|
||||
var ix = this.index[i];
|
||||
var h = hash[i];
|
||||
if(ix[h] == undefined) ix[h] = [];
|
||||
ix[h].push(st); // Set of things with this as subject, etc
|
||||
}
|
||||
|
||||
|
||||
//$rdf.log.debug("ADDING {"+subj+" "+pred+" "+obj+"} "+why);
|
||||
this.statements.push(st);
|
||||
return st;
|
||||
}; //add
|
||||
|
||||
|
||||
// Find out whether a given URI is used as symbol in the formula
|
||||
$rdf.IndexedFormula.prototype.mentionsURI = function(uri) {
|
||||
}; //add
|
||||
// Find out whether a given URI is used as symbol in the formula
|
||||
$rdf.IndexedFormula.prototype.mentionsURI = function (uri) {
|
||||
var hash = '<' + uri + '>';
|
||||
return (!!this.subjectIndex[hash] || !!this.objectIndex[hash]
|
||||
|| !!this.predicateIndex[hash]);
|
||||
}
|
||||
return (!!this.subjectIndex[hash]
|
||||
|| !!this.objectIndex[hash]
|
||||
|| !!this.predicateIndex[hash]);
|
||||
}
|
||||
|
||||
// Find an unused id for a file being edited: return a symbol
|
||||
// (Note: Slow iff a lot of them -- could be O(log(k)) )
|
||||
$rdf.IndexedFormula.prototype.nextSymbol = function(doc) {
|
||||
for(var i=0;;i++) {
|
||||
var uri = doc.uri + '#n' + i;
|
||||
if (!this.mentionsURI(uri)) return this.sym(uri);
|
||||
// Find an unused id for a file being edited: return a symbol
|
||||
// (Note: Slow iff a lot of them -- could be O(log(k)) )
|
||||
$rdf.IndexedFormula.prototype.nextSymbol = function (doc) {
|
||||
for(var i = 0;; i++) {
|
||||
var uri = doc.uri + '#n' + i;
|
||||
if(!this.mentionsURI(uri)) return this.sym(uri);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$rdf.IndexedFormula.prototype.anyStatementMatching = function(subj,pred,obj,why) {
|
||||
var x = this.statementsMatching(subj,pred,obj,why,true);
|
||||
if (!x || x == []) return undefined;
|
||||
$rdf.IndexedFormula.prototype.anyStatementMatching = function (subj, pred, obj, why) {
|
||||
var x = this.statementsMatching(subj, pred, obj, why, true);
|
||||
if(!x || x == []) return undefined;
|
||||
return x[0];
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
// Return statements matching a pattern
|
||||
// ALL CONVENIENCE LOOKUP FUNCTIONS RELY ON THIS!
|
||||
$rdf.IndexedFormula.prototype.statementsMatching = function(subj,pred,obj,why,justOne) {
|
||||
// Return statements matching a pattern
|
||||
// ALL CONVENIENCE LOOKUP FUNCTIONS RELY ON THIS!
|
||||
$rdf.IndexedFormula.prototype.statementsMatching = function (subj, pred, obj, why, justOne) {
|
||||
//$rdf.log.debug("Matching {"+subj+" "+pred+" "+obj+"}");
|
||||
|
||||
var pat = [ subj, pred, obj, why ];
|
||||
var pat = [subj, pred, obj, why];
|
||||
var pattern = [];
|
||||
var hash = [];
|
||||
var wild = []; // wildcards
|
||||
var given = []; // Not wild
|
||||
for (var p=0; p<4; p++) {
|
||||
pattern[p] = this.canon(RDFMakeTerm(this, pat[p]));
|
||||
if (pattern[p] == undefined) {
|
||||
wild.push(p);
|
||||
} else {
|
||||
given.push(p);
|
||||
hash[p] = pattern[p].hashString();
|
||||
}
|
||||
for(var p = 0; p < 4; p++) {
|
||||
pattern[p] = this.canon(RDFMakeTerm(this, pat[p]));
|
||||
if(pattern[p] == undefined) {
|
||||
wild.push(p);
|
||||
} else {
|
||||
given.push(p);
|
||||
hash[p] = pattern[p].hashString();
|
||||
}
|
||||
}
|
||||
if (given.length == 0) {
|
||||
return this.statements;
|
||||
if(given.length == 0) {
|
||||
return this.statements;
|
||||
}
|
||||
if (given.length == 1) { // Easy too, we have an index for that
|
||||
var p = given[0];
|
||||
var list = this.index[p][hash[p]];
|
||||
if(list && justOne) {
|
||||
if(list.length>1)
|
||||
list = list.slice(0,1);
|
||||
}
|
||||
return list == undefined ? [] : list;
|
||||
if(given.length == 1) { // Easy too, we have an index for that
|
||||
var p = given[0];
|
||||
var list = this.index[p][hash[p]];
|
||||
if(list && justOne) {
|
||||
if(list.length > 1)
|
||||
list = list.slice(0, 1);
|
||||
}
|
||||
return list == undefined ? [] : list;
|
||||
}
|
||||
|
||||
|
||||
// Now given.length is 2, 3 or 4.
|
||||
// We hope that the scale-free nature of the data will mean we tend to get
|
||||
// a short index in there somewhere!
|
||||
|
||||
var best = 1e10; // really bad
|
||||
var best_i;
|
||||
for (var i=0; i<given.length; i++) {
|
||||
var p = given[i]; // Which part we are dealing with
|
||||
var list = this.index[p][hash[p]];
|
||||
if (list == undefined) return []; // No occurrences
|
||||
if (list.length < best) {
|
||||
best = list.length;
|
||||
best_i = i; // (not p!)
|
||||
}
|
||||
for(var i = 0; i < given.length; i++) {
|
||||
var p = given[i]; // Which part we are dealing with
|
||||
var list = this.index[p][hash[p]];
|
||||
if(list == undefined) return []; // No occurrences
|
||||
if(list.length < best) {
|
||||
best = list.length;
|
||||
best_i = i; // (not p!)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Ok, we have picked the shortest index but now we have to filter it
|
||||
var best_p = given[best_i];
|
||||
var possibles = this.index[best_p][hash[best_p]];
|
||||
var check = given.slice(0, best_i).concat(given.slice(best_i+1)) // remove best_i
|
||||
var check = given.slice(0, best_i).concat(given.slice(best_i + 1)) // remove best_i
|
||||
var results = [];
|
||||
var parts = [ 'subject', 'predicate', 'object', 'why'];
|
||||
for (var j=0; j<possibles.length; j++) {
|
||||
var st = possibles[j];
|
||||
for (var i=0; i <check.length; i++) { // for each position to be checked
|
||||
var p = check[i];
|
||||
if (!this.canon(st[parts[p]]).sameTerm(pattern[p])) {
|
||||
st = null;
|
||||
break;
|
||||
}
|
||||
var parts = ['subject', 'predicate', 'object', 'why'];
|
||||
for(var j = 0; j < possibles.length; j++) {
|
||||
var st = possibles[j];
|
||||
for(var i = 0; i < check.length; i++) { // for each position to be checked
|
||||
var p = check[i];
|
||||
if(!this.canon(st[parts[p]]).sameTerm(pattern[p])) {
|
||||
st = null;
|
||||
break;
|
||||
}
|
||||
if (st != null) results.push(st);
|
||||
}
|
||||
if(st != null) results.push(st);
|
||||
}
|
||||
|
||||
if(justOne) {
|
||||
if(results.length>1)
|
||||
results = results.slice(0,1);
|
||||
if(results.length > 1)
|
||||
results = results.slice(0, 1);
|
||||
}
|
||||
return results;
|
||||
}; // statementsMatching
|
||||
|
||||
/** remove a particular statement from the bank **/
|
||||
$rdf.IndexedFormula.prototype.remove = function (st) {
|
||||
}; // statementsMatching
|
||||
/** remove a particular statement from the bank **/
|
||||
$rdf.IndexedFormula.prototype.remove = function (st) {
|
||||
//$rdf.log.debug("entering remove w/ st=" + st);
|
||||
var term = [ st.subject, st.predicate, st.object, st.why];
|
||||
for (var p=0; p<4; p++) {
|
||||
var c = this.canon(term[p]);
|
||||
var h = c.hashString();
|
||||
if (this.index[p][h] == undefined) {
|
||||
//$rdf.log.warn ("Statement removal: no index '+p+': "+st);
|
||||
} else {
|
||||
$rdf.Util.RDFArrayRemove(this.index[p][h], st);
|
||||
}
|
||||
var term = [st.subject, st.predicate, st.object, st.why];
|
||||
for(var p = 0; p < 4; p++) {
|
||||
var c = this.canon(term[p]);
|
||||
var h = c.hashString();
|
||||
if(this.index[p][h] == undefined) {
|
||||
//$rdf.log.warn ("Statement removal: no index '+p+': "+st);
|
||||
} else {
|
||||
$rdf.Util.RDFArrayRemove(this.index[p][h], st);
|
||||
}
|
||||
}
|
||||
$rdf.Util.RDFArrayRemove(this.statements, st);
|
||||
}; //remove
|
||||
|
||||
/** remove all statements matching args (within limit) **/
|
||||
$rdf.IndexedFormula.prototype.removeMany = function (subj, pred, obj, why, limit) {
|
||||
}; //remove
|
||||
/** remove all statements matching args (within limit) **/
|
||||
$rdf.IndexedFormula.prototype.removeMany = function (subj, pred, obj, why, limit) {
|
||||
//$rdf.log.debug("entering removeMany w/ subj,pred,obj,why,limit = " + subj +", "+ pred+", " + obj+", " + why+", " + limit);
|
||||
var sts = this.statementsMatching (subj, pred, obj, why, false);
|
||||
var sts = this.statementsMatching(subj, pred, obj, why, false);
|
||||
//This is a subtle bug that occcured in updateCenter.js too.
|
||||
//The fact is, this.statementsMatching returns this.whyIndex instead of a copy of it
|
||||
//but for perfromance consideration, it's better to just do that
|
||||
//so make a copy here.
|
||||
var statements = [];
|
||||
for (var i=0;i<sts.length;i++) statements.push(sts[i]);
|
||||
if (limit) statements = statements.slice(0, limit);
|
||||
for (var i=0;i<statements.length;i++) this.remove(statements[i]);
|
||||
}; //removeMany
|
||||
for(var i = 0; i < sts.length; i++) statements.push(sts[i]);
|
||||
if(limit) statements = statements.slice(0, limit);
|
||||
for(var i = 0; i < statements.length; i++) this.remove(statements[i]);
|
||||
}; //removeMany
|
||||
/** Utility**/
|
||||
|
||||
/** Utility**/
|
||||
|
||||
/* @method: copyTo
|
||||
/* @method: copyTo
|
||||
@description: replace @template with @target and add appropriate triples (no triple removed)
|
||||
one-direction replication
|
||||
*/
|
||||
$rdf.IndexedFormula.prototype.copyTo = function(template,target,flags){
|
||||
if (!flags) flags=[];
|
||||
var statList=this.statementsMatching(template);
|
||||
if ($rdf.Util.ArrayIndexOf(flags,'two-direction')!=-1)
|
||||
statList.concat(this.statementsMatching(undefined,undefined,template));
|
||||
for (var i=0;i<statList.length;i++){
|
||||
var st=statList[i];
|
||||
switch (st.object.termType){
|
||||
case 'symbol':
|
||||
this.add(target,st.predicate,st.object);
|
||||
break;
|
||||
case 'literal':
|
||||
case 'bnode':
|
||||
case 'collection':
|
||||
this.add(target,st.predicate,st.object.copy(this));
|
||||
}
|
||||
if ($rdf.Util.ArrayIndexOf(flags,'delete')!=-1) this.remove(st);
|
||||
*/
|
||||
$rdf.IndexedFormula.prototype.copyTo = function (template, target, flags) {
|
||||
if(!flags) flags = [];
|
||||
var statList = this.statementsMatching(template);
|
||||
if($rdf.Util.ArrayIndexOf(flags, 'two-direction') != -1)
|
||||
statList.concat(this.statementsMatching(undefined, undefined, template));
|
||||
for(var i = 0; i < statList.length; i++) {
|
||||
var st = statList[i];
|
||||
switch(st.object.termType) {
|
||||
case 'symbol':
|
||||
this.add(target, st.predicate, st.object);
|
||||
break;
|
||||
case 'literal':
|
||||
case 'bnode':
|
||||
case 'collection':
|
||||
this.add(target, st.predicate, st.object.copy(this));
|
||||
}
|
||||
if($rdf.Util.ArrayIndexOf(flags, 'delete') != -1) this.remove(st);
|
||||
}
|
||||
};
|
||||
//for the case when you alter this.value (text modified in userinput.js)
|
||||
$rdf.Literal.prototype.copy = function(){
|
||||
return new $rdf.Literal(this.value,this.lang,this.datatype);
|
||||
};
|
||||
$rdf.BlankNode.prototype.copy = function(formula){ //depends on the formula
|
||||
var bnodeNew=new $rdf.BlankNode();
|
||||
formula.copyTo(this,bnodeNew);
|
||||
};
|
||||
//for the case when you alter this.value (text modified in userinput.js)
|
||||
$rdf.Literal.prototype.copy = function () {
|
||||
return new $rdf.Literal(this.value, this.lang, this.datatype);
|
||||
};
|
||||
$rdf.BlankNode.prototype.copy = function (formula) { //depends on the formula
|
||||
var bnodeNew = new $rdf.BlankNode();
|
||||
formula.copyTo(this, bnodeNew);
|
||||
return bnodeNew;
|
||||
}
|
||||
/** Full N3 bits -- placeholders only to allow parsing, no functionality! **/
|
||||
}
|
||||
/** Full N3 bits -- placeholders only to allow parsing, no functionality! **/
|
||||
|
||||
$rdf.IndexedFormula.prototype.newUniversal = function(uri) {
|
||||
$rdf.IndexedFormula.prototype.newUniversal = function (uri) {
|
||||
var x = this.sym(uri);
|
||||
if (!this._universalVariables) this._universalVariables = [];
|
||||
if(!this._universalVariables) this._universalVariables = [];
|
||||
this._universalVariables.push(x);
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
$rdf.IndexedFormula.prototype.newExistential = function(uri) {
|
||||
if (!uri) return this.bnode();
|
||||
$rdf.IndexedFormula.prototype.newExistential = function (uri) {
|
||||
if(!uri) return this.bnode();
|
||||
var x = this.sym(uri);
|
||||
return this.declareExistential(x);
|
||||
}
|
||||
}
|
||||
|
||||
$rdf.IndexedFormula.prototype.declareExistential = function(x) {
|
||||
if (!this._existentialVariables) this._existentialVariables = [];
|
||||
$rdf.IndexedFormula.prototype.declareExistential = function (x) {
|
||||
if(!this._existentialVariables) this._existentialVariables = [];
|
||||
this._existentialVariables.push(x);
|
||||
return x;
|
||||
}
|
||||
}
|
||||
|
||||
$rdf.IndexedFormula.prototype.formula = function(features) {
|
||||
$rdf.IndexedFormula.prototype.formula = function (features) {
|
||||
return new $rdf.IndexedFormula(features);
|
||||
}
|
||||
}
|
||||
|
||||
$rdf.IndexedFormula.prototype.close = function() {
|
||||
$rdf.IndexedFormula.prototype.close = function () {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
$rdf.IndexedFormula.prototype.hashString = $rdf.IndexedFormula.prototype.toNT;
|
||||
$rdf.IndexedFormula.prototype.hashString = $rdf.IndexedFormula.prototype.toNT;
|
||||
|
||||
return $rdf.IndexedFormula;
|
||||
return $rdf.IndexedFormula;
|
||||
|
||||
}();
|
||||
// ends
|
||||
// ends
|
||||
|
|
@ -1,32 +1,32 @@
|
|||
/* Set up the environment before loading the rest of the files into Zotero */
|
||||
var $rdf = {
|
||||
Util: {
|
||||
ArrayIndexOf: function (arr, item, i) {
|
||||
//supported in all browsers except IE<9
|
||||
return arr.indexOf(item, i);
|
||||
},
|
||||
RDFArrayRemove: function(a, x) { //removes all statements equal to x from a
|
||||
for(var i=0; i<a.length; i++) {
|
||||
//TODO: This used to be the following, which didnt always work..why
|
||||
//if(a[i] == x)
|
||||
if (a[i].subject.sameTerm( x.subject ) &&
|
||||
a[i].predicate.sameTerm( x.predicate ) &&
|
||||
a[i].object.sameTerm( x.object ) &&
|
||||
a[i].why.sameTerm( x.why )) {
|
||||
a.splice(i,1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw "RDFArrayRemove: Array did not contain " + x;
|
||||
},
|
||||
},
|
||||
Util: {
|
||||
ArrayIndexOf: function (arr, item, i) {
|
||||
//supported in all browsers except IE<9
|
||||
return arr.indexOf(item, i);
|
||||
},
|
||||
RDFArrayRemove: function (a, x) { //removes all statements equal to x from a
|
||||
for(var i = 0; i < a.length; i++) {
|
||||
//TODO: This used to be the following, which didnt always work..why
|
||||
//if(a[i] == x)
|
||||
if(a[i].subject.sameTerm(x.subject)
|
||||
&& a[i].predicate.sameTerm(x.predicate)
|
||||
&& a[i].object.sameTerm(x.object)
|
||||
&& a[i].why.sameTerm(x.why)) {
|
||||
a.splice(i, 1);
|
||||
return;
|
||||
}
|
||||
}
|
||||
throw "RDFArrayRemove: Array did not contain " + x;
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
var tabulator = {
|
||||
log: {
|
||||
debug: Zotero.debug,
|
||||
warn: Zotero.debug
|
||||
}
|
||||
log: {
|
||||
debug: Zotero.debug,
|
||||
warn: Zotero.debug
|
||||
}
|
||||
};
|
||||
|
||||
var alert = tabulator.log.warn;
|
||||
|
|
@ -5,36 +5,42 @@
|
|||
//
|
||||
// We retpresent a set as an associative array whose value for
|
||||
// each member is set to true.
|
||||
|
||||
|
||||
$rdf.Symbol.prototype.sameTerm = function(other) {
|
||||
if (!other) { return false }
|
||||
return ((this.termType == other.termType) && (this.uri == other.uri))
|
||||
$rdf.Symbol.prototype.sameTerm = function (other) {
|
||||
if(!other) {
|
||||
return false
|
||||
}
|
||||
return((this.termType == other.termType) && (this.uri == other.uri))
|
||||
}
|
||||
|
||||
$rdf.BlankNode.prototype.sameTerm = function(other) {
|
||||
if (!other) { return false }
|
||||
return ((this.termType == other.termType) && (this.id == other.id))
|
||||
$rdf.BlankNode.prototype.sameTerm = function (other) {
|
||||
if(!other) {
|
||||
return false
|
||||
}
|
||||
return((this.termType == other.termType) && (this.id == other.id))
|
||||
}
|
||||
|
||||
$rdf.Literal.prototype.sameTerm = function(other) {
|
||||
if (!other) { return false }
|
||||
return ((this.termType == other.termType)
|
||||
&& (this.value == other.value)
|
||||
&& (this.lang == other.lang) &&
|
||||
((!this.datatype && !other.datatype)
|
||||
|| (this.datatype && this.datatype.sameTerm(other.datatype))))
|
||||
$rdf.Literal.prototype.sameTerm = function (other) {
|
||||
if(!other) {
|
||||
return false
|
||||
}
|
||||
return((this.termType == other.termType)
|
||||
&& (this.value == other.value)
|
||||
&& (this.lang == other.lang)
|
||||
&& ((!this.datatype && !other.datatype)
|
||||
|| (this.datatype && this.datatype.sameTerm(other.datatype))))
|
||||
}
|
||||
|
||||
$rdf.Variable.prototype.sameTerm = function (other) {
|
||||
if (!other) { return false }
|
||||
return((this.termType == other.termType) && (this.uri == other.uri))
|
||||
if(!other) {
|
||||
return false
|
||||
}
|
||||
return((this.termType == other.termType) && (this.uri == other.uri))
|
||||
}
|
||||
|
||||
$rdf.Collection.prototype.sameTerm = $rdf.BlankNode.prototype.sameTerm
|
||||
|
||||
$rdf.Formula.prototype.sameTerm = function (other) {
|
||||
return this.hashString() == other.hashString();
|
||||
return this.hashString() == other.hashString();
|
||||
}
|
||||
// Comparison for ordering
|
||||
//
|
||||
|
|
@ -44,7 +50,6 @@ $rdf.Formula.prototype.sameTerm = function (other) {
|
|||
// When we smush nodes we take the lowest value. This is not
|
||||
// arbitrary: we want the value actually used to be the literal
|
||||
// (or list or formula).
|
||||
|
||||
$rdf.Literal.prototype.classOrder = 1
|
||||
$rdf.Collection.prototype.classOrder = 3
|
||||
$rdf.Formula.prototype.classOrder = 4
|
||||
|
|
@ -53,77 +58,87 @@ $rdf.BlankNode.prototype.classOrder = 6
|
|||
|
||||
// Compaisons return sign(self - other)
|
||||
// Literals must come out before terms for smushing
|
||||
$rdf.Literal.prototype.compareTerm = function (other) {
|
||||
if(this.classOrder < other.classOrder) return -1
|
||||
if(this.classOrder > other.classOrder) return +1
|
||||
if(this.value < other.value) return -1
|
||||
if(this.value > other.value) return +1
|
||||
return 0
|
||||
}
|
||||
|
||||
$rdf.Literal.prototype.compareTerm = function(other) {
|
||||
if (this.classOrder < other.classOrder) return -1
|
||||
if (this.classOrder > other.classOrder) return +1
|
||||
if (this.value < other.value) return -1
|
||||
if (this.value > other.value) return +1
|
||||
return 0
|
||||
}
|
||||
$rdf.Symbol.prototype.compareTerm = function (other) {
|
||||
if(this.classOrder < other.classOrder) return -1
|
||||
if(this.classOrder > other.classOrder) return +1
|
||||
if(this.uri < other.uri) return -1
|
||||
if(this.uri > other.uri) return +1
|
||||
return 0
|
||||
}
|
||||
|
||||
$rdf.Symbol.prototype.compareTerm = function(other) {
|
||||
if (this.classOrder < other.classOrder) return -1
|
||||
if (this.classOrder > other.classOrder) return +1
|
||||
if (this.uri < other.uri) return -1
|
||||
if (this.uri > other.uri) return +1
|
||||
return 0
|
||||
}
|
||||
|
||||
$rdf.BlankNode.prototype.compareTerm = function(other) {
|
||||
if (this.classOrder < other.classOrder) return -1
|
||||
if (this.classOrder > other.classOrder) return +1
|
||||
if (this.id < other.id) return -1
|
||||
if (this.id > other.id) return +1
|
||||
return 0
|
||||
}
|
||||
$rdf.BlankNode.prototype.compareTerm = function (other) {
|
||||
if(this.classOrder < other.classOrder) return -1
|
||||
if(this.classOrder > other.classOrder) return +1
|
||||
if(this.id < other.id) return -1
|
||||
if(this.id > other.id) return +1
|
||||
return 0
|
||||
}
|
||||
|
||||
$rdf.Collection.prototype.compareTerm = $rdf.BlankNode.prototype.compareTerm
|
||||
|
||||
// Convenience routines
|
||||
|
||||
// Only one of s p o can be undefined, and w is optional.
|
||||
$rdf.Formula.prototype.each = function(s,p,o,w) {
|
||||
var results = []
|
||||
var st, sts = this.statementsMatching(s,p,o,w,false)
|
||||
var i, n=sts.length
|
||||
if (typeof s == 'undefined') {
|
||||
for (i=0; i<n; i++) {st=sts[i]; results.push(st.subject)}
|
||||
} else if (typeof p == 'undefined') {
|
||||
for (i=0; i<n; i++) {st=sts[i]; results.push(st.predicate)}
|
||||
} else if (typeof o == 'undefined') {
|
||||
for (i=0; i<n; i++) {st=sts[i]; results.push(st.object)}
|
||||
} else if (typeof w == 'undefined') {
|
||||
for (i=0; i<n; i++) {st=sts[i]; results.push(st.why)}
|
||||
$rdf.Formula.prototype.each = function (s, p, o, w) {
|
||||
var results = []
|
||||
var st, sts = this.statementsMatching(s, p, o, w, false)
|
||||
var i, n = sts.length
|
||||
if(typeof s == 'undefined') {
|
||||
for(i = 0; i < n; i++) {
|
||||
st = sts[i];
|
||||
results.push(st.subject)
|
||||
}
|
||||
return results
|
||||
} else if(typeof p == 'undefined') {
|
||||
for(i = 0; i < n; i++) {
|
||||
st = sts[i];
|
||||
results.push(st.predicate)
|
||||
}
|
||||
} else if(typeof o == 'undefined') {
|
||||
for(i = 0; i < n; i++) {
|
||||
st = sts[i];
|
||||
results.push(st.object)
|
||||
}
|
||||
} else if(typeof w == 'undefined') {
|
||||
for(i = 0; i < n; i++) {
|
||||
st = sts[i];
|
||||
results.push(st.why)
|
||||
}
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
$rdf.Formula.prototype.any = function(s,p,o,w) {
|
||||
var st = this.anyStatementMatching(s,p,o,w)
|
||||
if (typeof st == 'undefined') return undefined;
|
||||
|
||||
if (typeof s == 'undefined') return st.subject;
|
||||
if (typeof p == 'undefined') return st.predicate;
|
||||
if (typeof o == 'undefined') return st.object;
|
||||
$rdf.Formula.prototype.any = function (s, p, o, w) {
|
||||
var st = this.anyStatementMatching(s, p, o, w)
|
||||
if(typeof st == 'undefined') return undefined;
|
||||
|
||||
return undefined
|
||||
if(typeof s == 'undefined') return st.subject;
|
||||
if(typeof p == 'undefined') return st.predicate;
|
||||
if(typeof o == 'undefined') return st.object;
|
||||
|
||||
return undefined
|
||||
}
|
||||
|
||||
$rdf.Formula.prototype.holds = function(s,p,o,w) {
|
||||
var st = this.anyStatementMatching(s,p,o,w)
|
||||
if (typeof st == 'undefined') return false;
|
||||
return true;
|
||||
$rdf.Formula.prototype.holds = function (s, p, o, w) {
|
||||
var st = this.anyStatementMatching(s, p, o, w)
|
||||
if(typeof st == 'undefined') return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
$rdf.Formula.prototype.the = function(s,p,o,w) {
|
||||
// the() should contain a check there is only one
|
||||
var x = this.any(s,p,o,w)
|
||||
if (typeof x == 'undefined')
|
||||
$rdf.log.error("No value found for the(){" + s + " " + p + " " + o + "}.")
|
||||
return x
|
||||
$rdf.Formula.prototype.the = function (s, p, o, w) {
|
||||
// the() should contain a check there is only one
|
||||
var x = this.any(s, p, o, w)
|
||||
if(typeof x == 'undefined')
|
||||
$rdf.log.error("No value found for the(){" + s + " " + p + " " + o + "}.")
|
||||
return x
|
||||
}
|
||||
|
||||
$rdf.Formula.prototype.whether = function(s,p,o,w) {
|
||||
return this.statementsMatching(s,p,o,w,false).length;
|
||||
}
|
||||
$rdf.Formula.prototype.whether = function (s, p, o, w) {
|
||||
return this.statementsMatching(s, p, o, w, false).length;
|
||||
}
|
||||
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
File diff suppressed because it is too large
Load diff
|
|
@ -7,25 +7,27 @@
|
|||
//
|
||||
// W3C open source licence 2005.
|
||||
//
|
||||
|
||||
// Symbol
|
||||
|
||||
$rdf.Empty = function() {
|
||||
return this;
|
||||
$rdf.Empty = function () {
|
||||
return this;
|
||||
};
|
||||
|
||||
$rdf.Empty.prototype.termType = 'empty';
|
||||
$rdf.Empty.prototype.toString = function () { return "()" };
|
||||
$rdf.Empty.prototype.toString = function () {
|
||||
return "()"
|
||||
};
|
||||
$rdf.Empty.prototype.toNT = $rdf.Empty.prototype.toString;
|
||||
|
||||
$rdf.Symbol = function( uri ) {
|
||||
this.uri = uri;
|
||||
this.value = uri; // -- why? -tim
|
||||
return this;
|
||||
$rdf.Symbol = function (uri) {
|
||||
this.uri = uri;
|
||||
this.value = uri; // -- why? -tim
|
||||
return this;
|
||||
}
|
||||
|
||||
$rdf.Symbol.prototype.termType = 'symbol';
|
||||
$rdf.Symbol.prototype.toString = function () { return ("<" + this.uri + ">"); };
|
||||
$rdf.Symbol.prototype.toString = function () {
|
||||
return("<" + this.uri + ">");
|
||||
};
|
||||
$rdf.Symbol.prototype.toNT = $rdf.Symbol.prototype.toString;
|
||||
|
||||
// Some precalculated symbols
|
||||
|
|
@ -35,132 +37,131 @@ $rdf.Symbol.prototype.XSDfloat = new $rdf.Symbol('http://www.w3.org/2001/XMLSche
|
|||
$rdf.Symbol.prototype.XSDinteger = new $rdf.Symbol('http://www.w3.org/2001/XMLSchema#integer');
|
||||
$rdf.Symbol.prototype.XSDdateTime = new $rdf.Symbol('http://www.w3.org/2001/XMLSchema#dateTime');
|
||||
$rdf.Symbol.prototype.integer = new $rdf.Symbol('http://www.w3.org/2001/XMLSchema#integer'); // Used?
|
||||
|
||||
// Blank Node
|
||||
|
||||
if (typeof $rdf.NextId != 'undefined') {
|
||||
$rdf.log.error('Attempt to re-zero existing blank node id counter at '+$rdf.NextId);
|
||||
if(typeof $rdf.NextId != 'undefined') {
|
||||
$rdf.log.error('Attempt to re-zero existing blank node id counter at ' + $rdf.NextId);
|
||||
} else {
|
||||
$rdf.NextId = 0; // Global genid
|
||||
$rdf.NextId = 0; // Global genid
|
||||
}
|
||||
$rdf.NTAnonymousNodePrefix = "_:n";
|
||||
|
||||
$rdf.BlankNode = function ( id ) {
|
||||
/*if (id)
|
||||
$rdf.BlankNode = function (id) {
|
||||
/*if (id)
|
||||
this.id = id;
|
||||
else*/
|
||||
this.id = $rdf.NextId++
|
||||
this.value = id ? id : this.id.toString();
|
||||
return this
|
||||
this.id = $rdf.NextId++;
|
||||
this.value = id ? id : this.id.toString();
|
||||
return this
|
||||
};
|
||||
|
||||
$rdf.BlankNode.prototype.termType = 'bnode';
|
||||
$rdf.BlankNode.prototype.toNT = function() {
|
||||
return $rdf.NTAnonymousNodePrefix + this.id
|
||||
$rdf.BlankNode.prototype.toNT = function () {
|
||||
return $rdf.NTAnonymousNodePrefix + this.id
|
||||
};
|
||||
$rdf.BlankNode.prototype.toString = $rdf.BlankNode.prototype.toNT;
|
||||
|
||||
// Literal
|
||||
|
||||
$rdf.Literal = function (value, lang, datatype) {
|
||||
this.value = value
|
||||
if (lang == "" || lang == null) this.lang = undefined;
|
||||
else this.lang = lang; // string
|
||||
if (datatype == null) this.datatype = undefined;
|
||||
else this.datatype = datatype; // term
|
||||
return this;
|
||||
this.value = value
|
||||
if(lang == "" || lang == null) this.lang = undefined;
|
||||
else this.lang = lang; // string
|
||||
if(datatype == null) this.datatype = undefined;
|
||||
else this.datatype = datatype; // term
|
||||
return this;
|
||||
}
|
||||
|
||||
$rdf.Literal.prototype.termType = 'literal'
|
||||
$rdf.Literal.prototype.toString = function() {
|
||||
return ''+this.value;
|
||||
$rdf.Literal.prototype.termType = 'literal'
|
||||
$rdf.Literal.prototype.toString = function () {
|
||||
return '' + this.value;
|
||||
};
|
||||
$rdf.Literal.prototype.toNT = function() {
|
||||
var str = this.value
|
||||
if (typeof str != 'string') {
|
||||
if (typeof str == 'number') return ''+str;
|
||||
throw Error("Value of RDF literal is not string: "+str)
|
||||
}
|
||||
str = str.replace(/\\/g, '\\\\'); // escape backslashes
|
||||
str = str.replace(/\"/g, '\\"'); // escape quotes
|
||||
str = str.replace(/\n/g, '\\n'); // escape newlines
|
||||
str = '"' + str + '"' //';
|
||||
|
||||
if (this.datatype){
|
||||
str = str + '^^' + this.datatype.toNT()
|
||||
}
|
||||
if (this.lang) {
|
||||
str = str + "@" + this.lang;
|
||||
}
|
||||
return str;
|
||||
$rdf.Literal.prototype.toNT = function () {
|
||||
var str = this.value
|
||||
if(typeof str != 'string') {
|
||||
if(typeof str == 'number') return '' + str;
|
||||
throw Error("Value of RDF literal is not string: " + str)
|
||||
}
|
||||
str = str.replace(/\\/g, '\\\\'); // escape backslashes
|
||||
str = str.replace(/\"/g, '\\"'); // escape quotes
|
||||
str = str.replace(/\n/g, '\\n'); // escape newlines
|
||||
str = '"' + str + '"' //';
|
||||
if(this.datatype) {
|
||||
str = str + '^^' + this.datatype.toNT()
|
||||
}
|
||||
if(this.lang) {
|
||||
str = str + "@" + this.lang;
|
||||
}
|
||||
return str;
|
||||
};
|
||||
|
||||
$rdf.Collection = function() {
|
||||
this.id = $rdf.NextId++; // Why need an id? For hashstring.
|
||||
this.elements = [];
|
||||
this.closed = false;
|
||||
$rdf.Collection = function () {
|
||||
this.id = $rdf.NextId++; // Why need an id? For hashstring.
|
||||
this.elements = [];
|
||||
this.closed = false;
|
||||
};
|
||||
|
||||
$rdf.Collection.prototype.termType = 'collection';
|
||||
|
||||
$rdf.Collection.prototype.toNT = function() {
|
||||
return $rdf.NTAnonymousNodePrefix + this.id
|
||||
$rdf.Collection.prototype.toNT = function () {
|
||||
return $rdf.NTAnonymousNodePrefix + this.id
|
||||
};
|
||||
|
||||
$rdf.Collection.prototype.toString = function() {
|
||||
var str='(';
|
||||
for (var i=0; i<this.elements.length; i++)
|
||||
str+= this.elements[i] + ' ';
|
||||
return str + ')';
|
||||
$rdf.Collection.prototype.toString = function () {
|
||||
var str = '(';
|
||||
for(var i = 0; i < this.elements.length; i++)
|
||||
str += this.elements[i] + ' ';
|
||||
return str + ')';
|
||||
};
|
||||
|
||||
$rdf.Collection.prototype.append = function (el) {
|
||||
this.elements.push(el)
|
||||
this.elements.push(el)
|
||||
}
|
||||
$rdf.Collection.prototype.unshift=function(el){
|
||||
this.elements.unshift(el);
|
||||
$rdf.Collection.prototype.unshift = function (el) {
|
||||
this.elements.unshift(el);
|
||||
}
|
||||
$rdf.Collection.prototype.shift=function(){
|
||||
return this.elements.shift();
|
||||
$rdf.Collection.prototype.shift = function () {
|
||||
return this.elements.shift();
|
||||
}
|
||||
|
||||
|
||||
$rdf.Collection.prototype.close = function () {
|
||||
this.closed = true
|
||||
this.closed = true
|
||||
}
|
||||
|
||||
|
||||
// Convert Javascript representation to RDF term object
|
||||
//
|
||||
$rdf.term = function(val) {
|
||||
if (typeof val == 'object')
|
||||
if (val instanceof Date) {
|
||||
var d2=function(x) {return(''+(100+x)).slice(1,3)}; // format as just two digits
|
||||
return new $rdf.Literal(
|
||||
''+ val.getUTCFullYear() + '-'+
|
||||
d2(val.getUTCMonth()+1) +'-'+d2(val.getUTCDate())+
|
||||
'T'+d2(val.getUTCHours())+':'+d2(val.getUTCMinutes())+
|
||||
':'+d2(val.getUTCSeconds())+'Z',
|
||||
undefined, $rdf.Symbol.prototype.XSDdateTime);
|
||||
$rdf.term = function (val) {
|
||||
if(typeof val == 'object')
|
||||
if(val instanceof Date) {
|
||||
var d2 = function (x) {
|
||||
return('' + (100 + x)).slice(1, 3)
|
||||
}; // format as just two digits
|
||||
return new $rdf.Literal('' + val.getUTCFullYear() + '-' + d2(val.getUTCMonth() + 1)
|
||||
+ '-' + d2(val.getUTCDate()) + 'T' + d2(val.getUTCHours()) + ':'
|
||||
+ d2(val.getUTCMinutes()) + ':' + d2(val.getUTCSeconds()) + 'Z',
|
||||
undefined,
|
||||
$rdf.Symbol.prototype.XSDdateTime);
|
||||
|
||||
}
|
||||
else if (val instanceof Array) {
|
||||
var x = new $rdf.Collection();
|
||||
for (var i=0; i<val.length; i++) x.append($rdf.term(val[i]));
|
||||
return x;
|
||||
}
|
||||
else return val;
|
||||
if (typeof val == 'string') return new $rdf.Literal(val);
|
||||
if (typeof val == 'number') {
|
||||
var dt;
|
||||
if ((''+val).indexOf('e')>=0) dt = $rdf.Symbol.prototype.XSDfloat;
|
||||
else if ((''+val).indexOf('.')>=0) dt = $rdf.Symbol.prototype.XSDdecimal;
|
||||
else dt = $rdf.Symbol.prototype.XSDinteger;
|
||||
return new $rdf.Literal(val, undefined, dt);
|
||||
}
|
||||
if (typeof val == 'boolean') return new $rdf.Literal(val?"1":"0", undefined,
|
||||
$rdf.Symbol.prototype.XSDboolean);
|
||||
if (typeof val == 'undefined') return undefined;
|
||||
throw ("Can't make term from " + val + " of type " + typeof val);
|
||||
} else if(val instanceof Array) {
|
||||
var x = new $rdf.Collection();
|
||||
for(var i = 0; i < val.length; i++)
|
||||
x.append($rdf.term(val[i]));
|
||||
return x;
|
||||
} else
|
||||
return val;
|
||||
if(typeof val == 'string')
|
||||
return new $rdf.Literal(val);
|
||||
if(typeof val == 'number') {
|
||||
var dt;
|
||||
if(('' + val).indexOf('e') >= 0) dt = $rdf.Symbol.prototype.XSDfloat;
|
||||
else if(('' + val).indexOf('.') >= 0) dt = $rdf.Symbol.prototype.XSDdecimal;
|
||||
else dt = $rdf.Symbol.prototype.XSDinteger;
|
||||
return new $rdf.Literal(val, undefined, dt);
|
||||
}
|
||||
if(typeof val == 'boolean')
|
||||
return new $rdf.Literal(val ? "1" : "0", undefined, $rdf.Symbol.prototype.XSDboolean);
|
||||
if(typeof val == 'undefined')
|
||||
return undefined;
|
||||
throw("Can't make term from " + val + " of type " + typeof val);
|
||||
}
|
||||
|
||||
// Statement
|
||||
|
|
@ -169,25 +170,22 @@ $rdf.term = function(val) {
|
|||
//
|
||||
// The reason can point to provenece or inference
|
||||
//
|
||||
|
||||
$rdf.Statement = function(subject, predicate, object, why) {
|
||||
this.subject = $rdf.term(subject)
|
||||
this.predicate = $rdf.term(predicate)
|
||||
this.object = $rdf.term(object)
|
||||
if (typeof why !='undefined') {
|
||||
this.why = why;
|
||||
}
|
||||
return this;
|
||||
$rdf.Statement = function (subject, predicate, object, why) {
|
||||
this.subject = $rdf.term(subject)
|
||||
this.predicate = $rdf.term(predicate)
|
||||
this.object = $rdf.term(object)
|
||||
if(typeof why != 'undefined') {
|
||||
this.why = why;
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
$rdf.st= function(subject, predicate, object, why) {
|
||||
return new $rdf.Statement(subject, predicate, object, why);
|
||||
$rdf.st = function (subject, predicate, object, why) {
|
||||
return new $rdf.Statement(subject, predicate, object, why);
|
||||
};
|
||||
|
||||
$rdf.Statement.prototype.toNT = function() {
|
||||
return (this.subject.toNT() + " "
|
||||
+ this.predicate.toNT() + " "
|
||||
+ this.object.toNT() +" .");
|
||||
$rdf.Statement.prototype.toNT = function () {
|
||||
return (this.subject.toNT() + " " + this.predicate.toNT() + " " + this.object.toNT() + " .");
|
||||
};
|
||||
|
||||
$rdf.Statement.prototype.toString = $rdf.Statement.prototype.toNT;
|
||||
|
|
@ -195,106 +193,110 @@ $rdf.Statement.prototype.toString = $rdf.Statement.prototype.toNT;
|
|||
// Formula
|
||||
//
|
||||
// Set of statements.
|
||||
|
||||
$rdf.Formula = function() {
|
||||
this.statements = []
|
||||
this.constraints = []
|
||||
this.initBindings = []
|
||||
this.optional = []
|
||||
return this;
|
||||
$rdf.Formula = function () {
|
||||
this.statements = []
|
||||
this.constraints = []
|
||||
this.initBindings = []
|
||||
this.optional = []
|
||||
return this;
|
||||
};
|
||||
|
||||
|
||||
$rdf.Formula.prototype.termType = 'formula';
|
||||
$rdf.Formula.prototype.toNT = function() {
|
||||
return "{" + this.statements.join('\n') + "}"
|
||||
$rdf.Formula.prototype.toNT = function () {
|
||||
return "{" + this.statements.join('\n') + "}"
|
||||
};
|
||||
$rdf.Formula.prototype.toString = $rdf.Formula.prototype.toNT;
|
||||
|
||||
$rdf.Formula.prototype.add = function(subj, pred, obj, why) {
|
||||
this.statements.push(new $rdf.Statement(subj, pred, obj, why))
|
||||
$rdf.Formula.prototype.add = function (subj, pred, obj, why) {
|
||||
this.statements.push(new $rdf.Statement(subj, pred, obj, why))
|
||||
}
|
||||
|
||||
// Convenience methods on a formula allow the creation of new RDF terms:
|
||||
|
||||
$rdf.Formula.prototype.sym = function(uri,name) {
|
||||
if (name != null) {
|
||||
throw "This feature (kb.sym with 2 args) is removed. Do not assume prefix mappings."
|
||||
if (!$rdf.ns[uri]) throw 'The prefix "'+uri+'" is not set in the API';
|
||||
uri = $rdf.ns[uri] + name
|
||||
}
|
||||
return new $rdf.Symbol(uri)
|
||||
$rdf.Formula.prototype.sym = function (uri, name) {
|
||||
if(name != null) {
|
||||
throw "This feature (kb.sym with 2 args) is removed. Do not assume prefix mappings."
|
||||
if(!$rdf.ns[uri]) throw 'The prefix "' + uri + '" is not set in the API';
|
||||
uri = $rdf.ns[uri] + name
|
||||
}
|
||||
return new $rdf.Symbol(uri)
|
||||
}
|
||||
|
||||
$rdf.sym = function(uri) { return new $rdf.Symbol(uri); };
|
||||
$rdf.sym = function (uri) {
|
||||
return new $rdf.Symbol(uri);
|
||||
};
|
||||
|
||||
$rdf.Formula.prototype.literal = function(val, lang, dt) {
|
||||
return new $rdf.Literal(val.toString(), lang, dt)
|
||||
$rdf.Formula.prototype.literal = function (val, lang, dt) {
|
||||
return new $rdf.Literal(val.toString(), lang, dt)
|
||||
}
|
||||
$rdf.lit = $rdf.Formula.prototype.literal;
|
||||
|
||||
$rdf.Formula.prototype.bnode = function(id) {
|
||||
return new $rdf.BlankNode(id)
|
||||
$rdf.Formula.prototype.bnode = function (id) {
|
||||
return new $rdf.BlankNode(id)
|
||||
}
|
||||
|
||||
$rdf.Formula.prototype.formula = function() {
|
||||
return new $rdf.Formula()
|
||||
$rdf.Formula.prototype.formula = function () {
|
||||
return new $rdf.Formula()
|
||||
}
|
||||
|
||||
$rdf.Formula.prototype.collection = function () { // obsolete
|
||||
return new $rdf.Collection()
|
||||
return new $rdf.Collection()
|
||||
}
|
||||
|
||||
$rdf.Formula.prototype.list = function (values) {
|
||||
var li = new $rdf.Collection();
|
||||
if (values) {
|
||||
for(var i = 0; i<values.length; i++) {
|
||||
li.append(values[i]);
|
||||
}
|
||||
var li = new $rdf.Collection();
|
||||
if(values) {
|
||||
for(var i = 0; i < values.length; i++) {
|
||||
li.append(values[i]);
|
||||
}
|
||||
return li;
|
||||
}
|
||||
return li;
|
||||
}
|
||||
|
||||
/* Variable
|
||||
**
|
||||
** Variables are placeholders used in patterns to be matched.
|
||||
** In cwm they are symbols which are the formula's list of quantified variables.
|
||||
** In sparl they are not visibily URIs. Here we compromise, by having
|
||||
** a common special base URI for variables. Their names are uris,
|
||||
** but the ? nottaion has an implicit base uri of 'varid:'
|
||||
*/
|
||||
**
|
||||
** Variables are placeholders used in patterns to be matched.
|
||||
** In cwm they are symbols which are the formula's list of quantified variables.
|
||||
** In sparl they are not visibily URIs. Here we compromise, by having
|
||||
** a common special base URI for variables. Their names are uris,
|
||||
** but the ? nottaion has an implicit base uri of 'varid:'
|
||||
*/
|
||||
|
||||
$rdf.Variable = function(rel) {
|
||||
this.base = "varid:"; // We deem variabe x to be the symbol varid:x
|
||||
this.uri = $rdf.Util.uri.join(rel, this.base);
|
||||
return this;
|
||||
$rdf.Variable = function (rel) {
|
||||
this.base = "varid:"; // We deem variabe x to be the symbol varid:x
|
||||
this.uri = $rdf.Util.uri.join(rel, this.base);
|
||||
return this;
|
||||
}
|
||||
|
||||
$rdf.Variable.prototype.termType = 'variable';
|
||||
$rdf.Variable.prototype.toNT = function() {
|
||||
if (this.uri.slice(0, this.base.length) == this.base) {
|
||||
return '?'+ this.uri.slice(this.base.length);} // @@ poor man's refTo
|
||||
return '?' + this.uri;
|
||||
$rdf.Variable.prototype.toNT = function () {
|
||||
if(this.uri.slice(0, this.base.length) == this.base) {
|
||||
return '?' + this.uri.slice(this.base.length);
|
||||
} // @@ poor man's refTo
|
||||
return '?' + this.uri;
|
||||
};
|
||||
|
||||
$rdf.Variable.prototype.toString = $rdf.Variable.prototype.toNT;
|
||||
$rdf.Variable.prototype.classOrder = 7;
|
||||
|
||||
$rdf.variable = $rdf.Formula.prototype.variable = function(name) {
|
||||
return new $rdf.Variable(name);
|
||||
$rdf.variable = $rdf.Formula.prototype.variable = function (name) {
|
||||
return new $rdf.Variable(name);
|
||||
};
|
||||
|
||||
$rdf.Variable.prototype.hashString = $rdf.Variable.prototype.toNT;
|
||||
|
||||
|
||||
// The namespace function generator
|
||||
|
||||
$rdf.Namespace = function (nsuri) {
|
||||
return function(ln) { return new $rdf.Symbol(nsuri+(ln===undefined?'':ln)) }
|
||||
return function (ln) {
|
||||
return new $rdf.Symbol(nsuri + (ln === undefined ? '' : ln))
|
||||
}
|
||||
}
|
||||
|
||||
$rdf.Formula.prototype.ns = function(nsuri) {
|
||||
return function(ln) { return new $rdf.Symbol(nsuri+(ln===undefined?'':ln)) }
|
||||
$rdf.Formula.prototype.ns = function (nsuri) {
|
||||
return function (ln) {
|
||||
return new $rdf.Symbol(nsuri + (ln === undefined ? '' : ln))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -303,43 +305,42 @@ $rdf.Formula.prototype.ns = function(nsuri) {
|
|||
// The bnode bit should not be used on program-external values; designed
|
||||
// for internal work such as storing a bnode id in an HTML attribute.
|
||||
// This will only parse the strings generated by the vaious toNT() methods.
|
||||
$rdf.Formula.prototype.fromNT = function (str) {
|
||||
var len = str.length
|
||||
var ch = str.slice(0, 1)
|
||||
if(ch == '<') return $rdf.sym(str.slice(1, len - 1))
|
||||
if(ch == '"') {
|
||||
var lang = undefined;
|
||||
var dt = undefined;
|
||||
var k = str.lastIndexOf('"');
|
||||
if(k < len - 1) {
|
||||
if(str[k + 1] == '@') lang = str.slice(k + 2, len);
|
||||
else if(str.slice(k + 1, k + 3) == '^^') dt = $rdf.fromNT(str.slice(k + 3, len));
|
||||
else throw "Can't convert string from NT: " + str
|
||||
}
|
||||
var str = (str.slice(1, k));
|
||||
str = str.replace(/\\"/g, '"'); // unescape quotes '
|
||||
str = str.replace(/\\n/g, '\n'); // unescape newlines
|
||||
str = str.replace(/\\\\/g, '\\'); // unescape backslashes
|
||||
return $rdf.lit(str, lang, dt);
|
||||
}
|
||||
if(ch == '_') {
|
||||
var x = new $rdf.BlankNode();
|
||||
x.id = parseInt(str.slice(3));
|
||||
$rdf.NextId--
|
||||
return x
|
||||
}
|
||||
if(ch == '?') {
|
||||
var x = new $rdf.Variable(str.slice(1));
|
||||
return x;
|
||||
}
|
||||
throw "Can't convert from NT: " + str;
|
||||
|
||||
$rdf.Formula.prototype.fromNT = function(str) {
|
||||
var len = str.length
|
||||
var ch = str.slice(0,1)
|
||||
if (ch == '<') return $rdf.sym(str.slice(1,len-1))
|
||||
if (ch == '"') {
|
||||
var lang = undefined;
|
||||
var dt = undefined;
|
||||
var k = str.lastIndexOf('"');
|
||||
if (k < len-1) {
|
||||
if (str[k+1] == '@') lang = str.slice(k+2,len);
|
||||
else if (str.slice(k+1,k+3) == '^^') dt = $rdf.fromNT(str.slice(k+3,len));
|
||||
else throw "Can't convert string from NT: "+str
|
||||
}
|
||||
var str = (str.slice(1,k));
|
||||
str = str.replace(/\\"/g, '"'); // unescape quotes '
|
||||
str = str.replace(/\\n/g, '\n'); // unescape newlines
|
||||
str = str.replace(/\\\\/g, '\\'); // unescape backslashes
|
||||
return $rdf.lit(str, lang, dt);
|
||||
}
|
||||
if (ch == '_') {
|
||||
var x = new $rdf.BlankNode();
|
||||
x.id = parseInt(str.slice(3));
|
||||
$rdf.NextId--
|
||||
return x
|
||||
}
|
||||
if (ch == '?') {
|
||||
var x = new $rdf.Variable(str.slice(1));
|
||||
return x;
|
||||
}
|
||||
throw "Can't convert from NT: "+str;
|
||||
|
||||
}
|
||||
$rdf.fromNT = $rdf.Formula.prototype.fromNT; // Not for inexpert user
|
||||
|
||||
// Convenience - and more conventional name:
|
||||
$rdf.graph = function () {
|
||||
return new $rdf.IndexedFormula();
|
||||
};
|
||||
|
||||
$rdf.graph = function(){return new $rdf.IndexedFormula();};
|
||||
|
||||
// ends
|
||||
// ends
|
||||
|
|
@ -11,135 +11,139 @@
|
|||
//
|
||||
// See also http://www.w3.org/2000/10/swap/uripath.py
|
||||
//
|
||||
|
||||
if (typeof $rdf.Util.uri == "undefined") { $rdf.Util.uri = {}; };
|
||||
if(typeof $rdf.Util.uri == "undefined") {
|
||||
$rdf.Util.uri = {};
|
||||
};
|
||||
|
||||
$rdf.Util.uri.join = function (given, base) {
|
||||
// if (typeof $rdf.log.debug != 'undefined') $rdf.log.debug(" URI given="+given+" base="+base)
|
||||
var baseHash = base.indexOf('#')
|
||||
if (baseHash > 0) base = base.slice(0, baseHash)
|
||||
if (given.length==0) return base // before chopping its filename off
|
||||
if (given.indexOf('#')==0) return base + given
|
||||
var colon = given.indexOf(':')
|
||||
if (colon >= 0) return given // Absolute URI form overrides base URI
|
||||
var baseColon = base.indexOf(':')
|
||||
if (base == "") return given;
|
||||
if (baseColon < 0) {
|
||||
alert("Invalid base: "+ base + ' in join with ' +given);
|
||||
return given
|
||||
// if (typeof $rdf.log.debug != 'undefined') $rdf.log.debug(" URI given="+given+" base="+base)
|
||||
var baseHash = base.indexOf('#')
|
||||
if(baseHash > 0) base = base.slice(0, baseHash)
|
||||
if(given.length == 0) return base // before chopping its filename off
|
||||
if(given.indexOf('#') == 0) return base + given
|
||||
var colon = given.indexOf(':')
|
||||
if(colon >= 0) return given // Absolute URI form overrides base URI
|
||||
var baseColon = base.indexOf(':')
|
||||
if(base == "") return given;
|
||||
if(baseColon < 0) {
|
||||
alert("Invalid base: " + base + ' in join with ' + given);
|
||||
return given
|
||||
}
|
||||
var baseScheme = base.slice(0, baseColon + 1) // eg http:
|
||||
if(given.indexOf("//") == 0) // Starts with //
|
||||
return baseScheme + given;
|
||||
if(base.indexOf('//', baseColon) == baseColon + 1) { // Any hostpart?
|
||||
var baseSingle = base.indexOf("/", baseColon + 3)
|
||||
if(baseSingle < 0) {
|
||||
if(base.length - baseColon - 3 > 0) {
|
||||
return base + "/" + given
|
||||
} else {
|
||||
return baseScheme + given
|
||||
}
|
||||
}
|
||||
var baseScheme = base.slice(0,baseColon+1) // eg http:
|
||||
if (given.indexOf("//") == 0) // Starts with //
|
||||
return baseScheme + given;
|
||||
if (base.indexOf('//', baseColon)==baseColon+1) { // Any hostpart?
|
||||
var baseSingle = base.indexOf("/", baseColon+3)
|
||||
if (baseSingle < 0) {
|
||||
if (base.length-baseColon-3 > 0) {
|
||||
return base + "/" + given
|
||||
} else {
|
||||
return baseScheme + given
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var baseSingle = base.indexOf("/", baseColon+1)
|
||||
if (baseSingle < 0) {
|
||||
if (base.length-baseColon-1 > 0) {
|
||||
return base + "/" + given
|
||||
} else {
|
||||
return baseScheme + given
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var baseSingle = base.indexOf("/", baseColon + 1)
|
||||
if(baseSingle < 0) {
|
||||
if(base.length - baseColon - 1 > 0) {
|
||||
return base + "/" + given
|
||||
} else {
|
||||
return baseScheme + given
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (given.indexOf('/') == 0) // starts with / but not //
|
||||
return base.slice(0, baseSingle) + given
|
||||
|
||||
var path = base.slice(baseSingle)
|
||||
var lastSlash = path.lastIndexOf("/")
|
||||
if (lastSlash <0) return baseScheme + given
|
||||
if ((lastSlash >=0) && (lastSlash < (path.length-1)))
|
||||
path = path.slice(0, lastSlash+1) // Chop trailing filename from base
|
||||
|
||||
path = path + given
|
||||
while (path.match(/[^\/]*\/\.\.\//)) // must apply to result of prev
|
||||
path = path.replace( /[^\/]*\/\.\.\//, '') // ECMAscript spec 7.8.5
|
||||
path = path.replace( /\.\//g, '') // spec vague on escaping
|
||||
path = path.replace( /\/\.$/, '/' )
|
||||
return base.slice(0, baseSingle) + path
|
||||
if(given.indexOf('/') == 0) // starts with / but not //
|
||||
return base.slice(0, baseSingle) + given
|
||||
|
||||
var path = base.slice(baseSingle)
|
||||
var lastSlash = path.lastIndexOf("/")
|
||||
if(lastSlash < 0) return baseScheme + given
|
||||
if((lastSlash >= 0)
|
||||
&& (lastSlash < (path.length - 1)))
|
||||
path = path.slice(0, lastSlash + 1) // Chop trailing filename from base
|
||||
path = path + given
|
||||
while(path.match(/[^\/]*\/\.\.\//)) // must apply to result of prev
|
||||
path = path.replace(/[^\/]*\/\.\.\//, '') // ECMAscript spec 7.8.5
|
||||
path = path.replace(/\.\//g, '') // spec vague on escaping
|
||||
path = path.replace(/\/\.$/, '/')
|
||||
return base.slice(0, baseSingle) + path
|
||||
}
|
||||
|
||||
if (typeof tabulator != 'undefined' && tabulator.isExtension) {
|
||||
$rdf.Util.uri.join2 = function (given, base){
|
||||
var tIOService = Components.classes['@mozilla.org/network/io-service;1']
|
||||
.getService(Components.interfaces.nsIIOService);
|
||||
if(typeof tabulator != 'undefined' && tabulator.isExtension) {
|
||||
$rdf.Util.uri.join2 = function (given, base) {
|
||||
var tIOService = Components.classes['@mozilla.org/network/io-service;1']
|
||||
.getService(Components.interfaces.nsIIOService);
|
||||
|
||||
var baseURI = tIOService.newURI(base, null, null);
|
||||
return tIOService.newURI(baseURI.resolve(given), null, null).spec;
|
||||
}
|
||||
var baseURI = tIOService.newURI(base, null, null);
|
||||
return tIOService.newURI(baseURI.resolve(given), null, null).spec;
|
||||
}
|
||||
} else
|
||||
$rdf.Util.uri.join2 = $rdf.Util.uri.join;
|
||||
|
||||
$rdf.Util.uri.join2 = $rdf.Util.uri.join;
|
||||
|
||||
// refTo: Make a URI relative to a given base
|
||||
//
|
||||
// based on code in http://www.w3.org/2000/10/swap/uripath.py
|
||||
//
|
||||
$rdf.Util.uri.commonHost = new RegExp("^[-_a-zA-Z0-9.]+:(//[^/]*)?/[^/]*$");
|
||||
|
||||
$rdf.Util.uri.hostpart = function(u) { var m = /[^\/]*\/\/([^\/]*)\//.exec(u); return m? m[1]: '' };
|
||||
$rdf.Util.uri.hostpart = function (u) {
|
||||
var m = /[^\/]*\/\/([^\/]*)\//.exec(u);
|
||||
return m ? m[1] : ''
|
||||
};
|
||||
|
||||
$rdf.Util.uri.refTo = function(base, uri) {
|
||||
if (!base) return uri;
|
||||
if (base == uri) return "";
|
||||
var i =0; // How much are they identical?
|
||||
while (i<uri.length && i < base.length)
|
||||
if (uri[i] == base[i]) i++;
|
||||
else break;
|
||||
if (base.slice(0,i).match($rdf.Util.uri.commonHost)) {
|
||||
var k = uri.indexOf('//');
|
||||
if (k<0) k=-2; // no host
|
||||
var l = uri.indexOf('/', k+2); // First *single* slash
|
||||
if (uri.slice(l+1, l+2) != '/' && base.slice(l+1, l+2) != '/'
|
||||
&& uri.slice(0,l) == base.slice(0,l)) // common path to single slash
|
||||
return uri.slice(l); // but no other common path segments
|
||||
}
|
||||
// fragment of base?
|
||||
if (uri.slice(i, i+1) == '#' && base.length == i) return uri.slice(i);
|
||||
while (i>0 && uri[i-1] != '/') i--;
|
||||
$rdf.Util.uri.refTo = function (base, uri) {
|
||||
if(!base) return uri;
|
||||
if(base == uri) return "";
|
||||
var i = 0; // How much are they identical?
|
||||
while(i < uri.length && i < base.length)
|
||||
if(uri[i] == base[i]) i++;
|
||||
else break;
|
||||
if(base.slice(0, i).match($rdf.Util.uri.commonHost)) {
|
||||
var k = uri.indexOf('//');
|
||||
if(k < 0) k = -2; // no host
|
||||
var l = uri.indexOf('/', k + 2); // First *single* slash
|
||||
if(uri.slice(l + 1, l + 2) != '/'
|
||||
&& base.slice(l + 1, l + 2) != '/'
|
||||
&& uri.slice(0, l) == base.slice(0, l))
|
||||
// common path to single slash
|
||||
return uri.slice(l); // but no other common path segments
|
||||
}
|
||||
// fragment of base?
|
||||
if(uri.slice(i, i + 1) == '#' && base.length == i) return uri.slice(i);
|
||||
while(i > 0 && uri[i - 1] != '/') i--;
|
||||
|
||||
if (i<3) return uri; // No way
|
||||
if ((base.indexOf('//', i-2) > 0) || uri.indexOf('//', i-2) > 0)
|
||||
return uri; // an unshared '//'
|
||||
if (base.indexOf(':', i) >0) return uri; // unshared ':'
|
||||
var n = 0;
|
||||
for (var j=i; j<base.length; j++) if (base[j]=='/') n++;
|
||||
if (n==0 && i < uri.length && uri[i] =='#') return './' + uri.slice(i);
|
||||
if (n==0 && i == uri.length) return './';
|
||||
var str = '';
|
||||
for (var j=0; j<n; j++) str+= '../';
|
||||
return str + uri.slice(i);
|
||||
if(i < 3) return uri; // No way
|
||||
if((base.indexOf('//', i - 2) > 0)
|
||||
|| uri.indexOf('//', i - 2) > 0)
|
||||
return uri; // an unshared '//'
|
||||
if(base.indexOf(':', i) > 0) return uri; // unshared ':'
|
||||
var n = 0;
|
||||
for(var j = i; j < base.length; j++) if(base[j] == '/') n++;
|
||||
if(n == 0 && i < uri.length && uri[i] == '#') return './' + uri.slice(i);
|
||||
if(n == 0 && i == uri.length) return './';
|
||||
var str = '';
|
||||
for(var j = 0; j < n; j++) str += '../';
|
||||
return str + uri.slice(i);
|
||||
}
|
||||
|
||||
|
||||
/** returns URI without the frag **/
|
||||
$rdf.Util.uri.docpart = function (uri) {
|
||||
var i = uri.indexOf("#")
|
||||
if (i < 0) return uri
|
||||
return uri.slice(0,i)
|
||||
}
|
||||
var i = uri.indexOf("#")
|
||||
if(i < 0) return uri
|
||||
return uri.slice(0, i)
|
||||
}
|
||||
|
||||
/** The document in which something a thing defined **/
|
||||
$rdf.Util.uri.document = function (x) {
|
||||
return $rdf.sym($rdf.Util.uri.docpart(x.uri));
|
||||
}
|
||||
return $rdf.sym($rdf.Util.uri.docpart(x.uri));
|
||||
}
|
||||
|
||||
/** return the protocol of a uri **/
|
||||
/** return null if there isn't one **/
|
||||
$rdf.Util.uri.protocol = function (uri) {
|
||||
var index = uri.indexOf(':');
|
||||
if (index >= 0)
|
||||
return uri.slice(0, index);
|
||||
else
|
||||
return null;
|
||||
var index = uri.indexOf(':');
|
||||
if(index >= 0) return uri.slice(0, index);
|
||||
else return null;
|
||||
} //protocol
|
||||
|
||||
//ends
|
||||
//ends
|
||||
Loading…
Add table
Reference in a new issue