import {Injectable} from '@angular/core'; import {Subject} from 'rxjs'; import {AdjusterEventService} from './adjuster-event.service'; @Injectable({providedIn: 'root'}) export class AdjusterPresetsService { activeProblem: Problem; activeRole: Role; activeSchema: Schema; schemaChanges: Subject = new Subject(); problemChanges: Subject = new Subject(); ontology; roles: Role[] = []; schemas: Schema[] = []; constructor(public adjusterEventService: AdjusterEventService) { this.adjusterEventService.ontologyLoads.subscribe((ontology) => { this.ontology = ontology; this.loadRoles(); this.loadSchemas(); this.problemChanges.next(this.activeProblem ?? null); }) } applyProblem(problem: Problem) { console.log(problem); this.problemChanges.next(problem); } changeSchema(schema: Schema) { console.log(schema); this.schemaChanges.next(schema); } getActiveRoleProblems(): Array { return this.activeRole?.problems ?? []; } getActiveSchemaGroups(): Array { return this.activeSchema.groups ?? []; } getGroupDatasets(groupID: string): Array { return this.ontology .find((subject) => subject['@id'] == groupID)["http://www.semanticweb.org/attractiveness/hasDataset"] ?.map((entity) => { const datasetEntity = this.ontology.find((subject) => subject['@id'] == entity['@id']); return { id: datasetEntity["@id"], labels: datasetEntity["http://www.w3.org/2000/01/rdf-schema#label"], //TODO: i18n this! desc: '', included: true } }) ?? []; } getLabelInLang(labels: Label[], lang: string) { return labels.find((labelEntity) => labelEntity['@language'] == lang)?.['@value'] ?? labels[0]['@value']; } loadRoles() { this.roles = this.ontology .filter((subject) => subject['@type']?.includes("http://www.semanticweb.org/attractiveness/Role")) .map((subject) => { return { id: subject['@id'], labels: subject["http://www.w3.org/2000/01/rdf-schema#label"], problems: subject["http://www.semanticweb.org/attractiveness/solvesProblem"].map( (problem) => { const problemEntity = this.ontology.find((subject) => subject['@id'] == problem['@id']); return { id: problem['@id'], labels: problemEntity?.["http://www.w3.org/2000/01/rdf-schema#label"], requiredDatasets: problemEntity?.["http://www.semanticweb.org/attractiveness/requiresDataset"].map((dataset) => dataset['@id']) } } ) } }); this.activeRole = this.roles[0]; this.activeProblem = this.roles[0].problems[0]; } loadSchemas() { this.schemas = this.ontology .filter((subject) => subject['@type']?.includes("http://www.semanticweb.org/attractiveness/ClassificationSchema")) .map((subject) => { return { id: subject['@id'], labels: subject["http://www.w3.org/2000/01/rdf-schema#label"], groups: subject["http://www.semanticweb.org/attractiveness/consistsOf"].map( (group) => { return { id: group['@id'], labels: this.ontology.find((subject) => subject['@id'] == group['@id'])?.["http://www.w3.org/2000/01/rdf-schema#label"] } } ) } }); this.activeSchema = this.schemas[0]; this.schemaChanges.next(this.activeSchema); } pickProblem(role) { this.activeProblem = role.problems[0] ?? null; } } export type Dataset = { id: string; labels: Label[]; desc: string; included: boolean; } export type DatasetGroup = { id: string; labels?: Label[]; }; export type Factor = { id: string; labels: Label[]; weight: number; datasets: Dataset[]; }; export type Label = { "@value": string "@language"?: string, }; export type Problem = { id: string; labels?: Label[]; requiredDatasets: string[] }; export type Role = { id: string; labels?: Label[]; problems?: Problem[]; }; export type Schema = { id: string, labels?: Label[]; groups?: DatasetGroup[]; };