Thank you for your patience and support, Christian.

Then my second question has been answered as well. So basically, when the 
XQuery execution involves multiple imports of a Java module, regardless of 
packaging, BaseX may create multiple instances of the according Java class.

I slightly updated your queries as follows (it is not executable, but it shows 
our current setup):

testquery.xq:
-----
import module namespace m1 = 'de.interactive_instruments.module.MyQueryModule';
import module namespace m2 = 'm2' at 'm2.xqm';
import module namespace m3 = 'm3' at 'm3.xqm';

m1:init(... some data ...),
m1:add('m1'),

m1:functionMno(),
m2:do(),
m3:do(),

m1:values()
-----

m2.xqm:
-----
module namespace m2 = 'm2';

import module namespace m1 = 'de.interactive_instruments.module.MyQueryModule';

declare function m2:do() {
  ... some xquery that uses a function from m1, e.g. m1:functionAbc() ...
};
-----

m3.xqm:
-----
module namespace m3 = 'm3';

import module namespace m1 = 'de.interactive_instruments.module.MyQueryModule';

declare function m3:do() {
  ... some xquery that uses a function from m1, e.g. m1:functionXyz() ...
};
-----

m1 is a Java module, with de.interactive_instruments.module.MyQueryModule being 
the Java class that extends QueryModule.
m1 is initialised, i.e. fed with some information, using methods defined by 
class MyQueryModule.
Once that is done, expressions from testquery.xq call additional methods of 
MyQueryModule, either directly or indirectly (by calling functions from the 
XQuery modules m2 and m3).
We end up having multiple instances, i.e. Java objects, of class MyQueryModule, 
- presumably due to the multiple imports of that module - and only one of them 
has been initialised with the necessary information. What we would have wanted 
is a single instance/object of MyQueryModule, which has the information given 
to it by calling initialisation functions in testquery.xq, and which would 
therefore have this information regardless of where one of the m1 functions is 
called afterwards (be it in testquery.xq, or the modules m2 and m3). 

So some kind of QueryModule singleton construct would be handy, but I doubt 
that would work with BaseX as-is (assuming that a default constructor is used 
to create an object of MyQueryModule when one of its methods is invoked). I 
guess we could also explicitly create the MyQueryModule object (as outlined in 
the FileWriter example on 
http://docs.basex.org/wiki/Java_Bindings#Namespace_Declarations) and pass it 
around as a function parameter, but that would increase the level of complexity 
in our XQuery code quite a bit, so I doubt that this would be desirable for us. 
Maybe having a QueryModule implementation that acts as a façade to a singleton 
which does the actual work ... but I digress.

From what I've learned so far in this conversation, we'd better just ensure 
that there is only a single import of module m1.

Best regards,
Johannes


-----Ursprüngliche Nachricht-----
Von: Christian Grün [mailto:christian.gr...@gmail.com] 
Gesendet: Mittwoch, 18. Dezember 2019 15:59
An: Johannes Echterhoff <echterh...@interactive-instruments.de>
Cc: BaseX <basex-talk@mailman.uni-konstanz.de>
Betreff: Re: [basex-talk] How many QueryModule instances can be created?

> It may also answer my second question, where I was referring to expath 
> packaging and if that would make any difference - when compared to having a 
> pure JAR with the Java classes (and required libraries). It sounds like 
> expath packaging does not make a difference. Please correct me if I got this 
> wrong.

Right: EXPath is just another way of packaging the code. The XQuery parser will 
handle all modules equally, no matter if they have initially been packaged as 
XAR or via our own packaging mechanisms.

After having read your initial mail for a second time, I noticed I may have got 
your setup a little wrong. I think it will be easier to find a solution if we 
manage to construct a little example that shows the behavior you reported. 
Otherwise, there may be too many open questions to solve (how do you import and 
initialize your Java Code? is initialization identical to creating a class 
instance? is the initialization code embedded in global variables? etc).

I have attached a basic set of files that (I believe) simulates your setup. 
Could you extend it for me, or comment back what I may have wrongly understood?

Reply via email to