define([
"./query_filters",
"../utils"
], function (QueryFilters, TreeUtils) {
var RDFJSInterface = {};
/**
* Implementation of
*/
// Uris map
RDFJSInterface.defaultContext = {
"rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
"rdfs": "http://www.w3.org/2000/01/rdf-schema#",
"owl": "http://www.w3.org/2002/07/owl#",
"xsd": "http://www.w3.org/2001/XMLSchema#",
"dcterms": "http://purl.org/dc/terms/",
"foaf": "http://xmlns.com/foaf/0.1/",
"cal": "http://www.w3.org/2002/12/cal/ical#",
"vcard": "http://www.w3.org/2006/vcard/ns# ",
"geo": "http://www.w3.org/2003/01/geo/wgs84_pos#",
"cc": "http://creativecommons.org/ns#",
"sioc": "http://rdfs.org/sioc/ns#",
"doap": "http://usefulinc.com/ns/doap#",
"com": "http://purl.org/commerce#",
"ps": "http://purl.org/payswarm#",
"gr": "http://purl.org/goodrelations/v1#",
"sig": "http://purl.org/signature#",
"ccard": "http://purl.org/commerce/creditcard#"
};
RDFJSInterface.UrisMap = function() {
this.defaultNs = "";
this.interfaceProperties = ['get', 'remove', 'set', 'setDefault',
'addAll', 'resolve', 'shrink'];
};
RDFJSInterface.UrisMap.prototype.values = function() {
var collected = {};
for(var p in this) {
if(!TreeUtils.include(this.interfaceProperties,p) &&
typeof(this[p])!=='function' &&
p!=='defaultNs' &&
p!=='interfaceProperties') {
collected[p] = this[p];
}
}
return collected;
};
RDFJSInterface.UrisMap.prototype.get = function(prefix) {
if(prefix.indexOf(" ") != -1) {
throw "Prefix must not contain any whitespaces";
}
return this[prefix];
};
RDFJSInterface.UrisMap.prototype.remove = function(prefix) {
if(prefix.indexOf(" ") != -1) {
throw "Prefix must not contain any whitespaces";
}
delete this[prefix];
return null;
};
RDFJSInterface.UrisMap.prototype.set = function(prefix, iri) {
if(prefix.indexOf(" ") != -1) {
throw "Prefix must not contain any whitespaces";
}
this[prefix] = iri;
};
RDFJSInterface.UrisMap.prototype.setDefault = function(iri) {
this.defaultNs =iri;
};
RDFJSInterface.UrisMap.prototype.addAll = function(prefixMap, override) {
for(var prefix in prefixMap) {
if(!TreeUtils.include(this.interfaceProperties, prefix)) {
if(this[prefix] != null) {
if(override === true) {
this[prefix] = prefixMap[prefix];
}
} else {
this[prefix] = prefixMap[prefix];
}
}
}
return this;
};
RDFJSInterface.UrisMap.prototype.resolve = function(curie) {
var parts = curie.split(":");
var ns = parts[0];
var suffix = parts[1];
if(ns === '') {
if(this.defaultNs == null) {
return null;
} else {
return this.defaultNs + suffix;
}
} else if(this[ns] != null) {
return this[ns] + suffix;
} else {
return null;
}
};
RDFJSInterface.UrisMap.prototype.shrink = function(iri) {
for(var ns in this) {
var prefix = this[ns];
if(iri.indexOf(prefix) === 0) {
if(prefix !== '' && ns != 'defaultNs') {
var suffix = iri.split(prefix)[1];
return ns + ":" + suffix;
}
}
}
return iri;
};
// Profile
RDFJSInterface.Profile = function() {
this.prefixes = new RDFJSInterface.UrisMap();
this.terms = new RDFJSInterface.UrisMap();
};
RDFJSInterface.Profile.prototype.importProfile = function(profile, override) {
this.prefixes.addAll(profile.prefixes, override);
this.terms.addAll(profile.terms, override);
};
RDFJSInterface.Profile.prototype.resolve = function(toResolve) {
if(toResolve.indexOf(":") != -1) {
return this.prefixes.resolve(toResolve);
} else if(this.terms[toResolve] != null) {
return this.terms.resolve(toResolve);
} else {
return null;
}
};
RDFJSInterface.Profile.prototype.setDefaultPrefix = function(iri) {
this.prefixes.setDefault(iri);
};
RDFJSInterface.Profile.prototype.setDefaultVocabulary = function(iri) {
this.terms.setDefault(iri);
};
RDFJSInterface.Profile.prototype.setPrefix = function(prefix, iri) {
this.prefixes.set(prefix, iri);
};
RDFJSInterface.Profile.prototype.setTerm = function(term, iri) {
this.terms.set(term, iri);
};
// RDF environemnt
RDFJSInterface.RDFEnvironment = function () {
this.blankNodeCounter = 0;
var that = this;
this.filters = {
s:function (s) {
return function (t) {
return t.subject.equals(s);
};
},
p:function (p) {
return function (t) {
return t.predicate.equals(p);
};
},
o:function (o) {
return function (t) {
return t.object.equals(o);
};
},
sp:function (s, p) {
return function (t) {
return t.subject.equals(s) && t.predicate.equals(p);
};
},
so:function (s, o) {
return function (t) {
return t.subject.equals(s) && t.object.equals(o);
};
},
po:function (p, o) {
return function (t) {
return t.predicate.equals(p) && t.object.equals(o);
};
},
spo:function (s, p, o) {
return function (t) {
return t.subject.equals(s) && t.predicate.equals(p) && t.object.equals(o);
};
},
describes:function (v) {
return function (t) {
return t.subject.equals(v) || t.object.equals(v);
};
},
type:function (o) {
var type = that.resolve("rdf:type");
return function (t) {
return t.predicate.equals(type) && t.object.equals(o);
};
}
};
for (var p in RDFJSInterface.defaultContext) {
this.prefixes.set(p, RDFJSInterface.defaultContext[p]);
}
};
TreeUtils.extends(RDFJSInterface.Profile, RDFJSInterface.RDFEnvironment);
RDFJSInterface.RDFEnvironment.prototype.createBlankNode = function() {
var bnode = new RDFJSInterface.BlankNode(this.blankNodeCounter);
this.blankNodeCounter++;
return bnode;
};
RDFJSInterface.RDFEnvironment.prototype.createNamedNode = function(value) {
var resolvedValue = this.resolve(value);
if(resolvedValue != null) {
return new RDFJSInterface.NamedNode(resolvedValue);
} else {
return new RDFJSInterface.NamedNode(value);
}
};
RDFJSInterface.RDFEnvironment.prototype.createLiteral = function(value, language, datatype) {
if(datatype != null) {
return new RDFJSInterface.Literal(value, language, datatype.toString());
} else {
return new RDFJSInterface.Literal(value, language, datatype);
}
};
RDFJSInterface.RDFEnvironment.prototype.createTriple = function(subject, predicate, object) {
return new RDFJSInterface.Triple(subject, predicate, object);
};
RDFJSInterface.RDFEnvironment.prototype.createGraph = function(triples) {
var graph = new RDFJSInterface.Graph();
if(triples != null) {
for(var i=0; i";
}
return tmp;
};
RDFJSInterface.Literal.prototype.toNT = function() {
return this.toString();
};
RDFJSInterface.Literal.prototype.valueOf = function() {
return QueryFilters.effectiveTypeValue({token: 'literal',
type: (this.type || this.datatype),
value: this.nominalValue,
language: this.language});
};
// NamedNode node
RDFJSInterface.NamedNode = function(val) {
RDFJSInterface.RDFNode.call(this, "NamedNode");
if(val.value != null) {
this.nominalValue = val.value;
} else {
this.nominalValue = val;
}
};
TreeUtils.extends(RDFJSInterface.RDFNode,RDFJSInterface.NamedNode);
RDFJSInterface.NamedNode.prototype.toString = function(){
return this.nominalValue;
};
RDFJSInterface.NamedNode.prototype.toNT = function() {
return "<"+this.toString()+">";
};
RDFJSInterface.NamedNode.prototype.valueOf = function() {
return this.nominalValue;
};
// Triple interface
RDFJSInterface.Triple = function(subject, predicate, object){
this.subject = subject;
this.predicate = predicate;
this.object = object;
};
RDFJSInterface.Triple.prototype.equals = function(otherTriple) {
return this.subject.equals(otherTriple.subject) &&
this.predicate.equals(otherTriple.predicate) &&
this.object.equals(otherTriple.object);
};
RDFJSInterface.Triple.prototype.toString = function() {
return this.subject.toNT()+" "+this.predicate.toNT()+" "+this.object.toNT()+" . \r\n";
};
// Graph interface
RDFJSInterface.Graph = function() {
this.triples = [];
this.duplicates = {};
this.actions = [];
this.length = 0;
};
RDFJSInterface.Graph.prototype.add = function(triple) {
for(var i=0; i