-
Notifications
You must be signed in to change notification settings - Fork 0
/
StrategyComunicaSourceSelection.ts
71 lines (57 loc) · 2.88 KB
/
StrategyComunicaSourceSelection.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
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);
});
});
}
}