Skip to content

Commit

Permalink
redesigned filtering, querying for properties implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Germesin committed Mar 11, 2011
1 parent 179f36c commit 7dbe48a
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 45 deletions.
Empty file removed demos/.tmp_test.html.19483~
Empty file.
26 changes: 22 additions & 4 deletions demos/test.html
Expand Up @@ -10,6 +10,9 @@
<script type="text/javascript" src="../lib/underscoreJS/underscore.js"></script>
<script type="text/javascript" src="../lib/backboneJS/backbone.js"></script>

<!-- VIE -->
<script type="text/javascript" src="../lib/vie/vie.js"></script>

<!-- VIE^2 -->
<script type="text/javascript" src="../src/core.js"></script>
<script type="text/javascript" src="../src/connector.js"></script>
Expand All @@ -21,21 +24,36 @@
<script type="text/javascript" src="../src/connector-rdfa.js"></script>

<!-- Mapping plug-ins -->
<script type="text/javascript" src="../src/mapping-person.js"></script>
<script type="text/javascript" src="../src/mapping-entity.js"></script>

<!-- How the API would look like! -->
<script type="text/javascript">
$(function () {
var ns = {
'google' : 'http://dbpedia.org/resource/',
'dbpedia' : 'http://dbpedia.org/resource/',
'dbprop' : 'http://dbpedia.org/property/',
'dbonto' : 'http://dbpedia.org/ontology/',
'rdf' : 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
'rdfs' : 'http://www.w3.org/2000/01/rdf-schema#',
'iks' : 'http://www.iks-project.eu/#',
'foaf' : 'http://xmlns.com/foaf/0.1/',
'dc' : 'http://purl.org/dc/terms/',
'geo' : 'http://www.w3.org/2003/01/geo/wgs84_pos#'
};

var span = $('#test')
.vie2()
.vie2({namespaces: ns})
.vie2('analyze', false);

var matches = span
.vie2('filter', 'person')
.vie2('filter', 'entity')
.vie2('matches');

console.info(matches);

jQuery.each(matches, function () {
console.info("FOUND PERSON: " + this.get('name') + "|" + this.get('uri'));
console.info("FOUND PERSON: " + this['@'] + "|" + this['a']);
});
});

Expand Down
10 changes: 4 additions & 6 deletions src/connector-rdfa.js
Expand Up @@ -5,25 +5,23 @@

var rdfaConnector = new Connector('rdfa');

rdfaConnector.analyze = function (object, callback) {
rdfaConnector.analyze = function (object, namespaces, callback) {
if (object == undefined) {
jQuery.VIE2.log ("warn", "VIE2.Connector('" + this.id + "')", "Given object is undefined!");
return;
} else if (typeof object === 'object') {
//does only work on objects that have the 'typeof' attribute set!
if (object.attr('typeof')) {
//use rdfQuery to analyze the object
var rdf = jQuery( object ).rdfa();
var rdf = jQuery(object).rdfa();

callback(rdf);
} else {
jQuery.VIE2.log("info", "VIE2.Connector(" + this.id + ")", "Object has no 'typeof' attribute! Trying to find children.");
var rdf = jQuery.rdf();
object.find('[typeof]').each(function(i, e) {
var rdfa = jQuery(e).rdfa();
//merging the results into the main object
rdfa.databank.triples().each(function () {
rdf.add(this);
});
rdf.add(rdfa);
});
callback(rdf);
}
Expand Down
2 changes: 1 addition & 1 deletion src/connector-stanbol.js
Expand Up @@ -9,7 +9,7 @@ var stanbolConnector = new Connector('stanbol', {
"entityhub_url" : "http://stanbol.iksfordrupal.net:9000/entityhub/"
});

stanbolConnector.analyze = function (object, callback) {
stanbolConnector.analyze = function (object, namespaces, callback) {
if (object == undefined) {
jQuery.VIE2.log ("warn", "VIE2.Connector('" + this.id + "')", "Given object is undefined!");
return;
Expand Down
8 changes: 6 additions & 2 deletions src/connector.js
Expand Up @@ -13,6 +13,10 @@ Connector = function(id, options) {

Connector.prototype.init = function() {};

Connector.prototype.analyze = function (object, callback) {};
Connector.prototype.analyze = function (object, callback) {
$.VIE2.log("info", "VIE^2.Connector(" + this.id + ")", "Not implemented: analyze();");
};

Connector.prototype.query = function (uri, context) {};
Connector.prototype.query = function (uri, context) {
$.VIE2.log("info", "VIE^2.Connector(" + this.id + ")", "Not implemented: query();");
};
128 changes: 104 additions & 24 deletions src/core.js
Expand Up @@ -17,16 +17,57 @@
* You can call .vie2() on every jQuery object.
*/
$.widget('VIE2.vie2', {

// default options
options: {
/**
* namespaces to be used!
*/
namespaces: {},
//////////// EVENTS /////////////
/**
* ready: called as soon as the object is ready (after _create)
*/
ready: jQuery.noop,
/**
* contextchanged: TODO:
*/
contextchanged: jQuery.noop,
/**
* urischanged: TODO:
*/
urischanged: jQuery.noop
},

_context: {},

_oldMatches: [],
_matches: [],
/**
* TODO:
*/
_cache: {},

/**
* TODO:
*/
_matches: [],

/**
* TODO:
*/
_oldMatches: [],

_create: function() {
jQuery.VIE2.log("info", "VIE2.core", "Start " + this.element);
this._trigger("ready", this, {});
jQuery.VIE2.log("info", "VIE2.core", "End " + this.element);
},

/**
* options:
* async: true, false
*/
/**
* options:
* async: true, false
*/
analyze: function (async) {
jQuery.VIE2.log("info", "VIE2.core", "Start analyze!");
var that = this;
Expand All @@ -35,43 +76,82 @@
jQuery.VIE2.log("info", "VIE2.core", "Starting analysis with connector: '" + this.id + "'!");
var callback = function (conn) {
return function (rdf) {
that._context[conn.id] = rdf;
jQuery.each(that.options.namespaces, function(k, v) {
rdf.prefix(k, v);
});
that._cache[conn.id] = rdf;
that._trigger('contextchanged', conn, {'rdf': rdf});
jQuery.VIE2.log("info", "VIE2.core", "Received RDF annotation from connector '" + conn.id + "'!");
};
}(this);
if (async) {
window.setTimeout(this.analyze(elem, callback), 0); // execute the analysis in an own thread
window.setTimeout(this.analyze(elem, that.options.namespaces, callback), 0); // execute the analysis in an own thread
} else {
this.analyze(elem, callback);
this.analyze(elem, that.options.namespaces, callback);
}
});
jQuery.VIE2.log("info", "VIE2.core", "Finished task: 'analyze'!");

return this;
},

filter: function (type) {

this._oldMatches = this._matches;
this._matches = [];
var that = this;

jQuery.each(jQuery.VIE2.mappings, function () {
if (this.id === type) {
jQuery.merge(that._matches, this.filter(that._context, that._oldMatches));
return false;
}
});
filter: function (filterId) {
if (filterId === undefined) {
jQuery.VIE2.log("warn", "VIE2.core", "Invoked 'filter' with undefined argument!");
} else if (typeof filterId === 'string') {
var that = this;
this._oldMatches = this._matches;
this._matches = [];
$.each (jQuery.VIE2.mappings, function () {
if (this.id === filterId) {
jQuery.VIE2.log("info", "VIE2.core", "Invoking DSF '" + this.id + "'!");
jQuery.merge(that._matches, this.filter(that, that._cache, that._oldMatches));
}
});
} else {
jQuery.VIE2.log("warn", "VIE2.core", "Invoked 'filter' with wrong argument: '" + filterId + "'!");
}
return this;
},
},

query: function (uri, props) {
var ret = {};
if (uri === undefined) {
jQuery.VIE2.log("warn", "VIE2.core", "Invoked 'query' with undefined argument!");
}
if (uri instanceof jQuery.rdf.resource &&
uri.type === 'uri') {
var that = this;

jQuery.each(props, function () {
ret[this] = [];
});

jQuery.each(jQuery.VIE2.connectors, function () {
var retTmp = this.query(uri, props);
if (retTmp) {
jQuery.extend(ret, retTmp);
}

});
}
return ret;
},

matches: function () {
return this._matches;
},

undo: function () {
this._matches = this._oldMatches;
this._oldMatches = [];
return this;
},

matches: function () {
return this._matches;
clear: function () {
this._matches = [];
this._oldMatches = [];
return this;
}

});
Expand Down Expand Up @@ -117,7 +197,7 @@ jQuery.VIE2.unregisterConnector = function (connector) {
//TODO: untested code!
if (this.id === connector.id) {
//TODO: array slice
jQuery.Aviate.log("info", "VIE2.core", "De-registered connector '" + connector.id + "'");
jQuery.VIE2.log("info", "VIE2.core", "De-registered connector '" + connector.id + "'");
return;
}
});
Expand Down Expand Up @@ -149,7 +229,7 @@ jQuery.VIE2.unregisterMapping = function (mapping) {
//TODO: untested code!
if (this.id === mapping.id) {
//TODO: array slice
jQuery.Aviate.log("info", "VIE2.core", "De-registered mapping '" + mapping.id + "'");
jQuery.VIE2.log("info", "VIE2.core", "De-registered mapping '" + mapping.id + "'");
return;
}
});
Expand Down
19 changes: 13 additions & 6 deletions src/entity.js
Expand Up @@ -3,10 +3,17 @@
* @author <a href="mailto:sebastian.germesin@dfki.de">Sebastian Germesin</a>
*/

var RDFEntity = Backbone.Model.extend({
var JSONLDEntity = function (namespaces, uri, type, properties) {

'id' : undefined,
'rdf:type' : [],
'prefixes' : {}

});
var jsonld = {
"#": namespaces,
"@": uri,
"a": type
};

for (var key in properties) {
jsonld[key] = properties[key];
}

return jsonld;
};
2 changes: 1 addition & 1 deletion src/mapping-person.js
Expand Up @@ -7,7 +7,7 @@ var mappingPerson = new Mapping('person');

mappingPerson.connectorMappers = {};

mappingPerson.filter = function (context, matches) {
mappingPerson.filter = function (vie2, context, matches) {
var persons = [];
$.each(context, function (connectorId, rdf) {
var mapper = mappingPerson.connectorMappers[connectorId];
Expand Down
56 changes: 55 additions & 1 deletion src/mapping.js
Expand Up @@ -13,4 +13,58 @@ Mapping = function(id, options) {

Mapping.prototype.init = function() {};

Mapping.prototype.filter = function (context, matches) {};
Mapping.prototype.filter = function (vie2, context, matches) {
var entities = [];
var that = this;

jQuery.each(context, function (connId, rdf) {
if (that.options.mapping.type[connId]) {
//TODO: if (typeof that.options.mapping.type[connId] === 'array')
rdf
.where('?subject' + ' ' +
that.options.mapping.type[connId].type + ' ' +
that.options.mapping.type[connId].value)
.each(function () {
var entity = {};
var subject = this.subject;
var triples = rdf.databank.subjectIndex[subject];
jQuery.each(that.options.mapping, function (key, val) {
if (key !== 'type') {
if (key === '*') {
//TODO: key === '*'
} else {
entity[key] = [];
var property = val[connId];
if (property) {
if (typeof property === 'string') {
property = [property];
}
if (jQuery.isArray(property)) {
jQuery.each(property, function (i, v) {
var prop = jQuery.rdf.resource(v, { namespaces: rdf.databank.namespaces });
jQuery.each(triples, function () {
if (this.property === prop) {
entity[key].push(this.object);
}
});
//TODO: gather this to reduce numer of calls
if (entity[key].length === 0) {
//get it from another connector!
var queryResult = vie2.query(subject, [prop]);
if (queryResult[prop]) {
jQuery.extend(entity[key], queryResult[prop]);
}
}
});
}
}
}
}
});
entities.push(entity);
});
}
});

return entities;
};

0 comments on commit 7dbe48a

Please sign in to comment.