Skip to content

Commit

Permalink
Merge pull request #125 from biothings/property-warning
Browse files Browse the repository at this point in the history
Check Qnode/Qedge properties
  • Loading branch information
tokebe authored Jan 6, 2023
2 parents 98d9f10 + ab76c46 commit e65e2a2
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
2 changes: 1 addition & 1 deletion __test__/integration/integrity.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('Testing TRAPIQueryHandler Module', () => {
queryHandler.setQueryGraph(query.message.query_graph);
await queryHandler.query();
const res = queryHandler.getResponse();
console.log(res);
// console.log(res);
expect(res.message.knowledge_graph.nodes).toHaveProperty('NCBIGene:111');
});

Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,13 @@ exports.TRAPIQueryHandler = class TRAPIQueryHandler {
let expanded = Object.values(getDescendants(queryGraph.nodes[nodeId].ids)).flat();
console.log(expanded.length);
expanded = _.uniq([...queryGraph.nodes[nodeId].ids, ...expanded]);

let log_msg = `Expanded ids for node ${nodeId}: (${queryGraph.nodes[nodeId].ids.length} ids -> ${expanded.length} ids)`;
debug(log_msg);
this.logs.push(new LogEntry('INFO', null, log_msg).getLog());

queryGraph.nodes[nodeId].ids = expanded;

//make sure is_set is true
if (!queryGraph.nodes[nodeId].hasOwnProperty('is_set') || !queryGraph.nodes[nodeId].is_set) {
queryGraph.nodes[nodeId].is_set = true;
Expand All @@ -120,7 +120,7 @@ exports.TRAPIQueryHandler = class TRAPIQueryHandler {
*/
async _processQueryGraph(queryGraph) {
try {
let queryGraphHandler = new QueryGraph(queryGraph);
let queryGraphHandler = new QueryGraph(queryGraph, this.options.schema);
let queryEdges = await queryGraphHandler.calculateEdges();
this.logs = [...this.logs, ...queryGraphHandler.logs];
return queryEdges;
Expand Down
55 changes: 54 additions & 1 deletion src/query_graph.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ const _ = require('lodash');
const utils = require('./utils');

module.exports = class QueryGraphHandler {
constructor(queryGraph) {
constructor(queryGraph, schema) {
this.queryGraph = queryGraph;
this.schema = schema;
this.logs = [];
}

Expand Down Expand Up @@ -93,6 +94,56 @@ module.exports = class QueryGraphHandler {
}
}

_validateNodeProperties(queryGraph) {
const schemProps = this.schema?.components?.schemas?.QNode?.properties ? this.schema.components.schemas.QNode.properties : {};
const nodeProperties = new Set(Object.keys(schemProps));
const badProperties = new Set();
const badNodes = new Set();
for (const nodeID in queryGraph.nodes) {
for (const property in queryGraph.nodes[nodeID]) {
if (!nodeProperties.has(property)) {
badProperties.add(property);
badNodes.add(nodeID);
}
}
}

if (badProperties.size !== 0) {
this.logs.push(
new LogEntry(
'WARNING',
null,
`Ignoring unrecognized properties (${[...badProperties].join(',')}) on nodes (${[...badNodes].join(',')}).`,
).getLog()
);
}
}

_validateEdgeProperties(queryGraph) {
const schemProps = this.schema?.components?.schemas?.QEdge?.properties ? this.schema.components.schemas.QEdge.properties : {};
const edgeProperties = new Set(Object.keys(schemProps));
const badProperties = new Set();
const badEdges = new Set();
for (const edgeID in queryGraph.edges) {
for (const property in queryGraph.edges[edgeID]) {
if (!edgeProperties.has(property)) {
badProperties.add(property);
badEdges.add(edgeID);
}
}
}

if (badProperties.size !== 0) {
this.logs.push(
new LogEntry(
'WARNING',
null,
`Ignoring unrecognized properties (${[...badProperties].join(',')}) on edges (${[...badEdges].join(',')}).`,
).getLog()
);
}
}

_validateNoDuplicateQualifierTypes(queryGraph) {
Object.entries(queryGraph.edges).forEach(([id, edge]) => {
if (edge.qualifier_constraints) {
Expand All @@ -117,6 +168,8 @@ module.exports = class QueryGraphHandler {
this._validateOneNodeID(queryGraph);
this._validateNodeEdgeCorrespondence(queryGraph);
this._validateDuplicateEdges(queryGraph);
this._validateNodeProperties(queryGraph);
this._validateEdgeProperties(queryGraph);
this._validateCycles(queryGraph);
this._validateNoDuplicateQualifierTypes(queryGraph);
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
"esModuleInterop": true,
"resolveJsonModule": true,
"strict": false,
"noImplicitAny": false
"noImplicitAny": false,
"skipLibCheck": true
},
"include": [
"./src/**/*",
Expand Down

0 comments on commit e65e2a2

Please sign in to comment.