Skip to content

Commit 336b0ea

Browse files
committed
Replaced QueryBuilder::in with the new ldh:add-filter-in query transform mode
1 parent c4cef09 commit 336b0ea

3 files changed

Lines changed: 108 additions & 16 deletions

File tree

src/main/webapp/static/com/atomgraph/linkeddatahub/xsl/bootstrap/2.3.2/client/form.xsl

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -366,16 +366,52 @@ WHERE
366366
<xsl:param name="resource-types" select="if (ancestor::div[@class = 'controls']//input[@class = 'forClass']/@value) then ancestor::div[@class = 'controls']//input[@class = 'forClass']/@value else ancestor::label//input[@class = 'forClass']/@value" as="xs:anyURI*"/>
367367
<xsl:param name="select-string" select="$select-labelled-string" as="xs:string?"/>
368368
<xsl:param name="limit" select="100" as="xs:integer?"/>
369+
<xsl:param name="label-var-name" select="'label'" as="xs:string"/>
370+
<xsl:param name="type-var-name" select="'Type'" as="xs:string"/>
369371
<xsl:variable name="key-code" select="ixsl:get(ixsl:event(), 'code')" as="xs:string"/>
370-
<!-- convert resource type URIs to SPARQLBuilder URIs -->
371-
<xsl:variable name="value-uris" select="array { for $uri in $resource-types[not(. = '&rdfs;Resource')] return ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'QueryBuilder'), 'uri', [ $uri ]) }"/>
372372
<xsl:variable name="select-builder" select="ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'SelectBuilder'), 'fromString', [ $select-string ])"/>
373-
<!-- pseudo JS code: SPARQLBuilder.SelectBuilder.fromString(select-builder).wherePattern(SPARQLBuilder.QueryBuilder.filter(SPARQLBuilder.QueryBuilder.regex(QueryBuilder.var("label"), QueryBuilder.term($value)))) -->
374-
<xsl:variable name="select-builder" select="ixsl:call($select-builder, 'wherePattern', [ ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'QueryBuilder'), 'filter', [ ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'QueryBuilder'), 'regex', [ ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'QueryBuilder'), 'str', [ ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'QueryBuilder'), 'var', [ 'label' ]) ]), ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'QueryBuilder'), 'term', [ ac:escape-regex(ixsl:get(., 'value')) ]), true() ]) ]) ])"/>
375-
<!-- pseudo JS code: SPARQLBuilder.SelectBuilder.fromString(select-builder).wherePattern(SPARQLBuilder.QueryBuilder.filter(SPARQLBuilder.QueryBuilder.in(QueryBuilder.var("Type"), [ $value ]))) -->
376-
<xsl:variable name="select-builder" select="if (empty($resource-types[not(. = '&rdfs;Resource')])) then $select-builder else ixsl:call($select-builder, 'wherePattern', [ ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'QueryBuilder'), 'filter', [ ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'QueryBuilder'), 'in', [ ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'QueryBuilder'), 'var', [ 'Type' ]), $value-uris ]) ]) ])"/>
377-
<xsl:variable name="select-string" select="ixsl:call($select-builder, 'toString', [])" as="xs:string?"/>
378-
<xsl:variable name="query-string" select="ac:build-describe($select-string, $limit, (), (), true())" as="xs:string?"/>
373+
<xsl:variable name="select-json-string" select="ixsl:call(ixsl:get(ixsl:window(), 'JSON'), 'stringify', [ ixsl:call($select-builder, 'build', []) ])" as="xs:string"/>
374+
<xsl:variable name="select-xml" select="json-to-xml($select-json-string)" as="document-node()"/>
375+
<!-- append FILTER(regex()) -->
376+
<xsl:variable name="select-xml" as="document-node()">
377+
<xsl:document>
378+
<xsl:apply-templates select="$select-xml" mode="ldh:add-regex-filter">
379+
<xsl:with-param name="var-name" select="$var-name" tunnel="yes"/>
380+
<xsl:with-param name="pattern" select="$text" tunnel="yes"/>
381+
<xsl:with-param name="flags" select="'iq'" tunnel="yes"/> <!-- case insensitive, ignore meta-characters: https://www.w3.org/TR/xpath-functions-31/#flags -->
382+
</xsl:apply-templates>
383+
</xsl:document>
384+
</xsl:variable>
385+
<!-- append FILTER($var IN ()) -->
386+
<xsl:variable name="select-xml" as="document-node()">
387+
<xsl:document>
388+
<xsl:choose>
389+
<!-- do not FILTER by $resource-types if the only type is rdfs:Resource -->
390+
<xsl:when test="empty($resource-types[not(. = '&rdfs;Resource')])">
391+
<xsl:sequence select="$select-xml"/>
392+
</xsl:when>
393+
<xsl:otherwise>
394+
<xsl:apply-templates select="$select-xml" mode="ldh:add-filter-in">
395+
<xsl:with-param name="var-name" select="$var-name" tunnel="yes"/>
396+
<xsl:with-param name="values" select="$resource-types" tunnel="yes"/>
397+
</xsl:apply-templates>
398+
</xsl:otherwise>
399+
</xsl:choose>
400+
</xsl:document>
401+
</xsl:variable>
402+
<!-- set LIMIT -->
403+
<xsl:variable name="select-xml" as="document-node()">
404+
<xsl:document>
405+
<xsl:apply-templates select="$select-xml" mode="ldh:replace-limit"/>
406+
</xsl:document>
407+
</xsl:variable>
408+
<!-- wrap SELECT into a DESCRIBE -->
409+
<xsl:variable name="query-xml" as="element()">
410+
<xsl:apply-templates select="$select-xml" mode="ldh:wrap-describe"/>
411+
</xsl:variable>
412+
<xsl:variable name="query-json-string" select="xml-to-json($query-xml)" as="xs:string"/>
413+
<xsl:variable name="query-json" select="ixsl:call(ixsl:get(ixsl:window(), 'JSON'), 'parse', [ $query-json-string ])"/>
414+
<xsl:variable name="query-string" select="ixsl:call(ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'SelectBuilder'), 'fromQuery', [ $query-json ]), 'toString', [])" as="xs:string"/>
379415
<xsl:variable name="results-uri" select="ac:build-uri($endpoint, map{ 'query': string($query-string) })" as="xs:anyURI"/>
380416
<xsl:variable name="request-uri" select="ldh:href($ldt:base, ldh:absolute-path(ldh:href()), map{}, $results-uri)" as="xs:anyURI"/> <!-- proxy the results -->
381417
<!-- TO-DO: use <ixsl:schedule-action> instead of document() -->

src/main/webapp/static/com/atomgraph/linkeddatahub/xsl/client.xsl

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1302,13 +1302,13 @@ WHERE
13021302
<!-- trigger typeahead in the search bar -->
13031303

13041304
<xsl:template match="input[@id = 'uri']" mode="ixsl:onkeyup" priority="1">
1305-
<xsl:param name="var-name" select="'label'" as="xs:string"/>
13061305
<xsl:param name="text" select="ixsl:get(., 'value')" as="xs:string?"/>
13071306
<xsl:param name="menu" select="following-sibling::ul" as="element()"/>
13081307
<xsl:param name="delay" select="400" as="xs:integer"/>
13091308
<xsl:param name="resource-types" as="xs:anyURI?"/>
13101309
<xsl:param name="select-string" select="$select-labelled-string" as="xs:string"/>
13111310
<xsl:param name="limit" select="100" as="xs:integer"/>
1311+
<xsl:param name="label-var-name" select="'label'" as="xs:string"/>
13121312
<xsl:variable name="key-code" select="ixsl:get(ixsl:event(), 'code')" as="xs:string"/>
13131313

13141314
<xsl:choose>
@@ -1363,7 +1363,7 @@ WHERE
13631363
<xsl:variable name="select-xml" as="document-node()">
13641364
<xsl:document>
13651365
<xsl:apply-templates select="$select-xml" mode="ldh:add-regex-filter">
1366-
<xsl:with-param name="var-name" select="$var-name" tunnel="yes"/>
1366+
<xsl:with-param name="var-name" select="$label-var-name" tunnel="yes"/>
13671367
<xsl:with-param name="pattern" select="$text" tunnel="yes"/>
13681368
<xsl:with-param name="flags" select="'iq'" tunnel="yes"/> <!-- case insensitive, ignore meta-characters: https://www.w3.org/TR/xpath-functions-31/#flags -->
13691369
</xsl:apply-templates>
@@ -1382,12 +1382,6 @@ WHERE
13821382
<xsl:variable name="query-json-string" select="xml-to-json($query-xml)" as="xs:string"/>
13831383
<xsl:variable name="query-json" select="ixsl:call(ixsl:get(ixsl:window(), 'JSON'), 'parse', [ $query-json-string ])"/>
13841384
<xsl:variable name="query-string" select="ixsl:call(ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'SelectBuilder'), 'fromQuery', [ $query-json ]), 'toString', [])" as="xs:string"/>
1385-
<!--
1386-
<xsl:variable name="select-builder" select="ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'SelectBuilder'), 'fromString', [ $select-string ])"/>
1387-
<xsl:variable name="select-builder" select="ixsl:call($select-builder, 'wherePattern', [ ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'QueryBuilder'), 'filter', [ ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'QueryBuilder'), 'regex', [ ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'QueryBuilder'), 'str', [ ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'QueryBuilder'), 'var', [ 'label' ]) ]), ixsl:call(ixsl:get(ixsl:get(ixsl:window(), 'SPARQLBuilder'), 'QueryBuilder'), 'term', [ ac:escape-regex($text) ]), true() ] ) ] ) ])"/>
1388-
<xsl:variable name="select-string" select="ixsl:call($select-builder, 'toString', [])" as="xs:string"/>
1389-
<xsl:variable name="query-string" select="ac:build-describe($select-string, $limit, (), (), true())" as="xs:string"/>
1390-
-->
13911385
<xsl:variable name="service-uri" select="xs:anyURI(ixsl:get(id('search-service'), 'value'))" as="xs:anyURI?"/>
13921386
<xsl:variable name="service" select="key('resources', $service-uri, ixsl:get(ixsl:window(), 'LinkedDataHub.apps'))" as="element()?"/>
13931387
<xsl:variable name="endpoint" select="($service/sd:endpoint/@rdf:resource/xs:anyURI(.), resolve-uri('sparql', $ldt:base))[1]" as="xs:anyURI"/>

src/main/webapp/static/com/atomgraph/linkeddatahub/xsl/query-transforms.xsl

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<!DOCTYPE xsl:stylesheet [
33
<!ENTITY ldh "https://w3id.org/atomgraph/linkeddatahub#">
44
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#">
5+
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#">
56
]>
67
<xsl:stylesheet version="3.0"
78
xmlns:xhtml="http://www.w3.org/1999/xhtml"
@@ -732,4 +733,65 @@ extension-element-prefixes="ixsl"
732733
</xsl:copy>
733734
</xsl:template>
734735

736+
<!-- FILTER($var IN ()) -->
737+
738+
<!-- identity transform -->
739+
<xsl:template match="@* | node()" mode="ldh:add-filter-in">
740+
<xsl:copy>
741+
<xsl:apply-templates select="@* | node()" mode="#current"/>
742+
</xsl:copy>
743+
</xsl:template>
744+
745+
<!-- append FILTER($var IN ()) to WHERE -->
746+
<xsl:template match="json:array[@key = 'where']" mode="ldh:add-filter-in" priority="1">
747+
<xsl:param name="var-name" as="xs:string" tunnel="yes"/>
748+
<xsl:param name="values" as="xs:anyAtomicType*" tunnel="yes"/>
749+
750+
<xsl:copy>
751+
<xsl:apply-templates select="@* | node()" mode="#current"/>
752+
753+
<json:map>
754+
<json:string key="type">filter</json:string>
755+
<json:map key="expression">
756+
<json:string key="type">operation</json:string>
757+
<json:string key="operator">in</json:string>
758+
<json:array key="args">
759+
<json:string>?<xsl:value-of select="$var-name"/></json:string>
760+
<json:array>
761+
<xsl:for-each select="$values">
762+
<xsl:choose>
763+
<xsl:when test=". instance of xs:anyURI">
764+
<json:string><xsl:value-of select="."/></json:string>
765+
</xsl:when>
766+
<xsl:when test=". instance of xs:string">
767+
<json:string>"<xsl:value-of select="."/>"</json:string>
768+
</xsl:when>
769+
<xsl:when test=". instance of xs:integer">
770+
<json:string>"<xsl:value-of select="."/>"^^&xsd;integer</json:string>
771+
</xsl:when>
772+
<xsl:when test=". instance of xs:float">
773+
<json:string>"<xsl:value-of select="."/>"^^&xsd;float</json:string>
774+
</xsl:when>
775+
<xsl:when test=". instance of xs:boolean">
776+
<json:string>"<xsl:value-of select="."/>"^^&xsd;boolean</json:string>
777+
</xsl:when>
778+
<xsl:when test=". instance of xs:date">
779+
<json:string>"<xsl:value-of select="."/>"^^&xsd;date</json:string>
780+
</xsl:when>
781+
<xsl:when test=". instance of xs:dateTime">
782+
<json:string>"<xsl:value-of select="."/>"^^&xsd;dateTime</json:string>
783+
</xsl:when>
784+
<!-- TO-DO: support more XSD types -->
785+
<xsl:otherwise>
786+
<xsl:message>Value type not recognized</xsl:message>
787+
</xsl:otherwise>
788+
</xsl:choose>
789+
</xsl:for-each>
790+
</json:array>
791+
</json:array>
792+
</json:map>
793+
</json:map>
794+
</xsl:copy>
795+
</xsl:template>
796+
735797
</xsl:stylesheet>

0 commit comments

Comments
 (0)