<commit_before>'use strict';
/**
 * JSON Translator Factory 
 * A factory to handle translation of JSON -> SPARQL
 *
 */

angular.module('GSB.services.translatorToSPARQL', ['GSB.config'])
  .factory('TranslatorToSPARQL', ['globalConfig', '$log', function (globalConfig, $log) {
    
    var factory = {};

    /**
     * Function to start translation process, with call to changeURIs for the mockup data
	   * and replaceAliasSpaces to replace spaces with underscores
	   * @param json
     */
    factory.translateJSONToSPARQL = function (json) {
      
      //json = factory.changeURIs(json);
	    
	    json = factory.replaceAliasSpaces(json);
	    
	    return factory.translateAll(json);
    };
	  
    /**
     *  Initial translation function that takes a JSON-object, looks for the main class and starts translation there, 
     *  and in the end of the process adds shown values and SPARQL-'header' (e.g. SELECT DISTINCT..)
     * @param json
     */
    factory.translateAll = function (json) {

      var shownValues = [];
      var translated = [];
      var SPARQL = "";

      for(var i = 0; i < json.SUBJECTS.length; i++)
      {
        if(json.SUBJECTS[i].alias === json['START'].link.linkPartner)
        {
          SPARQL +=  factory.translateSubject(json.SUBJECTS[i], shownValues, translated, json);
        }
      }

    return factory.translateStartpoint(json, shownValues) + "\nwhere {\n" + SPARQL + "\n} LIMIT 200";
  };
	
	
	
	
	
	
  /**
   * Function to translate the header of a SPARQL query, including the shown values
   * @param json
   * @param shownValues
   */
  factory.translateStartpoint = function (json, shownValues) {

    var SPARQLStart = "";

    if(json.START.type === "LIST_ALL")   {
      SPARQLStart = "SELECT DISTINCT ";
    }
    else {
      SPARQLStart = "SELECT ";
    }

    for(var i = 0; i < shownValues.length; i++) {
      SPARQLStart += "?" + shownValues[i] + " ";
    }

    return SPARQLStart;
  };


	

    /**
     * Function to translate the header of a SPARQL query, including the shown values<commit_msg>Extended translation to all subjects<commit_after>'use strict';
/**
 * JSON Translator Factory 
 * A factory to handle translation of JSON -> SPARQL
 *
 */

angular.module('GSB.services.translatorToSPARQL', ['GSB.config'])
  .factory('TranslatorToSPARQL', ['globalConfig', '$log', function (globalConfig, $log) {
    
    var factory = {};

    /**
     * Function to start translation process, with call to changeURIs for the mockup data
	   * and replaceAliasSpaces to replace spaces with underscores
	   * @param json
     */
    factory.translateJSONToSPARQL = function (json) {
      
      //json = factory.changeURIs(json);
	    
	    json = factory.replaceAliasSpaces(json);
	    
	    return factory.translateAll(json);
    };
	  
    /**
     *  Initial translation function that takes a JSON-object, looks for the main class and starts translation there, 
     *  and in the end of the process adds shown values and SPARQL-'header' (e.g. SELECT DISTINCT..)
     * @param json
     */
    factory.translateAll = function (json) {

      var shownValues = [];
      var translated = [];
      var SPARQL = "";

      for(var i = 0; i < json.SUBJECTS.length; i++)
      {
        if(json.SUBJECTS[i].alias === json['START'].link.linkPartner)
        {
          SPARQL +=  factory.translateSubject(json.SUBJECTS[i], shownValues, translated, json);
        }
      }

	 SPARQL += factory.translateInverseSubjects(shownValues, translated, json);
	  
    return factory.translateStartpoint(json, shownValues) + "\nwhere {\n" + SPARQL + "\n} LIMIT 200";
  };
	
	
	factory.translateInverseSubjects = function (shownValues, translated, json) {
	
	  var SPARQL = "";
	
	  for(var i = 0; i < json.SUBJECTS.length; i++)
        {
          if(!factory.presentInArray(translated, json.SUBJECTS[i].alias))
          {
            SPARQL +=  factory.translateSubject(json.SUBJECTS[i], shownValues, translated, json);
          }
        }
		
		return SPARQL;
	};
	
	
	
  /**
   * Function to translate the header of a SPARQL query, including the shown values
   * @param json
   * @param shownValues
   */
  factory.translateStartpoint = function (json, shownValues) {

    var SPARQLStart = "";

    if(json.START.type === "LIST_ALL")   {
      SPARQLStart = "SELECT DISTINCT ";
    }
    else {
      SPARQLStart = "SELECT ";
    }

    for(var i = 0; i < shownValues.length; i++) {
      SPARQLStart += "?" + shownValues[i] + " ";
    }

    return SPARQLStart;
  };


	

    /**
     * Function to translate the header of a SPARQL query, including the shown values
{'fraction_non_alphanumeric': 0.12278808804488563, 'fraction_numerical': 0.002373759171342253, 'mean_word_length': 2.54087089381207, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 5, 'https://': 0, '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': '5091774', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1386, 'n_words': 373}