Alloy - Generate .xml instance from .als

末鹿安然 提交于 2019-12-01 21:32:17

问题


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


回答1:


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

  1. Read the alloy model from its source file.

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

  2. Get the command to execute. for example :

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

  3. Execute the model using the command obtained in step 2

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

  4. Write the solution generated in step 3

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

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



来源:https://stackoverflow.com/questions/20567608/alloy-generate-xml-instance-from-als

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!