//query
sparqlquery=`PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ecrm: <http://erlangen-crm.org/current/>
PREFIX hdn: <http://hdn.dantenetwork.it/resource/work/commedia/cantica/>
PREFIX lemon: <http://lemon-model.net/lemon#>
PREFIX orl: <http://dantenetwork.it/resource/ontology/ORL/>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>

SELECT ?pos ?sc ?ver ?canto ?cantica ?form
WHERE {
     ?occ ecrm:P148_is_component_of ?ver .
     ?occ hdn:isInPosition ?pos .
     ?canto ecrm:P148_has_component ?ver .
     ?ver ecrm:P190_has_symbolic_content ?sc .
     ?cantica ecrm:P148_has_component ?canto .
     ?occ hdn:occurrenceOf ?form .
     ?form a orl:FormaVerboVolgare .
     ?form orl:TipoTransitivitàVerboVolgare orl:VerboVolgareTransitivo .
     ?form orl:TipoDiatesiVerboVolgare orl:VerboVolgareAttivo .
     ?form orl:TipoConiugazioneVerboVolgare orl:VerboVolgarePrimaConiugazione .
     ?form orl:TipoTempoVerboVolgare orl:FuturoSempliceIndicativoVerboVolgare .
     ?form orl:TipoPersonaVerboVolgare orl:PrimaPersonaSingolareVerboVolgare .
     ?form lemon:writtenRep ?Rappresentazione .
     FILTER regex(?Rappresentazione,"^farò$","i") .
 }`

	function writesparqlquery(query) {
	return (query);
}
// write the query in natural language
	
function writesearchValue(form) {
	var strq='il lemma';
	var strcategoria='in tutte le categorie'
		
	if (form.oggetto.value=='forma')
		strq='la forma';
	if (form.categoria.value!='all')
		strcategoria='nella categoria '+ form.categoria.value;
			
	return `Cerco: ${strq} ${form.query.value}, ${form.tipo.value},  ${strcategoria}`
}

// the basic initial query is parsed

function gimmespq(){
	  var SparqlParser = sparqljs.Parser;
	   var parser = new SparqlParser();
	  //const sparqlParser = sp_js.Parser;
	  return parser.parse(
	  `PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  PREFIX ecrm: <http://erlangen-crm.org/current/>
  PREFIX hdn: <http://hdn.dantenetwork.it/resource/work/commedia/cantica/>
  PREFIX lemon: <http://lemon-model.net/lemon#>
  PREFIX orl: <http://dantenetwork.it/resource/ontology/ORL/>
  PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
  PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns>

  SELECT ?pos ?sc ?ver ?canto ?cantica ?form ?categ
  WHERE {
     ?occ ecrm:P148_is_component_of ?ver .
     ?occ hdn:isInPosition ?pos .
     ?canto ecrm:P148_has_component ?ver .
     ?ver ecrm:P190_has_symbolic_content ?sc .
     ?cantica ecrm:P148_has_component ?canto .
     ?occ hdn:occurrenceOf ?form .
     ?form a ?categ .
     ?form lemon:writtenRep ?Rappresentazione .
     FILTER regex(?Rappresentazione,"^farò$","i") .
  }`);
	}

// the query is transformed according the form values

function gimmespqfromjson(form){
	  const sparqlgen = sparqljs.Generator;
	  const generator = new sparqlgen({orl: "http://www.w3.org/2000/01/rdf-schema#" })
	  const parsedquery=gimmespq()
	  if (form.categoria.value!='all') 
	    parsedquery.where[0].triples.push(JSON.parse(data21)[form.categoria.value]);
	  if (form.query.value!="")
	    switch(form.tipo.value) {
	      case 'parola':
	        parsedquery.where[1].expression.args[1].value="^"+form.query.value+"$";
	        break;
	      case 'sottostringa':
	        parsedquery.where[1].expression.args[1].value=form.query.value;
	      break;
	      case 'prefisso':
	        parsedquery.where[1].expression.args[1].value=form.query.value;
	      break;
	      case 'suffisso':
	        parsedquery.where[1].expression.args[1].value=form.query.value+"$";
	      break;
	      default:
	        parsedquery.where[1].expression.args[1].value="^"+form.query.value+"$";
	    };
	  return generator.stringify(parsedquery);
	
}

data21 = `{"verbovolgare": {"subject": {"termType": "Variable", "value":"form"}, "predicate": {"termType":"NamedNode", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"}, "object": {"termType": "NamedNode", "value": "http://dantenetwork.it/resource/ontology/ORL/FormaVerboVolgare"}},
	"sostantivovolgare": {"subject": {"termType": "Variable", "value":"form"}, "predicate": {"termType":"NamedNode", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"}, "object": {"termType": "NamedNode", "value": "http://dantenetwork.it/resource/ontology/ORL/FormaSostantivoVolgare"}},
	"aggettivovolgare": {"subject": {"termType": "Variable", "value":"form"}, "predicate": {"termType":"NamedNode", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"}, "object": {"termType": "NamedNode", "value": "http://dantenetwork.it/resource/ontology/ORL/FormaAggettivoVolgare"}},
	"avverbiovolgare": {"subject": {"termType": "Variable", "value":"form"}, "predicate": {"termType":"NamedNode", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"}, "object": {"termType": "NamedNode", "value": "http://dantenetwork.it/resource/ontology/ORL/FormaAvverbioVolgare"}},
	"pronomevolgare": {"subject": {"termType": "Variable", "value":"form"}, "predicate": {"termType":"NamedNode", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"}, "object": {"termType": "NamedNode", "value": "http://dantenetwork.it/resource/ontology/ORL/FormaPronomeVolgare"}},
	"congiunzionevolgare": {"subject": {"termType": "Variable", "value":"form"}, "predicate": {"termType":"NamedNode", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"}, "object": {"termType": "NamedNode", "value": "http://dantenetwork.it/resource/ontology/ORL/FormaCongiunzioneVolgare"}},
	"articolovolgare": {"subject": {"termType": "Variable", "value":"form"}, "predicate": {"termType":"NamedNode", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"}, "object": {"termType": "NamedNode", "value": "http://dantenetwork.it/resource/ontology/ORL/FormaArticoloVolgare"}},
	"preposizionevolgare": {"subject": {"termType": "Variable", "value":"form"}, "predicate": {"termType":"NamedNode", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"}, "object": {"termType": "NamedNode", "value": "http://dantenetwork.it/resource/ontology/ORL/FormaPreposizioneVolgare"}},
	"interiezionevolgare": {"subject": {"termType": "Variable", "value":"form"}, "predicate": {"termType":"NamedNode", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"}, "object": {"termType": "NamedNode", "value": "http://dantenetwork.it/resource/ontology/ORL/FormaInteriezioneVolgare"}},
	"onomasticavolgare": {"subject": {"termType": "Variable", "value":"form"}, "predicate": {"termType":"NamedNode", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"}, "object": {"termType": "NamedNode", "value": "http://dantenetwork.it/resource/ontology/ORL/FormaOnomasticaVolgare"}},
	"citazionevolgare": {"subject": {"termType": "Variable", "value":"form"}, "predicate": {"termType":"NamedNode", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"}, "object": {"termType": "NamedNode", "value": "http://dantenetwork.it/resource/ontology/ORL/FormaCitazioneVolgare"}}, 
	"locuzionevolgare": {"subject": {"termType": "Variable", "value":"form"}, "predicate": {"termType":"NamedNode", "value": "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"}, "object": {"termType": "NamedNode", "value": "http://dantenetwork.it/resource/ontology/ORL/FormaLocuzioneVolgare"}}
	}
	`