合金规格问题

时间:2013-01-07 14:15:27

标签: alloy model-checking

以下是我最近为手机发短信完成的马马虎虎合金规格。这只是基本的短信要求,因为这是一种做法,我没有严格的要求维护。但是,我有一些尴尬的问题,为什么我不能得到超过1对Name-Mobile对?为什么2个MessageSet始终指向一个名称?除问题外,事实和谓词都很直接。请尽量批评,因为我需要反馈来学习合金本身。

在进行以下操作时我的想法是什么;

一个消息框有0个或更多MessageSet。一套只属于一个人,没有一套是免费的。每组有一条或多条消息,包括消息行,开始和结束键以及行和光标位置。多条消息可以属于同一个人,但同时没有消息可以属于2个人。每行有一个或多个键,并有自己的开始和结束键。一条线可能有也可能没有新线。每个密钥可能有也可能没有下一个密钥。按键通过触摸板按下。每个名称都有一个手机号码,它们记录在ContactList中。没有2个名字可以拥有相同的手机,但一个人可以有多个电话号码。

感谢。

    sig Lines{
formedOfKeys:some Keys,
lineStartKey:one Keys,
lineEndKey:one Keys,
nextLine: lone  Lines,
    }

one sig TouchPad{  pressed: set Keys }

sig Keys{ nextKey: one Keys, }

one sig MessageBox{}

    sig MessageSet{
isIn:one MessageBox,
isSetOf: one Name
    }

     sig Messages{
belongsToSet:  one MessageSet,
firstLine: one Lines,
lastLine:one Lines,
lastKey:one Keys,
firstKey: one Keys,
curserLoc: one Keys,
formedOfLines: set Lines,
sentFrom : one Name
   }

sig Name,Mobile {}

    one sig ContactList {
contact: Name, 
mobile: contact -> one Mobile
}

    fact No2NamesBelongingToMessageSetsSame{
//  all ms1,ms2:MessageSet| ms2 = ms1 implies ms1.isSetOf != ms2.isSetOf
    }

    //make it more than 2
fact NameAndMobileNumbersMatchedAndEqual{
//  #Name>2 and #Mobile>2
#Name=#Mobile
    }

    fact MessageSetBelongsToName{
all  ms:MessageSet, m:Messages |ms.isSetOf=m.sentFrom
    }

    fact AllNamesInContactList{
all c:ContactList| #c.contact =#Name
}

    fact NoLastMessageLineWithNextLine{
no l:Lines | l.nextLine not in (Messages.lastLine +Messages.firstLine)
    }

    fact NoNextKeyisSelf{
all k: Keys| k !in k.nextKey
    }

    fact NoNextLineisSelf{
all l: Lines | l !in l.nextLine
    }
    fact No2WayConnectionsBetweenLines{
all disj l1, l2: Lines | l2 in l1.nextLine implies l1 !in l2.nextLine
    }

    fact No2WayConnectionsBetweenKeys{
all disj k1, k2: Keys| k2 in k1.nextKey implies k1 !in k2.nextKey
    }

    fact MessageHasMoreThan1LineHasNextLine{
all m:Messages|#m.formedOfLines>1 implies #m.formedOfLines.nextLine>0
    }

fact LineStartInMessageLines{ all l:Lines| l.lineStartKey in l.formedOfKeys }
fact LineEndInMessageLines{ all l:Lines| l.lineEndKey in l.formedOfKeys }
fact CurserLocInMessageKeys{ all m:Messages| m.curserLoc in m.formedOfLines.formedOfKeys  }
fact FirstKeyInMessageKeys{ all m:Messages| m.firstKey in m.formedOfLines.formedOfKeys  }
fact firstLineInMessageLines{ all m:Messages| m.firstLine in m.formedOfLines }
fact MessageNextLineInMessageLines{ all m:Messages| m.formedOfLines.nextLine in m.formedOfLines }

fact MessageFirstKeyisInLineStartKeySet{ all m:Messages , l:Lines | m.firstKey in l.lineStartKey }

fact MessageLastKeyisInLineEndKeySet{ all m:Messages , l:Lines | l in m.lastLine && l.lineEndKey in m.lastLine.lineEndKey }

fact allKeysArePressed { all k:Keys | k in TouchPad.pressed }

fact NoMessageSetsWithoutMessages{ no ms:MessageSet| ms not in Messages.belongsToSet }

fact NoMessageSetWithoutBox{ no mb:MessageBox| mb not in MessageSet.isIn }

    pred nonReturnKeyPress[k: Keys, l,l':Lines, t:TouchPad]{
//key pressed in message, cursor and line info (new state =original state+1letter)
 l'.formedOfKeys= l.formedOfKeys++(t.pressed)
}

    pred deleteKeyPressed[k: Keys, l,l':Lines, t:TouchPad]{
//key deleted from line. cursor and line info (new state =original state-1key)
l'.formedOfKeys = l.formedOfKeys - (t.pressed)
}

    pred returnKeyPressed[k: Keys, l,l':Lines, t:TouchPad]{
//TODO return key pressed. cursor loc is new line's cursor loc
 l'.formedOfKeys= l.nextLine.formedOfKeys
}
    assert keyPressWorks {
    //TODO check if key press works properly
    }
    pred pressThenDeleteSame {
    all l1,l2,l3: Lines,  t:TouchPad, k: Keys|
    nonReturnKeyPress [k,l1,l2,t] && deleteKeyPressed [k,l2,l3,t]
        => l1.formedOfKeys = l3.formedOfKeys
}

    pred ReceiveThenSendSame {
all ms1,ms2,ms3, m: Messages|
    receive [m,ms1,ms2] && send [m,ms2,ms3]
        => ms1 = ms3
}

    pred findCursorLoc [l:Lines, k:lone Keys]{
//TODO I have a line and a key...How can i check where i am
}

    pred send[m, mS,mS':Messages]{
//final message set is one less than previous
mS'= mS - (m)
}

    pred receive[m, mS,mS':Messages]{
//final message set is one more than previous
mS'= mS++ (m)
}

pred AddContact [cl, cl': ContactList, n: Name, m: Mobile] {
   cl'.mobile = cl.mobile ++ (n->m)
    }

    pred RemoveContact [cl, cl': ContactList, n: Name] {
cl'.mobile = cl.mobile - (n->Mobile)
}

    pred FindContact [cl: ContactList, n: Name, m: Mobile] {
m = cl.mobile[n]
}

    assert AddingContact {
all cl, cl': ContactList, n: Name, m,m': Mobile |
    AddContact [cl,cl',n,m] && FindContact [cl',n,m'] => m = m'
}

    assert AddingThenDeleteSame {
all cl1,cl2,cl3: ContactList, n: Name, m: Mobile|
    AddContact [cl1,cl2,n,m] && RemoveContact [cl2,cl3,n]=> cl1.mobile = cl3.mobile
}

    assert pressThenDeleteSame {
all l1,l2,l3: Lines,  t:TouchPad, k: Keys|
    nonReturnKeyPress [k,l1,l2,t] && deleteKeyPressed [k,l2,l3,t]=> l1.formedOfKeys = l3.formedOfKeys
}

   assert ReceiveThenSendSame {
all ms1,ms2,ms3 , m: Messages|
    receive [m,ms1,ms2] && send [m,ms2,ms3]=> ms1 = ms3
}

//check AddingContact  expect 0
//check AddingThenDeleteSame  expect 1
//check pressThenDeleteSame  expect 0
//check ReceiveThenSendSame  expect 0

pred show{ }

run show 

//for exactly 3 Name,3 Mobile, 4 MessageSet,6 Messages, 5 Lines, 6 Keys  
//for exactly 4 MessageSet,14 Messages, 20 Lines, 40 Keys

1 个答案:

答案 0 :(得分:2)

  

为什么我不能获得超过1对Name-Mobile对?

要调试这样的问题,可以使用“不满核心”功能。首先,我将#Name > 1添加到show谓词,然后从“选项”菜单中选择了“带有Unsat Core的MiniSat”解算器,最后执行“show”谓词。该模型是不可满足的(如预期的那样),因此不是一个实例,而是返回一个不满核心,即一组相互不一致的最小公式。换句话说,不饱和核心为您提供了模型不可满足的原因,而且,如果您删除不饱和核心中的任何一个公式,模型应该变得令人满意。

在您的具体示例中,不饱和核心包含以下行:

  contact: Name // field declaration inside the ContactList sig

  all c:ContactList| #c.contact =#Name

  #Name > 1

这应该立即告诉你为什么模型不可满足:contact字段被声明为恰好指向一个Name;强制该字段基数等于Name原子数的下一个约束意味着只能有1 Name个原子,所以当你明确要求更多Name个原子时( #Name > 1)你变得矛盾了。

要解决此问题,您可以将contact字段声明更改为contact: set Name

  

为什么2个MessageSets会一直指向一个名称?

为了对此进行测试,我将以下约束添加到show谓词(在修复contact声明之后,如上所述)

some disj ms1, ms2: MessageSet | ms1.isSetOf != ms2.isSetOf

确实,该模型不可满足。核心包含以下几行

  all  ms:MessageSet, m:Messages | ms.isSetOf = m.sentFrom

  no ms:MessageSet | ms not in Messages.belongsToSet 

  some disj ms1, ms2: MessageSet | ms1.isSetOf != ms2.isSetOf

同样,很明显问题是什么。您希望并非所有MessageSet都指向相同的Name(第3行),但在您的模型中,您有一个事实(第一行)恰好相反,即所有{{1 }和ms: MessageSetm: Messages的值必须相同且等于ms.isSetOf。满足这些约束的唯一方法是使0 m.sentFrom(因此通用量词很简单),但在这种情况下,不满足来自不满核心的第2行。