aboutsummaryrefslogtreecommitdiffstats
path: root/Domains/crossingScenario/queries/crossingScenarioQueries.vql
blob: d1c72820c4771d7283e1ca75af61008869cb8cb5 (plain) (blame)
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
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
package queries

import "http://www.example.com/crossingScenario"
import "http://www.eclipse.org/emf/2002/Ecore"

//////Minimal Failing Example
//@Constraint(severity = "error", key = {l}, message = "this defines the placedOn relation")
//pattern patterThatOnlyWorksWithInt(l : Lane) {
//	Lane.referenceCoord(l, w);
//	check(w <= 0-10.0);
//}

//////////////
//CrossingScenario
/////////////

/* 
//TODO Hard-code xSize?
//TODO Hard-code ySize?

//TODO Hard-code maxTime?
@Constraint(severity="error", key={l}, message="3 CrossingScenari")
pattern define_cs_maxTime(cs:CrossingScenario) {
	CrossingScenario.maxTime(cs, mt);
	check(mt != 60.0);
}
*/

///////*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*//////
//Lane
///////*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*//////
/*

////<<<<OLD>>>>
////Width: Different Lanes
//@Constraint(severity="error", key={l}, message="1 Lane")
//pattern define_numWidth_small(l : Lane) {
//	Lane.width(l, Size::S_Small);
//	Lane.numWidth(l, nw);
//	check(nw <= 5.0);
//} or {
//	Lane.width(l, Size::S_Small);
//	Lane.numWidth(l, nw);
//	check(nw >= 10.0);
//}
//
//@Constraint(severity="error", key={l}, message="2 Lane")
//pattern define_numWidth_medium(l : Lane) { 
//	Lane.width(l, ::S_Med);
//	Lane.numWidth(l, nw);
//	check(nw <= 10.0);
//}
//or {
//	Lane.width(l, Size::S_Med);
//	Lane.numWidth(l, nw);
//	check(nw >= 15.0);
//}
//
//@Constraint(severity="error", key={l}, message="3 Lane")
//pattern define_numWidth_large(l : Lane) { 
//	Lane.width(l, Size::S_Large);
//	Lane.numWidth(l, nw);
//	check(nw <= 15.0);
//}
//or {
//	Lane.width(l, Size::S_Large);
//	Lane.numWidth(l, nw);
//	check(nw >= 20.0);
//}

//<<<<NEW>>>>
//Width is FIXED: always 5

/////////////Prevlane

////<<<<OLD>>>>
////Generating 2 linkedlists (1 for horizontal, 1 for vertical lanes)
//////////head lanes do not have prevLane
//@Constraint(severity="error", key={l}, message="6.1 Lane")
//pattern define_prevLane_headVertLaneDoesNotHavePrevLane(cs:CrossingScenario, l:Lane_Vertical) {
//	CrossingScenario.vertical_head(cs, l);
//	Lane.prevLane(l, _);
//}
//
//@Constraint(severity="error", key={l}, message="6.2 Lane")
//pattern define_prevLane_headHoriLaneDoesNotHavePrevLane(cs:CrossingScenario, l:Lane_Horizontal) {
//	CrossingScenario.horizontal_head(cs, l);
//	Lane.prevLane(l, _);
//}
//
//////////Non-head lanes must have prevLane
//@Constraint(severity="error", key={l}, message="6.1 Lane")
//pattern define_prevLane_nonheadVertLaneHasPrevLane(l:Lane_Vertical) {
//	neg find find_headVertLane(l);
//	neg find find_laneWithPrevLane(l);
//}
//
//@Constraint(severity="error", key={l}, message="6.1 Lane")
//pattern define_prevLane_nonheadHoriLaneHasPrevLane(l:Lane_Horizontal) { 
//	neg find find_headHoriLane(l);
//	neg find find_laneWithPrevLane(l);
//}
//
//private pattern find_headVertLane(l:Lane_Vertical) {
//	CrossingScenario.vertical_head(_, l);
//}
//private pattern find_headHoriLane(l:Lane_Horizontal) {
//	CrossingScenario.horizontal_head(_, l);
//}
//private pattern find_laneWithPrevLane(l:Lane) {
//	Lane.prevLane(l, _);
//}
//
///////////Lane cannot be its own recursive prevLane
//@Constraint(severity="error", key={l}, message="6.1 Lane")
//pattern define_prevLane_lanecannotBeItsOwnPrevLane(l:Lane) {
//	Lane.prevLane(l, l);
//}
//
//@Constraint(severity="error", key={l}, message="6.2 Lane")
//pattern define_prevLane_lanecannotBeItsOwnRecursivePrevLane(l:Lane) {
//	find find_prevLane+(l, l);
//}
//private pattern find_prevLane(l1:Lane, l2:Lane) {
//	Lane.prevLane(l1, l2);
//}
//
////////Lane cannot be prevLane of >1 other lane
//@Constraint(severity="error", key={l1, l2}, message="7 Lane")
//pattern define_prevLane_lanecannotbeprevLaneof2lanes(l1:Lane, l2:Lane) {
//	Lane.prevLane(l1, l);
//	Lane.prevLane(l2, l);
//	l1 != l2;
//}
//
////////consecutive lanes must have same orientation
//@Constraint(severity="error", key={l1, l2}, message="8 Lane")
//pattern define_prevLane_consecutiveLanesMustHaveSameOrientation1(l1:Lane_Horizontal, l2:Lane_Vertical) {
//	Lane.prevLane(l1, l2);
//}
//
//@Constraint(severity="error", key={l1, l2}, message="8 Lane")
//pattern define_prevLane_consecutiveLanesMustHaveSameOrientation2(l1:Lane_Vertical, l2:Lane_Horizontal) {
//	Lane.prevLane(l1, l2);
//}
//
///////////////ReferenceCoord
//
///////refCoord of head lanes must be 0
//@Constraint(severity="error", key={l}, message="6.2 Lane")
//pattern define_prevLane_headHoriLaneHas0RefCoord(cs:CrossingScenario, l:Lane_Horizontal) {
//	CrossingScenario.horizontal_head(cs, l);
//	Lane.referenceCoord(l, rc);
//	check(rc != 0.0);
//}
//
//@Constraint(severity="error", key={l}, message="6.2 Lane")
//pattern define_prevLane_headVertLaneHas0RefCoord(cs:CrossingScenario, l:Lane_Vertical) {
//	CrossingScenario.vertical_head(cs, l);
//	Lane.referenceCoord(l, rc);
//	check(rc != 0.0);
//}
//
////////refCoord of a lane is prevLane.rc + curLane.numWidth
//
//@Constraint(severity="error", key={l}, message="6.2 Lane")
//pattern define_referenceCoord_laneWithPrevHasCorrectRefCoord(l:Lane) {
//	Lane.prevLane(l, prev);
//	Lane.referenceCoord(l, rcCur);
//	
//	Lane.numWidth(prev, wPrev);
//	Lane.referenceCoord(prev, rcPrev);
//	check(rcCur != rcPrev + wPrev);
//}

//<<<<NEW>>>>
//Lanes are all predefind
*/

///////*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*//////
//Lane+Actor
///////*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*//////

/////////Actor (a) on vertical lane (l) must have a.xPos=[l.rc, l.rc + l.nw]
@Constraint(severity = "error", key = {a}, message = "this defines the placedOn relation for vertical lanes")
pattern define_placedOn_actorOnVerticalLane(a : Actor, vl:Lane_Vertical) {
    Actor.placedOn(a, vl);
    Actor.xPos(a, x);
    Lane.referenceCoord(vl, r);
    check(x <= r);
} or {
    Actor.placedOn(a, vl);
    Actor.xPos(a, x);
    Lane.referenceCoord(vl, r);
//     //<<<<OLD>>>>
//    Lane.numWidth(vl, w);
//    check(x >= (r + w));
    
    //<<<<NEW>>>>: lanes all have width=5
    check(x >= (r + 5.0));
}

@Constraint(severity = "error", key = {a}, message = "this defines the placedOn relation for vertical lanes")
pattern define_placedOn_actorOnHorizLane(a : Actor, hl:Lane_Horizontal) {
    Actor.placedOn(a, hl);
    Actor.yPos(a, y);
    Lane.referenceCoord(hl, r);
    check(y <= r);
} or {
    Actor.placedOn(a, hl);
    Actor.yPos(a, y);
    Lane.referenceCoord(hl, r);
//    //<<OLD>>
//    Lane.numWidth(hl, w);
//    check(y >= (r + w));
    
    //<<NEW>>
    check(y >= (r + 5.0));
    
}

///////*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*//////
//Actor
///////*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*//////

/////----------------
//Xpos and Ypos Bounds
/////----------------

private pattern helper_horiz_getXAndBounds(cs:CrossingScenario, a:Actor,
	xMax:java Double, xP:java Double) {
	Actor.placedOn(a, hl);
	Lane_Horizontal(hl);
	CrossingScenario.actors(cs, a);
	CrossingScenario.xSize(cs, xMax);
	Actor.xPos(a, xP);
}

private pattern helper_vert_getYAndBounds(cs:CrossingScenario, a:Actor,
	yMax:java Double, yP:java Double) {
	Actor.placedOn(a, vl);
	Lane_Vertical(vl);
	CrossingScenario.actors(cs, a);
	CrossingScenario.ySize(cs, yMax);
	Actor.yPos(a, yP);
}

@Constraint(severity="error", key={cs}, message="x")
pattern define_actor_maxXp(cs:CrossingScenario, a:Actor) {
	find helper_horiz_getXAndBounds(cs, a, xMax, xP); 
	check(xP >= xMax);}

@Constraint(severity="error", key={cs}, message="x")
pattern define_actor_minXp(cs:CrossingScenario, a:Actor) {
	find helper_horiz_getXAndBounds(cs, a, xMax, xP); 
	check(xP <= 0-xMax);}
	
@Constraint(severity="error", key={cs}, message="x")
pattern define_actor_maxYp(cs:CrossingScenario, a:Actor) {
	find helper_vert_getYAndBounds(cs, a, yMax, yP); 
	check(yP >= yMax);}

@Constraint(severity="error", key={cs}, message="x")
pattern define_actor_minYp(cs:CrossingScenario, a:Actor) {
	find helper_vert_getYAndBounds(cs, a, yMax, yP); 
	check(yP <= 0-yMax);}

/////----------------	
//Xspeed and Yspeed bounds
/////----------------
	
//TODO THIS IS HARD_CODED

/////////VERTICAL LANE
@Constraint(severity="error", key={a}, message="7 Actor")
pattern define_actor_actorOnVertLaneHasxSpeed0(a:Actor) {
	Actor.placedOn(a, vl);
	Lane_Vertical(vl);
    Actor.xSpeed(a, xSpeed);
    check(xSpeed != 0);
}

private pattern helper_vert_getYSpeedAndBounds(cs:CrossingScenario, a:Actor,
	ySpeedMax:java Double, ySpeed:java Double) {
	Actor.placedOn(a, vl);
	Lane_Vertical(vl);
	CrossingScenario.actors(cs, a);
	CrossingScenario.maxYSpeed(cs, ySpeedMax);
	Actor.ySpeed(a, ySpeed);
}

@Constraint(severity="error", key={cs}, message="x")
pattern define_actor_actorOnVertLaneMinYs(cs:CrossingScenario, a:Actor) {
	find helper_vert_getYSpeedAndBounds(cs, a, ySMax, yS); 
	check(yS <= 0-ySMax);
}

@Constraint(severity="error", key={cs}, message="x")
pattern define_actor_actorOnVertLaneMaxYs(cs:CrossingScenario, a:Actor) {
	find helper_vert_getYSpeedAndBounds(cs, a, ySMax, yS); 
	check(yS >= ySMax);
}

/////////HORIZONTAL LANE
@Constraint(severity="error", key={a}, message="7 Actor")
pattern define_actor_actorOnHorizLaneHasySpeed0(a:Actor) {
	Actor.placedOn(a, hl);
	Lane_Horizontal(hl);
    Actor.ySpeed(a, ySpeed);
    check(ySpeed != 0);
}

private pattern helper_horiz_getXSpeedAndBounds(cs:CrossingScenario, a:Actor,
	xSpeedMax:java Double, xSpeed:java Double) {
	Actor.placedOn(a, hl);
	Lane_Horizontal(hl);
	CrossingScenario.actors(cs, a);
	CrossingScenario.maxXSpeed(cs, xSpeedMax);
	Actor.xSpeed(a, xSpeed);
}

@Constraint(severity="error", key={cs}, message="x")
pattern define_actor_actorOnHorizLaneMinXs(cs:CrossingScenario, a:Actor) {
	find helper_horiz_getXSpeedAndBounds(cs, a, xSMax, xS); 
	check(xS <= 0-xSMax);
}

@Constraint(severity="error", key={cs}, message="x")
pattern define_actor_actorOnHorizLaneMaxXs(cs:CrossingScenario, a:Actor) {
	find helper_horiz_getXSpeedAndBounds(cs, a, xSMax, xS); 
	check(xS >= xSMax);
}

/////----------------
/////WIDTH and LENGTH
/////----------------

///////pedestrian-width (3)
@Constraint(severity="error", key={p}, message="3 Actor")
pattern define_actor_pedestrianWidth(p:Pedestrian) {
    Pedestrian.width(p, w);
    check(w != 1.0);
}

/////////pedestrian-width (4)
@Constraint(severity="error", key={p}, message="4 Actor")
pattern define_actor_pedestrianLength(p:Pedestrian) {
    Pedestrian.length(p, l);
    check(l != 1.0);
}

/////////actor-width (5)
@Constraint(severity="error", key={v}, message="5 Actor")
pattern define_actor_vehicleWidth(v:Vehicle) {
	Vehicle.placedOn(v, lane);
	Lane_Vertical(lane);
    Vehicle.width(v, w);
    check(w != 1.0);
} or {
	Vehicle.placedOn(v, lane);
	Lane_Horizontal(lane);
    Vehicle.width(v, w);
    check(w != 3.0);
}

/////////actor-width (6)
@Constraint(severity="error", key={v}, message="6 Actor")
pattern define_actor_vehicleLength(v:Vehicle) {
	Vehicle.placedOn(v, lane);
	Lane_Vertical(lane);
    Vehicle.length(v, l);
    check(l != 3.0);
} or {
	Vehicle.placedOn(v, lane);
	Lane_Horizontal(lane);
    Vehicle.length(v, l);
    check(l != 1.0);
}

/////----------------
/////DERIVED FEATURES
/////----------------

private pattern helper_getCoords(a1:Actor, 
	a2: Actor, x1:java Double, x2:java Double, y1:java Double, y2:java Double) {
	Actor.xPos(a1, x1);
	Actor.yPos(a1, y1);
	Actor.xPos(a2, x2);
	Actor.yPos(a2, y2);
}

@QueryBasedFeature 
pattern dist_near(a1: Actor, a2: Actor) {
	find helper_actorsAreNear(a1, a2);
	Actor.dist_near(a1, a2);
}

private pattern helper_actorsAreNear(a1: Actor, a2: Actor) {
	find helper_getCoords(a1, a2, x1, x2, y1, y2);
	//check(dx^2 + dy^2 < 10^2)
	check((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) < 10*10);	
}

@QueryBasedFeature
pattern dist_med(a1: Actor, a2: Actor) {
	find helper_actorsAreMed(a1, a2);
	Actor.dist_med(a1, a2);
}

private pattern helper_actorsAreMed(a1: Actor, a2: Actor) {
	find helper_getCoords(a1, a2, x1, x2, y1, y2);
	//check(10^2 < dx^2 + dy^2 < 15^2)
	check((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) > 10*10);
	check((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) < 15*15);
}

@QueryBasedFeature
pattern dist_far(a1: Actor, a2: Actor) {
	find helper_actorsAreFar(a1, a2);
	Actor.dist_far(a1, a2);
}

private pattern helper_actorsAreFar(a1: Actor, a2: Actor) {
	find helper_getCoords(a1, a2, x1, x2, y1, y2);
	//check(dx^2 + dy^2 > 20^2)
	check((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) > 20*20);	
}

///////*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*//////
//Relation
///////*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*//////

@Constraint(severity="error", key={a1, a2}, message="1 Relation")
pattern define_relation_noSelfRelation(a1:Actor, a2:Actor) {
	Relation.source(r, a1);
	Relation.target(r, a2);
	a1 == a2;
}

//TODO do above but transitively?/
//////*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*//////
//CollisionExists
///////*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*//////


//<<QUALTATIF ABSTRACTION>>

@Constraint(severity="error", key={a1, a2}, message="x")
pattern collisionExists_qualAbstr(a1:Actor, a2:Actor) {
	CollisionExists.source(ce, a1);
	CollisionExists.target(ce, a2);
	Actor.placedOn(a1, vl1);
	Lane_Vertical(vl1);
	Actor.placedOn(a2, vl2);
	Lane_Vertical(vl2);
} or {
	CollisionExists.source(ce, a1);
	CollisionExists.target(ce, a2);
	Actor.placedOn(a1, hl1);
	Lane_Horizontal(hl1);
	Actor.placedOn(a2, hl2);
	Lane_Horizontal(hl2);
}

//<<END QUALITATIF ABSTRACTION>>
/*
////
//VS VisionBlocked
////
//TODO Very prone to corner cases

@Constraint(severity="error", key={a1, a2}, message="x")
pattern collisionExists_vsVisionBlocked(a1:Actor, a2:Actor) {	
	VisionBlocked.source(vb, a1);
	VisionBlocked.target(vb, a2);
	neg find helper_collidingActors(a1, a2);
} or {
	VisionBlocked.source(vb, a1);
	VisionBlocked.target(vb, a2);
	neg find helper_collidingActors(a2, a1);
}

private pattern helper_collidingActors(a1:Actor, a2:Actor){
	CollisionExists.source(vb, a1);
	CollisionExists.target(vb, a2);
}

//<<END QUALITATIF ABSTRACTION>>

////
//CollisionExists - Time
////


@Constraint(severity="error", key={c}, message="x")
pattern collisionExists_timeWithinBound(ss:CrossingScenario, c:CollisionExists) {
	CrossingScenario.relations(ss, c);
	CrossingScenario.maxTime(ss, maxTime);
	CollisionExists.collisionTime(c, cTime);
	check(cTime >= maxTime);}

@Constraint(severity="error", key={c}, message="x")
pattern collisionExists_timeNotNegative(c:CollisionExists) {
	CollisionExists. collisionTime(c, cTime);
	check(cTime <= 0.0);}

////
//CollisionExists - Physical Positioniung
////

private pattern helper_getCollExistsTime(a1:Actor, a2: Actor, cTime:java Double) {
	CollisionExists.source(c, a1);
	CollisionExists.target(c, a2);
	CollisionExists. collisionTime(c, cTime);
}

private pattern helper_getYCoords(a:Actor, l:java Double, 
	yPos:java Double, ySpeed:java Double) {

	Actor.length(a, l);
	Actor.yPos(a, yPos);
	Actor.ySpeed(a, ySpeed);
}

private pattern helper_getAllYCoords(a1:Actor, a2: Actor, 
	l1:java Double, l2:java Double, yPos1:java Double, yPos2:java Double,
	ySpeed1:java Double, ySpeed2:java Double) {

	CollisionExists.source(c, a1);
	CollisionExists.target(c, a2);
	find helper_getYCoords(a1, l1, yPos1, ySpeed1);
	find helper_getYCoords(a2, l2, yPos2, ySpeed2);
}

private pattern helper_getXCoords(a:Actor, l:java Double, 
	xPos:java Double, xSpeed:java Double) {

	Actor.length(a, l);
	Actor.xPos(a, xPos);
	Actor.xSpeed(a, xSpeed);
}

private pattern helper_getAllXCoords(a1:Actor, a2: Actor, 
	w1:java Double, w2:java Double, xPos1:java Double, xPos2:java Double,
	xSpeed1:java Double, xSpeed2:java Double) {
	
	CollisionExists.source(c, a1);
	CollisionExists.target(c, a2);
	find helper_getXCoords(a1, w1, xPos1, xSpeed1);
	find helper_getXCoords(a2, w2, xPos2, xSpeed2);
}

@Constraint(severity="error", key={a1}, message="x")
pattern collisionExists_defineCollision_y1(a1:Actor, a2:Actor) {

	find helper_getCollExistsTime(a1, a2, cTime);
	find helper_getAllYCoords(a1, a2, l1, l2, yPos1, yPos2, ySpeed1, ySpeed2);
	
	//check(y_1_bottom > y_2_top
	check((yPos1 + (ySpeed1 * cTime)) - (l1/2) > (yPos2 + (ySpeed2 * cTime)) + (l2/2));	
}

@Constraint(severity="error", key={a1}, message="x")
pattern collisionExists_defineCollision_y2(a1:Actor, a2:Actor) {
	//This second one is required because we do not want to enforce both a1->c->a2 and a2->c->a1

	find helper_getCollExistsTime(a1, a2, cTime);
	find helper_getAllYCoords(a1, a2, l1, l2, yPos1, yPos2, ySpeed1, ySpeed2);
	
	//check(y_1_top < y_2_bottom)
	check((yPos1 + (ySpeed1 * cTime)) + (l1/2) < (yPos2 + (ySpeed2 * cTime)) - (l2/2));	
}

@Constraint(severity="error", key={a1}, message="x")
pattern collisionExists_defineCollision_x1(a1:Actor, a2:Actor) {

	find helper_getCollExistsTime(a1, a2, cTime);
	find helper_getAllXCoords(a1, a2, w1, w2, xPos1, xPos2, xSpeed1, xSpeed2);
	
	//check(x_1_left > x_2_right)
	check((xPos1 + (xSpeed1 * cTime)) - (w1/2) > (xPos2 + (xSpeed2 * cTime)) + (w2/2));	
}

@Constraint(severity="error", key={a1}, message="x")
pattern collisionExists_defineCollision_x2(a1:Actor, a2:Actor) {
	//This second one is required because we do not want to enforce both a1->c->a2 and a2->c->a1

	find helper_getCollExistsTime(a1, a2, cTime);
	find helper_getAllXCoords(a1, a2, w1, w2, xPos1, xPos2, xSpeed1, xSpeed2);
	
	//check(x_1_right < x_2_left)
	check((xPos1 + (xSpeed1 * cTime)) + (w1/2) < (xPos2 + (xSpeed2 * cTime)) - (w2/2));	
}

*/
///////*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*//////
//VisionBlocked
///////*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*//////

//<<QUALTATIF ABSTRACTION>>

@Constraint(severity="error", key={a1, a2}, message="x")
pattern visionBlocked_qualAbstr(a1:Actor, a2:Actor) {
	VisionBlocked.source(vb, a1);
	VisionBlocked.target(vb, a2);
	Actor.placedOn(a1, l);
	Actor.placedOn(a2, l);
} or {
	VisionBlocked.source(vb, a1);
	VisionBlocked.blockedBy(vb, a2);
	Actor.placedOn(a1, l);
	Actor.placedOn(a2, l);
} or {
	VisionBlocked.blockedBy(vb, a1);
	VisionBlocked.target(vb, a2);
	Actor.placedOn(a1, l);
	Actor.placedOn(a2, l);
}

@Constraint(severity="error", key={a1, a2}, message="x")
pattern visionBlocked_qualAbstr2(a1:Actor, a2:Actor) {
	VisionBlocked.source(ce, a1);
	VisionBlocked.target(ce, a2);
	Actor.placedOn(a1, vl1);
	Lane_Vertical(vl1);
	Actor.placedOn(a2, vl2);
	Lane_Vertical(vl2);
} or {
	VisionBlocked.source(ce, a1);
	VisionBlocked.target(ce, a2);
	Actor.placedOn(a1, hl1);
	Lane_Horizontal(hl1);
	Actor.placedOn(a2, hl2);
	Lane_Horizontal(hl2);
}

//<<END QUALITATIF ABSTRACTION>>

@Constraint(severity="error", key={a1, a2}, message="x")
pattern visionBlocked_invalidBlocker(a1:Actor, a2:Actor) {
	VisionBlocked.source(vb, a1);
	VisionBlocked.target(vb, a2);
	VisionBlocked.blockedBy(vb, a2);
} or {	
	VisionBlocked.source(vb, a1);
	VisionBlocked.target(vb, a2);
	VisionBlocked.blockedBy(vb, a1);
}

@Constraint(severity="error", key={a1, vb}, message="x")
pattern visionBlocked_ites_notOnSameVertLine(a1:Actor, a2:Actor, vb:VisionBlocked) {
	//REQUIRED to avoid division by 0 in next 2 queries
	VisionBlocked.source(vb, a1);
	VisionBlocked.target(vb, a2);
	
	Actor.xPos(a1, x1);
	Actor.xPos(a2, x2);
	
	//check(slope of a1-to-BlockerTop < slope of a1-to-a2)
	check(x1 == x2);	
}

//TODO refactor?
@Constraint(severity="error", key={a1, vb}, message="x")
pattern visionBlocked_ites_top(a1:Actor, a2:Actor, vb:VisionBlocked) {
	//This second one is required because we do not want to enforce both a1->c->a2 and a2->c->a1
	VisionBlocked.source(vb, a1);
	VisionBlocked.target(vb, a2);
	VisionBlocked.blockedBy(vb, aBlocker);
	
	Actor.xPos(a1, x1);
	Actor.yPos(a1, y1);
	Actor.xPos(a2, x2);
	Actor.yPos(a2, y2);
	Actor.xPos(aBlocker, xBlocker);
	Actor.yPos(aBlocker, yBlocker);
	Actor.length(aBlocker, lenBlocker);
	Actor.width(aBlocker, widBlocker);
	
	//check(slope of a1-to-BlockerTop < slope of a1-to-a2)
	check(
		( yBlocker - y1 + (if(xBlocker > x1){lenBlocker/2}else{0-lenBlocker/2})) / 
		( xBlocker - x1 + (if(yBlocker > y1){0-widBlocker/2}else{widBlocker/2}))
		< ((y1-y2)/(x1-x2)));	
}

//TODO refactor?
@Constraint(severity="error", key={a1, vb}, message="x")
pattern visionBlocked_ites_bottom(a1:Actor, a2:Actor, vb:VisionBlocked) {
	//This second one is required because we do not want to enforce both a1->c->a2 and a2->c->a1
	VisionBlocked.source(vb, a1);
	VisionBlocked.target(vb, a2);
	VisionBlocked.blockedBy(vb, aBlocker);
	
	Actor.xPos(a1, x1);
	Actor.yPos(a1, y1);
	Actor.xPos(a2, x2);
	Actor.yPos(a2, y2);
	Actor.xPos(aBlocker, xBlocker);
	Actor.yPos(aBlocker, yBlocker);
	Actor.length(aBlocker, lenBlocker);
	Actor.width(aBlocker, widBlocker);
	
	//check(slope of a1-to-BlockerBottom > slope of a1-to-a2)
	check(
		( yBlocker - y1 + (if(xBlocker > x1){0-lenBlocker/2}else{lenBlocker/2})) / 
		( xBlocker - x1 + (if(yBlocker > y1){widBlocker/2}else{0-widBlocker/2}))
		> ((y1-y2)/(x1-x2)));	
}

@Constraint(severity="error", key={a1, a2}, message="x")
pattern visionBlocked_blockerNotToRightOfBoth(a1:Actor, a2:Actor) {
	//This second one is required because we do not want to enforce both a1->c->a2 and a2->c->a1
	VisionBlocked.source(vb, a1);
	VisionBlocked.target(vb, a2);
	VisionBlocked.blockedBy(vb, aBlocker);
	
	Actor.xPos(a1, x1);
	Actor.xPos(a2, x2);
	Actor.xPos(aBlocker, xBlocker);
	
	check( x1 < xBlocker && x2 < xBlocker);
}

@Constraint(severity="error", key={a1, a2}, message="x")
pattern visionBlocked_blockerNotToLeftOfBoth(a1:Actor, a2:Actor) {
	//This second one is required because we do not want to enforce both a1->c->a2 and a2->c->a1
	VisionBlocked.source(vb, a1);
	VisionBlocked.target(vb, a2);
	VisionBlocked.blockedBy(vb, aBlocker);
	
	Actor.xPos(a1, x1);
	Actor.xPos(a2, x2);
	Actor.xPos(aBlocker, xBlocker);
	
	check( x1 > xBlocker && x2 > xBlocker);
}


/////*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*//////
//SeparationDistance
/////*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*//////
/*
private pattern helper_getSeperateActorsCoords(a1:Actor, 
	a2: Actor, sd:SeparationDistance, 
	x1:java Double, x2:java Double, y1:java Double, y2:java Double) {
	SeperationDistance.source(sd, a1);
	SeparationDistance.target(sd, a2);
	
	find helper_getCoords(a1, a2, x1, x2, y1, y2);
}

@Constraint(severity="error", key={a1, sd}, message="x")
pattern SeparationDistance_near_lb(a1:Actor, a2:Actor, sd:SeparationDistance) {
	//This second one is required because we do not want to enforce both a1->c->a2 and a2->c->a1
	
	find helper_getSeparateActorsCoords(a1, a2, sd, x1, x2, y1, y2);
	SeparationDistance.distance(sd, Distance::D_Near);

	//check(dx^2 + dy^2 < 5^2)
	check((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) < 5*5);	
}

@Constraint(severity="error", key={a1, sd}, message="x")
pattern SeparationDistance_near_ub(a1:Actor, a2:Actor, sd:SeparationDistance) {
	//This second one is required because we do not want to enforce both a1->c->a2 and a2->c->a1
	
	find helper_getSeparateActorsCoords(a1, a2, sd, x1, x2, y1, y2);
	SeparationDistance.distance(sd, Distance::D_Near);

	//check(dx^2 + dy^2 > 10^2)
	check((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) > 10*10);	
}

@Constraint(severity="error", key={a1, sd}, message="x")
pattern SeparationDistance_medium_lb(a1:Actor, a2:Actor, sd:SeparationDistance) {
	//This second one is required because we do not want to enforce both a1->c->a2 and a2->c->a1
	
	find helper_getSeparateActorsCoords(a1, a2, sd, x1, x2, y1, y2);
	SeparationDistance.distance(sd, Distance::D_Med);

	//check(dx^2 + dy^2 < 10^2)
	check((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) < 10*10);	
}

@Constraint(severity="error", key={a1, sd}, message="x")
pattern SeparationDistance_medium_ub(a1:Actor, a2:Actor, sd:SeparationDistance) {
	//This second one is required because we do not want to enforce both a1->c->a2 and a2->c->a1
	
	find helper_getSeparateActorsCoords(a1, a2, sd, x1, x2, y1, y2);
	SeparationDistance.distance(sd, Distance::D_Med);

	//check(dx^2 + dy^2 > 1^2)
	check((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) > 15*15);	
}

@Constraint(severity="error", key={a1, sd}, message="x")
pattern SeparationDistance_far_lb(a1:Actor, a2:Actor, sd:SeparationDistance) {
	//This second one is required because we do not want to enforce both a1->c->a2 and a2->c->a1
	
	find helper_getSeparateActorsCoords(a1, a2, sd, x1, x2, y1, y2);
	SeparationDistance.distance(sd, Distance::D_Far);

	//check(dx^2 + dy^2 < 15^2)
	check((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) < 15*15);	
}
*/

/////*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*//////
//CollisionDoesNotExist
/////*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*-_-*//////
/*
//TODO
@Constraint(severity="error", key={a1, cdne}, message="x")
pattern collisionDoesNotExist(a1:Actor, a2:Actor, ss:CrossingScenario, cdne:CollisionDoesNotExist) {
	//This second one is required because we do not want to enforce both a1->c->a2 and a2->c->a1
	
	CrossingScenario.actors(ss, a1);
	CrossingScenario.actors(ss, a2);
	Actor.relations(a1, cdne);
	CollisionDoesNotExist.target(cdne, a2);
	CrossingScenario.maxTime(ss, maxTime);
	
	Actor.width(a1, w1);
	Actor.length(a1, l1);
	Actor.xPos(a1, xPos1);
	Actor.yPos(a1, yPos1);
	Actor.xSpeed(a1, xSpeed1);
	Actor.ySpeed(a1, ySpeed1);
	
	Actor.width(a2, w2);
	Actor.length(a2, l2);
	Actor.xPos(a2, xPos2);
	Actor.yPos(a2, yPos2);
	Actor.xSpeed(a2, xSpeed2);
	Actor.ySpeed(a2, ySpeed2);
	//check(dx^2 + dy^2 < 15^2)
	check((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2) < 15*15);	
}
*/