colinsheppard / time

A NetLogo extension that brings date/time utilities and discrete event scheduling to NetLogo
12 stars 13 forks source link

time:ts-load doesn't close file after execution? #36

Closed dk-forestry closed 9 years ago

dk-forestry commented 9 years ago

Hi Colin,

Thanks for fixing the issues I've submitted in such a fast way! :)

I just ran into a strange problem, and I'm not sure if this is an issue with the time extension or rather with my lack of NetLogo experience. I created a minimal working example of the problem (it maybe doesn't sound completely logical what I want to do, but in our model, it makes sense):

extensions [time]

globals [ ts ]

to setup clear-all

if (file-exists? "test.csv") [file-delete "test.csv"] file-open "test.csv" file-print "date,value1" file-print "2020-01-01,5" file-print "2020-01-02,10" file-close

file-open "test.csv" print file-read-line file-close

;set ts time:ts-load "test.csv" ;print ts end

Like this, it works as intended: test.csv is created and can then be opened. Pressing setup multiple times repeats this procedure without error.

Now, when I uncomment the last 2 lines, something strange happens: The first time I press "setup", the code works. The second time I press setup, there's an error from "if (file-exists? "test.csv") [file-delete "test.csv"]", saying "deletion failed". When I try to delete the file with Windows Explorer, I get the error "The action can't be completed because the file is open in NetLogo5.1.0.exe".

After closing and opening NetLogo or pressing "halt" on the GUI, I can again press setup once before I get the error.

Is it possible that "time:ts-load" doesn't properly close the connection to the file after the import is finished?

dk-forestry commented 9 years ago

Hi,

At the moment, I'm using a quick (and very dirty!) workaround to the problem:

extensions [time]

globals [ ts ]

to setup clear-all

; find 'deletable' file name: let i 1 let filename-temp "" while [i != 0] [ set filename-temp (word "temp/test_" i ".csv") ifelse (file-exists? filename-temp) [carefully [file-delete filename-temp file-open filename-temp set i 0] [set i (i + 1) print i] ] [ file-open filename-temp set i 0 ] ]

file-print "date,value1" file-print "2020-01-01,5" file-print "2020-01-02,10" file-close

file-open filename-temp ; print file-read-line file-close

set ts time:ts-load filename-temp ; print ts file-close

end

I had a look at your source code. Could the fix be as easy as inserting those lines at line 290 in "public void parseTimeSeriesFile"?

} finally { try { br.close(); } catch (IOException e) { throw new ExtensionException(e.getMessage()); } }

colinsheppard commented 9 years ago

Try the latest release (Version 1.2.1) and see if that helps. I couldn't reproduce the problem (on OS X) so I'm not sure.

Thanks again.

dk-forestry commented 9 years ago

Yes, Version 1.2.1 fixed the issue. My working example now runs without errors on Windows. Thanks!

colinsheppard commented 9 years ago

Great.