-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add source selection with one criteria only
- Loading branch information
1 parent
ebfcedf
commit ae6b5b5
Showing
5 changed files
with
114 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
sib-replica/app/src/lib/strategy/StrategyComunicaSourceSelection.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { QueryEngine } from "@comunica/query-sparql"; | ||
import { SourceProvider } from "../sourceProvider/SourceProvider"; | ||
import User from "../user/User"; | ||
import UserBase from "../user/UserBase"; | ||
import { Targets } from "./Strategy"; | ||
import { DynamicSparqlQuery } from "./StrategyBaseSparql"; | ||
import StrategyComunica from "./StrategyComunica"; | ||
import { SourceProviderBase } from "../sourceProvider/SourceProviderBase"; | ||
|
||
/** | ||
* A `StrategyBase` that uses Comunica to find results. | ||
*/ | ||
export default class StrategyComunicaSourceSelection extends StrategyComunica { | ||
|
||
private sparqlQueryFromSourceSelection: string | DynamicSparqlQuery; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
constructor(name: string, description: string, sourceSelectionSparqlQuery: string | DynamicSparqlQuery, sparqlQueryFromSourceSelection: string | DynamicSparqlQuery, engine: any, sourceProvider: (targets: Targets) => SourceProvider) { | ||
super(name, description, sourceSelectionSparqlQuery, engine, sourceProvider); | ||
this.sparqlQueryFromSourceSelection = sparqlQueryFromSourceSelection; | ||
} | ||
|
||
public async execute(targets: Targets): Promise<void> { | ||
this.setSources(targets); | ||
this.setRunning(); | ||
|
||
const bindingsStream = await this.getEngine().queryBindings(this.getSparqlQuery(targets), { | ||
lenient: true, // ignore HTTP fails | ||
sources: this.getSources(), | ||
}); | ||
|
||
const promises: Promise<void>[] = []; | ||
|
||
bindingsStream.on('data', (binding: any) => { | ||
//console.log(binding.toString()); | ||
|
||
const skillIndex: string = binding.has("instancesSkill")? binding.get('instancesSkill').value: "unknown"; | ||
const cityIndex: string = binding.has("instancesCity")? binding.get('instancesCity').value: "unknown"; | ||
|
||
const sources: string[] = []; | ||
|
||
if (skillIndex !== "unknown") { | ||
console.log("Source selection found source: ", skillIndex); | ||
sources.push(skillIndex) | ||
} | ||
|
||
if (cityIndex !== "unknown") { | ||
console.log("Source selection found source: ", cityIndex); | ||
sources.push(cityIndex) | ||
} | ||
|
||
const strategy = new StrategyComunica("", "", this.sparqlQueryFromSourceSelection, new QueryEngine(), (t: Targets) => new SourceProviderBase().addSourceAll(sources)); | ||
strategy.registerCallbackForMatchesChange((matches) => matches.forEach(match => this.addMatchToResults(match.getUser()))); | ||
promises.push(strategy.execute(targets)); | ||
}); | ||
|
||
return new Promise<void>((resolve, reject) => { | ||
bindingsStream.on('end', async () => { | ||
await Promise.all(promises); | ||
this.setTerminated(); | ||
resolve(); | ||
}); | ||
bindingsStream.on('error', (error: Error) => { | ||
reject(error); | ||
}); | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters