Netlogo过渡,向后兼容 [英] Netlogo transition, backwards compatibility

查看:82
本文介绍了Netlogo过渡,向后兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想要将100-150个本地的Netlogo 4.1.3程序(如果是这样的话)升级到Netlogo 6,最好通过Perl或其他脚本语言分批升级,然后再加上(必要的)手册检查并完成.

'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.

令我沮丧的是,Netlogo 6无法打开Netlogo 4文件,因此我已经通过在Netlogo 5中打开它们来升级了其中的一些文件,保存并在Netlogo 6中重新打开并保存.这不是一种特别优雅的方式.

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.

有什么建议吗?

推荐答案

看起来Netlogo 6不会读取4.1.3文件的原因是它希望有12个部分,而4.1.3文件有10或11个部分据我所知,部分由字符串"@#$#@ #### @"分解.此外,较早的.nlogo文件具有版本6无法识别的"CC-WINDOW"的参数.最后,还需要将Netlogo 6中的按钮参数化为1或0,以确定在刻度线开始之前是否禁用该按钮.

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.

以下python 3代码将所有 Netlogo文件放在同一文件夹中,并切出"CC-WINDOW"行.它还在每个按钮"块的末尾添加1.当代码读取文件时,它会计算"@#$#@ #### @"中断的数量.如果在文件末尾少于11个,它将附加足够的"@#$#@ #### @"中断以使总数为11.

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.

如果要运行此代码,我会将要更新的旧文件复制到新文件夹中.将具有以下代码的.py文件放入同一文件夹中,并在运行该文件时将为6.0兼容版本创建新文件(请注意,它不会更新4.1". 3个文件,但该文件夹中的任何个netlogo文件).这不适用于每个文件-例如,一个文件未正确更新,因为未正确参数化原始模型的"GRAPHICS-WINDOW".也就是说,此代码适用于我测试的大多数4.1.3模型库模型.另外,我只知道它允许您在Netlogo 6中打开文件,但我不知道在那之后必须执行什么操作才能确保模型按预期运行.

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))

这篇关于Netlogo过渡,向后兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆