adjuster-presets.service.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. import {Injectable} from '@angular/core';
  2. import {BehaviorSubject} from 'rxjs';
  3. import ontology from '../data/rural_attractiveness.owl.json';
  4. @Injectable({providedIn: 'root'})
  5. export class AdjusterPresetsService {
  6. activeProblem: Problem;
  7. activeRole: Role;
  8. activeSchema: Schema;
  9. schemaChanges: BehaviorSubject<Schema>;
  10. roles: Role[] = [];
  11. schemas: Schema[] = [];
  12. constructor() {
  13. this.loadRoles();
  14. this.loadSchemas();
  15. }
  16. changeSchema(schema: Schema) {
  17. console.log(schema);
  18. this.schemaChanges.next(schema);
  19. }
  20. getActiveRoleProblems(): Array<Problem> {
  21. return this.activeRole.problems ?? [];
  22. }
  23. getActiveSchemaGroups(): Array<DatasetGroup> {
  24. return this.activeSchema.groups ?? [];
  25. }
  26. //TODO: Where to store factor's (group's) weight??
  27. getGroupDatasets(groupID: string): Array<any> {
  28. return ontology
  29. .find((subject) => subject['@id'] == groupID)["http://www.semanticweb.org/attractiveness/hasDataset"]
  30. ?.map((entity) => {
  31. const datasetEntity = ontology.find((subject) => subject['@id'] == entity['@id']);
  32. return {
  33. id: datasetEntity["@id"],
  34. name: datasetEntity["http://www.w3.org/2000/01/rdf-schema#label"][0]["@value"], //TODO: i18n this!
  35. desc: '',
  36. included: true
  37. }
  38. }) ?? [];
  39. }
  40. loadRoles() {
  41. this.roles = ontology
  42. .filter((subject) => subject['@type']?.includes("http://www.semanticweb.org/attractiveness/Role"))
  43. .map((subject) => {
  44. return {
  45. id: subject['@id'],
  46. labels: subject["http://www.w3.org/2000/01/rdf-schema#label"],
  47. problems: subject["http://www.semanticweb.org/attractiveness/solvesProblem"].map(
  48. (problem) => {
  49. const problemEntity = ontology.find((subject) => subject['@id'] == problem['@id']);
  50. return {
  51. id: problem['@id'],
  52. labels: problemEntity?.["http://www.w3.org/2000/01/rdf-schema#label"],
  53. requiredDatasets: problemEntity?.["http://www.semanticweb.org/attractiveness/requiresDataset"].map((dataset) => dataset['@id'])
  54. }
  55. }
  56. )
  57. }
  58. });
  59. this.activeRole = this.roles[0];
  60. this.activeProblem = this.roles[0].problems[0];
  61. console.log("Roles", this.roles);
  62. }
  63. loadSchemas() {
  64. this.schemas = ontology
  65. .filter((subject) => subject['@type']?.includes("http://www.semanticweb.org/attractiveness/ClassificationSchema"))
  66. .map((subject) => {
  67. return {
  68. id: subject['@id'],
  69. labels: subject["http://www.w3.org/2000/01/rdf-schema#label"],
  70. groups: subject["http://www.semanticweb.org/attractiveness/consistsOf"].map(
  71. (group) => {
  72. return {
  73. id: group['@id'],
  74. labels: ontology.find((subject) => subject['@id'] == group['@id'])?.["http://www.w3.org/2000/01/rdf-schema#label"]
  75. }
  76. }
  77. )
  78. }
  79. });
  80. this.activeSchema = this.schemas[0];
  81. if (this.schemaChanges === undefined) {
  82. this.schemaChanges = new BehaviorSubject(this.activeSchema);
  83. } else {
  84. this.schemaChanges.next(this.activeSchema);
  85. }
  86. console.log("Schemas", this.schemas);
  87. }
  88. pickProblem(role) {
  89. this.activeProblem = role.problems[0] ?? null;
  90. }
  91. }
  92. export type DatasetGroup = {
  93. id: string;
  94. labels?: Label[];
  95. };
  96. export type Label = {
  97. "@value": string
  98. "@language"?: string,
  99. };
  100. export type Problem = {
  101. id: string;
  102. labels?: Label[];
  103. requiredDatasets: string[]
  104. };
  105. export type Role = {
  106. id: string;
  107. labels?: Label[];
  108. problems?: Problem[];
  109. };
  110. export type Schema = {
  111. id: string,
  112. labels?: Label[];
  113. groups?: DatasetGroup[];
  114. };