Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Z3P可以将数据类型/枚举与字符串进行比较_Python_Enums_Z3_Z3py - Fatal编程技术网

Python Z3P可以将数据类型/枚举与字符串进行比较

Python Z3P可以将数据类型/枚举与字符串进行比较,python,enums,z3,z3py,Python,Enums,Z3,Z3py,按照(在此处找到:)中的示例,我可以将c与例如Color.green进行比较 Color = Datatype('Color') Color.declare('red') Color.declare('green') Color.declare('blue') Color = Color.create() # Let c be a constant of sort Color c = Const('c', Color) # Then, c must be red, green or blue

按照(在此处找到:)中的示例,我可以将
c
与例如
Color.green
进行比较

Color = Datatype('Color')
Color.declare('red')
Color.declare('green')
Color.declare('blue')
Color = Color.create()

# Let c be a constant of sort Color
c = Const('c', Color)
# Then, c must be red, green or blue
prove(Or(c == Color.green, 
         c == Color.blue,
         c == Color.red))
在我的应用程序中,我必须将
c
与python字符串进行比较: 我想要这样的东西:

c = Const('c', Color)
solve(c == "green") # this doesn't work, but it works with Color.green
该方法适用于
IntSort
(见下文),但不适用于我自己的数据类型

i = Int("i")
solve(i < 10)
i=Int(“i”)
求解(i<10)

Z3 python接口对字符串的重载非常有限。您可以将字符串文字用于“string”类型。否则,字符串不会被强制为其他类型。此外,使用字符串的方法也不适用于整数,例如

 I = Int("I") 
 solve(I < "10")

一个对我有效的解决方案(将数据类型/枚举与字符串进行比较)是在
z3.py
中向
类DatatypeSortRef(SortRef)
添加一个
cast
例程。 它将尝试找到与给定字符串匹配的构造函数并使用它,否则将继续使用现有行为(
super().cast(val)

以下是我使用的代码:

def cast(self, val):
    """This is so we can cast a string to a Z3 DatatypeRef. This is useful if we want to compare strings with a Datatype/Enum to a String.

    >>> Color = Datatype("Color")
    >>> Color.declare("red")
    >>> Color.declare("green")
    >>> Color.declare("blue")
    >>> Color = Color.create()

    >>> x = Const("x", Color)
    >>> solve(x != "red", x != "blue")
    [x = green]
    """
    if type(val) == str:
        for i in range(self.num_constructors()):
            if self.constructor(i).name() == val:
                return self.constructor(i)()
    return super().cast(val)

注意:我没有注意一般的正确性。这种方法对我来说很有效,但可能会导致代码出现问题。

我曾考虑过创建名称到值的全局映射(类似于您的“速记”),但我希望有某种“官方”方式。你认为通过一些合理的努力可以实现这样的事情吗?请参阅下面我的(潜在)解决方案。我没有找到任何要运行的python测试,所以我还不想创建一个pull请求@尼古拉·比约纳
def cast(self, val):
    """This is so we can cast a string to a Z3 DatatypeRef. This is useful if we want to compare strings with a Datatype/Enum to a String.

    >>> Color = Datatype("Color")
    >>> Color.declare("red")
    >>> Color.declare("green")
    >>> Color.declare("blue")
    >>> Color = Color.create()

    >>> x = Const("x", Color)
    >>> solve(x != "red", x != "blue")
    [x = green]
    """
    if type(val) == str:
        for i in range(self.num_constructors()):
            if self.constructor(i).name() == val:
                return self.constructor(i)()
    return super().cast(val)