Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/biothings/smartapi-kg.js in…
Browse files Browse the repository at this point in the history
…to dev
  • Loading branch information
tokebe committed Nov 20, 2024
2 parents a86f304 + 23e7e21 commit 82d903d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 13 deletions.
52 changes: 41 additions & 11 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ export default class MetaKG {
/**
* constructor to build meta knowledge graph from SmartAPI Specifications
*/
constructor(path: string = undefined, predicates_path: string = undefined, ops: SmartAPIKGOperationObject[] = []) {
constructor(
path: string = undefined,
predicates_path: string = undefined,
ops: SmartAPIKGOperationObject[] = [],
) {
// store all meta-kg operations
ops.forEach(op => {
op.query_operation = QueryOperationObject.unfreeze(op.query_operation);
Expand Down Expand Up @@ -53,7 +57,10 @@ export default class MetaKG {
* Construct API Meta Knowledge Graph based on SmartAPI Specifications.
* @param {boolean} includeReasoner - specify whether to include reasonerStdAPI into meta-kg
*/
async constructMetaKG(includeReasoner = false, options: BuilderOptions = {}): Promise<SmartAPIKGOperationObject[]> {
async constructMetaKG(
includeReasoner = false,
options: BuilderOptions = {},
): Promise<SmartAPIKGOperationObject[]> {
this._ops = await asyncBuilderFactory(options, includeReasoner);
return this.ops;
}
Expand All @@ -62,19 +69,35 @@ export default class MetaKG {
* Construct API Meta Knowledge Graph based on SmartAPI Specifications.
* @param {string} tag - the SmartAPI tag to be filtered on
*/
constructMetaKGSync(includeReasoner = false, options: BuilderOptions = {}): SmartAPIKGOperationObject[] {
this._ops = syncBuilderFactory(options, includeReasoner, this._file_path, this._predicates_path);
constructMetaKGSync(
includeReasoner = false,
options: BuilderOptions = {},
): SmartAPIKGOperationObject[] {
this._ops = syncBuilderFactory(
options,
includeReasoner,
this._file_path,
this._predicates_path,
);
return this.ops;
}

/* Async wrapper for using constructMetaKGSync to enable using async file locking */
async constructMetaKGWithFileLock(includeReasoner = false, options: BuilderOptions = {}): Promise<SmartAPIKGOperationObject[]> {
async constructMetaKGWithFileLock(
includeReasoner = false,
options: BuilderOptions = {},
): Promise<SmartAPIKGOperationObject[]> {
this._ops = await lockWithActionAsync(
[this._file_path, this._predicates_path],
async () => {
return syncBuilderFactory(options, includeReasoner, this._file_path, this._predicates_path);
return syncBuilderFactory(
options,
includeReasoner,
this._file_path,
this._predicates_path,
);
},
debug
debug,
);
return this.ops;
}
Expand All @@ -85,17 +108,24 @@ export default class MetaKG {
*/
filterKG(options: BuilderOptions = {}) {
if (options.smartAPIID) {
this._ops = this._ops.filter(op => op.association.smartapi.id === options.smartAPIID);
this._ops = this._ops.filter(
op => op.association.smartapi.id === options.smartAPIID,
);
} else if (options.teamName) {
this._ops = this._ops.filter(op => op.association?.["x-translator"]?.teamName === options.teamName);
this._ops = this._ops.filter(
op =>
Array.isArray(op.association?.["x-translator"]?.team) &&
op.association?.["x-translator"]?.team.includes(options.teamName),
);
} else if (options.tag) {
this._ops = this._ops.filter(op => op.tags?.includes(options.tag));
} else if (options.component) {
this._ops = this._ops.filter(op => op.association?.["x-translator"]?.component === options.component);
this._ops = this._ops.filter(
op => op.association?.["x-translator"]?.component === options.component,
);
}
}


/**
* Filter the Meta-KG operations based on specific criteria
* @param {Object} - filtering criteria, each key represents the field to be quried
Expand Down
2 changes: 1 addition & 1 deletion src/load/single_spec_async_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SmartAPISpec } from "../parser/types";
export default class SingleSpecAsyncLoader extends BaseAsyncLoader {
private _smartapi_id: string;
constructor(smartAPIID: string) {
super(SINGLE_API_SMARTAPI_QUERY_TEMPLATE.replace("{smartapi_id}", smartAPIID));
super(SINGLE_API_SMARTAPI_QUERY_TEMPLATE.replace("{smartAPIID}", smartAPIID));
this._smartapi_id = smartAPIID;
}

Expand Down
2 changes: 1 addition & 1 deletion src/load/team_specs_async_loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { SmartAPISpec } from "../parser/types";

export default class TeamSpecsAsyncLoader extends BaseAsyncLoader {
constructor(teamName: string) {
super(TEAM_SMARTAPI_QUERY_TEMPLATE.replace("{team_name}", teamName));
super(TEAM_SMARTAPI_QUERY_TEMPLATE.replace("{teamName}", teamName));
}

protected async fetch(): Promise<SmartAPIQueryResult> {
Expand Down

0 comments on commit 82d903d

Please sign in to comment.