Skip to content

Commit

Permalink
[MOD] towards #21
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrewSales committed Aug 4, 2023
1 parent c938978 commit 887e03a
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 25 deletions.
44 changes: 30 additions & 14 deletions context.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ declare variable $c:ALL_PATTERNS as xs:string := '#ALL';
: @param instance the document instance
: @param schema the Schematron schema
: @param phase the active phase
: @param options map of options
:)
declare function c:get-context(
$instance as node(),
$schema as element(sch:schema),
$phase as xs:string?
$phase as xs:string?,
$options as map(*)?
)
as map(*)
{
Expand All @@ -39,11 +41,15 @@ as map(*)
$instance,
$namespaces,
$schema/sch:ns,
map{}
map{},
$options
)
else map{}

return map{
return
map:merge(
($options,
map{
'phase' : $active-phase,
'patterns' : $active-patterns,
'ns-decls' : $namespaces,
Expand All @@ -52,7 +58,8 @@ as map(*)
'diagnostics' : $schema/sch:diagnostics/sch:diagnostic,
'properties' : $schema/sch:properties/sch:property,
'functions' : $schema/xqy:function
}
})
)
};

(:NAMESPACE DECLARATIONS:)
Expand Down Expand Up @@ -140,13 +147,15 @@ as element(sch:let)*
: @param instance the document instance
: @param namespaces namespace declarations
: @param bindings global variable bindings
: @param options map of options
:)
declare function c:evaluate-global-variables(
$variables as element(sch:let)*,
$instance as node(),
$namespaces as xs:string?,
$ns-elems as element(sch:ns)*,
$bindings as map(*)
$bindings as map(*),
$options as map(*)?
)
as map(*)
{
Expand All @@ -167,7 +176,8 @@ as map(*)
$instance,
$prolog || '$' || $var/@name,
$ns-elems,
$bindings
$bindings,
$options
)

(: let $_ := trace('[5]$bindings='||serialize($binding, map{'method':'adaptive'})) :)
Expand All @@ -177,7 +187,8 @@ as map(*)
$instance,
$namespaces,
$ns-elems,
$binding
$binding,
$options
)
};

Expand All @@ -189,13 +200,15 @@ as map(*)
: @param variables pattern variables
: @param instance the document instance
: @param prolog global variable and namespace declarations
: @param options map of options
:)
declare function c:evaluate-root-context-variables(
$variables as element(sch:let)*,
$instance as node()+,
$namespaces as xs:string?,
$ns-elems as element(sch:ns)*,
$bindings as map(*)
$bindings as map(*),
$options as map(*)?
)
as map(*)
{
Expand All @@ -215,7 +228,8 @@ as map(*)
$instance,
$prolog || utils:local-variable-decls($var) || ' return $' || $var/@name,
$ns-elems,
$bindings
$bindings,
$options
)

(: let $_ := trace('[5]$bindings='||serialize($binding, map{'method':'adaptive'})) :)
Expand All @@ -225,7 +239,8 @@ as map(*)
$instance,
$namespaces,
$ns-elems,
$binding
$binding,
$options
)
};

Expand All @@ -242,16 +257,17 @@ declare function c:evaluate-global-variable(
$instance as node(),
$query as xs:string?,
$ns-elems as element(sch:ns)*,
$bindings as map(*)
$bindings as map(*),
$options as map(*)?
)
as map(*)
{
(: let $_ := trace('>>>QUERY='||$query) :)
let $value as item()* := if($variable/@value)
then xquery:eval(
then utils:eval(
$query => utils:escape(),
map:merge(($bindings, map{'':$instance})),
map{'pass':'true'}
map:merge($options, map{'pass':'true'})
)
else $variable/*
let $bindings := map:merge(
Expand Down Expand Up @@ -283,7 +299,7 @@ as map(*)
{
if($documents)
then
let $uris := xquery:eval(
let $uris := utils:eval(
utils:make-query-prolog($context) || $documents => utils:escape(),
map:merge(($context?globals, map{'':$context?instance})),
map{'pass':'true'} (:report exception details:)
Expand Down
43 changes: 36 additions & 7 deletions evaluate.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,33 @@ import module namespace utils = 'http://www.andrewsales.com/ns/xqs-utils' at
declare namespace sch = "http://purl.oclc.org/dsdl/schematron";
declare namespace svrl = "http://purl.oclc.org/dsdl/svrl";

(:~ Evaluates the schema to produce SVRL output.
: @param instance the document instance
: @param schema the Schematron schema
: @param phase the active phase
: @param options map of options
:)
declare function eval:schema(
$instance as node(),
$schema as element(sch:schema),
$phase as xs:string?,
$options as map(*)?
)
{
if($options?dry-run eq 'true')
then
<svrl:schematron-output phase='#ALL'>
{output:schema-title($schema/sch:title)}
{$schema/@schemaVersion}
{output:namespace-decls-as-svrl($schema/sch:ns)}
{for $phase in ($schema/sch:phase/@id/data(), '')
let $context as map(*) := context:get-context($instance, $schema, $phase, $options)
return eval:phase($context)}
</svrl:schematron-output>
else
eval:schema($instance, $schema, $phase)
};

(:~ Evaluates the schema to produce SVRL output.
: @param instance the document instance
: @param schema the Schematron schema
Expand All @@ -25,7 +52,7 @@ declare function eval:schema(
$phase as xs:string?
)
{
let $context as map(*) := context:get-context($instance, $schema, $phase)
let $context as map(*) := context:get-context($instance, $schema, $phase, map{})

return
<svrl:schematron-output>
Expand Down Expand Up @@ -57,7 +84,8 @@ declare function eval:pattern(
$context?instance,
$context?ns-decls,
$pattern/../sch:ns,
$context?globals
$context?globals,
map{'dry-run':$context?dry-run}
)
(: let $_ := trace('PATTERN $globals='||serialize($globals, map{'method':'adaptive'})) :)
let $context := map:put($context, 'globals', $globals)
Expand Down Expand Up @@ -122,10 +150,10 @@ as element()*
' '
)
(: let $_ := trace('[2]RULE query='||$query) :)
let $rule-context := xquery:eval(
let $rule-context := utils:eval(
$query => utils:escape(),
map:merge((map{'':$context?instance}, $context?globals)),
map{'pass':'true'} (:report exception details:)
map{'pass':'true', 'dry-run':$context?dry-run}
)
return
if($rule-context)
Expand Down Expand Up @@ -181,10 +209,10 @@ declare function eval:assertion(
$context as map(*)
)
{
let $result := xquery:eval(
let $result := utils:eval(
$prolog || $assertion/@test => utils:escape(),
map:merge((map{'':$rule-context}, $context?globals)),
map{'pass':'true'}
map{'pass':'true', 'dry-run':$context?dry-run}
)
return
typeswitch($assertion)
Expand Down Expand Up @@ -212,7 +240,8 @@ declare function eval:phase($context as map(*))
$context?instance,
$context?ns-decls,
$phase/../sch:ns,
$context?globals
$context?globals,
map{'dry-run':$context?dry-run}
)
let $context := map:put($context, 'globals', $globals)

Expand Down
13 changes: 9 additions & 4 deletions svrl.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
:)
module namespace output = 'http://www.andrewsales.com/ns/xqs-output';

import module namespace utils = 'http://www.andrewsales.com/ns/xqs-utils' at
'utils.xqm';

declare namespace sch = "http://purl.oclc.org/dsdl/schematron";
declare namespace svrl = "http://purl.oclc.org/dsdl/svrl";

Expand Down Expand Up @@ -113,15 +116,17 @@ declare function output:assertion-message-content(
typeswitch($node)
case element(sch:name)
return if($node/@path)
then xquery:eval(
then utils:eval(
$prolog || $node/@path,
map:merge((map{'':$rule-context}, $context?globals))
map:merge((map{'':$rule-context}, $context?globals)),
map{'pass':'true'}
)
else name($rule-context)
case element(sch:value-of)
return xquery:eval(
return utils:eval(
$prolog || $node/@select,
map:merge((map{'':$rule-context}, $context?globals))
map:merge((map{'':$rule-context}, $context?globals)),
map{'pass':'true'}
)
=> string()
case element(sch:emph)
Expand Down
1 change: 1 addition & 0 deletions test/test-evaluate.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -814,6 +814,7 @@ declare %unit:test function _:global-variable-bindings()
document{<foo bar='some value'><blort/></foo>},
'',
(),
map{},
map{}
)
return (
Expand Down
12 changes: 12 additions & 0 deletions utils.xqm
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,15 @@ declare function utils:check-duplicate-variable-names($decls as element(sch:let)
|| $names[index-of($names, .)[2]]
) else()
};

(:~ Wrapper around xquery:eval() :)
declare function utils:eval(
$query as xs:string,
$bindings as map(*),
$options as map(*)
) as item()*
{
if($options?dry-run eq 'true')
then ()
else xquery:eval($query, $bindings, map{'pass':'true'})
};

0 comments on commit 887e03a

Please sign in to comment.