Hi Sebastian,
thanks for your e-mail; I’m pleased to see you are still working with BaseX.
So just by dropping a file like „custom.xqm" in the restxq path, we can customize the system.
…sounds like a nice idea.
The drawback is that we can only use this for HTTPS requests from the client, but not from the server, because we use HTTPS. So we cannot use RESTXQ requests server side with http:send-request. (Or do we?)
I guess I have no answer to that, as I haven’t actually come across this challenge yet. This might be completely off the track, but maybe it’s also a question of how Jetty is configured? Did you already check out the jetty.xml file and possible settings?
Christian
Hi Sebastian,
On this problem, a quick comment coming into my mind, since I faced a comparable problem recently. Without changing XQUERY or BaseX, it is possible to emulate overloading using the introspection module of BaseX. But you need a map which keys and values are functions (not possible with BaseX map module, but possible with XQUERY).
With such a map, keys are computed using the BaseX introspection module for your prod functions, since values are computed using the BaseX introspection module for your custom functions.
All calls are then concentrated through a dispatcher : something like
call($function,$argument) { if contains($MapOfFunctions, $function) then map:get($MapOfFunctions,$function)(argument) else $function($argument) }.
I tested this trick very recently actually, to overload the xquery:eval BaseX function. I named the resulting function evil_eval (a very dangerous function) !
Hope this help
Cheers
Jean-Marc
2013/11/30 Wiemer, Sebastian Sebastian.Wiemer@adesso.de
Hi Christian,
Hi Sebastian,
thanks for your e-mail; I’m pleased to see you are still working with BaseX.
And we still love BaseX :-) Thanks for keeping up the good work!
BTW: I unfortunately missed your and Alexanders presentation at the MarkUp Forum: are there any slides available?
So just by dropping a file like „custom.xqm" in the restxq path, we can customize the system.
…sounds like a nice idea.
The drawback is that we can only use this for HTTPS requests from the client, but not from the server, because we use HTTPS. So we cannot use RESTXQ requests server side with http:send-request. (Or do we?)
I guess I have no answer to that, as I haven’t actually come across this challenge yet. This might be completely off the track, but maybe it’s also a question of how Jetty is configured? Did you already check out the jetty.xml file and possible settings?
Yes, I checked jetty configuration (but I gave up), and it seems to me, that jetty.xml or web.xml might not be the right place for a solution after all.
What Florian and I probably are trying to accomplish here is a way to introduce aspect oriented or rule based programming to XQuery to decouple custom extensions from product code.
This is similar to imported modules in XSLT: If you import template rules, which are more specific, they will be applied first. Could this be applied to XQuery as well? (But in XSLT you would have to import the custom code and hence change your code. We can do that in XQuery as well, but it is not completely decoupled, is it?)
Florian and I came around with an idea, that does not use HTTP requests and maybe clarify our goal: how about introducing a „dispatch" annotation to XQuery?
Let’s assume we have a function „age“ that takes an age as integer and a name as string and prints a message. A customization would be to be more specific for teenagers. But we don’t want to change the code. the annotation „dispatch:declare“ declares a function to be potentially dispatched: if there is not a more specific function with a „dispatch:replace“ annotation, than that function is called. Maybe "dispatch:prepend" and "dispatch:append“ would be nice as well. Also it would come in very handy if the replacing function is allowed be updating or not updating.
product.xqm %dispatch:declare(age($age,$name)) declare function prod:age($age as xs:integer,$name as xs:string){ $name||" is "||$age||" years old." };
prod:age(14,'Emily’) = „Emily is 14 years old."
custom.xqm %dispatch:replace(age(19>$age>13,$name)) declare function custom:age($age as xs:integer,$name as xs:string){ $name||" is a "||$age||“ years old teenager." };
So a use of „prod:age“ would yield the result of „custom:age“: *prod*:age(14,'Emily’) = „Emily is a 14 years old *teenager*.“
Comments on this idea are very appreciated.
Sebastian
Christian
BaseX-Talk mailing list BaseX-Talk@mailman.uni-konstanz.de https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk
Hi Sebastian,
BTW: I unfortunately missed your and Alexanders presentation at the MarkUp Forum: are there any slides available?
The slides should soon be available here: http://www.markupforum.de/archiv.html
This is similar to imported modules in XSLT: If you import template rules, which are more specific, they will be applied first. Could this be applied to XQuery as well? (But in XSLT you would have to import the custom code and hence change your code. We can do that in XQuery as well, but it is not completely decoupled, is it?)
As Jean-Marc pointed out, the Inspection Module may help out. The module is fairly new, so we are interested in your (and everyone’s) feedback if you would like to have some functions added..
Christian
Hi Sebastian,
A quick comment for your code : it is better to use it sparsely, because inspect:functions is quite slow. If you want to improve perfs, create first a map. Here is the code that I am using for this kind of "overloading"
declare variable $common:MapIntrospecting := common:map_inspect_functions();
declare function common:name-function($fun as function(*)){fn:data(inspect:function($fun)/@name)};
declare function common:map_inspect_functions() { map:new(for $fun in inspect:functions() return map:entry(common:name-function($fun),$fun)) }; (this one has to be modified accordingly to your %dispatch annotation, i.e. add where exists(inspect:function($f)[annotation[@name='dispatch: default'][literal[1]=$id]]))
and the dispatcher becomes simply
declare function common:dispatcher(nodes as node()*,$fun as *function(*)*) as item()* { let $disptchedfun := map:get($common:MapIntrospecting, *common:name-function($fun)*) return if (empty($disptchedfun)) then $fun($nodes) else $disptchedfun($nodes) ) };
By „map of functions“ did you mean what "inspection:functions()“ is doing?
No, I am talking about a map that can take any item a keys, and any item as values. For instance, in the code above, I would prefer to write the following dispatcher
declare function common:dispatcher(nodes as node()*,$fun as *function(*)*) as item()* { let $disptchedfun := map:get(common:MapIntrospecting(),$fun) return if (empty($disptchedfun)) then $fun($nodes) else $disptchedfun($nodes) ) };
Its is simply a question of performance : in the first mechanism, you have first to "serialize" the function as a string (calling the inspect:function), then to look to a map(string, function), to finally get the correct function call. In the second you directly access to the function, avoiding an expensive call (inspect:function).
However, most probably this enhancement would be of less impact in your code, if you don't call this dispatcher trillions of time.
I don’t get it: how would you be able to overload the query eval function?
exactly as above. It is something a little bit more elaborate than the following code
declare function common:evil_eval($nodes as node()*,$expr as xs:string) as item()* { try {
let $fun := map:get($common:MapIntrospecting,$expr) return if (empty($fun)) then xquery:eval('$xml'||$expr,map{'$xml' := $nodes}) else $fun($nodes) } catch *("ouch") }; Hope this helps
Cheers,
Jean-Marc
2013/12/2 Christian Grün christian.gruen@gmail.com
Hi Sebastian,
BTW: I unfortunately missed your and Alexanders presentation at the
MarkUp
Forum: are there any slides available?
The slides should soon be available here: http://www.markupforum.de/archiv.html
This is similar to imported modules in XSLT: If you import template
rules,
which are more specific, they will be applied first. Could this be applied to XQuery as well?
(But in
XSLT you would have to import the custom code and hence change your code. We can do that in
XQuery
as well, but it is not completely decoupled, is it?)
As Jean-Marc pointed out, the Inspection Module may help out. The module is fairly new, so we are interested in your (and everyone’s) feedback if you would like to have some functions added..
Christian _______________________________________________ BaseX-Talk mailing list BaseX-Talk@mailman.uni-konstanz.de https://mailman.uni-konstanz.de/mailman/listinfo/basex-talk
basex-talk@mailman.uni-konstanz.de