在自定义语法中修改变量的最有效方法是什么?

时间:2016-08-18 01:15:22

标签: python grammar scilab parsimonious

我正在使用一个适当的驾驶模拟器,它可以在Scilab的自定义版本中生成“方案”文件。我提供了一个11,000行长的“主”文件,从中我需要替换某些值来生成场景的 n 版本。

单个父TASK的语法的最小示例是这样的:

TYPEOF TASK (57)
{
    LABEL="Dot 3a"/*replace with name for name in list */
    TASK_KIND="0"

    TYPEOF VARIABLE (53)
    {
        LABEL="Time1"
        TYPE="FLOAT"
        VALUE="14.000000" /* replace with random.integer() */
        INTERACTIVE="VOID"

    TYPEOF VARIABLE (54)
    {
        LABEL="X_pos1"
        TYPE="FLOAT"
        VALUE="23.600000" 
        INTERACTIVE="VOID"


    TYPEOF TASK (58)
    {
        LABEL="Task: ISI"
        TASK_KIND="0"

        TYPEOF RULE (115)
        {
            LABEL="Rule: Go to subtask after Time1 seconds"

            TYPEOF CONDITION (SUPERIOR)
            {
                IS_EXPANDED="1"
                MODIFIER="BECOMES_TRUE"

                TYPEOF PARAMETER (OPERAND_1)
                {
                    KIND="FUNCTION"

                    TYPEOF FUNCTION (GET_TASK_CLOCK)
                    {
                    }
                    OWNER_FILE=""
                }

                TYPEOF PARAMETER (OPERAND_2)
                {
                    KIND="VARIABLE"
                    VALUE="53"
                    OWNER_FILE=""
                }
            }

            TYPEOF ACTION (GOTO_TASK)
            {
                IS_EXPANDED="1"

                TYPEOF PARAMETER (TASK_NUMBER)
                {
                    KIND="ENUM"
                    VALUE="GOTO_NEXT_TASK"
                    OWNER_FILE=""
                }
            }
        }
    }

我需要用标准输入替换此脚本中的某些值。例如,有一个名称列表,它将替换父级别LABELTASK的值;并且必须使用6到16之间的随机数替换第一个父VALUE的{​​{1}}。

我的第一个解决方案是基于Python REGEX,类似于follow(但是我想要改变的每个值):

VARIABLE

有人告诉我,我可以用Parsimonious写一个自定义语法,然后用Mustache重新生成输出。

for row in scenarioInput:
    parenttaskmatch = re.search("^\t\tTYPEOF TASK",row)
    if parenttaskmatch:
        replacementrow = re.sub(r"([0-9]{1,3})",repl,row)

正如您所看到的,这也不是解决问题的有效方法。你认为什么是更好的解决方案?

1 个答案:

答案 0 :(得分:1)

您可以将文件转换为Scilab脚本,生成具有新值的文件。

转型非常简单 首先是Scilab(要做一次)

 T=mgetl("Task_file");mputl(sci2exp(T),"Task_file.sce")

对于每个实验,使用文本编辑器修改生成的脚本以将预期值替换为默认值(可以通过从文件中读取这些值,或者......)

参见下面的示例,time1值由grand生成,X_pos1从Scilab控制台读取

 T=["TYPEOF TASK (57)"
 "{"
 "    LABEL=""Dot 3a""/*replace with name for name in list */"
 "    TASK_KIND=""0"""
 ""
 "    TYPEOF VARIABLE (53)"
 "    {"
 "        LABEL=""Time1"""
 "        TYPE=""FLOAT"""
 "        VALUE="""+string(grand(1,1,"uin",6,16)+"""" /* replace with random.integer() */"
 "        INTERACTIVE=""VOID"""
 ""
 "    TYPEOF VARIABLE (54)"
 "    {"
 "        LABEL=""X_pos1"""
 "        TYPE=""FLOAT"""
 "        VALUE=""""+string(input("X_pos1")+""""
 "        INTERACTIVE=""VOID"""
 ""
 ""
 "    TYPEOF TASK (58)"
 "    {"
 "        LABEL=""Task: ISI"""
 "        TASK_KIND=""0"""
 ""
 "        TYPEOF RULE (115)"
 "        {"
 "            LABEL=""Rule: Go to subtask after Time1 seconds"""
 ""
 "            TYPEOF CONDITION (SUPERIOR)"
 "            {"
 "                IS_EXPANDED=""1"""
 "                MODIFIER=""BECOMES_TRUE"""
 ""
 "                TYPEOF PARAMETER (OPERAND_1)"
 "                {"
 "                    KIND=""FUNCTION"""
 ""
 "                    TYPEOF FUNCTION (GET_TASK_CLOCK)"
 "                    {"
 "                    }"
 "                    OWNER_FILE="""""
 "                }"
 ""
 "                TYPEOF PARAMETER (OPERAND_2)"
 "                {"
 "                    KIND=""VARIABLE"""
 "                    VALUE=""53"""
 "                    OWNER_FILE="""""
 "                }"
 "            }"
 ""
 "            TYPEOF ACTION (GOTO_TASK)"
 "            {"
 "                IS_EXPANDED=""1"""
 ""
 "                TYPEOF PARAMETER (TASK_NUMBER)"
 "                {"
 "                    KIND=""ENUM"""
 "                    VALUE=""GOTO_NEXT_TASK"""
 "                    OWNER_FILE="""""
 "                }"
 "            }"
 "        }"
 "    }"];
 muptl(T,"Task")