简体   繁体   English

使用util / ordering在Alloy中订购

[英]Ordering in Alloy using util/ordering

I am trying to learn about how ordering works in Alloy. 我正在尝试了解Alloy中的订购方式。 I have a time signature which I have used to instantiate the ordering module. 我有一个用于实例化订购模块的时间签名。 I want the predicate addPage to add a page to the book at time t' where t' = t.next. 我希望谓词addPage在时间t'(t'= t.next)添加页面到书中。 (Basically add a page to the Book on the next time) However it is not working as expected and instead Time2 has lesser number of pages than Time1. (基本上是在下一次将页面添加到Book中)。但是它没有按预期运行,因此Time2的页面数少于Time1的页面数。 Can someone explain to me why this is happening? 有人可以向我解释为什么会这样吗? Thanks. 谢谢。

open util/ordering[Page] as P0
open util/ordering[Time] as T0


sig Page {}


sig Time {}


sig Book
{   
  pages: Page -> Time
}


pred addPage(b:Book, p:Page, t: Time)
{
     t != T0/last implies
   {
       let t' = t.next |
           b.pages.t' = b.pages.t + p
   }
}


run addPage {} for 3

在此处输入图片说明

The problem are the extra curly braces in the run statement. 问题是run语句中多余的花括号。 I think Alloy executes an empty predicate in this case. 我认为在这种情况下Alloy会执行一个空谓词。

Try: 尝试:

run addPage for 3

instead. 代替。 You will see a visualization where the selected instances for b, t and p are marked. 您将看到一个可视化效果,其中标记了b,t和p的所选实例。

You're trying to change state which can only be simulated in constraint logic. 您正在尝试更改只能在约束逻辑中模拟的状态。 Please notice that the expression in addPage is basically ineffective /run your model without it/ and that there's only one Book atom in the solution. 请注意,addPage中的表达式基本上是无效的,如果没有它,则运行您的模型/并且解决方案中只有一个Book原子。

Here's a model you can start with and gradually refine. 这是您可以开始并逐步完善的模型。

open util/ordering[Time]

sig Page {}
sig Time {}

sig Book {   
  pages : Page lone -> Time // each Time atom is mapped to at most one Page atom
}

pred addPage(b0, b1 : Book, pg : Page, t0, t1 : Time) {
    one pg                          and // one page at a time (it's likely redundant)

    not pg in b0.pages.Time         and // it's a 'new' page
    b0.pages + pg->t1 = b1.pages    and // 'new state' of b0
    t1 = t0.next                        // pg is 'added' with the next time stamp
}

run addPage for 3 but 2 Book

I used the optional 'and' operators, placed t1 = t0.next at the end of the constraint, positioned b1.pages /representing the 'new state'/ on the right and used quotes in the comments to emphasize that there's no real state change and sequence of operation in the sense imperative programming works. 我使用了可选的“和”运算符,将t1 = t0.next置于约束的末尾,将b1.pages /代表“新状态” /置于右侧,并在注释中使用了引号,以强调没有真实状态从某种意义上说,更改和操作顺序必须执行编程工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM