问题
'd like to upgrade (if that's the word) a 100-150 of home-grown Netlogo 4.1.3 programs to Netlogo 6, preferably in batch by means of Perl or another scripting language, followed by a (necessary) manual inspection and finish.
To my dismay Netlogo 6 won't open Netlogo 4 files, so I've upgraded a few of them by opening them in Netlogo 5, save and reopen in Netlogo 6 and save. Not a particularly elegant way.
Any advice?.
回答1:
It looks like the reason Netlogo 6 won't read the 4.1.3 files is that it expects 12 sections, whereas the 4.1.3 files have 10 or 11. As far as I can tell, sections are broken up by the string "@#$#@#$#@". Additionally, older .nlogo files had parameters for a "CC-WINDOW" that version 6 does not understand. Finally, buttons in Netlogo 6 also need to be parameterized with a value of 1 or 0 to determine whether that button is disabled until ticks start or not.
The following python 3 code takes all Netlogo files in the same folder and cuts out the "CC-WINDOW" lines. It also adds a 1 to the end of every "Button" block. As the code reads the file, it counts the number of "@#$#@#$#@" breaks. If at the end of the file, there are fewer than 11, it appends enough "@#$#@#$#@" breaks to make the total 11.
If you want to run this code, I would copy the old files you want to update into a new folder. Place the .py file with the following code into that same folder, and when you run it it will create new files for the 6.0 compatible versions (Note that it will not only update the 4.1.3 files, but any netlogo files in that folder). This doesn't work for every file- for example, one file did not update correctly because the original model's "GRAPHICS-WINDOW" was not properly parameterized. That said, this code worked for the majority of the 4.1.3 model library models that I tested. Also, I only know that it allows you to open the files in Netlogo 6, I don't know what will have to be done after that to make sure that the models actually run as you would expect.
Hopefully that helps! Let me know if I was not clear on some point.
import os
with open("files_updated.txt", "w") as files:
for filename in os.listdir("."):
if filename.endswith(".nlogo") and not filename.startswith("6"):
files.write(filename + '\n')
opened = open(filename, "r")
n = 0
printat = -1
cut_count = 0
count_breakers = 0
new_file_name = ("6_"+filename.strip(".txt") + ".nlogo")
print(new_file_name)
with open(new_file_name, "w") as out:
for line in opened:
n += 1
if line == "@#$#@#$#@\n":
count_breakers += 1
if line == "CC-WINDOW\n":
cut_count = 8
cut_count -= 1
if cut_count < 0:
out.write(line)
if line == "BUTTON\n" :
printat = n + 14
if printat == n:
out.write("1\n")
if count_breakers < 11:
out.write("@#$#@#$#@\n" * (11 - count_breakers))
回答2:
Here is a simple conversion script. Feel free to suggest improvements.
来源:https://stackoverflow.com/questions/42442640/netlogo-transition-backwards-compatibility