Alloy - 从.als生成.xml实例 [英] Alloy - Generate .xml instance from .als

查看:255
本文介绍了Alloy - 从.als生成.xml实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在程序中从.als生成随机的.xml实例。我设法通过在后台运行合金(不可见的JFrame)并调用doOpen,doExecuteLatest和doShowLatest函数来做到这一点。但每次运行我的代码时都要等合金开始是一件痛苦的事。我认为如果我只使用合成代码部分执行此程序(我想这将是kodkod)会更有效率。
有谁知道怎么做?我发现合金的代码非常令人困惑......

I need to generate random .xml instances from an .als in my program. I managed to do that by running alloy in background (invisible JFrame) and calling the doOpen, doExecuteLatest and doShowLatest functions. But having to wait alloy to start every time I run my code is a pain. I think it would be more efficient if I simply used the alloy code section that does this procedure (I imagine that would be kodkod). Does anyone know how to do that? I found alloy's code to be pretty confusing...

推荐答案

您可以使用Alloy API。
生成实例并将其写为XML文件可以按照以下步骤轻松完成:

You can use the Alloy API. Generating an instance and writing it as an XML file can be easily done following those steps:


  1. 读取合金来自其源文件的模型。

  1. Read the alloy model from its source file.


model = CompUtil.parseEverything_fromFile(null,null,
yourmodel.als);

model = CompUtil.parseEverything_fromFile(null, null, "yourmodel.als");


  • 获取执行命令。例如:

  • Get the command to execute. for example :


    命令cmd = model.getAllCommands()。get(0);

    Command cmd=model.getAllCommands().get(0);

    使用步骤2中获得的命令执行模型

  • Execute the model using the command obtained in step 2


    A4Solution solution = TranslateAlloyToKodkod.execute_command(null,
    model.getAllReachableSigs(),cmd,new A4Options());

    A4Solution solution= TranslateAlloyToKodkod.execute_command(null, model.getAllReachableSigs(), cmd, new A4Options());


  • 写下在步骤3中生成的解决方案

  • Write the solution generated in step 3




    solution.writeXML(path / to / your.xml);

    solution.writeXML("path/to/your.xml");

    可以在Alloy jar文件的edu.mit.csail.sdg.alloy4whole包中找到示例

    Examples can be found in the edu.mit.csail.sdg.alloy4whole package of the Alloy jar file

    这篇关于Alloy - 从.als生成.xml实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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