1

我正在使用一个适当的驾驶模拟器,它在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=""
                }
            }
        }
    }

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

我的第一个解决方案是基于 Python REGEX,如下所示(但对于我寻求更改的每个值):

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

有人建议我可以用 Parsimonious 之类的东西编写自定义语法,然后用 Mustache 重新生成输出。

from parsimonious.grammar import Grammar
grammar = Grammar(r"""
    any = task / data
    task = "TYPEOF " key " (" number ")" newline open_curly any+ close_curly
    data = key "=" quote text quote newline
    open_curly = "{" newline
    close_curly = "}" newline
    key = ~"[A-Z 0-9_]*"
    text = ~"[A-Z0-9 ]*"i
    number = ~"[0-9]*"
    newline = "\n"
    space = " "
    quote = "\""
""")

text = open('example_driving_rule.sci').read()
grammar.parse(text)
# Note doesn't work

如您所见,这也不是解决问题的有效方法。大家觉得有什么更好的解决方案?

4

1 回答 1

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")
于 2016-08-18T13:34:09.617 回答