-
Notifications
You must be signed in to change notification settings - Fork 0
/
alloy.als
537 lines (436 loc) · 14.2 KB
/
alloy.als
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
// ================================================
// ======================== PRIMITIVE SIGNATURES
sig Name {}
sig Surname {}
sig Email {}
sig Address {}
sig Double {}
enum Bool {
True,
False
}
// ================================================
// ======================== SIGNATURES
sig Time {
value: Int
} { value >= 0 }
sig Date {
value: Int
} { value >= 0 }
sig Ride {
makeUseTicket: lone Ticket,
byTranMean: TransportationMean,
fromLocation: Location,
toLocation: Location
} { fromLocation != toLocation }
sig TransportationCompany {}
abstract sig Ticket {
usedFor: some TransportationMean,
providedByCompany: TransportationCompany
}
sig User {
name: Name,
surname: Surname,
email: Email,
ownsTicket: set Ticket,
hasPreferences: Preferences,
hasConstraints: set TransportationMeanConstraint,
speaksLanguage: Language,
setBreakWindows: set BreakWindow,
createsAppointment: set Appointment,
participatesToAppointment: set Appointment,
hasTravelPlan: set TravelPlan,
currentlyAtLoc: Location
}
sig Appointment {
id: Int,
start: Time,
end: Time,
atLocation: Location,
hasType: AppointmentType,
isRepeatable: lone RepeatableAppointment,
isModified: Bool,
isIncoming: Bool
} { start.value < end.value }
sig Location {
address: Address,
latitude: Double,
longitude: Double
}
enum AppointmentType {
Personal,
Work
}
sig RepeatableAppointment {
every: Int,
start: Date,
end: Date
} { every > 0
start.value < end.value }
sig TravelPlan {
passengers: Int,
baggage: Int,
startRide: Ride,
intermediateRides: set Ride,
endRide: Ride,
forAppointment: Appointment
} {
passengers >= 0
baggage >= 0
// structural constraints on start, intermediate and end Rides
no ir: intermediateRides | startRide = ir or endRide = ir
lone ir: intermediateRides | startRide.toLocation = ir.fromLocation
lone ir: intermediateRides | endRide.fromLocation = ir.toLocation
no ir: intermediateRides | startRide.fromLocation = ir.toLocation
no ir: intermediateRides | endRide.toLocation = ir.fromLocation
all ir: intermediateRides | ir.toLocation = endRide.fromLocation or
one ir1: intermediateRides | ir.toLocation = ir1.fromLocation
all ir: intermediateRides | ir.fromLocation = startRide.toLocation or
one ir1: intermediateRides | ir.fromLocation = ir1.toLocation
#intermediateRides = 0 implies
(startRide = endRide or startRide.toLocation = endRide.fromLocation)
}
// retrieves the whole set of Rides of a travel plan
fun travelPlanRides[t: TravelPlan] : some Ride {
t.startRide + t.intermediateRides + t.endRide
}
abstract sig BreakWindow {}
sig FixedBreakWindow extends BreakWindow {
from: Time,
to: Time
} { from.value < to.value }
sig FlexibleBreakWindow extends BreakWindow {
from: Time,
to: Time,
atLeast: Time
} { from.value < to.value
(atLeast.value > 0 and atLeast.value < minus[to.@value, from.@value]) }
enum Language {
Italiano,
English,
Francais
}
abstract sig TransportationMean {
belongsToCompany: lone TransportationCompany
}
one sig Foot, MoBike, PersonalCar, EnjoyCar, Metro, Tram extends TransportationMean {}
sig Preferences {
ecoFriendly: Bool,
disabledTranMean: set TransportationMean
}
abstract sig TransportationMeanConstraint {
associatedToTranMean: TransportationMean
}
sig DistanceConstraint extends TransportationMeanConstraint {
fromValue: Int,
toValue: Int
} { fromValue >= 0
toValue >= 0
fromValue < toValue }
sig TimeWindowConstraint extends TransportationMeanConstraint{
from: Time,
to: Time
} { from.value < to.value }
// ================================================
// ======================== ADDITIONAL SIGNATURES
sig SuggestedSolution {
suggestsTo: User,
containsTravelPlan: TravelPlan
}
sig Device {
belongsTo: User,
language: Language
}
sig AppInstance{
installedOn: Device,
displayLanguage: Language
}{ let d = installedOn |
( d.language not in SupportedLanguages.setOfLanguages implies ( displayLanguage = English )
else (displayLanguage = d.language) )
}
one sig SupportedLanguages {
setOfLanguages: set Language
}
sig Person {
name: Name,
surname: Surname,
email: Email,
isUser: lone User
}
sig Invitation {
fromUser: User,
toEmail: Email,
forAppointment: Appointment
}{
forAppointment in fromUser.createsAppointment
fromUser.email != toEmail
}
sig Notification {
toUser: User,
incomingAppointment: Appointment
}
// ================================================
// ======================== FACTS
fact EmailsAreUnique {
all disjoint u1, u2: User | u1.email != u2.email
}
fact NoOverlappingLocations {
all disjoint l1, l2: Location | (l1.latitude != l2.latitude) || (l1.longitude != l2.longitude)
}
fact TimeIsUnique {
all disjoint t1, t2: Time | t1.value != t2.value
}
fact ATicketBelongsOnlyToOneUser {
all disjoint u1, u2: User | u1.ownsTicket & u2.ownsTicket = none
}
fact TicketMustBeAssociatedToRides {
all t: Ticket | some r: Ride | t in r.makeUseTicket
}
fact APreferenceBelongsOnlyToOneUser {
all disjoint u1, u2: User | u1.hasPreferences & u2.hasPreferences = none
}
fact TranMeanConstraintsRefersOnlyToOneUser {
all disjoint u1, u2: User | u1.hasConstraints & u2.hasConstraints = none
}
fact ABreakWindowIsSetOnlyByOneUser {
all disjoint u1, u2: User | u1.setBreakWindows & u2.setBreakWindows = none
}
fact AnAppointmentIsCreatedOnlyByOneUser {
all disjoint u1, u2: User | u1.createsAppointment & u2.createsAppointment = none
}
fact AppointmentsMustBeCreatedOnlyByUsers {
all a: Appointment | some u: User | a in u.createsAppointment
}
fact ATravelPlanBelongsOnlyToOneUser {
all disjoint u1, u2: User | u1.hasTravelPlan & u2.hasTravelPlan = none
}
// if an appointment is associated to a travel plan of a User, the User must participate to the appointment
fact ConsistentUserTravelPlanAppointment {
all u: User, a: Appointment, tp: TravelPlan |
(tp.forAppointment = a and tp in u.hasTravelPlan) implies (a in u.participatesToAppointment)
}
fact AppointmentCreationImpliesParticipation {
all u: User, a: Appointment |
(a in u.createsAppointment) implies (a in u.participatesToAppointment)
}
fact AllNameMustBelongToUsers {
all n: Name | some u: User | u.name = n
}
fact AllSurnameMustBelongToUsers {
all s: Surname | some u: User | u.surname = s
}
fact AllEmailMustBelongToPersons {
all e: Email | some p: Person | p.email = e
}
fact AllAddressesMustBelongToLocations {
all a: Address | some loc: Location | loc.address = a
}
fact TicketMustBelongToUsers {
all t: Ticket | some u: User | t in u.ownsTicket
}
fact AllTicketsMustBeProvidedByTranCompany {
all t: Ticket | some tc: TransportationCompany | t.providedByCompany = tc
}
fact TranCompanyMustBeAssociatedWithTranMean {
all tc: TransportationCompany | some tm: TransportationMean | tm.belongsToCompany = tc
}
// no tickets for personal and shared transportation means
fact TicketsUsedOnlyIfNecessary {
all t: Ticket | (Foot & t.usedFor = none) and
(MoBike & t.usedFor = none) and
(PersonalCar & t.usedFor = none) and
(EnjoyCar & t.usedFor = none)
}
fact NoTranCompanyForPersonalTranMeans {
(Foot.belongsToCompany = none) and
(PersonalCar.belongsToCompany= none)
}
fact PreferenceMustBelongToUser {
all p: Preferences | some u: User | u.hasPreferences = p
}
fact TranMeanConstraintsMustBelongToUsers {
all tmc: TransportationMeanConstraint | some u: User | tmc in u.hasConstraints
}
fact BreakWindowMustBeSetByUsers {
all bw: BreakWindow | some u: User | bw in u.setBreakWindows
}
fact RideMustBelongToTravelPlans {
all r: Ride | some tp: TravelPlan | r in travelPlanRides[tp]
}
fact RideBelongsToOnlyOneTravelPlan {
all disjoint tp1, tp2: TravelPlan | travelPlanRides[tp1] & travelPlanRides[tp2] = none
}
fact TravelPlanMustBelongToUsers {
all tp: TravelPlan | some u: User | tp in u.hasTravelPlan
}
fact LocationAssociatedToRideAppointmentOrUser {
all l: Location | some r: Ride, a: Appointment, u: User |
l in (r.fromLocation + r.toLocation + a.atLocation + u.currentlyAtLoc)
}
fact RepeatableAppointmentIsAnAppointment {
all ra: RepeatableAppointment | some a: Appointment | ra in a.isRepeatable
}
fact RepeatableAppointmentsAtTheSameTime {
all a1, a2: Appointment | (a1.isRepeatable = a2.isRepeatable) implies
(a1.start = a2.start and a1.end = a2.end)
}
fact NoStartRideFromAppointmentLocation {
all tp: TravelPlan | tp.startRide.fromLocation != tp.forAppointment.atLocation
}
// before registraion we have a person, after registration we have another person who is a User;
//person associated with User and person before registration have the same data (email, name, surname)
fact EveryUserHasA2Person {
all u: User | some disjoint p1, p2: Person | p1.isUser = u and samePerson[p1, p2]
}
fact SameEmailImpliesSamePerson {
all p1, p2: Person | p1.email = p2.email implies (samePerson[p1, p2])
}
// if two persons are the same, they represent the person before and after registration
fact SamePersonImpliesOldAndNew {
all disjoint p1, p2: Person | samePerson[p1, p2] implies
((p1.isUser = none and p2.isUser != none) or (p1.isUser != none and p2.isUser = none))
}
fact UserAndPersonSameData {
all p: Person | p.isUser != none implies
let u = p.isUser |
p.email = u.email and
p.name = u.name and
p.surname = u.surname
}
fact IsModifiedImpliesAnotherAppointment {
all apOld: Appointment | some apNew: Appointment |
apOld.isModified = True implies
apOld.id = apNew.id and apOld != apNew and apNew.isModified = False
}
fact SameAppointmentIdSameUser {
all disjoint ap1, ap2: Appointment | all u: User |
ap1.id = ap2.id and
(ap1 in u.participatesToAppointment implies (ap2 in u.participatesToAppointment))
and
(ap1 in u.createsAppointment implies (ap2 in u.createsAppointment))
}
fact AllUsersMustBeCreatorOrInvited {
all a: Appointment, u: User | a in u.participatesToAppointment implies
(a in u.createsAppointment or invitedToAppointment[u, a])
}
fact NotificationOnlyIfAppointmentIsIncoming {
all n: Notification | n.incomingAppointment.isIncoming = True
}
fact NotificationForAllIncomingAppointment {
all a: Appointment, u: User |
(a.isIncoming = True and a in u.participatesToAppointment)
implies
(one n1: Notification | n1.incomingAppointment = a and n1.toUser = u)
}
fact UserReceivesNotificationOfOwnedAppointments {
all n: Notification, u: User |
n.toUser = u implies n.incomingAppointment in u.participatesToAppointment
}
fact EachDeviceHasMaxOneAppInstance {
all d: Device | lone a: AppInstance | a.installedOn = d
}
fact EveryUserHasAtLeastOneAppInstance {
all u: User | some a: AppInstance | a.installedOn.belongsTo = u
}
fact TravelPlanAppointmentLocationConsistency {
all tp: TravelPlan, ap:Appointment | ap=tp.forAppointment implies (ap.atLocation=tp.endRide.toLocation)
}
fact OneConstraintPerTravelMean {
all u:User, tc1, tc2:TransportationMeanConstraint | ( tc1 in u.hasConstraints and tc2 in u.hasConstraints
and tc1.associatedToTranMean = tc2.associatedToTranMean ) implies (tc1 = tc2)
}
fact TicketWithOneUser{
all t:Ticket | one u:User | t in u.ownsTicket
}
fact TicketWithAtLeastOneRide{
all t:Ticket | some r:Ride | r.makeUseTicket = t
}
fact TicketConsistency{
all t:Ticket, r:Ride, u:User, tp:TravelPlan | ( r in (tp.startRide + tp.intermediateRides + tp.endRide)
and tp in u.hasTravelPlan and r.makeUseTicket = t ) implies t in u.ownsTicket
}
// if a User has disabled a transportation mean, it should never be suggested to him/her
fact DisabledTranMeansAreNotSuggested {
all u: User, s: SuggestedSolution, tp: TravelPlan |
(u = s.suggestsTo and (tp in u.hasTravelPlan) and tp = s.containsTravelPlan) implies (
(u.hasPreferences.disabledTranMean & s.containsTravelPlan.startRide.byTranMean = none) and
(u.hasPreferences.disabledTranMean & s.containsTravelPlan.intermediateRides.byTranMean = none) and
(u.hasPreferences.disabledTranMean & s.containsTravelPlan.endRide.byTranMean = none)
)
}
fact UserTravelPlanSolutionConsistency {
all s: SuggestedSolution | s.containsTravelPlan in s.suggestsTo.hasTravelPlan
}
fact TravelPlanMustBeSuggested {
all tp: TravelPlan | some s: SuggestedSolution | s.containsTravelPlan = tp
}
fact AppointmentWithSameIdAreEqualsIfNotModified {
all ap1, ap2: Appointment |
(ap1.id = ap2.id and ap1.isModified = False and ap2.isModified = False)
implies appointmentsAreEquals[ap1, ap2]
}
fact EnglishAlwaysSupported {
English in SupportedLanguages.setOfLanguages
}
// ================================================
// ======================== UTILITY PREDICATES
pred samePerson[p1, p2: Person] {
p1.email = p2.email and p1.name=p2.name and p1.surname = p2.surname
}
pred invitedToAppointment[u: User, a: Appointment] {
some i: Invitation | i.forAppointment = a and i.toEmail = u.email
}
pred appointmentsAreEquals[ap1, ap2: Appointment] {
ap1.id = ap2.id and
ap1.start = ap2.start and
ap1.end = ap2.end and
ap1.atLocation = ap2.atLocation and
ap1.hasType = ap2.hasType and
ap1.isRepeatable = ap2.isRepeatable
// no reason to check isModified
// isIncoming can be different!
}
// ================================================
// ======================== ASSERTIONS
assert CanDisplayInAllSupportedLanguages {
no l: AppInstance.installedOn.language |
l in SupportedLanguages.setOfLanguages and
no ap: AppInstance | ap.displayLanguage = l
}
check CanDisplayInAllSupportedLanguages
assert EveryPersonShouldBeAbleToHaveAnAccount {
#User > 0
implies
some p1, p2: Person, u: User |
p1.email = p2.email and p1.isUser = none and p2.isUser = u
}
check EveryPersonShouldBeAbleToHaveAnAccount
assert UserAlwaysNotified {
all a: Appointment, u: User |
(a.isIncoming = True and a in u.participatesToAppointment)
implies
(one n: Notification | n.toUser = u and n.incomingAppointment = a)
}
check UserAlwaysNotified for 2
// ================================================
// ======================== RUNNABLE PREDICATES
pred showSomeAppointmentModified {
some ap: Appointment | ap.isModified = True
}
run showSomeAppointmentModified
pred showSomeMeeting {
some a: Appointment, disjoint u1, u2: User |
a in u1.participatesToAppointment and a in u2.participatesToAppointment
}
run showSomeMeeting for 4
pred show {
#Appointment = 2 and
#User = 1 and
#Notification > 0 and
#TravelPlan = 2 and
#SupportedLanguages.setOfLanguages > 1 and
#Person = 2 and
#Device = 2 and
some d: Device | d.language not in SupportedLanguages.setOfLanguages
}
run show for 3 but 4 Notification, 8 Int