From a22ad682679935d3d737ac64d5e8f8dfafc10d4b Mon Sep 17 00:00:00 2001 From: cesare Date: Fri, 7 Oct 2022 17:06:36 +0200 Subject: [PATCH] implementaione OR --- js/utils.js | 122 +++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 117 insertions(+), 5 deletions(-) diff --git a/js/utils.js b/js/utils.js index 81bfb8d..afda403 100644 --- a/js/utils.js +++ b/js/utils.js @@ -1,3 +1,57 @@ + + +tq=` +PREFIX ecrm: +PREFIX orl: +PREFIX ontolex: +PREFIX olires: +SELECT (?form AS ?IRIForma) (?Rappresentazione AS ?Forma) (?lm AS ?Lemma) (?textver AS ?Verso) (?NumVerso AS ?NumeroVerso) (?t_canto AS ?Canto) (?t_cantica AS ?Cantica) WHERE { + ?fa orl:occurrenceOf ?form; + ecrm:P148i_is_component_of ?ver. + ?ver ecrm:P190_Has_Symbolic_Content ?textver; + olires:hasNumber ?NumVerso; + ecrm:P148i_is_component_of ?canto. + ?canto ecrm:P102_has_title ?t_canto; + ecrm:P148i_is_component_of ?cantica. + ?cantica ecrm:P102_has_title ?t_cantica. + ?form ontolex:writtenRep ?Rappresentazione. + ?lent ontolex:lexicalForm ?form; + ontolex:canonicalForm ?cf. + ?cf ontolex:writtenRep ?lm. + { ?form orl:FormaVerboVolgare; + orl:TipoDiatesiVerboVolgare orl:VerboVolgareAttivo.} + UNION + { ?form orl:FormaSostantivoVolgare; + orl:TipoNumeroSostantivoVolgare orl:NumeroSingolareSostantivoVolgare. } +} +ORDER BY (?Cantica) (?Canto) (?NumVerso) +LIMIT 50 +` + +var hdnquerygrammar=` +Expression + = head:((_)?"("(_)?("AND "/"OR ") Filtro+ (_)?(")")?){return '{"'+head[3]+'": ['+head[4]+"]}"}//{ return head}.join("").replaceAll(", ,","").replaceAll("(,","(").replaceAll(",("," (")} + / resu:Filtro {return resu[1].split().join()+' ';} +Filtro + = (_)"("?head:(("AND "/"OR "))? tail:Clausola+ (_)? closecl:(")")? + + {if (head != null) return ' {"'+head+'":['+tail+"]}"; + else if (tail!=null & tail.length <2) return ' {"TERM": ['+tail+']}' + else return ""+tail+"";} + / cl:Clausola + +Clausola + = cla:("Clausola"Integer)(_)? {return '"'+cla[0]+cla[1]+'"';} + + +Integer "integer" + = _ [0-9]+ { return parseInt(text(), 10); } + +_ "whitespace"= " "* + +` +var testmm=0 + //query ontoSparqlQuery=`PREFIX rdfs: PREFIX ecrm: @@ -95,10 +149,59 @@ function getStatements(){ return data21 +} +var theguiQ='' +var multipleCondSpQuery='' +var conditions=[] +var filters=[] +var unionop='{"union": {"type": "union", "patterns":[]}, "bgp": {"type": "bgp", "triples":[]}}' + + +function parseClause(op, tokens){ + alert (op+tokens) + if (op.trim()=='OR'){ + multipleCondSpQuery.where.push(JSON.parse(unionop)['union']); + //multipleCondSpQuery.where.at(-1).patterns.push(JSON.parse(unionop)['bgp']); + for (var mtoken in tokens){ + cond=theguiQ[tokens[mtoken]] + multipleCondSpQuery=buildClause(cond, multipleCondSpQuery, -1) + } + } + + + + + +} + +function buildSPQuery(guiquery){ + theguiQ=''; + theguiQ=guiquery; + var SparqlParser = sparqljs.Parser; + var spqparser = new SparqlParser(); + multipleCondSpQuery=spqparser.parse(ontoSparqlQuery); + mytestq=spqparser.parse(tq); + var parser = peg.generate(hdnquerygrammar); + test=guiquery['EsprLogica'].join(" "); + test=test.replaceAll("( ","(") + test=test.replaceAll(" )",")") + clauses= parser.parse(test); + jsonclauses=JSON.parse(clauses) + + for (const ck of Object.keys(jsonclauses)){ + parseClause(ck, jsonclauses[ck]) + } + + } function buildSQ(stquery) { - //alert (stquery['EsprLogica']) + + if(stquery['EsprLogica']!=null && stquery['EsprLogica'].length>1){ + buildSPQuery (stquery) + return multipleCondSpQuery; + } + var SparqlParser = sparqljs.Parser; var parser = new SparqlParser(); const parsedquery =parser.parse(ontoSparqlQuery); @@ -111,11 +214,12 @@ function buildSQ(stquery) { return (myquery) } + if (stquery['EsprLogica']!=null && stquery['EsprLogica']!='' && stquery['EsprLogica'].length==1){ clause=stquery['EsprLogica'] - myquery=buildClause(stquery[clause], parsedquery) + myquery=buildClause(stquery[clause], parsedquery, 0) return (myquery) } @@ -185,7 +289,7 @@ function buildClauseNick(clause, parsedquery){ } -function buildClause(clause, parsedquery){ +function buildClause(clause, parsedquery, whereind){ if (clause['queryText'].trim() != "") { if (clause['queryText'].trim().slice(-1) !=='*') @@ -201,7 +305,12 @@ function buildClause(clause, parsedquery){ parsedquery.where.splice(1, 1) } if (clause['typeGramm0'] !=null && clause['typeGramm0'] != "all") - parsedquery.where[0].triples.push(JSON.parse(data21)[clause['typeGramm0']]); + if(whereind> -1) + parsedquery.where[whereind].triples.push(JSON.parse(data21)[clause['typeGramm0']]); + else{ + parsedquery.where.at(-1).patterns.push(JSON.parse(unionop)['bgp']); + parsedquery.where.at(-1).patterns.at(-1).triples.push(JSON.parse(data21)[clause['typeGramm0']]); + } if (clause['typeGramm0'] !=null && Object.keys(sottoCategorie).includes(clause['typeGramm0'].trim())){ @@ -215,7 +324,10 @@ function buildClause(clause, parsedquery){ sttmts=sctmp[sctk[ke]] if (sttmts[keval]!=null && (sttmts[keval].split(':')[1]) != null){ subcatstat= buildsubcategoryClause(sttmts[keval]) - parsedquery.where[0].triples.push(subcatstat); + if(whereind> -1) + parsedquery.where[whereind].triples.push(subcatstat); + else + parsedquery.where.at(-1).patterns.at(-1).triples.push(subcatstat); } } }