Write functions in sequence

Tuesday, 12 March 2019 · 8 messages
GC
Giuseppe G. A. Celano12 Mar 2019 11:19 #
Hi I wrote a single script which should do: write a file -> open this file -> write another different file. I put the write expressions in the right sequence, but it seems that the second one cannot happen because the file created by the first write function has not yet been created at the time the second function is invoked. Does anyone have a suggestion about this? Thanks. Best, Giuseppe
CG
Christian Grün12 Mar 2019 11:21 #
Hi Giuseppe, a little self-contained example would be appreciated! Best, Christian
Quoted text · Giuseppe G. A. Celano · 9 lines
On Tue, Mar 12, 2019 at 11:19 AM Giuseppe G. A. Celano <celano at informatik.uni-leipzig.de> wrote: > > Hi > > I wrote a single script which should do: write a file -> open this file -> write another different file. I put the write expressions in the right sequence, but it seems that the second one cannot happen because the file created by the first write function has not yet been created at the time the second function is invoked. Does anyone have a suggestion about this? Thanks. > > Best, > Giuseppe
MS
Michael Seiferlein reply to Giuseppe G. A. Celano, 12 Mar 2019, 11:1912 Mar 2019 11:27 #
Hi Guiseppe, The following pattern is supposed to / does work:
Quoted text · Giuseppe G. A. Celano · 3 lines
> file:write("1.txt", "Written to 1.txt"), > file:write("2.txt", file:read-text("1.txt")), > "Read from 2.txt: " || file:read-text('2.txt')
Could you maybe elaborate a bit more on your code? Best from Konstanz Michael
Quoted text · Giuseppe G. A. Celano · 8 lines
> Am 12.03.2019 um 11:19 schrieb Giuseppe G. A. Celano <celano at informatik.uni-leipzig.de>: > > Hi > > I wrote a single script which should do: write a file -> open this file -> write another different file. I put the write expressions in the right sequence, but it seems that the second one cannot happen because the file created by the first write function has not yet been created at the time the second function is invoked. Does anyone have a suggestion about this? Thanks. > > Best, > Giuseppe
GC
Giuseppe G. A. Celano12 Mar 2019 11:44 #
let $o :=  file:create-dir("/Users/mycomputer/prova")
let $o2 := file:write("/Users/mycomputer/prova/file1.xml", "ciao")
let $o3 := file:write("/Users/mycomputer/prova/file2.xml", file:read-text("/Users/mycomputer/prova/file1.xml"))
return
($o, $o2, $o3)
This actually works. In my real example the writing of $o2 requires e few seconds. It might be that $o3 is evaluated while $o2 is still running? Dr. Giuseppe G. A. Celano DFG-project leader <http://gepris.dfg.de/gepris/projekt/408121292> Universität Leipzig Institute of Computer Science, NLP Augustusplatz 10 Tel: +4934132223 04109 Leipzig Deutschland E-mail: celano at informatik.uni-leipzig.de <mailto:celano at informatik.uni-leipzig.de> Web site 1: http://asv.informatik.uni-leipzig.de/en/staff/Giuseppe_Celano <http://asv.informatik.uni-leipzig.de/en/staff/Giuseppe_Celano> Web site 2: https://sites.google.com/site/giuseppegacelano/ <https://sites.google.com/site/giuseppegacelano/>
Quoted text · Michael Seiferle · 25 lines
> On Mar 12, 2019, at 11:27 AM, Michael Seiferle <ms at basex.org> wrote: > > Hi Guiseppe, > > The following pattern is supposed to / does work: >> file:write("1.txt", "Written to 1.txt"), >> file:write("2.txt", file:read-text("1.txt")), >> "Read from 2.txt: " || file:read-text('2.txt') > > > Could you maybe elaborate a bit more on your code? > > Best from Konstanz > > Michael > >> Am 12.03.2019 um 11:19 schrieb Giuseppe G. A. Celano <celano at informatik.uni-leipzig.de <mailto:celano at informatik.uni-leipzig.de>>: >> >> Hi >> >> I wrote a single script which should do: write a file -> open this file -> write another different file. I put the write expressions in the right sequence, but it seems that the second one cannot happen because the file created by the first write function has not yet been created at the time the second function is invoked. Does anyone have a suggestion about this? Thanks. >> >> Best, >> Giuseppe >
CG
Christian Grün12 Mar 2019 11:57 #
Could you attach one of the queries that you mentioned in your first question (i.e., that didn’t work for you)? We might then be able to resolve why $o2 seems to take longer than you would expect it to take. In general, it’s recommendable not to bind code to variables if the expression does not yield any results. I would probably rewrite your code as follows:
let $dir := "/Users/mycomputer/prova/"
return (
  file:create-dir($dir),
  file:write($dir || "file1.xml", "ciao"),
  file:write($dir || "file2.xml", file:read-text($dir || "file1.xml"))
)
As XQuery is a function language, it would be allowed for a processor to run the second write call before the first. However, we enforce the evaluation of the original expression if expressions are non-deterministic or side-effecting (which is the case for all of your function calls).
Quoted text · Giuseppe G. A. Celano · 52 lines
On Tue, Mar 12, 2019 at 11:44 AM Giuseppe G. A. Celano <celano at informatik.uni-leipzig.de> wrote: > > let $o := file:create-dir("/Users/mycomputer/prova") > let $o2 := file:write("/Users/mycomputer/prova/file1.xml", "ciao") > let $o3 := file:write("/Users/mycomputer/prova/file2.xml", file:read-text("/Users/mycomputer/prova/file1.xml")) > return > ($o, $o2, $o3) > > This actually works. In my real example the writing of $o2 requires e few seconds. It might be that $o3 is evaluated while $o2 is still running? > > Dr. Giuseppe G. A. Celano > DFG-project leader > Universität Leipzig > Institute of Computer Science, NLP > Augustusplatz 10 > Tel: +4934132223 > 04109 Leipzig > Deutschland > > E-mail: celano at informatik.uni-leipzig.de > Web site 1: http://asv.informatik.uni-leipzig.de/en/staff/Giuseppe_Celano > Web site 2: https://sites.google.com/site/giuseppegacelano/ > > On Mar 12, 2019, at 11:27 AM, Michael Seiferle <ms at basex.org> wrote: > > Hi Guiseppe, > > The following pattern is supposed to / does work: > > file:write("1.txt", "Written to 1.txt"), > file:write("2.txt", file:read-text("1.txt")), > "Read from 2.txt: " || file:read-text('2.txt') > > > Could you maybe elaborate a bit more on your code? > > Best from Konstanz > > Michael > > Am 12.03.2019 um 11:19 schrieb Giuseppe G. A. Celano <celano at informatik.uni-leipzig.de>: > > Hi > > I wrote a single script which should do: write a file -> open this file -> write another different file. I put the write expressions in the right sequence, but it seems that the second one cannot happen because the file created by the first write function has not yet been created at the time the second function is invoked. Does anyone have a suggestion about this? Thanks. > > Best, > Giuseppe > > >
GC
Giuseppe G. A. Celanoin reply to Giuseppe G. A. Celano, 12 Mar 2019, 11:4412 Mar 2019 12:04 #
The code is long, because there are many functions. However, I did something like the following, and it works:
let $o :=  file:create-dir("/Users/mycomputer/prova")
let $o3 := file:write("/Users/mycomputer/prova/file2.xml",
                     (file:write("/Users/mycomputer/prova/file1.xml", "ciao"), file:read-text("/Users/mycomputer/prova/file1.xml"))
                              )

return
($o, $o3)
The evaluation of the first write() is forced to happen and conclude before the second write() is evaluated. I am still experimenting on this, but would there be a way to force completion of evaluation without embedding?
Quoted text · Giuseppe G. A. Celano · 49 lines
> On Mar 12, 2019, at 11:44 AM, Giuseppe G. A. Celano <celano at informatik.uni-leipzig.de> wrote: > > let $o := file:create-dir("/Users/mycomputer/prova") > let $o2 := file:write("/Users/mycomputer/prova/file1.xml", "ciao") > let $o3 := file:write("/Users/mycomputer/prova/file2.xml", file:read-text("/Users/mycomputer/prova/file1.xml")) > return > ($o, $o2, $o3) > > This actually works. In my real example the writing of $o2 requires e few seconds. It might be that $o3 is evaluated while $o2 is still running? > > Dr. Giuseppe G. A. Celano > DFG-project leader <http://gepris.dfg.de/gepris/projekt/408121292> > Universität Leipzig > Institute of Computer Science, NLP > Augustusplatz 10 > Tel: +4934132223 > 04109 Leipzig > Deutschland > > E-mail: celano at informatik.uni-leipzig.de <mailto:celano at informatik.uni-leipzig.de> > Web site 1: http://asv.informatik.uni-leipzig.de/en/staff/Giuseppe_Celano <http://asv.informatik.uni-leipzig.de/en/staff/Giuseppe_Celano> > Web site 2: https://sites.google.com/site/giuseppegacelano/ <https://sites.google.com/site/giuseppegacelano/> > >> On Mar 12, 2019, at 11:27 AM, Michael Seiferle <ms at basex.org <mailto:ms at basex.org>> wrote: >> >> Hi Guiseppe, >> >> The following pattern is supposed to / does work: >>> file:write("1.txt", "Written to 1.txt"), >>> file:write("2.txt", file:read-text("1.txt")), >>> "Read from 2.txt: " || file:read-text('2.txt') >> >> >> Could you maybe elaborate a bit more on your code? >> >> Best from Konstanz >> >> Michael >> >>> Am 12.03.2019 um 11:19 schrieb Giuseppe G. A. Celano <celano at informatik.uni-leipzig.de <mailto:celano at informatik.uni-leipzig.de>>: >>> >>> Hi >>> >>> I wrote a single script which should do: write a file -> open this file -> write another different file. I put the write expressions in the right sequence, but it seems that the second one cannot happen because the file created by the first write function has not yet been created at the time the second function is invoked. Does anyone have a suggestion about this? Thanks. >>> >>> Best, >>> Giuseppe >> >
MS
Michael Seiferlein reply to Giuseppe G. A. Celano, 12 Mar 2019, 11:4412 Mar 2019 12:05 #
Hi Guiseppe, actually the file-operations are non-deterministic and thus (1) never executed out of order and (2) even executed if you would not return their results ($o, $o2, $o3) and thus might be removed by the compiler. (Christian might correct me if I am wrong ;-)) I came up with the following example that waits 2 seconds before actually writing to $o2:
Quoted text · Giuseppe G. A. Celano · 10 lines
> let $o := file:create-dir("foo") > let $o2 := file:write("foo/file1.xml", > (function(){ > prof:sleep(2000), > "Written to File1.xml" > })() > ) > let $o3 := file:write("foo/file2.xml", file:read-text("foo/file1.xml")) > > return file:read-text("foo/file2.xml“)
So most probably your example does not fully reflect what’s actually happening — maybe you could provide some more context? If you want to, you may even send your query to me directly so you won’t have disclose it on this mailing list. Best from Konstanz Michael
Quoted text · Giuseppe G. A. Celano · 49 lines
> Am 12.03.2019 um 11:44 schrieb Giuseppe G. A. Celano <celano at informatik.uni-leipzig.de>: > > let $o := file:create-dir("/Users/mycomputer/prova") > let $o2 := file:write("/Users/mycomputer/prova/file1.xml", "ciao") > let $o3 := file:write("/Users/mycomputer/prova/file2.xml", file:read-text("/Users/mycomputer/prova/file1.xml")) > return > ($o, $o2, $o3) > > This actually works. In my real example the writing of $o2 requires e few seconds. It might be that $o3 is evaluated while $o2 is still running? > > Dr. Giuseppe G. A. Celano > DFG-project leader <http://gepris.dfg.de/gepris/projekt/408121292> > Universität Leipzig > Institute of Computer Science, NLP > Augustusplatz 10 > Tel: +4934132223 > 04109 Leipzig > Deutschland > > E-mail: celano at informatik.uni-leipzig.de <mailto:celano at informatik.uni-leipzig.de> > Web site 1: http://asv.informatik.uni-leipzig.de/en/staff/Giuseppe_Celano <http://asv.informatik.uni-leipzig.de/en/staff/Giuseppe_Celano> > Web site 2: https://sites.google.com/site/giuseppegacelano/ <https://sites.google.com/site/giuseppegacelano/> > >> On Mar 12, 2019, at 11:27 AM, Michael Seiferle <ms at basex.org <mailto:ms at basex.org>> wrote: >> >> Hi Guiseppe, >> >> The following pattern is supposed to / does work: >>> file:write("1.txt", "Written to 1.txt"), >>> file:write("2.txt", file:read-text("1.txt")), >>> "Read from 2.txt: " || file:read-text('2.txt') >> >> >> Could you maybe elaborate a bit more on your code? >> >> Best from Konstanz >> >> Michael >> >>> Am 12.03.2019 um 11:19 schrieb Giuseppe G. A. Celano <celano at informatik.uni-leipzig.de <mailto:celano at informatik.uni-leipzig.de>>: >>> >>> Hi >>> >>> I wrote a single script which should do: write a file -> open this file -> write another different file. I put the write expressions in the right sequence, but it seems that the second one cannot happen because the file created by the first write function has not yet been created at the time the second function is invoked. Does anyone have a suggestion about this? Thanks. >>> >>> Best, >>> Giuseppe >> >
CG
Christian Grünin reply to Giuseppe G. A. Celano, 12 Mar 2019, 12:0412 Mar 2019 12:08 #
As indicated, don’t bind expressions to variables if they yield empty sequences (here: $o, $o3), and it should be easier to reproduce what’s happening.
Quoted text · Giuseppe G. A. Celano · 69 lines
On Tue, Mar 12, 2019 at 12:04 PM Giuseppe G. A. Celano <celano at informatik.uni-leipzig.de> wrote: > > The code is long, because there are many functions. However, I did something like the following, and it works: > > let $o := file:create-dir("/Users/mycomputer/prova") > let $o3 := file:write("/Users/mycomputer/prova/file2.xml", > (file:write("/Users/mycomputer/prova/file1.xml", "ciao"), file:read-text("/Users/mycomputer/prova/file1.xml")) > ) > > return > ($o, $o3) > > The evaluation of the first write() is forced to happen and conclude before the second write() is evaluated. I am still experimenting on this, but would there be a way to force completion of evaluation without embedding? > > > > On Mar 12, 2019, at 11:44 AM, Giuseppe G. A. Celano <celano at informatik.uni-leipzig.de> wrote: > > let $o := file:create-dir("/Users/mycomputer/prova") > let $o2 := file:write("/Users/mycomputer/prova/file1.xml", "ciao") > let $o3 := file:write("/Users/mycomputer/prova/file2.xml", file:read-text("/Users/mycomputer/prova/file1.xml")) > return > ($o, $o2, $o3) > > This actually works. In my real example the writing of $o2 requires e few seconds. It might be that $o3 is evaluated while $o2 is still running? > > Dr. Giuseppe G. A. Celano > DFG-project leader > Universität Leipzig > Institute of Computer Science, NLP > Augustusplatz 10 > Tel: +4934132223 > 04109 Leipzig > Deutschland > > E-mail: celano at informatik.uni-leipzig.de > Web site 1: http://asv.informatik.uni-leipzig.de/en/staff/Giuseppe_Celano > Web site 2: https://sites.google.com/site/giuseppegacelano/ > > On Mar 12, 2019, at 11:27 AM, Michael Seiferle <ms at basex.org> wrote: > > Hi Guiseppe, > > The following pattern is supposed to / does work: > > file:write("1.txt", "Written to 1.txt"), > file:write("2.txt", file:read-text("1.txt")), > "Read from 2.txt: " || file:read-text('2.txt') > > > Could you maybe elaborate a bit more on your code? > > Best from Konstanz > > Michael > > Am 12.03.2019 um 11:19 schrieb Giuseppe G. A. Celano <celano at informatik.uni-leipzig.de>: > > Hi > > I wrote a single script which should do: write a file -> open this file -> write another different file. I put the write expressions in the right sequence, but it seems that the second one cannot happen because the file created by the first write function has not yet been created at the time the second function is invoked. Does anyone have a suggestion about this? Thanks. > > Best, > Giuseppe > > > >

⚡Generated with XQuery