Code Monkey home page Code Monkey logo

pyuppaal's Introduction

Introduction

Documentation Status PyPI version Licence

PyUPPAAL is a python package developed basically for reflecting UPPAAL's model editing, verification, and counter-example parsing operations into scripts. Implementing iterative model checking workflow is a typical application of pyuppaal, such as CEGAR, CEGIS, fault diagnosis, risk analysis, ect. We will add references and case studies for these problems. Some function have been implemented such as find_all_patterns(), fault_diagnosability(), fault_identification(), and fault_tolerance().

Notice:

  • report issues / requirements at: github-issues.
  • more demos for basic & advanced usage will come soon.
  • [todo] Support for SMC analyzing.

Demos are provided to help users get familiar with PyUPPAAL:

Quickstart

1. Installation

pip install pyuppaal

2. Before Coding

  1. Be sure to set the verifyta_path in your first line of code, which serves as model checking engine: Download UPPAAL4.x/5.x.
  2. You should activate UPPAAL, e.g., verify a model with UPPAAL GUI before use pyuppaal, to make sure that you have UPPAAL backend actiavted.

pyuppaal.set_verifyta_path("your/path/to//verifyta.exe")

3. Load, Edit, and Verify a Model

  1. Firstly we load the model demo.xml shown below.
  2. Then you can verify, and return the verify results as terminal outputs, or parsed SimTrace.
  3. In this demo, we just edit the queries of the .xml model, and we also provide a demo showing how to edit the template, locations, edges, etc.: Demo-Scripted Model Construction.

import pyuppaal
from pyuppaal import UModel

print(f"pyuppaal version: {pyuppaal.__version__}\n")
pyuppaal.set_verifyta_path(r"C:\Users\10262\Documents\GitHub\cav2024\bin\uppaal64-4.1.26\bin-Windows\verifyta.exe")

umodel = UModel('demo.xml') # load the model
umodel.queries = ['E<> P1.pass']

# verify and return the terminal result.
print(f"======== terminal res ========\n{umodel.verify()}")

# verify and return the parsed trace as simulation trace: SimTrace.
simulation_trace = umodel.easy_verify() 
print("======== parsed res ========") 
print(f"untime pattern: {simulation_trace.untime_pattern}")
print(f"full trace: {simulation_trace}")
pyuppaal version: 1.2.1

======== terminal res ========
Writing example trace to demo-1.xtr
Options for the verification:
  Generating shortest trace
  Search order is breadth first
  Using conservative space optimisation
  Seed is 1713425560
  State space representation uses minimal constraint systems
�[2K
Verifying formula 1 at /nta/queries/query[1]/formula
�[2K -- Formula is satisfied.

======== parsed res ========
untime pattern: ['a', 'b']
full trace: State [0]: ['P1.start']
global_variables [0]: None
Clock_constraints [0]: [t(0) - P1.t ≤ 0; P1.t - t(0) ≤ 10; ]
transitions [0]: a: P1 -> ; P1.start -> P1._id2;
-----------------------------------
State [1]: ['P1._id2']
global_variables [1]: None
Clock_constraints [1]: [t(0) - P1.t ≤ -10; ]
transitions [1]: b: P1 -> ; P1._id2 -> P1.pass;
-----------------------------------
State [2]: ['P1.pass']
global_variables [2]: None
Clock_constraints [2]: [t(0) - P1.t ≤ -10; ]

4. Find all patterns

Now we want find all possible patterns that leads to P1.pass. The red line is pattern1, and the green line is pattern2.

for i, st in enumerate(umodel.find_all_patterns()):
    print(f'pattern{i+1}: ', st.untime_pattern)
pattern1:  ['a', 'b']
pattern2:  ['c', 'd']

4. Verify with Multi-threads

import pyuppaal as pyu
import time
import multiprocessing.dummy as mp

print(pyu.__version__)
# set verifyta path
pyu.set_verifyta_path(r"C:\Users\10262\Documents\GitHub\cav2024\bin\uppaal64-4.1.26\bin-Windows\verifyta.exe")

model_path_list = ['demo.xml', 'demo_new.xml'] * 100
trace_path_list = ['demo_trace.xtr', 'demo_new_grace.xtr'] * 100
# for loop
t0 = time.time()
for model, trace in zip(model_path_list, trace_path_list):
    pyu.Verifyta().verify(model_path=model, trace_path=trace)
print(f'Verify with for loop, time usage {time.time() - t0}')

# multi-threads
t0 = time.time()
# pyu.Verifytaeasy_verify(model_path=model_path_list, trace_path=trace_path_list, num_threads=20)
p = mp.Pool()
p.starmap(pyu.Verifyta().verify, zip(model_path_list, trace_path_list))
print(f'Verify with multi-threads, time usage {time.time() - t0}')
1.2.1
Verify with for loop, time usage 9.384526014328003
Verify with multi-threads, time usage 1.61281418800354

5. Get Communication Graph

For models with multiple processes, you can use umod.get_communication_graph() method to visualize the sturcture of your UPPAAL model.

An example communication graph of a complex model in Demo_PipeNet is shown below:

6. Backup of old docs

Demos are provided to help users get familiar with PyUPPAAL (can not be rendered by github):

This demo demonstrates how to

  1. Load and verify a model.
  2. Model the input & observation sequence.
  3. Build communication graph.
  4. Find all patterns.
描述2

This demo constructs a model solely with PyUPPAAL APIs, including:

  1. Construct Template with Edge, Location.
  2. Set Declarations, Systems, Queries.
  3. Verify the constructed model.
描述3

This demo shows how to identify all event sequences that could result in a fault state, and see you can get ALL possible patterns only with PyUPPAAL find_all_patterns().

描述3

In this demo, you will learn how to model the input and observations events of a descrete event system (DES), and how to extract information from parsed counter example.

描述3

In this demo, you will analyze the identification and diagnosability of certain fault, wich advanced methods of pyuppaal.

pyuppaal's People

Contributors

ducky-duckd avatar hzyrc6011 avatar jack0chan avatar llleko avatar mousany avatar t1-edward avatar uniontake avatar wzqvip avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

pyuppaal's Issues

Type Error for python<=3.9

| operation is supported for python>=3.10, and we are fixing it by converting type a | type b into Union(type a, type b).
We will fix this bug in version 1.2.1.

In addition, the fault_diagnosability() function is of exponential complex, and we have optimized it for scalability. For a model with $10^8\sim 10^{10}$ states, before optimization: over 20h, after optimization: within 100s.

image

test_umodel.py疑似死循环

>> python .\test_umodel.py
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Writing example trace to C:\Users\ccc\Documents\GitHub\pyuppaal\src\tests\pedestrian_pattern-1.xtr
Traceback (most recent call last):

NTA中构建edge的时候不应该要求location位置

目前创建两个Location之间的edge的时候传入参数为两个Location的id和position,这里position有点多余,希望可以根据location的id自动获取position,传入location的id或者location结构。

utab效能更新

问题最新进度

  • [需求改进] 正在讨论将tracer_custom打包成.dll供python直接调用。

  • [api]

def utap_parser(if_str: str, xtr_file: str) -> str:
    """接口来展示utap_parser的用法

    Args:
        if_str (str): uppaal编译出来的if文件的内容
        xtr_file (str): `.xtr`路径文件

    Returns:
        str: 返回解析好的raw trace
    """
    raise NotImplemented

需求描述

我们希望去掉解析tracer时的.if中间文件。

使用tracer_custom需要下面步骤

  1. 设置环境变量
    • $env:UPPAAL_COMPILE_ONLY=1$PSDefaultParameterValues['Out-File:Encoding'] = 'Default'
  2. 使用verifyta产生.if文件
    • ./verifyta AVNRT_Fake_GroundTruth.xml > AVNRT_Fake_GroundTruth.if
  3. 使用tracer_custom解析trace,并将结果返回到命令行
    • ./程序.exe AVNRT_Fake_GroundTruth.if trace_AVNRT-1.xtr

在第2步中,我们注意到>将命令行的输出重定向到.if文件,这个文件我们完全不需要。
能否在使用时向下面一样直接将字符串给到trace_custom:

# 获取cmd运行结果
if_str = cmd('./verifyta AVNRT_Fake_GroundTruth.xml')
if is_valid(if_str):
    raw_strace_str = cmd(f'./程序.exe {if_str} trace_AVNRT-1.xtr')

更新后样子

原来的功能保留,添加新的参数

trace_custom a.if b.xtr
trace_custom a.if b.xtr c.txt
// 一个-还是两个--?
trace_custom b.xtr -if_str xxxxxxxx

Support for Linux, MacOS

Currently we found pyuppaal cannot work on Linux, as this tool is developed in Windows. We are not trying our best to make it available on Linux and MacOS.

get_communication_graph效能升级

希望get_communication_graph功能能够导出.svg.png等格式的图片。

参考方案

  1. 使用mermaid引擎导出来(优先推荐)
  2. 使用在线api(类似爬虫),找在线的编译网站。这个链接的难点在于pako如何获取reference-link

更新SimTrace功能的描述

印象中对于SimTrace的过滤功能好像没有案例或者描述之类的,看看能不能补上。下面是需要考虑的点。

FilterByAction

You can filter specific edges with specific actions.

FilterByClock

You can filter constraints with specific clocks (e.g. gclk).

Constraints with certain clocks:
t1
t1 or t2 or t3
t1 and t2
t1 or (t3 and t4)
'or' means exist one in DBM form.
'and' means both in DBM form.
For example:
DBM = 
 [(t1, t2, True, 100), 
   (t1, t3, True, 500), 
   (t2, t4, True, 200),
   (t3, t4, True, 300)]
t1 will filter: 
 [(t1, t2, True, 100), 
   (t1, t3, True, 500), 
   (t2, t4, True, 200),
   (t3, t4, True, 300)]
t1 or t2 or t3 will filter:
 [(t1, t2, True, 100), 
 (t1, t3, True, 500), 
 (t2, t4, True, 200),
 (t3, t4, True, 300)]
t1 and t2 will filter:
 [(t1, t2, True, 100), 
   (t1, t3, True, 500), 
   (t2, t4, True, 200),
   (t3, t4, True, 300)]
t1 or (t3 and t4) will filter:
 [(t1, t2, True, 100), 
   (t1, t3, True, 500), 
   (t2, t4, True, 200),
   (t3, t4, True, 300)]
(t1 and t2 and t3) or t4
S = [t1, t2, t3], [t1, t2, t3, *]

(t1 and t2) or (t1 and t3) or (t2 and t3)

find (exp1, exp2) \in S*S
exp1 * exp2 \subset S*S

Action Pattern (Untime)

In most conditions, experts are concerned more about the sequence of the events than the exact clock constraints. Pattern is the sequence of focused actions without clock constraints. Due to the partial observations, there are different patterns that can explain the current features. In the pipes examples, there are two different paths that can lead to exit2.
Pattern = Untime(Trace)
All patterns.

utap无法在非Windows环境import

涉及branch

api_adjust

问题描述

MacOS下无法通过pytest,报错内容如下图,关键信息:

No module named `pyuppaal.utap.utap_39`.

image

我们的检查

  1. 换另一台Windows电脑运行pytest,通过测试;
  2. 检查MacOS的python的具体版本,和Windows一致,均为3.9.12

需求

在至少Ubuntu下通过pytest。

No err raised when verifyta raises an error.

#17 In this issue, we ignored std_err from the terminal while using verifyta. However, we fix this by ignoring all std_err, which causes additional problems like :

"if an .xml model is not valid, verifyta raises error, but pyuppaal cannot catch it."

A potential method is to check whether "counter example" is in the std_err.

verifyta_res = xxx
if "counter example" in verifyta_res:
    regard it as normal message.
if not:
    raise this error.

为什么SimTrace里的self.__parse_raw()不能删掉?

涉及branch

api_adjust

问题描述

如图,self.__parse_raw()出于性能考虑改成了默认在构建时不parse,需要parse的时候自动判断是否需要parse(self.__has_parse_raw)。现在想要把这一行删除,但是删掉就跑不过pytest。

image

需求描述

  1. 找到SimTrace.__init__()中最后一行self.__parse_raw()不能删除的原因;
  2. 尝试删除self.__parse_raw(),并通过pytest,以实现提高性能的目的。

其他说明

pytest使用方法

pip install pytest
cd path_to_pyuppaal_tests
pytest

pytest会自动collect我们写的test并且运行。

get_communication_graph合并同类项

需求描述

  • 合并source与target相同的edges。
  • 把行人过马路的例子写在代码注释里。
  • 增加filter功能,能够删除不希望看到的process或者actions

局部channel不能对应到全局channel

问题描述

在声明Process的时候需要传入参数,构建模型时是形参(局部参数)

但是如果我们想用自己声明的全局参数就会出现问题

问题表现为代码提取到的是形式参数,而我们需要的则是实际参数,需要从UPPAAL模型里找到对应的关系。

说明

暂时不是要紧的问题,先推进其他的进度。

pyuppaal接口升级

  • pyuppaal.load(model_path: str) -> UModel
  • Verifyta的接口全部暴露给pyuppaal,比如
    • pyuppaal.set_verifyta_path()
    • pyuppaal.simple_verify()
    • pyuppaal.cmds()等
    • pyuppaal.get_communication_graph()
  • 升级后的文档结构

Readme typo

Last paragraph in Introduction, "pauppaal" may be "pyuppaal".

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.