implementaione OR

This commit is contained in:
cesare 2022-10-07 17:06:36 +02:00
parent e147920973
commit a22ad68267
1 changed files with 117 additions and 5 deletions

View File

@ -1,3 +1,57 @@
tq=`
PREFIX ecrm: <http://erlangen-crm.org/current/>
PREFIX orl: <http://dantenetwork.it/ontology/ORL/>
PREFIX ontolex: <http://www.w3.org/ns/lemon/ontolex#>
PREFIX olires: <https://dantenetwork.it/ontology/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 <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> orl:FormaVerboVolgare;
orl:TipoDiatesiVerboVolgare orl:VerboVolgareAttivo.}
UNION
{ ?form <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> 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: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX ecrm: <http://erlangen-crm.org/current/>
@ -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);
}
}
}