Skip to content
This repository has been archived by the owner on Jun 27, 2023. It is now read-only.

Commit

Permalink
Fix little rdf predicate property ordering
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Jan 12, 2014
1 parent 96a612c commit e91bc6f
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions app/js/services.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ angular.module('myApp.services', [])
if( nodeMatchArray(triple.subject, subjectArray)
&& nodeMatchArray(triple.predicate, predicateArray)
&& nodeMatchArray(triple.object, objectArray) ) {
newGraph.add(triple);
if ( limit != null ) {
newGraph.add(triple);
matched++;
if ( matched === limit ) {
return newGraph;
Expand Down Expand Up @@ -71,6 +71,8 @@ angular.module('myApp.services', [])

.service('RdfParser',['$q','RdfEnv',function($q,RdfEnv) {



// TODO see https://github.com/antoniogarrote/rdfstore-js/issues/70
// it seems we need to create a different rdfstore instance each time to avoid a concurrency issue :(
this.parseGraph = function parseGraph(graphUri, graphData, graphMimeType) {
Expand Down Expand Up @@ -132,11 +134,25 @@ angular.module('myApp.services', [])
var predicateNodeArray = _.map(predicateArray,function(predicate) {
return RdfEnv.createNamedNode(RdfEnv.resolve(predicate));
})
var triples = graph.matchAnyOf(subjectNodeArray,predicateNodeArray, null,limit).toArray();
var matches = _.map(triples, function(t){ return t.object.valueOf() });
var matchGraph = graph.matchAnyOf(subjectNodeArray,predicateNodeArray, null,limit);
var triples = matchGraph.toArray();
var sortedTriples = sortTriplesByPredicateArrayOrder(triples,predicateNodeArray);
var matches = _.map(sortedTriples, function(t){ return t.object.valueOf() });
return matches;
}

// If we ask for [foaf:name, foaf:nick, foaf:something], this permits to order the triples by this predicate order
// the triples with foaf:name will be returned first in the list
function sortTriplesByPredicateArrayOrder(triples,predicateArray) {
return _.sortBy(triples,function(t) {
var predicateInArrayFound = _.find(predicateArray,function(predicateInArray) {
// see RDFJSInterface.RDFNode.prototype.equals
return t.predicate.equals(predicateInArray);
});
return predicateArray.indexOf(predicateInArrayFound);
});
}

}])


Expand All @@ -161,26 +177,28 @@ angular.module('myApp.services', [])
}
}

this.createOrReplaceObject = function createOrReplaceObject(pointedGraph,predicate,newObjectValue) {
var newGraph = RdfGraphService.createOrReplaceObject(pointedGraph.graph,pointedGraph.subject,predicate,newObjectValue);
this.createOrReplaceObject = function createOrReplaceObject(pointedGraph, predicate, newObjectValue) {
var newGraph = RdfGraphService.createOrReplaceObject( pointedGraph.graph, pointedGraph.subject, predicate, newObjectValue);
return self.pointedGraph( newGraph, pointedGraph.subject );
}

this.findObjectsByPredicate = function findObjectsByPredicate(pointedGraph,predicate,limit) {
return RdfGraphService.findObjectsByPredicate(pointedGraph.subjectGraph,null,predicate,limit);
this.findObjectsByPredicate = function findObjectsByPredicate(pointedGraph, predicate, limit) {
return RdfGraphService.findObjectsByPredicate( pointedGraph.subjectGraph, pointedGraph.subject, predicate,limit);
}

this.findObjectsByPredicateArray = function findObjectsByPredicateArray(pointedGraph,predicate,limit) {
return RdfGraphService.findObjectsByPredicateArray(pointedGraph.subjectGraph,null,predicate,limit);
this.findObjectsByPredicateArray = function findObjectsByPredicateArray(pointedGraph, predicate, limit) {
return RdfGraphService.findObjectsByPredicateArray( pointedGraph.subjectGraph, pointedGraph.subject, predicate,limit);
}

this.findFirstObjectByPredicate = function findFirstObjectByPredicate(pointedGraph,predicate) {
var result = RdfGraphService.findObjectsByPredicate(pointedGraph.subjectGraph,null,predicate,1);
this.findFirstObjectByPredicate = function findFirstObjectByPredicate(pointedGraph, predicate) {
var limit = 1;
var result = RdfGraphService.findObjectsByPredicate( pointedGraph.subjectGraph, pointedGraph.subject, predicate,limit);
return _.first(result);
}

this.findFirstObjectByPredicateArray = function findFirstObjectByPredicateArray(pointedGraph,predicate) {
var result = RdfGraphService.findObjectsByPredicateArray(pointedGraph.subjectGraph,null,predicate,1);
this.findFirstObjectByPredicateArray = function findFirstObjectByPredicateArray(pointedGraph, predicate) {
var limit = undefined; // done on purpose because we want the results to be sorted
var result = RdfGraphService.findObjectsByPredicateArray( pointedGraph.subjectGraph, pointedGraph.subject, predicate,limit);
return _.first(result);
}

Expand Down

0 comments on commit e91bc6f

Please sign in to comment.