<commit_before>'use strict';

/* Services */


// Demonstrate how to register services
// In this case it is a simple value service.
angular.module('myApp.services', [])

  .value('version',
    '0.1'
  )

  .provider('RdfEnv',function() {
    this.$get = function($http) {
      var rdfEnv = rdfstore.create().rdf;

      // TODO perhaps not the best way to enhance the rdfstore graph...
      // permits to add a more efficient method for searching through the graph
      rdfEnv.enhanceRdfStoreGraph = function enhanceRdfStoreGraph(graph) {
        graph.matchAnyOf = function matchAnyOf(subjectArray,predicateArray,objectArray,limit) {
          return createGraphMatchingArrays(rdfEnv,graph,subjectArray,predicateArray,objectArray,limit);
        }
      }

      return rdfEnv;
    }

    function createGraphMatchingArrays(env,graph,subjectArray, predicateArray, objectArray, limit) {
      var triples = graph.toArray();
      var newGraph = env.createGraph([]);
      env.enhanceRdfStoreGraph(newGraph);
      var matched = 0;
      for ( var i=0; i < triples.length; i++ ) {
        var triple = triples[i];
        if( nodeMatchArray(triple.subject, subjectArray)
          && nodeMatchArray(triple.predicate, predicateArray)
          && nodeMatchArray(triple.object, objectArray)  ) {
          if ( limit != null ) {
            newGraph.add(triple);
            matched++;
            if ( matched === limit ) {
              return newGraph;
            }
          }
        }
      }
      return newGraph;
    };


    function nodeMatchArray(rdfNode,rdfNodeArray) {
      if ( rdfNodeArray == null || rdfNodeArray.length == 0 ) {
        return true;
      }
      return inArray(rdfNodeArray,rdfNode);
    }

    function inArray(rdfNodeArray,rdfNode) {
      var findResult = _.find(rdfNodeArray,function(rdfNodeInArray) {
        // see RDFJSInterface.RDFNode.prototype.equals
        return rdfNode.equals(rdfNodeInArray);
      });
      return findResult != null;
    }

  })




  .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) {
      var deferred = $q.defer();
      var temporaryRdfStore = rdfstore.create();
      temporaryRdfStore.load(graphMimeType, graphData, function(success, results) {
        if ( success ) {
          temporaryRdfStore.graph(function(success,graph){
            if ( success ) {
              RdfEnv.enhanceRdfStoreGraph(graph);
              deferred.resolve(graph);
            } else {
              deferred.reject("Can't GET graph with GraphURI="+graphUri);
            }
          });
        } else {
          deferred.reject("Can't LOAD graph with GraphURI="+graphUri+" and graphMimeType="+graphMimeType+" -> "+results);
        }
      });
      return deferred.promise;
    }


  }])



  .service('RdfGraphService',['RdfEnv',function(RdfEnv) {

    var self = this;

    this.createOrReplaceObject = function createOrReplaceObject(graph,subject,predicate,newObjectValue) {
      var subjectNode = RdfEnv.createNamedNode(subject);
      var predicateNode = RdfEnv.createNamedNode(RdfEnv.resolve(predicate));
      var newObjectLiteral = RdfEnv.createLiteral(newObjectValue);
      graph.removeMatches(subjectNode,predicateNode,null);
      graph.add(RdfEnv.createTriple(subjectNode,predicateNode,newObjectLiteral));
      return graph;
    }

    // returns a filtered graph which only contain the triples of the subject
    this.subjectGraph = function subjectGraph(graph,subject) {
      var subjectNode = RdfEnv.createNamedNode(subject);
      var newGraph = graph.match(subjectNode,null,null);
      RdfEnv.enhanceRdfStoreGraph(newGraph);
      return newGraph;
    }

    this.findObjectsByPredicate = function findObjectsByPredicate(graph,subject,predicate,limit) {
      var subjectNode = (subject != null) ? RdfEnv.createNamedNode(subject) : null;
      var predicateNode = RdfEnv.createNamedNode(RdfEnv.resolve(predicate));
      var triples = graph.match(subjectNode,predicateNode, null,limit).toArray();
      var matches = _.map(triples, function(t){ return t.object.valueOf() });
      return matches;
    }

    this.findObjectsByPredicateArray = function findObjectsByPredicateArray(graph,subject,predicateArray,limit) {
      var subjectNodeArray = (subject != null) ? [RdfEnv.createNamedNode(subject)] : null;
      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() });
      return matches;
    }

  }])




  .service('RdfPointedGraphService',['RdfGraphService','RdfHttpService',function(RdfGraphService,RdfHttpService) {

    var self = this;

    this.pointedGraph = function pointedGraph(graph,subject) {

      // When trying to get a predicate's object for a given subject,
      // it is faster to use this filtered version of the original graph
      // because this will produce less iteration...
      // however it can be a problem if the graph is modified: the subjectGraph could be unsync
      var subjectGraph = RdfGraphService.subjectGraph(graph,subject);

      return {
        graph: graph,
        subject: subject,
        subjectGraph: subjectGraph
      }
    }

    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.findObjectsByPredicateArray = function findObjectsByPredicateArray(pointedGraph,predicate,limit) {
      return RdfGraphService.findObjectsByPredicateArray(pointedGraph.subjectGraph,null,predicate,limit);
    }

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

    this.findFirstObjectByPredicateArray = function findFirstObjectByPredicateArray(pointedGraph,predicate) {
      var result =  RdfGraphService.findObjectsByPredicateArray(pointedGraph.subjectGraph,null,predicate,1);<commit_msg>Fix little rdf predicate property ordering<commit_after>'use strict';

/* Services */


// Demonstrate how to register services
// In this case it is a simple value service.
angular.module('myApp.services', [])

  .value('version',
    '0.1'
  )

  .provider('RdfEnv',function() {
    this.$get = function($http) {
      var rdfEnv = rdfstore.create().rdf;

      // TODO perhaps not the best way to enhance the rdfstore graph...
      // permits to add a more efficient method for searching through the graph
      rdfEnv.enhanceRdfStoreGraph = function enhanceRdfStoreGraph(graph) {
        graph.matchAnyOf = function matchAnyOf(subjectArray,predicateArray,objectArray,limit) {
          return createGraphMatchingArrays(rdfEnv,graph,subjectArray,predicateArray,objectArray,limit);
        }
      }

      return rdfEnv;
    }

    function createGraphMatchingArrays(env,graph,subjectArray, predicateArray, objectArray, limit) {
      var triples = graph.toArray();
      var newGraph = env.createGraph([]);
      env.enhanceRdfStoreGraph(newGraph);
      var matched = 0;
      for ( var i=0; i < triples.length; i++ ) {
        var triple = triples[i];
        if( nodeMatchArray(triple.subject, subjectArray)
          && nodeMatchArray(triple.predicate, predicateArray)
          && nodeMatchArray(triple.object, objectArray)  ) {
          newGraph.add(triple);
          if ( limit != null ) {
            matched++;
            if ( matched === limit ) {
              return newGraph;
            }
          }
        }
      }
      return newGraph;
    };


    function nodeMatchArray(rdfNode,rdfNodeArray) {
      if ( rdfNodeArray == null || rdfNodeArray.length == 0 ) {
        return true;
      }
      return inArray(rdfNodeArray,rdfNode);
    }

    function inArray(rdfNodeArray,rdfNode) {
      var findResult = _.find(rdfNodeArray,function(rdfNodeInArray) {
        // see RDFJSInterface.RDFNode.prototype.equals
        return rdfNode.equals(rdfNodeInArray);
      });
      return findResult != null;
    }

  })




  .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) {
      var deferred = $q.defer();
      var temporaryRdfStore = rdfstore.create();
      temporaryRdfStore.load(graphMimeType, graphData, function(success, results) {
        if ( success ) {
          temporaryRdfStore.graph(function(success,graph){
            if ( success ) {
              RdfEnv.enhanceRdfStoreGraph(graph);
              deferred.resolve(graph);
            } else {
              deferred.reject("Can't GET graph with GraphURI="+graphUri);
            }
          });
        } else {
          deferred.reject("Can't LOAD graph with GraphURI="+graphUri+" and graphMimeType="+graphMimeType+" -> "+results);
        }
      });
      return deferred.promise;
    }


  }])



  .service('RdfGraphService',['RdfEnv',function(RdfEnv) {

    var self = this;

    this.createOrReplaceObject = function createOrReplaceObject(graph,subject,predicate,newObjectValue) {
      var subjectNode = RdfEnv.createNamedNode(subject);
      var predicateNode = RdfEnv.createNamedNode(RdfEnv.resolve(predicate));
      var newObjectLiteral = RdfEnv.createLiteral(newObjectValue);
      graph.removeMatches(subjectNode,predicateNode,null);
      graph.add(RdfEnv.createTriple(subjectNode,predicateNode,newObjectLiteral));
      return graph;
    }

    // returns a filtered graph which only contain the triples of the subject
    this.subjectGraph = function subjectGraph(graph,subject) {
      var subjectNode = RdfEnv.createNamedNode(subject);
      var newGraph = graph.match(subjectNode,null,null);
      RdfEnv.enhanceRdfStoreGraph(newGraph);
      return newGraph;
    }

    this.findObjectsByPredicate = function findObjectsByPredicate(graph,subject,predicate,limit) {
      var subjectNode = (subject != null) ? RdfEnv.createNamedNode(subject) : null;
      var predicateNode = RdfEnv.createNamedNode(RdfEnv.resolve(predicate));
      var triples = graph.match(subjectNode,predicateNode, null,limit).toArray();
      var matches = _.map(triples, function(t){ return t.object.valueOf() });
      return matches;
    }

    this.findObjectsByPredicateArray = function findObjectsByPredicateArray(graph,subject,predicateArray,limit) {
      var subjectNodeArray = (subject != null) ? [RdfEnv.createNamedNode(subject)] : null;
      var predicateNodeArray = _.map(predicateArray,function(predicate) {
        return RdfEnv.createNamedNode(RdfEnv.resolve(predicate));
      })
      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);
      });
    }

  }])




  .service('RdfPointedGraphService',['RdfGraphService','RdfHttpService',function(RdfGraphService,RdfHttpService) {

    var self = this;

    this.pointedGraph = function pointedGraph(graph,subject) {

      // When trying to get a predicate's object for a given subject,
      // it is faster to use this filtered version of the original graph
      // because this will produce less iteration...
      // however it can be a problem if the graph is modified: the subjectGraph could be unsync
      var subjectGraph = RdfGraphService.subjectGraph(graph,subject);

      return {
        graph: graph,
        subject: subject,
        subjectGraph: subjectGraph
      }
    }

    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, pointedGraph.subject, 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 limit = 1;
      var result = RdfGraphService.findObjectsByPredicate( pointedGraph.subjectGraph, pointedGraph.subject, predicate,limit);
      return _.first(result);
    }

    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);
{'fraction_non_alphanumeric': 0.09994508511806699, 'fraction_numerical': 0.0011669412410763318, 'mean_word_length': 3.3619760479041916, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1954730', 'n_tokens_mistral': 4207, 'n_tokens_neox': 4089, 'n_words': 842}