--
--  $Id$
--
--  This file is part of the OpenLink Software Virtuoso Open-Source (VOS)
--  project.
--
--  Copyright (C) 1998-2014 OpenLink Software
--
--  This project is free software; you can redistribute it and/or modify it
--  under the terms of the GNU General Public License as published by the
--  Free Software Foundation; only version 2 of the License, dated June 1991.
--
--  This program is distributed in the hope that it will be useful, but
--  WITHOUT ANY WARRANTY; without even the implied warranty of
--  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
--  General Public License for more details.
--
--  You should have received a copy of the GNU General Public License along
--  with this program; if not, write to the Free Software Foundation, Inc.,
--  51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
--

-- Sample facet queries 


-- <query> <class iri="http://xmlns.com/foaf/0.1/Person" /><view type="list" limit="10" /></query>
set blobs on;

select fct_query (xtree_doc ('
<query> <class iri="http://xmlns.com/foaf/0.1/Person" />
<property iri="foaf:knows"><property iri="foaf:name"><value>"Joe"</value>  </property>
</property>
<view type="list" limit="10" /></query>
 '));
 
 select fct_test ('
<query> <class iri="http://xmlns.com/foaf/0.1/Person" />
<property iri="foaf:knows"><property iri="foaf:name"><value>"Joe"</value>  </property>
</property>
<view type="list" limit="10" /></query>
 ');

select fct_query (xtree_doc ('
<query> <class iri="http://xmlns.com/foaf/0.1/Person" />
<property iri="foaf:knows"><property iri="foaf:name"><value>"Joe"</value></property>
</property>
<view type="properties" limit="10" /></query>
 '));
 
 select fct_test ('
<query> <class iri="http://xmlns.com/foaf/0.1/Person" />
<property iri="foaf:knows"><property iri="foaf:name"><value>"Joe"</value></property>
</property>
<view type="properties" limit="10" /></query>
 ');

select fct_query (xtree_doc ('
<query><text>semantic</text> <view type="text" limit="10" />
</query>'));

select fct_test ('
<query><text>semantic</text> <view type="text" limit="10" />
</query>');

select fct_test ('
<query> 
  <class iri="http://xmlns.com/foaf/0.1/Person" />
<view type="properties" limit="10" /></query>
 ', 1000);

select fct_query (xtree_doc ('<query><class iri="http://xmlns.com/foaf/0.1/Person"/> <view type="list" limit="10"/></query>'));

select fct_test ('<query><class iri="http://xmlns.com/foaf/0.1/Person"/> <view type="list" limit="10"/></query>');

select fct_test ('<query> <text>semantic web</text><view type="text" limit="20"/></query>');

select fct_test ('<query> <text>hottie</text><view type="text-properties" limit="20"/></query>');

select fct_test ('<query> <text property="http://purl.org/dc/elements/1.1/description">hottie</text><view type="text" limit="20"/></query>');

select xslt ('file://fct/fct_vsp.xsl',
             xtree_doc ('<facets><result><row><column>http://xyz.com/xyz.htm</column></row></result></facets>'), 
             vector ('sid', 2, 'type', 'properties'))

create procedure fct_exp (in str varchar)
{
  declare txt any;
  declare max_s int;
  txt := string_output ();
  	max_s := 0;
  fct_query_info (xpath_eval ('/query', xtree_doc (str)), 0, max_s, 0, 1, txt);
  http_value (xtree_doc ('<test />'), null, txt);
  return string_output_string (txt);
}
;


select fct_query (xtree_doc ('<query> <text>new york</text><view type="text-d" limit="20"/></query>'));

cl_exec ('__dbf_set (''dc_batch_sz'', 100000)');
cl_exec ('__dbf_set (''enable_qp'', 1)');

select count (*) from (sparql select ?c u_count (( <SHORT_OR_LONG::IRI_RANK> (?s1))) as ?ct where { ?s1 ?s1textp ?o1 . ?o1 bif:contains  '(NEW AND YORK)'  option (score ?sc) . ?s1 a ?c . } group by ?c) s;

select c, ct  from (sparql select ?c sql:u_count (( <SHORT_OR_LONG::IRI_RANK> (?s1))) as ?ct where { ?s1 ?s1textp ?o1 . ?o1 bif:contains  '(NEW AND YORK)'  option (score ?sc) . ?s1 a ?c . } group by ?c) s;

sparql     select 
     (<sql:s_sum_page> (<sql:vector_agg> (<bif:vector> (?c1, ?sm)), <bif:vector> ('NEW', 'YORK'))) as ?res where { { 
      select (<SHORT_OR_LONG::>(?s1)) as ?c1,  (<sql:S_SUM> ( <SHORT_OR_LONG::IRI_RANK> (?s1), <SHORT_OR_LONG::>(?s1textp), <SHORT_OR_LONG::>(?o1), ?sc ) ) as ?sm  where { ?s1 ?s1textp ?o1 . ?o1 bif:contains  '(NEW AND YORK)'  option (score ?sc)  . }order by desc (<sql:sum_rank> ((<sql:S_SUM> ( <SHORT_OR_LONG::IRI_RANK> (?s1), <SHORT_OR_LONG::>(?s1textp), <SHORT_OR_LONG::>(?o1), ?sc ) ) ) ) limit 20 }};


cl_exec ('__dbf_set (''dc_batch_sz'', 100000)');


select c, ct  from (sparql select ?c sql:u_count (( <SHORT_OR_LONG::IRI_RANK> (?s1))) as ?ct where { ?s1 ?s1textp ?o1 . ?o1 bif:contains  '(NEW AND haven)'  option (score ?sc) . ?s1 a ?c . filter (?c = <http://dbpedia.org/ontology/Bridge> ) } group by ?c order by desc (?ct)) s;

explain ('select c, ct  from (sparql select ?c sql:u_count (( <SHORT_OR_LONG::IRI_RANK> (?s1))) as ?ct where { ?s1 ?s1textp ?o1 . ?o1 bif:contains  ''(NEW AND haven)''  option (score ?sc) . ?s1 a ?c . filter (?c = <http://dbpedia.org/ontology/Bridge> ) } group by bif:idn (?c) order by desc (?ct)) s');;

select xmlelement ("result", xmlattributes ('properties' as "type"),
xmlagg (xmlelement ("row",
xmlelement ("column",
xmlattributes (fct_lang ("c1") as "xml:lang",
fct_dtp ("c1") as "datatype",
fct_short_form(__ro2sq("c1")) as "shortform"),
__ro2sq ("c1")),
xmlelement ("column",
fct_label_np ("c1", 0, 'facets' )),
xmlelement ("column",
fct_bold_tags("c2")))))
from (sparql define output:valmode "LONG"     select ?s1p as ?c1 count (*) as ?c2  where {?s1 a <http://xmlns.com/foaf/0.1/Person> . ?s1 ?s1p ?s1o . } group by ?s1p order by desc 2 limit 10 ) xx option (quietcast);

explain (' select xmlelement ("result", xmlattributes (''properties''
as "type"), xmlagg (xmlelement ("row", xmlelement ("column",
xmlattributes (fct_lang ("c1") as "xml:lang", fct_dtp ("c1") as
"datatype", fct_short_form(__ro2sq("c1")) as "shortform"), __ro2sq
("c1")), xmlelement ("column", fct_label_np ("c1", 0, ''facets'' )),
xmlelement ("column", fct_bold_tags("c2"))))) from (sparql define
output:valmode "LONG" select ?s1p as ?c1 count (*) as ?c2 where {?s1 a
<http://xmlns.com/foaf/0.1/Person> . ?s1 ?s1p ?s1o . } group by ?s1p
order by desc 2 limit 10 ) xx option (quietcast)');

-- crash #1

sparql select ?s1 as ?c1, ( bif:search_excerpt ( bif:vector ( ) , ?o1 ) ) as ?c2, ?sc, ?rank where 
  { 
    { 
      { 
        select ?s1, ( ?sc * 3e-1 ) as ?sc, ?o1, ( sql:rnk_scale ( <LONG::IRI_RANK> ( ?s1 ) ) ) as ?rank where 
        { 
          ?s1 <http://dbpedia.org/ontology/birthPlace> ?s2 .
          filter ( ?s2 = <http://dbpedia.org/resource/Amsterdam> ) .
          
        }
       order by desc ( ?sc * 3e-1 + sql:rnk_scale ( <LONG::IRI_RANK> ( ?s1 ) ) ) limit 20 offset 0 
      }
    }
  }
;

-- crash #2

sparql define input:inference "b3sifp"  
select ?s1p as ?c1 count ( * ) as ?c2 
where {
  ?s1 ?s1textp ?o1 .
  ?o1 bif:contains '"Amsterdam"' .
  ?s1 ?s1p ?s1o .
}  
group by ?s1p order by desc 2 limit 20 offset 0 
;

-- crash #3

sparql define input:inference "b3sifp" select ?s1ip as ?c1 count ( * ) as ?c2 where 
  { 
    ?s1 ?s1textp ?o1 .
    ?o1 bif:contains ' ( IDEHEN AND KINGSLEY ) ' .?s1 a <http://xmlns.com/foaf/0.1/Person> .
    ?s1o ?s1ip ?s1 .
    
  }
 group by ?s1ip order by desc 2 limit 20 offset 0  
;

