1
1
import { SyntaxNode , Tree , TreeCursor } from "@lezer/common" ;
2
2
import { CompletionContext } from "@codemirror/autocomplete" ;
3
3
import * as tokens from "./generated/typeql.grammar.generated.terms" ;
4
-
4
+ import { TypeDBHttpDriver } from "../typedb-driver" ;
5
+ import { finalize } from "rxjs" ;
6
+ import { DriverState } from "../../service/driver-state.service" ;
7
+ import { ConceptRow , ConceptRowsQueryResponse } from "../typedb-driver/response" ;
8
+ import { Concept , EntityType , RelationType , Type } from "../typedb-driver/concept" ;
9
+ type ConceptRowAnswer = { involvedBlocks : number [ ] ; data : ConceptRow } ;
5
10
type TypeLabel = string ;
6
- type AttributeType = { } ;
7
-
8
- interface ObjectType {
11
+ type AttributeTypeEntry = { } ;
12
+ interface ObjectTypeEntry {
9
13
owns : TypeLabel [ ] ;
10
14
plays : TypeLabel [ ] ;
11
15
relates : TypeLabel [ ] ;
@@ -40,15 +44,15 @@ export class TypeQLAutocompleteSchema {
40
44
return Object . keys ( this . fromDB . objectTypes ) . concat ( Object . keys ( this . fromEditor . objectTypes ) ) ;
41
45
}
42
46
43
- attributeType ( type : TypeLabel ) : AttributeType {
47
+ attributeType ( type : TypeLabel ) : AttributeTypeEntry {
44
48
if ( this . fromDB . attributes [ type ] ) {
45
49
return this . fromDB . attributes [ type ] ;
46
50
} else {
47
51
return this . fromEditor . attributes [ type ] ;
48
52
}
49
53
}
50
54
51
- objectType ( type : TypeLabel ) : ObjectType {
55
+ objectType ( type : TypeLabel ) : ObjectTypeEntry {
52
56
if ( this . fromDB . objectTypes [ type ] ) {
53
57
return this . fromDB . objectTypes [ type ] ;
54
58
} else {
@@ -72,16 +76,16 @@ export class TypeQLAutocompleteSchema {
72
76
}
73
77
74
78
export class TypeQLAutocompleteSchemaImpl {
75
- objectTypes : Record < TypeLabel , ObjectType > ;
76
- attributes : Record < TypeLabel , AttributeType > ;
79
+ objectTypes : Record < TypeLabel , ObjectTypeEntry > ;
80
+ attributes : Record < TypeLabel , AttributeTypeEntry > ;
77
81
constructor (
78
- objectTypes : Record < TypeLabel , ObjectType > ,
79
- attributes : Record < TypeLabel , AttributeType > ,
82
+ objectTypes : Record < TypeLabel , ObjectTypeEntry > ,
83
+ attributes : Record < TypeLabel , AttributeTypeEntry > ,
80
84
) {
81
85
this . attributes = attributes ;
82
86
this . objectTypes = objectTypes ;
83
87
}
84
-
88
+
85
89
static fromTypeQL ( text : string , tree : Tree ) : TypeQLAutocompleteSchemaImpl {
86
90
let builder = new SchemaBuilder ( ) ;
87
91
// TODO: Replace iterate with a more targetted traversal that considers only define queries.
@@ -141,25 +145,76 @@ export class TypeQLAutocompleteSchemaImpl {
141
145
} ) ;
142
146
return builder . build ( ) ;
143
147
}
148
+
149
+ static fromDriver ( driver : DriverState , database : string ) : TypeQLAutocompleteSchemaImpl | null {
150
+ function runQuery ( driver : DriverState , database : string , query : string ) : ConceptRowAnswer [ ] {
151
+ // todo: Help please alex
152
+ return [
153
+ {
154
+ involvedBlocks : [ 0 ] ,
155
+ data : { // Unioning them all makes it pass
156
+ [ "owner" ] : { kind : "entityType" , label : "person" } ,
157
+ [ "owned" ] : { kind : "attributeType" , label : "name" , valueType : "string" } ,
158
+ [ "relation" ] : { kind : "relationType" , label : "friendship" } ,
159
+ [ "related" ] : { kind : "roleType" , label : "friend" } ,
160
+ [ "player" ] : { kind : "entityType" , label : "person" } ,
161
+ [ "played" ] : { kind : "roleType" , label : "friend" } ,
162
+ }
163
+ }
164
+ ] ;
165
+ }
166
+ let ownsQuery = "match $owner owns $owned;"
167
+ let relatesQuery = "match $relation relates $related;" ;
168
+ let playsQuery = "match $player plays $played" ;
169
+
170
+ let ownsAnswers = runQuery ( driver , database , ownsQuery ) ; // TODO:
171
+ let relatesAnswers = runQuery ( driver , database , relatesQuery ) ;
172
+ let playsAnswers = runQuery ( driver , database , playsQuery ) ;
173
+
174
+ let builder = new SchemaBuilder ( ) ;
175
+ ownsAnswers . forEach ( ( answer ) => {
176
+ let data : ConceptRow = answer . data ;
177
+ let owner = ( data [ "owner" ] as Type ) . label ;
178
+ let owned = ( data [ "owned" ] as Type ) . label ;
179
+ builder . objectType ( owner ) ;
180
+ builder . attributeType ( owned ) ;
181
+ builder . recordOwns ( owner , owned ) ;
182
+ } ) ;
183
+ relatesAnswers . forEach ( ( answer ) => {
184
+ let data : ConceptRow = answer . data ;
185
+ let relation = ( data [ "relation" ] as Type ) . label ;
186
+ let related = ( data [ "related" ] as Type ) . label ;
187
+ builder . objectType ( relation ) ;
188
+ builder . recordRelates ( relation , related )
189
+ } ) ;
190
+ playsAnswers . forEach ( ( answer ) => {
191
+ let data : ConceptRow = answer . data ;
192
+ let player = ( data [ "player" ] as Type ) . label ;
193
+ let played = ( data [ "played" ] as Type ) . label ;
194
+ builder . objectType ( player ) ;
195
+ builder . recordRelates ( player , played )
196
+ } )
197
+ return builder . build ( ) ;
198
+ }
144
199
}
145
200
146
201
class SchemaBuilder {
147
- objectTypes : Record < TypeLabel , ObjectType > ;
148
- attributes : Record < TypeLabel , AttributeType > ;
202
+ objectTypes : Record < TypeLabel , ObjectTypeEntry > ;
203
+ attributes : Record < TypeLabel , AttributeTypeEntry > ;
149
204
150
205
constructor ( ) {
151
206
this . objectTypes = { } ;
152
207
this . attributes = { } ;
153
208
}
154
209
155
- attributeType ( type : TypeLabel ) : AttributeType {
210
+ attributeType ( type : TypeLabel ) : AttributeTypeEntry {
156
211
if ( ! this . attributes [ type ] ) {
157
212
this . attributes [ type ] = { owners : [ ] } ;
158
213
}
159
214
return this . attributes [ type ] ;
160
215
}
161
216
162
- objectType ( type : TypeLabel ) : ObjectType {
217
+ objectType ( type : TypeLabel ) : ObjectTypeEntry {
163
218
if ( ! this . objectTypes [ type ] ) {
164
219
this . objectTypes [ type ] = { owns : [ ] , plays : [ ] , relates : [ ] } ;
165
220
}
0 commit comments