adjuster-presets.service.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import {Injectable} from '@angular/core';
  2. import {Subject} from 'rxjs';
  3. import {AdjusterEventService} from './adjuster-event.service';
  4. @Injectable({providedIn: 'root'})
  5. export class AdjusterPresetsService {
  6. activeProblem: Problem;
  7. activeRole: Role;
  8. activeSchema: Schema;
  9. schemaChanges: Subject<Schema> = new Subject();
  10. problemChanges: Subject<Problem> = new Subject();
  11. ontology;
  12. roles: Role[] = [];
  13. schemas: Schema[] = [];
  14. constructor(public adjusterEventService: AdjusterEventService) {
  15. this.adjusterEventService.ontologyLoads.subscribe((ontology) => {
  16. this.ontology = ontology;
  17. this.loadRoles();
  18. this.loadSchemas();
  19. this.problemChanges.next(this.activeProblem ?? null);
  20. })
  21. }
  22. applyProblem(problem: Problem) {
  23. console.log(problem);
  24. this.problemChanges.next(problem);
  25. }
  26. changeSchema(schema: Schema) {
  27. console.log(schema);
  28. this.schemaChanges.next(schema);
  29. }
  30. getActiveRoleProblems(): Array<Problem> {
  31. return this.activeRole?.problems ?? [];
  32. }
  33. getActiveSchemaGroups(): Array<DatasetGroup> {
  34. return this.activeSchema.groups ?? [];
  35. }
  36. getGroupDatasets(groupID: string): Array<Dataset> {
  37. return this.ontology
  38. .find((subject) => subject['@id'] == groupID)["http://www.semanticweb.org/attractiveness/hasDataset"]
  39. ?.map((entity) => {
  40. const datasetEntity = this.ontology.find((subject) => subject['@id'] == entity['@id']);
  41. return {
  42. id: datasetEntity["@id"],
  43. labels: datasetEntity["http://www.w3.org/2000/01/rdf-schema#label"], //TODO: i18n this!
  44. desc: '',
  45. included: true
  46. }
  47. }) ?? [];
  48. }
  49. getLabelInLang(labels: Label[], lang: string) {
  50. return labels.find((labelEntity) => labelEntity['@language'] == lang)?.['@value'] ?? labels[0]['@value'];
  51. }
  52. loadRoles() {
  53. this.roles = this.ontology
  54. .filter((subject) => subject['@type']?.includes("http://www.semanticweb.org/attractiveness/Role"))
  55. .map((subject) => {
  56. return {
  57. id: subject['@id'],
  58. labels: subject["http://www.w3.org/2000/01/rdf-schema#label"],
  59. problems: subject["http://www.semanticweb.org/attractiveness/solvesProblem"].map(
  60. (problem) => {
  61. const problemEntity = this.ontology.find((subject) => subject['@id'] == problem['@id']);
  62. return {
  63. id: problem['@id'],
  64. labels: problemEntity?.["http://www.w3.org/2000/01/rdf-schema#label"],
  65. requiredDatasets: problemEntity?.["http://www.semanticweb.org/attractiveness/requiresDataset"].map((dataset) => dataset['@id'])
  66. }
  67. }
  68. )
  69. }
  70. });
  71. this.activeRole = this.roles[0];
  72. this.activeProblem = this.roles[0].problems[0];
  73. }
  74. loadSchemas() {
  75. this.schemas = this.ontology
  76. .filter((subject) => subject['@type']?.includes("http://www.semanticweb.org/attractiveness/ClassificationSchema"))
  77. .map((subject) => {
  78. return {
  79. id: subject['@id'],
  80. labels: subject["http://www.w3.org/2000/01/rdf-schema#label"],
  81. groups: subject["http://www.semanticweb.org/attractiveness/consistsOf"].map(
  82. (group) => {
  83. return {
  84. id: group['@id'],
  85. labels: this.ontology.find((subject) => subject['@id'] == group['@id'])?.["http://www.w3.org/2000/01/rdf-schema#label"]
  86. }
  87. }
  88. )
  89. }
  90. });
  91. this.activeSchema = this.schemas[0];
  92. this.schemaChanges.next(this.activeSchema);
  93. }
  94. pickProblem(role) {
  95. this.activeProblem = role.problems[0] ?? null;
  96. }
  97. }
  98. export type Dataset = {
  99. id: string;
  100. labels: Label[];
  101. desc: string;
  102. included: boolean;
  103. }
  104. export type DatasetGroup = {
  105. id: string;
  106. labels?: Label[];
  107. };
  108. export type Factor = {
  109. id: string;
  110. labels: Label[];
  111. weight: number;
  112. datasets: Dataset[];
  113. };
  114. export type Label = {
  115. "@value": string
  116. "@language"?: string,
  117. };
  118. export type Problem = {
  119. id: string;
  120. labels?: Label[];
  121. requiredDatasets: string[]
  122. };
  123. export type Role = {
  124. id: string;
  125. labels?: Label[];
  126. problems?: Problem[];
  127. };
  128. export type Schema = {
  129. id: string,
  130. labels?: Label[];
  131. groups?: DatasetGroup[];
  132. };