You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: core-concepts/modules/ROOT/pages/typeql/invalid-patterns.adoc
+37Lines changed: 37 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -224,3 +224,40 @@ match
224
224
----
225
225
226
226
====
227
+
228
+
=== Nested optional blocks in write stages
229
+
230
+
A `try` block in a write stage is executed as a unit. When all variables referenced in the `try` block are bound, the write instructions are executed. While this could allow for arbitrary nesting of
231
+
`try` blocks where the bound variables are checked at every level, for simplicity's sake nesting `try` blocks is not permitted.
232
+
233
+
[,typeql]
234
+
----
235
+
#!test[write, fail_at=runtime]
236
+
match
237
+
$person isa person;
238
+
try {
239
+
$edu isa education, links (institute: $institute, attendee: $person);
240
+
try { $emp isa employment, links (employer: $company, employee: $person); }; # allowed in match
241
+
};
242
+
delete
243
+
try {
244
+
$edu;
245
+
try { $emp; }; # not allowed!
246
+
};
247
+
----
248
+
249
+
Use instead:
250
+
251
+
[,typeql]
252
+
----
253
+
#!test[write, rollback]
254
+
match
255
+
$person isa person;
256
+
try {
257
+
$edu isa education, links (institute: $institute, attendee: $person);
Copy file name to clipboardExpand all lines: typeql-reference/modules/ROOT/pages/patterns/optionals.adoc
+67-13Lines changed: 67 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -34,15 +34,11 @@ match
34
34
$_ isa marriage, links (spouse: $person, spouse: $spouse);
35
35
};
36
36
----
37
-
Here, `$spouse` will be bound to the spouse if `$person` is in a `marriage`.
38
-
Else, `$spouse` will be bound to `None`
37
+
Here, `$spouse` is bound to the spouse if `$person` is in a `marriage`.
38
+
Else, `$spouse` is bound to `None`
39
39
40
-
=== insert & delete
41
-
[NOTE]
42
-
====
43
-
Coming soon!
44
-
====
45
-
Optional patterns in insert or delete stages will execute only if ALL the variables present in them are bound.
40
+
=== insert, update, & delete
41
+
Optional patterns in insert, update, or delete stages execute only if ALL the variables present in them are bound.
46
42
// Re-enable the tests when we do implement it
47
43
[,typeql]
48
44
----
@@ -56,18 +52,76 @@ insert
56
52
$policy links (covered: $person);
57
53
try { $policy links (covered: $spouse); };
58
54
----
59
-
If `$person` is in a marriage, the match stage will bind `$spouse`
60
-
and the insert stage will add them to the policy.
55
+
If `$person` is in a marriage, the match stage binds `$spouse` and the insert stage adds them to the policy.
56
+
57
+
=== put
58
+
Because a put stage only inserts data if no existing data matches the body of the put, the patterns in a `try` block of a put stage only attempt to insert data if the mandatory patterns (i.e. those
59
+
outside `try` blocks) fail to produce a match. When inserting data, put behaves identically to an insert stage in that a `try` block only executes if all referenced variables are bound.
60
+
61
+
[,typeql]
62
+
----
63
+
#!test[write, rollback]
64
+
match
65
+
$person isa person;
66
+
$spouse isa person;
67
+
try {
68
+
$policy isa policy, links (covered: $person);
69
+
};
70
+
put
71
+
$_ isa marriage, links (spouse: $person, spouse: $spouse);
72
+
try {
73
+
$policy links (covered: $spouse);
74
+
};
75
+
----
76
+
77
+
In this example, if the marriage relation already exists, the put stage does not attempt to add `$spouse` to the optionally bound `$policy`.
78
+
79
+
If this behavior is not desirable, the `try` block should be put in a separate insert stage instead.
80
+
81
+
[,typeql]
82
+
----
83
+
#!test[write, rollback]
84
+
match
85
+
$person isa person;
86
+
$spouse isa person;
87
+
try {
88
+
$policy isa policy, links (covered: $person);
89
+
not { $policy links (covered: $spouse); };
90
+
};
91
+
put
92
+
$_ isa marriage, links (spouse: $person, spouse: $spouse);
93
+
insert
94
+
try {
95
+
$policy links (covered: $spouse);
96
+
};
97
+
----
98
+
99
+
In this pipeline, the `marriage` relation is created if it does not already exist, and `$spouse` is always inserted as a roleplayer in `$policy` provided it is bound.
61
100
62
101
[NOTE]
63
102
====
64
-
Optional patterns are banned in `put` stages.
103
+
In particular, a put stage with only `try` blocks never inserts any data!
104
+
[,typeql]
105
+
----
106
+
#!test[write, rollback]
107
+
match
108
+
$_ isa marriage, links (spouse: $person, spouse: $spouse);
109
+
try {
110
+
$policy isa policy, links (covered: $person);
111
+
};
112
+
put
113
+
try {
114
+
$policy links (covered: $spouse);
115
+
};
116
+
----
117
+
118
+
In this example the `$policy` is bound only if `$person` plays a role in it, but if it doesn't also link `$spouse`, that link is not inserted.
65
119
====
66
120
67
121
== Behavior in match clauses
68
122
=== Single origin
69
123
In the stage that it first occurs, An optional variable may only occur in a single try-block.
70
-
[,typeql]
124
+
71
125
----
72
126
#!test[read, fail_at=runtime]
73
127
match
@@ -82,7 +136,7 @@ match
82
136
83
137
=== Optional variables in subsequent stages
84
138
An optional variable can be used in subsequent stages.
85
-
An unbound optional variable will cause the pattern to fail.
139
+
An unbound optional variable causes the pattern to fail.
86
140
87
141
The following is a valid way to express the intent of the example above:
0 commit comments