Repository navigation
Expand file tree
/
Copy pathCorrectStateValues.java
More file actions
105 lines (82 loc) · 3.4 KB
/
Copy pathCorrectStateValues.java
File metadata and controls
105 lines (82 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package testSuite;
import liquidjava.specification.Refinement;
import liquidjava.specification.RefinementAlias;
import liquidjava.specification.StateRefinement;
import liquidjava.specification.StateSet;
// an unrelated set with the same name and other states, in a class whose sets are listed before those of
// CorrectStateValues
@StateSet(name = "door", value = {"ajar", "shut"})
class CorrectStateValueDoor {}
@StateSet(name = "door", value = {"closed", "opened"})
@StateSet(name = "lock", value = {"unlocked", "locked"})
@RefinementAlias("Shut(CorrectStateValues d) { door(d) == closed }")
public class CorrectStateValues {
@StateRefinement(to = "door(this) == closed && lock(this) == unlocked")
public CorrectStateValues() {}
@StateRefinement(from = "door(this) == closed && lock(this) != locked", to = "door(this) == opened")
public void open() {}
// the set can also be applied as a method of this, or with this left out
@StateRefinement(from = "this.door() == opened", to = "door() == closed")
public void close() {}
// when it fails, the lock set keeps its value
@StateRefinement(from = "door(this) == closed", to = "return ? lock(this) == locked : lock(this) == lock(old(this))")
public boolean tryLock() {
return true;
}
@StateRefinement(from = "locked == this.lock()", to = "lock() != locked")
public void unlock() {}
// the parameter closed keeps its meaning: the door takes its value
@StateRefinement(to = "door(this) == closed")
public void choose(int closed) {}
static void needsShut(@Refinement("Shut(_)") CorrectStateValues d) {}
public static void main(String[] args) {
CorrectStateValues d = new CorrectStateValues();
int closed = 1; // the alias body still means the state closed
needsShut(d);
d.open();
d.close();
if (d.tryLock())
d.unlock();
d.open();
d.choose(1);
d.close();
}
}
// states named like the variables _ and return do not take their place
@StateSet(name = "mode", value = {"_", "return", "on"})
class CorrectStateValueVariableNames {
@StateRefinement(to = "mode(this) == on")
public CorrectStateValueVariableNames() {}
public void check(@Refinement("mode(this) == _") int value) {}
@Refinement("_ == 2")
@StateRefinement(to = "mode(this) == return")
public int pick() {
return 2;
}
@StateRefinement(from = "mode(this) == on")
public void needsOn() {}
static void test() {
CorrectStateValueVariableNames v = new CorrectStateValueVariableNames();
v.check(2);
v.pick();
v.needsOn();
}
}
// a subclass uses the set of its superclass, not the unrelated one with the same name
class CorrectStateValueSubclass extends CorrectStateValues {
@StateRefinement(from = "door(this) == opened", to = "door(this) == closed")
public void shut() {}
static void test() {
CorrectStateValueSubclass s = new CorrectStateValueSubclass();
s.open();
s.shut();
}
}
// a subclass that declares its own set named door uses its states, as the shorthand does
@StateSet(name = "door", value = {"open", "closed"})
class CorrectStateValueBase {}
@StateSet(name = "door", value = {"ajar", "shut"})
class CorrectStateValueChild extends CorrectStateValueBase {
@StateRefinement(from = "door(this) == shut", to = "door(this) == ajar")
public void push() {}
}