Mercurial > hg > ng2-query-ismi
diff app/query.service.ts @ 20:34cd764e234b
make interfaces into classes. factor out NormalizationService.
author | casties |
---|---|
date | Fri, 22 Jan 2016 17:32:33 +0100 |
parents | d75224bb8147 |
children | 930fe7460f6b |
line wrap: on
line diff
--- a/app/query.service.ts Thu Jan 21 20:22:02 2016 +0100 +++ b/app/query.service.ts Fri Jan 22 17:32:33 2016 +0100 @@ -12,29 +12,19 @@ //public neo4jBaseUrl = 'https://ismi-dev.mpiwg-berlin.mpg.de/neo4j-ismi/db/data'; public neo4jBaseUrl = 'http://localhost:7474/db/data'; - public openMindBaseUrl = 'https://ismi-dev.mpiwg-berlin.mpg.de/om4-ismi/'; - //public openMindBaseUrl = 'http://localhost:18080/ismi-richfaces/'; + public neo4jAuthentication = {'user': 'neo4j', 'password': 'neo5j'}; public excludedAttributes = {'type': true}; public invRelPrefix = '<- '; public state: QueryState; - public ismiObjectTypes: any; + public objectTypes: string[]; constructor(private _http: Http) { - this.state = { - 'steps': [], - 'resultCypherQuery': '', - 'cypherQueryParams': {}, - 'attributesCypherQuery': '', - 'relationsCypherQuery': '', - 'results': [], - 'resultTypes': '', - 'numResults': 0, - 'resultInfo': '' - }; + // init query state + this.state = new QueryState(); } setup() { - this.setupIsmiObjectTypes(); + this.setupObjectTypes(); } getState() { @@ -45,11 +35,14 @@ return QUERY_MODES; } + /** + * return the first set of options for the given query mode. + */ getQueryOptions(queryMode: QueryMode) { var options = []; if (queryMode == null) return options; if (queryMode.id === 'type_is') { - options = this.ismiObjectTypes; + options = this.objectTypes; } else if (queryMode.id === 'relation_is') { options = this.state.nextQueryRelations; } else if (queryMode.id === 'att_contains') { @@ -63,7 +56,10 @@ return options; } - setupIsmiObjectTypes() { + /** + * fetch all object types from Neo4j and store in this.objectTypes. + */ + setupObjectTypes() { var query = `MATCH (n) WITH DISTINCT labels(n) AS labels UNWIND labels AS label RETURN DISTINCT label ORDER BY label`; @@ -72,19 +68,26 @@ res.subscribe( data => { console.debug("neo4j data=", data); - this.ismiObjectTypes = data.results[0].data.map(elem => elem.row[0]).filter(elem => elem[0] != "_"); - console.debug("ismi types=", this.ismiObjectTypes); + this.objectTypes = data.results[0].data.map(elem => elem.row[0]).filter(elem => elem[0] != "_"); + console.debug("object types=", this.objectTypes); }, err => console.error("neo4j error=", err), () => console.debug('neo4j query Complete') ); } + /** + * Set the query step at index. + */ setQueryStep(index: number, step: QueryStep) { this.state.steps[index] = step; - this.createCypherQuery(); } + /** + * Create the cypher queries for the current query state. + * + * Updates the queries for results, attributes and relations. + */ createCypherQuery() { var queryMatch = ''; var queryWhere = ''; @@ -96,11 +99,14 @@ var returnType = ''; var nIdx = 1; this.state.steps.forEach((step, stepIdx) => { + var mode = step.mode.id; + var params = step.params; + /* * step: object type is */ - if (step.mode.id === 'type_is') { - queryMatch = `MATCH (n${nIdx}:${step.objectType})`; + if (mode === 'type_is') { + queryMatch = `MATCH (n${nIdx}:${params.objectType})`; queryWhere = ''; queryReturn = `RETURN n${nIdx}`; returnType = 'node'; @@ -109,9 +115,9 @@ /* * step: relation type is */ - if (step.mode.id === 'relation_is') { + if (mode === 'relation_is') { nIdx += 1; - var rel = step.relationType; + var rel = params.relationType; if (rel.indexOf(this.invRelPrefix) == 0) { // inverse relation rel = rel.substr(this.invRelPrefix.length); @@ -126,24 +132,24 @@ /* * step: attribute contains(_norm) */ - if (step.mode.id === 'att_contains' || step.mode.id === 'att_contains_norm') { + if (mode === 'att_contains' || mode === 'att_contains_norm') { if (!queryWhere) { queryWhere = 'WHERE '; } else { queryWhere += ' AND '; } - if (step.attribute === 'ismi_id') { + if (params.attribute === 'ismi_id') { // ismi_id is integer queryWhere += `n${nIdx}.ismi_id = {att_val${stepIdx}}`; - queryParams[`att_val${stepIdx}`] = parseInt(step.value, 10); + queryParams[`att_val${stepIdx}`] = parseInt(params.value, 10); } else { - if (step.mode.id === 'att_contains_norm') { + if (mode === 'att_contains_norm') { // match _n_attribute with normValue - queryWhere += `lower(n${nIdx}._n_${step.attribute}) CONTAINS lower({att_val${stepIdx}})`; - queryParams[`att_val${stepIdx}`] = step.normValue; + queryWhere += `lower(n${nIdx}._n_${params.attribute}) CONTAINS lower({att_val${stepIdx}})`; + queryParams[`att_val${stepIdx}`] = params.normValue; } else { - queryWhere += `lower(n${nIdx}.${step.attribute}) CONTAINS lower({att_val${stepIdx}})`; - queryParams[`att_val${stepIdx}`] = step.value; + queryWhere += `lower(n${nIdx}.${params.attribute}) CONTAINS lower({att_val${stepIdx}})`; + queryParams[`att_val${stepIdx}`] = params.value; } } } @@ -151,16 +157,16 @@ /* * step: attribute number range */ - if (step.mode.id === 'att_num_range') { + if (mode === 'att_num_range') { if (!queryWhere) { queryWhere = 'WHERE '; } else { queryWhere += ' AND '; } - queryWhere += `toint(n${nIdx}.${step.attribute}) >= toint({att_nlo${stepIdx}})` - + ` AND toint(n${nIdx}.${step.attribute}) <= toint({att_nhi${stepIdx}})`; - queryParams[`att_nlo${stepIdx}`] = step.numLo; - queryParams[`att_nhi${stepIdx}`] = step.numHi; + queryWhere += `toint(n${nIdx}.${params.attribute}) >= toint({att_nlo${stepIdx}})` + + ` AND toint(n${nIdx}.${params.attribute}) <= toint({att_nhi${stepIdx}})`; + queryParams[`att_nlo${stepIdx}`] = params.numLo; + queryParams[`att_nhi${stepIdx}`] = params.numHi; } }); @@ -178,6 +184,11 @@ this.state.resultTypes = returnType; } + /** + * Create and run the cypher queries for the current query state. + * + * Updates the results and nextQuery attributes and relations. + */ updateQuery() { this.createCypherQuery(); this.state.resultInfo = 'loading...'; @@ -246,10 +257,16 @@ ); } + /** + * Run the given queries on the Neo4J server. + * + * Returns an Observable with the results. + */ fetchCypherResults(queries: string[], params=[{}]) { console.debug("fetching cypher queries: ", queries); var headers = new Headers(); - headers.append('Authorization', 'Basic ' + btoa('neo4j' + ':' + 'neo5j')); + var auth = this.neo4jAuthentication; + headers.append('Authorization', 'Basic ' + btoa(`${auth.user}:${auth.password}`)); headers.append('Content-Type', 'application/json'); headers.append('Accept', 'application/json'); // put headers in options @@ -268,19 +285,4 @@ return resp; } - fetchNormalizedString(text: string) { - console.debug("fetching normalized string: ", text); - var headers = new Headers(); - headers.append('Accept', 'application/json'); - // put headers in options - var opts = {'headers': headers}; - // make get request asynchronously - var url = this.openMindBaseUrl+'jsonInterface?method=normalize_string&type=arabic_translit&text='; - url += text; - var resp = this._http.get(url, opts) - // filter result as JSON - .map(res => res.json()); - // return Observable - return resp; - } } \ No newline at end of file