Mercurial > hg > ng2-query-ismi
annotate src/app/query.service.ts @ 48:f8d6f8479e77 ng2-final
first working version with angular 2.4 (using old table w/o pager).
author | casties |
---|---|
date | Mon, 20 Mar 2017 18:12:48 +0100 |
parents | b65a031c4967 |
children | 308c96f734c8 |
rev | line source |
---|---|
45 | 1 import {Injectable} from '@angular/core'; |
2 import {Http, Headers} from '@angular/http'; | |
1
59b7c3afcc6b
first interface and http request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
3 |
59b7c3afcc6b
first interface and http request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
4 import 'rxjs/Rx'; // import all RxJS operators |
22 | 5 //import 'rxjs/add/operator/map'; |
1
59b7c3afcc6b
first interface and http request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
6 |
41 | 7 import {NEO4J_BASE_URL, NEO4J_AUTHENTICATION} from './app-config'; |
34 | 8 import {QueryMode, QUERY_MODES, FIRST_QUERY_MODES} from './query-mode'; |
5
b06a5d61afed
new query state stuff.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
3
diff
changeset
|
9 import {QueryState} from './query-state'; |
b06a5d61afed
new query state stuff.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
3
diff
changeset
|
10 import {QueryStep} from './query-step'; |
30
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
25
diff
changeset
|
11 import {getResultType} from './result-type'; |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
25
diff
changeset
|
12 import {ISMI_RESULT_TYPES} from './ismi-result-types'; |
39 | 13 import {getRelationType} from './ismi-relation-types'; |
1
59b7c3afcc6b
first interface and http request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
14 |
59b7c3afcc6b
first interface and http request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
15 @Injectable() |
59b7c3afcc6b
first interface and http request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
16 export class QueryService { |
59b7c3afcc6b
first interface and http request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
17 |
32 | 18 public typeAttribute = '_type'; |
19 public excludedAttributes = {}; | |
7
6cd6c09032aa
object type query with results!
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
20 public state: QueryState; |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
21 public objectTypes: string[]; |
2 | 22 |
5
b06a5d61afed
new query state stuff.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
3
diff
changeset
|
23 constructor(private _http: Http) { |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
24 // init query state |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
25 this.state = new QueryState(); |
7
6cd6c09032aa
object type query with results!
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
26 } |
6cd6c09032aa
object type query with results!
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
27 |
8 | 28 setup() { |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
29 this.setupObjectTypes(); |
8 | 30 } |
31 | |
7
6cd6c09032aa
object type query with results!
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
32 getState() { |
6cd6c09032aa
object type query with results!
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
33 return this.state; |
5
b06a5d61afed
new query state stuff.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
3
diff
changeset
|
34 } |
1
59b7c3afcc6b
first interface and http request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
35 |
34 | 36 getQueryModes(index: number): QueryMode[] { |
37 if (index == 0) { | |
38 return FIRST_QUERY_MODES; | |
39 } else { | |
40 return QUERY_MODES; | |
41 } | |
1
59b7c3afcc6b
first interface and http request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
42 } |
59b7c3afcc6b
first interface and http request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
43 |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
44 /** |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
45 * return the first set of options for the given query mode. |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
46 */ |
3
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
47 getQueryOptions(queryMode: QueryMode) { |
47
b65a031c4967
first step to angular2-final (2.4) version of the query browser.
casties
parents:
45
diff
changeset
|
48 let options: any[] = []; |
18
65bb467abcc6
inverse relations are now generated on the fly.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
17
diff
changeset
|
49 if (queryMode == null) return options; |
11
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
50 if (queryMode.id === 'type_is') { |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
51 options = this.objectTypes; |
11
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
52 } else if (queryMode.id === 'relation_is') { |
21 | 53 options = this.state.resultRelations; |
11
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
54 } else if (queryMode.id === 'att_contains') { |
21 | 55 options = this.filterAttributes(this.state.resultAttributes); |
11
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
56 } else if (queryMode.id === 'att_contains_norm') { |
21 | 57 options = this.filterAttributes(this.state.resultAttributes, true); |
15
f84ff6781e57
added att_num_range query type.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
14
diff
changeset
|
58 } else if (queryMode.id === 'att_num_range') { |
21 | 59 options = this.filterAttributes(this.state.resultAttributes); |
1
59b7c3afcc6b
first interface and http request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
60 } |
5
b06a5d61afed
new query state stuff.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
3
diff
changeset
|
61 console.debug("getQueryOptions returns: ", options); |
b06a5d61afed
new query state stuff.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
3
diff
changeset
|
62 return options; |
1
59b7c3afcc6b
first interface and http request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
63 } |
59b7c3afcc6b
first interface and http request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
64 |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
65 /** |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
66 * fetch all object types from Neo4j and store in this.objectTypes. |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
67 */ |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
68 setupObjectTypes() { |
41 | 69 let query = `MATCH (n) WITH DISTINCT labels(n) AS labels |
3
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
70 UNWIND labels AS label |
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
71 RETURN DISTINCT label ORDER BY label`; |
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
72 |
41 | 73 let res = this.fetchCypherResults([query]); |
3
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
74 res.subscribe( |
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
75 data => { |
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
76 console.debug("neo4j data=", data); |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
77 this.objectTypes = data.results[0].data.map(elem => elem.row[0]).filter(elem => elem[0] != "_"); |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
78 console.debug("object types=", this.objectTypes); |
3
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
79 }, |
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
80 err => console.error("neo4j error=", err), |
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
81 () => console.debug('neo4j query Complete') |
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
82 ); |
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
83 } |
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
84 |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
85 /** |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
86 * Set the query step at index. |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
87 */ |
6 | 88 setQueryStep(index: number, step: QueryStep) { |
7
6cd6c09032aa
object type query with results!
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
89 this.state.steps[index] = step; |
6 | 90 } |
91 | |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
92 /** |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
93 * Create the cypher queries for the current query state. |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
94 * |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
95 * Updates the queries for results, attributes and relations. |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
96 */ |
6 | 97 createCypherQuery() { |
41 | 98 let queryMatch = ''; |
99 let queryWhere = ''; | |
100 let queryReturn = ''; | |
101 let queryParams = {}; | |
102 let resultQuery = ''; | |
103 let attributesQuery = ''; | |
104 let outRelsQuery = ''; | |
105 let inRelsQuery = ''; | |
106 let returnType = ''; | |
107 let nIdx = 1; | |
11
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
108 this.state.steps.forEach((step, stepIdx) => { |
41 | 109 let mode = step.mode.id; |
110 let params = step.params; | |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
111 |
14
7dc7ea95ca26
show result types below query steps.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
13
diff
changeset
|
112 /* |
7dc7ea95ca26
show result types below query steps.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
13
diff
changeset
|
113 * step: object type is |
7dc7ea95ca26
show result types below query steps.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
13
diff
changeset
|
114 */ |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
115 if (mode === 'type_is') { |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
116 queryMatch = `MATCH (n${nIdx}:${params.objectType})`; |
10
66dce99cef4e
attribute contains works now.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
9
diff
changeset
|
117 queryWhere = ''; |
11
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
118 queryReturn = `RETURN n${nIdx}`; |
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
119 returnType = 'node'; |
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
120 } |
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
121 |
14
7dc7ea95ca26
show result types below query steps.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
13
diff
changeset
|
122 /* |
32 | 123 * step: object id is |
124 */ | |
125 if (mode === 'id_is') { | |
126 if (!queryMatch) { | |
127 // first step - use match clause | |
128 queryMatch = `MATCH (n${nIdx} {ismi_id: {att_val${stepIdx}}})`; | |
129 queryParams[`att_val${stepIdx}`] = parseInt(params.value, 10); | |
130 queryWhere = ''; | |
131 queryReturn = `RETURN n${nIdx}`; | |
132 returnType = 'node'; | |
133 } else { | |
134 // use where clause | |
135 if (!queryWhere) { | |
136 queryWhere = 'WHERE '; | |
137 } else { | |
138 queryWhere += ' AND '; | |
139 } | |
140 queryWhere += `n${nIdx}.ismi_id = {att_val${stepIdx}}`; | |
141 queryParams[`att_val${stepIdx}`] = parseInt(params.value, 10); | |
142 } | |
143 } | |
144 | |
145 /* | |
14
7dc7ea95ca26
show result types below query steps.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
13
diff
changeset
|
146 * step: relation type is |
7dc7ea95ca26
show result types below query steps.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
13
diff
changeset
|
147 */ |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
148 if (mode === 'relation_is') { |
11
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
149 nIdx += 1; |
39 | 150 let rel = params.relationType; |
151 if (rel.isOutgoing()) { | |
40 | 152 queryMatch += `-[:\`${rel.getRelType()}\`]->(n${nIdx})`; |
39 | 153 } else { |
18
65bb467abcc6
inverse relations are now generated on the fly.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
17
diff
changeset
|
154 // inverse relation |
40 | 155 queryMatch += `<-[:\`${rel.getRelType()}\`]-(n${nIdx})`; |
18
65bb467abcc6
inverse relations are now generated on the fly.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
17
diff
changeset
|
156 } |
39 | 157 queryReturn = `RETURN DISTINCT n${nIdx}`; |
8 | 158 returnType = 'node'; |
159 } | |
10
66dce99cef4e
attribute contains works now.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
9
diff
changeset
|
160 |
14
7dc7ea95ca26
show result types below query steps.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
13
diff
changeset
|
161 /* |
7dc7ea95ca26
show result types below query steps.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
13
diff
changeset
|
162 * step: attribute contains(_norm) |
7dc7ea95ca26
show result types below query steps.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
13
diff
changeset
|
163 */ |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
164 if (mode === 'att_contains' || mode === 'att_contains_norm') { |
11
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
165 if (!queryWhere) { |
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
166 queryWhere = 'WHERE '; |
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
167 } else { |
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
168 queryWhere += ' AND '; |
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
169 } |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
170 if (params.attribute === 'ismi_id') { |
14
7dc7ea95ca26
show result types below query steps.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
13
diff
changeset
|
171 // ismi_id is integer |
7dc7ea95ca26
show result types below query steps.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
13
diff
changeset
|
172 queryWhere += `n${nIdx}.ismi_id = {att_val${stepIdx}}`; |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
173 queryParams[`att_val${stepIdx}`] = parseInt(params.value, 10); |
11
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
174 } else { |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
175 if (mode === 'att_contains_norm') { |
14
7dc7ea95ca26
show result types below query steps.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
13
diff
changeset
|
176 // match _n_attribute with normValue |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
177 queryWhere += `lower(n${nIdx}._n_${params.attribute}) CONTAINS lower({att_val${stepIdx}})`; |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
178 queryParams[`att_val${stepIdx}`] = params.normValue; |
14
7dc7ea95ca26
show result types below query steps.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
13
diff
changeset
|
179 } else { |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
180 queryWhere += `lower(n${nIdx}.${params.attribute}) CONTAINS lower({att_val${stepIdx}})`; |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
181 queryParams[`att_val${stepIdx}`] = params.value; |
14
7dc7ea95ca26
show result types below query steps.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
13
diff
changeset
|
182 } |
11
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
183 } |
10
66dce99cef4e
attribute contains works now.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
9
diff
changeset
|
184 } |
66dce99cef4e
attribute contains works now.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
9
diff
changeset
|
185 |
15
f84ff6781e57
added att_num_range query type.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
14
diff
changeset
|
186 /* |
f84ff6781e57
added att_num_range query type.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
14
diff
changeset
|
187 * step: attribute number range |
f84ff6781e57
added att_num_range query type.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
14
diff
changeset
|
188 */ |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
189 if (mode === 'att_num_range') { |
15
f84ff6781e57
added att_num_range query type.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
14
diff
changeset
|
190 if (!queryWhere) { |
f84ff6781e57
added att_num_range query type.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
14
diff
changeset
|
191 queryWhere = 'WHERE '; |
f84ff6781e57
added att_num_range query type.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
14
diff
changeset
|
192 } else { |
f84ff6781e57
added att_num_range query type.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
14
diff
changeset
|
193 queryWhere += ' AND '; |
f84ff6781e57
added att_num_range query type.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
14
diff
changeset
|
194 } |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
195 queryWhere += `toint(n${nIdx}.${params.attribute}) >= toint({att_nlo${stepIdx}})` |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
196 + ` AND toint(n${nIdx}.${params.attribute}) <= toint({att_nhi${stepIdx}})`; |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
197 queryParams[`att_nlo${stepIdx}`] = params.numLo; |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
198 queryParams[`att_nhi${stepIdx}`] = params.numHi; |
15
f84ff6781e57
added att_num_range query type.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
14
diff
changeset
|
199 } |
f84ff6781e57
added att_num_range query type.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
14
diff
changeset
|
200 |
9 | 201 }); |
18
65bb467abcc6
inverse relations are now generated on the fly.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
17
diff
changeset
|
202 // compose query |
32 | 203 resultQuery = queryMatch + (queryWhere ? '\n'+queryWhere : '') + '\n' + queryReturn; |
18
65bb467abcc6
inverse relations are now generated on the fly.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
17
diff
changeset
|
204 // compose query for attributes of result |
15
f84ff6781e57
added att_num_range query type.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
14
diff
changeset
|
205 attributesQuery = queryMatch + ' ' + queryWhere + ` WITH DISTINCT keys(n${nIdx}) AS atts` |
f84ff6781e57
added att_num_range query type.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
14
diff
changeset
|
206 + ` UNWIND atts AS att RETURN DISTINCT att ORDER BY att`; |
18
65bb467abcc6
inverse relations are now generated on the fly.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
17
diff
changeset
|
207 // compose query for relations of result |
36
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
208 outRelsQuery = queryMatch + '-[r]->() ' + queryWhere + ' RETURN DISTINCT type(r)'; |
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
209 inRelsQuery = queryMatch + '<-[r]-() ' + queryWhere + ' RETURN DISTINCT type(r)'; |
10
66dce99cef4e
attribute contains works now.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
9
diff
changeset
|
210 this.state.resultCypherQuery = resultQuery; |
11
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
211 this.state.cypherQueryParams = queryParams; |
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
212 this.state.attributesCypherQuery = attributesQuery; |
36
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
213 this.state.outRelsCypherQuery = outRelsQuery; |
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
214 this.state.inRelsCypherQuery = inRelsQuery; |
7
6cd6c09032aa
object type query with results!
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
215 this.state.resultTypes = returnType; |
6cd6c09032aa
object type query with results!
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
216 } |
6cd6c09032aa
object type query with results!
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
217 |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
218 /** |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
219 * Create and run the cypher queries for the current query state. |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
220 * |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
221 * Updates the results and nextQuery attributes and relations. |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
222 */ |
40 | 223 runQuery() { |
7
6cd6c09032aa
object type query with results!
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
224 this.createCypherQuery(); |
13
98b435bb6c0c
more query work.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
12
diff
changeset
|
225 this.state.resultInfo = 'loading...'; |
16
7d82ca32833c
omit some attributes from list.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
15
diff
changeset
|
226 /* |
7d82ca32833c
omit some attributes from list.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
15
diff
changeset
|
227 * run query for result table |
7d82ca32833c
omit some attributes from list.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
15
diff
changeset
|
228 */ |
41 | 229 let queries = [this.state.resultCypherQuery]; |
230 let params = [this.state.cypherQueryParams]; | |
17
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
231 if (this.state.attributesCypherQuery) { |
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
232 queries.push(this.state.attributesCypherQuery); |
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
233 params.push(this.state.cypherQueryParams); |
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
234 } |
36
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
235 if (this.state.outRelsCypherQuery) { |
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
236 queries.push(this.state.outRelsCypherQuery); |
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
237 params.push(this.state.cypherQueryParams); |
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
238 } |
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
239 if (this.state.inRelsCypherQuery) { |
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
240 queries.push(this.state.inRelsCypherQuery); |
17
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
241 params.push(this.state.cypherQueryParams); |
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
242 } |
41 | 243 let res = this.fetchCypherResults(queries, params); |
17
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
244 res.subscribe( |
7
6cd6c09032aa
object type query with results!
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
245 data => { |
10
66dce99cef4e
attribute contains works now.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
9
diff
changeset
|
246 console.debug("neo4j result data=", data); |
41 | 247 let resIdx = 0; |
17
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
248 /* |
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
249 * results for result table |
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
250 */ |
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
251 this.state.results = data.results[resIdx].data.map(elem => elem.row[0]); |
7
6cd6c09032aa
object type query with results!
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
252 this.state.numResults = this.state.results.length; |
11
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
253 // count all types |
32 | 254 let resTypes = {}; |
11
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
255 this.state.results.forEach((r) => { |
32 | 256 let t = r[this.typeAttribute]; |
257 if (resTypes[t] == null) { | |
258 resTypes[t] = 1; | |
11
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
259 } else { |
32 | 260 resTypes[t] += 1; |
11
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
261 } |
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
262 }); |
32 | 263 let info = ''; |
41 | 264 for (let t in resTypes) { |
11
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
265 info += t + '(' + resTypes[t] + ') '; |
6989cd00e8d7
relations work now as well as longer queries.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
10
diff
changeset
|
266 } |
14
7dc7ea95ca26
show result types below query steps.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
13
diff
changeset
|
267 info = info.substr(0, info.length-1); |
7dc7ea95ca26
show result types below query steps.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
13
diff
changeset
|
268 this.state.resultInfo = info; |
7dc7ea95ca26
show result types below query steps.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
13
diff
changeset
|
269 // save info also in last step |
7dc7ea95ca26
show result types below query steps.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
13
diff
changeset
|
270 this.state.steps[this.state.steps.length-1].resultInfo = info; |
17
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
271 /* |
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
272 * results for attribute list |
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
273 */ |
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
274 if (this.state.attributesCypherQuery) { |
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
275 resIdx += 1; |
30
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
25
diff
changeset
|
276 let atts = data.results[resIdx].data.map(elem => elem.row[0]); |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
25
diff
changeset
|
277 this.state.resultAttributes = atts; |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
25
diff
changeset
|
278 // the following assumes only one type in the result |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
25
diff
changeset
|
279 for (let t in resTypes) { |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
25
diff
changeset
|
280 this.state.resultType = getResultType(t, ISMI_RESULT_TYPES); |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
25
diff
changeset
|
281 break; |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
25
diff
changeset
|
282 } |
193271b6b9d2
configure attributes per result type. select number of items per result page.
casties
parents:
25
diff
changeset
|
283 this.state.resultColumns = this.state.resultType.getColumns(atts); |
17
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
284 } |
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
285 /* |
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
286 * results for relations list |
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
287 */ |
36
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
288 if (this.state.outRelsCypherQuery) { |
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
289 // outgoing aka forward relations |
17
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
290 resIdx += 1; |
36
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
291 let rels = data.results[resIdx].data.map(elem => elem.row[0]) |
39 | 292 .filter(elem => elem[0] != "_") |
293 .map(elem => getRelationType(elem, true)); | |
36
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
294 this.state.resultRelations = rels; |
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
295 } |
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
296 if (this.state.inRelsCypherQuery) { |
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
297 // incoming aka reverse relations |
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
298 resIdx += 1; |
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
299 let rels = data.results[resIdx].data.map(elem => elem.row[0]) |
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
300 .filter(elem => elem[0] != "_") |
39 | 301 .map(elem => getRelationType(elem, false)); |
36
e8dc6a4c6773
only show possible incoming/outgoing relation types.
casties
parents:
35
diff
changeset
|
302 this.state.resultRelations = this.state.resultRelations.concat(rels); |
17
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
303 } |
7
6cd6c09032aa
object type query with results!
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
304 }, |
10
66dce99cef4e
attribute contains works now.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
9
diff
changeset
|
305 err => console.error("neo4j result error=", err), |
66dce99cef4e
attribute contains works now.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
9
diff
changeset
|
306 () => console.debug('neo4j result query Complete') |
7
6cd6c09032aa
object type query with results!
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
6
diff
changeset
|
307 ); |
6 | 308 } |
309 | |
21 | 310 |
311 filterAttributes(attributes: string[], normalized=false) { | |
41 | 312 let atts = []; |
21 | 313 if (normalized) { |
314 attributes.forEach((att) => { | |
315 if (att.substr(0, 3) == "_n_") { | |
316 atts.push(att.substr(3)); | |
317 } | |
318 }); | |
319 } else { | |
320 atts = attributes.filter(elem => elem[0] != "_" && !this.excludedAttributes[elem]); | |
321 } | |
322 return atts; | |
323 } | |
324 | |
325 /** | |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
326 * Run the given queries on the Neo4J server. |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
327 * |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
328 * Returns an Observable with the results. |
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
329 */ |
17
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
330 fetchCypherResults(queries: string[], params=[{}]) { |
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
331 console.debug("fetching cypher queries: ", queries); |
41 | 332 let headers = new Headers(); |
333 let auth = NEO4J_AUTHENTICATION; | |
20
34cd764e234b
make interfaces into classes. factor out NormalizationService.
casties
parents:
19
diff
changeset
|
334 headers.append('Authorization', 'Basic ' + btoa(`${auth.user}:${auth.password}`)); |
1
59b7c3afcc6b
first interface and http request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
335 headers.append('Content-Type', 'application/json'); |
59b7c3afcc6b
first interface and http request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
336 headers.append('Accept', 'application/json'); |
3
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
337 // put headers in options |
41 | 338 let opts = {'headers': headers}; |
17
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
339 // unpack queries into statements |
41 | 340 let statements = queries.map((q, i) => { |
17
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
341 return {'statement': q, 'parameters': (params[i])?params[i]:{}}; |
f6af2c8347de
send multiple cypher queries in one request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
16
diff
changeset
|
342 }); |
3
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
343 // create POST data from query |
41 | 344 let data = JSON.stringify({'statements': statements}); |
3
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
345 // make post request asynchronously |
41 | 346 let resp = this._http.post(NEO4J_BASE_URL+'/transaction/commit', data, opts) |
3
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
347 // filter result as JSON |
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
348 .map(res => res.json()); |
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
349 // return Observable |
c741a00d38de
first list of object types :-)
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
2
diff
changeset
|
350 return resp; |
1
59b7c3afcc6b
first interface and http request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
351 } |
13
98b435bb6c0c
more query work.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
12
diff
changeset
|
352 |
1
59b7c3afcc6b
first interface and http request.
Robert Casties <casties@mpiwg-berlin.mpg.de>
parents:
diff
changeset
|
353 } |