Browse Source

✨ enable role, problem and schema pickers

jmacura 4 years ago
parent
commit
64f6aef704

+ 71 - 6
src/adjuster/adjuster-presets.service.ts

@@ -1,25 +1,52 @@
 import {Injectable} from '@angular/core';
 import {Injectable} from '@angular/core';
+import {BehaviorSubject} from 'rxjs';
 
 
 import ontology from '../data/rural_attractiveness.owl.json';
 import ontology from '../data/rural_attractiveness.owl.json';
 
 
 @Injectable({providedIn: 'root'})
 @Injectable({providedIn: 'root'})
 export class AdjusterPresetsService {
 export class AdjusterPresetsService {
-  activeProblem;
-  activeRole;
-  activeSchema;
+  activeProblem: Problem;
+  activeRole: Role;
+  activeSchema: Schema;
 
 
-  roles = [];
-  schemas = [];
+  schemaChanges: BehaviorSubject<Schema>;
+
+  roles: Role[] = [];
+  schemas: Schema[] = [];
 
 
   constructor() {
   constructor() {
     this.loadRoles();
     this.loadRoles();
     this.loadSchemas();
     this.loadSchemas();
   }
   }
 
 
-  getActiveRoleProblems() {
+  changeSchema(schema: Schema) {
+    console.log(schema);
+    this.schemaChanges.next(schema);
+  }
+
+  getActiveRoleProblems(): Array<Problem> {
     return this.activeRole.problems ?? [];
     return this.activeRole.problems ?? [];
   }
   }
 
 
+  getActiveSchemaGroups(): Array<DatasetGroup> {
+    return this.activeSchema.groups ?? [];
+  }
+
+  //TODO:  Where to store factor's (group's) weight??
+  getGroupDatasets(groupID: string): Array<any> {
+    return ontology
+      .find((subject) => subject['@id'] == groupID)["http://www.semanticweb.org/attractiveness/hasDataset"]
+      ?.map((entity) => {
+        const datasetEntity = ontology.find((subject) => subject['@id'] == entity['@id']);
+        return {
+          id: datasetEntity["@id"],
+          name: datasetEntity["http://www.w3.org/2000/01/rdf-schema#label"][0]["@value"], //TODO: i18n this!
+          desc: '',
+          included: true
+        }
+      }) ?? [];
+  }
+
   loadRoles() {
   loadRoles() {
     this.roles = ontology
     this.roles = ontology
       .filter((subject) => subject['@type']?.includes("http://www.semanticweb.org/attractiveness/Role"))
       .filter((subject) => subject['@type']?.includes("http://www.semanticweb.org/attractiveness/Role"))
@@ -62,6 +89,44 @@ export class AdjusterPresetsService {
         }
         }
       });
       });
     this.activeSchema = this.schemas[0];
     this.activeSchema = this.schemas[0];
+    if (this.schemaChanges === undefined) {
+      this.schemaChanges = new BehaviorSubject(this.activeSchema);
+    } else {
+      this.schemaChanges.next(this.activeSchema);
+    }
     console.log("Schemas", this.schemas);
     console.log("Schemas", this.schemas);
   }
   }
+
+  pickProblem(role) {
+    this.activeProblem = role.problems[0] ?? null;
+  }
 }
 }
+
+export type DatasetGroup = {
+  id: string;
+  labels?: Label[];
+};
+
+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[];
+};
+

+ 4 - 4
src/adjuster/adjuster.component.html

@@ -26,17 +26,17 @@
     <!-- TODO: i18n this-->
     <!-- TODO: i18n this-->
     <div class="p-2 center-block">
     <div class="p-2 center-block">
       My role is:&emsp;
       My role is:&emsp;
-      <select class="form-select form-select-lg" [(ngModel)]="adjusterPresetsService.activeRole">
+      <select class="form-select" [(ngModel)]="adjusterPresetsService.activeRole" (ngModelChange)="adjusterPresetsService.pickProblem($event)">
         <option *ngFor="let role of adjusterPresetsService.roles" [ngValue]="role">{{role.labels[0]['@value']}}</option>
         <option *ngFor="let role of adjusterPresetsService.roles" [ngValue]="role">{{role.labels[0]['@value']}}</option>
       </select>
       </select>
       and I want to solve a problem with:&emsp;
       and I want to solve a problem with:&emsp;
-      <select class="form-select form-select-lg" [(ngModel)]="adjusterPresetsService.activeProblem">
+      <select class="form-select" [(ngModel)]="adjusterPresetsService.activeProblem">
         <option *ngFor="let problem of adjusterPresetsService.getActiveRoleProblems()" [ngValue]="problem">{{problem.labels[0]['@value']}}</option>
         <option *ngFor="let problem of adjusterPresetsService.getActiveRoleProblems()" [ngValue]="problem">{{problem.labels[0]['@value']}}</option>
       </select>
       </select>
     </div>
     </div>
     <div class="p-2 center-block">
     <div class="p-2 center-block">
       Classify factors by schema:&emsp;
       Classify factors by schema:&emsp;
-      <select class="form-select form-select-lg" [(ngModel)]="adjusterPresetsService.activeSchema">
+      <select class="form-select" [(ngModel)]="adjusterPresetsService.activeSchema" (ngModelChange)="adjusterPresetsService.changeSchema($event)">
         <option *ngFor="let schema of adjusterPresetsService.schemas" [ngValue]="schema">{{schema.labels[0]['@value']}}</option>
         <option *ngFor="let schema of adjusterPresetsService.schemas" [ngValue]="schema">{{schema.labels[0]['@value']}}</option>
       </select>
       </select>
     </div>
     </div>
@@ -46,7 +46,7 @@
       <input type="range" class="form-range" [(ngModel)]="adjusterService.numberOfClusters" min="5" max="15" step="1">
       <input type="range" class="form-range" [(ngModel)]="adjusterService.numberOfClusters" min="5" max="15" step="1">
     </div>
     </div>
     <div *ngFor="let factor of adjusterService.factors">
     <div *ngFor="let factor of adjusterService.factors">
-      <pra-dataset-list [factor]="factor"></pra-dataset-list>
+      <pra-dataset-list [factor]="factor" *ngIf="hasDatasets(factor)"></pra-dataset-list>
     </div>
     </div>
     <hr>
     <hr>
     <div class="pt-3 center-block" [hidden]="!adjusterService.clustersLoaded()">
     <div class="pt-3 center-block" [hidden]="!adjusterService.clustersLoaded()">

+ 4 - 0
src/adjuster/adjuster.component.ts

@@ -41,6 +41,10 @@ export class AdjusterComponent implements HsPanelComponent, OnInit {
     this.hsDialogContainerService.create(AdjusterLoaderComponent, {});
     this.hsDialogContainerService.create(AdjusterLoaderComponent, {});
   }
   }
 
 
+  hasDatasets(factor): boolean {
+    return factor.datasets.length > 0;
+  }
+
   isVisible(): boolean {
   isVisible(): boolean {
     return this.hsLayoutService.panelVisible('adjuster');
     return this.hsLayoutService.panelVisible('adjuster');
   }
   }

+ 23 - 5
src/adjuster/adjuster.service.ts

@@ -14,6 +14,7 @@ import attractivenessConfig from '../attractiveness.config.json';
 import clusteringMethods from '../data/clustering_methods.json';
 import clusteringMethods from '../data/clustering_methods.json';
 import {AdjusterEventService} from './adjuster-event.service';
 import {AdjusterEventService} from './adjuster-event.service';
 import {AdjusterLegendService} from './adjuster-legend.service';
 import {AdjusterLegendService} from './adjuster-legend.service';
+import {AdjusterPresetsService} from './adjuster-presets.service';
 import {obce, obceIndexLayer, osmLayer} from '../app.config';
 import {obce, obceIndexLayer, osmLayer} from '../app.config';
 
 
 @Injectable({providedIn: 'root'})
 @Injectable({providedIn: 'root'})
@@ -25,7 +26,7 @@ export class AdjusterService {
   /** To be read from a config file */
   /** To be read from a config file */
   serviceBaseUrl: string;
   serviceBaseUrl: string;
   /** Used in the UI as a selector */
   /** Used in the UI as a selector */
-  allowClusters = true;
+  allowClusters = false //FIXME: (WIP) true;
   /** Used in the UI as a selector */
   /** Used in the UI as a selector */
   allowIndex = true;
   allowIndex = true;
   factors = [];
   factors = [];
@@ -42,6 +43,7 @@ export class AdjusterService {
   constructor(
   constructor(
     public adjusterEventService: AdjusterEventService,
     public adjusterEventService: AdjusterEventService,
     public adjusterLegendService: AdjusterLegendService,
     public adjusterLegendService: AdjusterLegendService,
+    public adjusterPresetsService: AdjusterPresetsService,
     public hsConfig: HsConfig,
     public hsConfig: HsConfig,
     public hsEventBus: HsEventBusService,
     public hsEventBus: HsEventBusService,
     //public hsLayerMetadataService: HsLayerManagerMetadataService,
     //public hsLayerMetadataService: HsLayerManagerMetadataService,
@@ -80,8 +82,23 @@ export class AdjusterService {
       load: this.adjusterEventService.loaderReady,
       load: this.adjusterEventService.loaderReady,
     }).subscribe(() => {
     }).subscribe(() => {
       console.log('Oll layers Korekt! Initializing adjuster...');
       console.log('Oll layers Korekt! Initializing adjuster...');
+      //this._loadInProcess = false;
       this.init();
       this.init();
     });
     });
+
+    /* Listen to schema changes so the factors can be re-arranged in the view */
+    this.adjusterPresetsService.schemaChanges.subscribe((newSchema) => {
+      //TODO: i18n this!
+      this.factors = newSchema.groups.map((group) => {
+        return {
+          id: group.id,
+          name: group.labels[0]['@value'],
+          //FIXME: load weights from file
+          weight: 0.5,
+          datasets: this.adjusterPresetsService.getGroupDatasets(group.id)
+        }
+      });
+    });
   }
   }
 
 
   /**
   /**
@@ -103,11 +120,11 @@ export class AdjusterService {
       .post(this.serviceBaseUrl + 'cz/scores/', {
       .post(this.serviceBaseUrl + 'cz/scores/', {
         factors: this.factors.map((f) => {
         factors: this.factors.map((f) => {
           return {
           return {
-            factor: f.name,
+            factor: f.id,
             weight: f.weight,
             weight: f.weight,
             datasets: f.datasets
             datasets: f.datasets
               .filter((ds) => ds.included)
               .filter((ds) => ds.included)
-              .map((ds) => ds.name),
+              .map((ds) => ds.id),
           };
           };
         }),
         }),
       })
       })
@@ -275,7 +292,8 @@ export class AdjusterService {
       .get(this.serviceBaseUrl + 'cz/datasets/')
       .get(this.serviceBaseUrl + 'cz/datasets/')
       .toPromise()
       .toPromise()
       .then((data: any) => {
       .then((data: any) => {
-        this.factors = data.map((dataset) => {
+        //console.log(data);
+        /*this.factors = data.map((dataset) => {
           return {
           return {
             name: dataset.Factor,
             name: dataset.Factor,
             weight: this.initialWeights[dataset.Factor] ?? 1,
             weight: this.initialWeights[dataset.Factor] ?? 1,
@@ -296,7 +314,7 @@ export class AdjusterService {
                 included: true,
                 included: true,
               };
               };
             });
             });
-        });
+        });*/
         this._loadInProcess = false;
         this._loadInProcess = false;
         this.apply();
         this.apply();
         // In HSL 2.5, setting layer greyscale breaks the print() functionality
         // In HSL 2.5, setting layer greyscale breaks the print() functionality

+ 1 - 1
src/app.config.ts

@@ -209,7 +209,7 @@ obceIndexLayer.set('popUp', {
       label: 'agregovaný index',
       label: 'agregovaný index',
       displayFunction: decimal2prettyPerc,
       displayFunction: decimal2prettyPerc,
     },
     },
-    {
+    { //FIXME: This needs to be made versite to schemas
       attribute: 'Konec chudoby',
       attribute: 'Konec chudoby',
       displayFunction: decimal2prettyPerc,
       displayFunction: decimal2prettyPerc,
     },
     },

+ 1 - 1
src/attractiveness.config.json

@@ -11,5 +11,5 @@
     "Udržitelná města a obce": 0.85,
     "Udržitelná města a obce": 0.85,
     "Ostatní": 0
     "Ostatní": 0
   },
   },
-  "serviceBaseUrl": "https://publish.lesprojekt.cz/nodejs/"
+  "serviceBaseUrl": "https://jmacura.eu/ws/"
 }
 }