生成合金中的固定值列表

时间:2013-04-24 13:45:30

标签: list alloy

我正在尝试创建一个表示值的硬币列表。对应的9 rs将包含序列5-> 2-> 2。 8rs将具有5rs-> 2rs-> 1rs。 7rs将具有5> 2。硬币可以是10Rs,5rs,2rs,1rs类型。在此代码中,tenr表示值为10的硬币列表,而tenrs表示10 rs的硬币。我该如何创建这样的列表?我一直在努力,但一切都是徒劳的。此外,当我执行并单击显示时,它显示分配给单个硬币节点的多个硬币。任何帮助将不胜感激。其实我想在自动售货机上使用它。我检查了你的代码,我正在尝试实现它。我也提供自动售货机的代码。

sig coin{}

sig oners,twors,fivers,tenrs,null1 in coin {}

sig list
{

next :  coin lone -> lone coin
}


sig oner,twor,threer,fourr,sixr,sevenr,eightr,niner,fiver,tenr,nullr in list {}

fact {
    //  all c:choco | all c1:choco -c | c1.choice != c.choice
    //  all m: machine| some n:m.null1 | some ors: m.oners| ors->n in (m.oner).next
    all  l:list| all c:coin| c->c not  in l.next
    all  l:list|no c : coin | (c in c.^(l.next ))
    //all m: machine| all o:m.oner | oners->null1 in o
   all o:oner| all n: null1|all l:list|oners->null1 in o.next and #n.(l.next)=0
            all n:nullr| all n1:null1| #n1.(n.next)= 1  and #(n.next).n1=0 and n1->null1 in n.next
    all t:tenr| all tr:tenrs | #tr.(t.next)= 1 and #(t.next).tr=0 and tr->null1 in t.next
all t:twor| all tr:twors | #tr.(t.next)= 1 and #(t.next).tr=0 and tr->null1 in t.next
}

自动售货机

sig button {}

sig coin{}

sig choco{
    value:one coin,
    choice :one button
            }
fact {
        all c:choco | all c1:choco -c | c1.choice != c.choice
        }
sig machine{
    a,b,c,d : one button,
    oners,twors,fivers ,tenrs,null1: set coin,
    cadbury, kitkat, eclairs , gum,null: lone choco
    }

{
  #(fivers+tenrs+null+twors+oners) = 5 
  #(a+b+c+d) = 4
    #   (cadbury+kitkat+ eclairs +gum) =4
    cadbury=choice.a
    cadbury= value.tenrs
        kitkat=choice.b
        kitkat=value.fivers
        eclairs=choice.c
        eclairs=value.oners
        gum=choice.d
            gum=value.twors
}

pred show{}

pred chocolate [before,after:machine , x: coin, op:choco, opc: coin, ip:button]
    {
    before.a=after.a
    before.b=after.b
    before.c=after.c
    before.d=after.d
    before.cadbury=after.cadbury
    before.kitkat=after.kitkat
    before.eclairs=after.eclairs
    before.gum=after.gum
    before.null1=after.null1
    before.oners=after.oners
    before.twors=after.twors
    before.fivers=after.fivers
    before.tenrs=after.tenrs
        before.null=after.null
    x = before.oners or x= before.twors or x= before.fivers or x= before.tenrs
    ip = before.a or ip = before.b or ip=before.c or ip = before.d 
    ip=before.a =>op=before.cadbury or op=none

        ip=before.b =>op=before.kitkat or op=none

    ip=before.c =>op=before.eclairs or op=none

    ip=before.d =>op=before.gum or op=none
    op=before.gum=>x=before.twors
    op=before.cadbury=>x=before.tenrs
    op=before.eclairs=>x=before.oners
    op=before.kitkat=>x=before.fivers
    (ip=before.c and x=before.oners =>op=after.eclairs and opc=after.null1)
    or   
    (ip=before.c and x=before.twors =>op=after.eclairs and opc=after.oners)
    or
        (ip=before.c and x=before.fivers =>op=after.eclairs and opc=after.twors)
    or
        (ip=before.c and x=before.tenrs =>op=after.eclairs and opc=after.fivers)
    or
    (ip=before.c and x=before.null1 =>op=after.null and opc=after.null1)
or
(ip=before.d and x=before.twors =>op=after.gum and opc=after.null1)
    or
(ip=before.d and x=before.oners =>op=after.null and opc=after.oners)
or
(ip=before.d and x=before.fivers =>op=after.gum and opc=after.twors)
or
(ip=before.d and x=before.tenrs =>op=after.gum and opc=after.fivers)
or
    (ip=before.d and x=before.null1 =>op=after.null and opc=after.null1)
or
(ip=before.b and x=before.fivers =>op=after.kitkat and opc=after.null1)   
    or
(ip=before.b and x=before.tenrs =>op=after.kitkat and opc=after.fivers)
or 
(ip=before.b and x=before.oners =>op=after.null and opc=after.oners)
or
(ip=before.b and x=before.twors =>op=after.null and opc=after.twors)
or
    (ip=before.b and x=before.null1 =>op=after.null and opc=after.null1)
or
    (ip=before.a and x=before.tenrs =>op=after.cadbury and opc=after.null1)
or
    (ip=before.a and x=before.fivers =>op=after.null and opc=after.fivers)
or
    (ip=before.a and x=before.twors =>op=after.null and opc=after.twors)
or
    (ip=before.a and x=before.null1 =>op=after.null and opc=after.null1)
}

run chocolate for exactly 2 machine, 8 button, 8 choco, 10 coin

1 个答案:

答案 0 :(得分:1)

abstract sig Coin {
  value: Int
}

sig Oners extends Coin {} { value = 1 }
sig Twors extends Coin {} { value = 2 }
sig Fivers extends Coin {}{ value = 5 }
sig Tenrs extends Coin {} { value = 10 }

sig List {
  coins: seq Coin
}

pred sumTo(l: List, val: Int) {
  (sum idx: l.coins.Coin | l.coins[idx].value) = val
}

run {
  //some l5: List | sumTo[l5, 5]
  some l10: List | sumTo[l10, 10]
} for 10 but 5 Int