Skip to content

Commit f3d6fe0

Browse files
committed
Merge branch 'main' of github.com:Citizen-Knowledge-Graph/foerderfunke-react-app
2 parents 5668c40 + 90221dc commit f3d6fe0

4 files changed

Lines changed: 74 additions & 28 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"@babel/plugin-transform-private-property-in-object": "^7.24.7",
99
"@emotion/react": "^11.11.4",
1010
"@emotion/styled": "^11.11.5",
11-
"@foerderfunke/matching-engine": "^1.3.0",
11+
"@foerderfunke/matching-engine": "^1.3.3",
1212
"@mui/icons-material": "^5.15.19",
1313
"@mui/material": "^5.15.19",
1414
"@mui/x-charts": "^7.6.2",

src/core/managers/matchingEngineManager.js

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,43 +57,28 @@ const matchingEngineManager = {
5757
return engine.metadata || {};
5858
},
5959

60-
async fetchValidationReport(userId, language = "en") {
60+
// fetchValidationReport and fetchQuizReport could probably be unified further to only expose fetchMatchingReport() TODO
61+
async fetchMatchingReport(userId, rps, lang = "en") {
6162
if (!this.matchingEngineInstance) {
62-
await this.initMatchingEngine(language);
63+
await this.initMatchingEngine(lang);
6364
}
64-
65-
const validationConfig = await resourceService.fetchResourceWithCache(
66-
"assets/data/requirement-profiles/requirement-profiles.json"
67-
);
68-
6965
const userProfile = userManager.retrieveUserData(userId);
7066
const userProfileTurtle = await convertUserProfileToTurtle(userProfile);
67+
return this.matchingEngineInstance.matching(userProfileTurtle, rps, FORMAT.JSON_LD);
68+
},
69+
70+
async fetchValidationReport(userId, language = "en") {
71+
const validationConfig = await resourceService.fetchResourceWithCache("assets/data/requirement-profiles/requirement-profiles.json");
7172
const requirementProfiles = [];
7273
for (const { rpUri, behindFeatureFlag } of validationConfig["queries"]) {
7374
if (behindFeatureFlag && !featureFlags[behindFeatureFlag]) continue;
7475
requirementProfiles.push(rpUri);
7576
}
76-
77-
const report = await this.matchingEngineInstance.matching(
78-
userProfileTurtle,
79-
requirementProfiles,
80-
FORMAT.JSON_LD
81-
);
82-
83-
return report;
77+
return await this.fetchMatchingReport(userId, requirementProfiles, language);
8478
},
8579

8680
async fetchQuizReport(userId, requirementProfiles, language = "en") {
87-
if (!this.matchingEngineInstance) {
88-
await this.initMatchingEngine(language);
89-
}
90-
const userProfile = userManager.retrieveUserData(userId);
91-
const userProfileTurtle = await convertUserProfileToTurtle(userProfile);
92-
return this.matchingEngineInstance.matching(
93-
userProfileTurtle,
94-
requirementProfiles.map(rp => expand(rp)),
95-
FORMAT.JSON_LD
96-
);
81+
return await this.fetchMatchingReport(userId, requirementProfiles.map(rp => expand(rp)), language);
9782
},
9883

9984
async fetchEvaluationGraph(userId, requirementProfile, language = "en") {
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import { ProfileManager } from "@foerderfunke/matching-engine/src/ProfileManager";
2+
import resourceService from "@/core/services/resourceService";
3+
4+
const profileManager = {
5+
profileManagerInstance: null,
6+
constructionPromise: null,
7+
8+
async constructProfileManagerOnce() {
9+
if (this.profileManagerInstance) return this.profileManagerInstance;
10+
if (this.constructionPromise) return this.constructionPromise;
11+
12+
console.log("Constructing Profile Manager...");
13+
this.constructionPromise = (async () => {
14+
const validationConfig = await resourceService.fetchResourceWithCache(
15+
"assets/data/requirement-profiles/requirement-profiles.json"
16+
);
17+
18+
const datafieldsString = await resourceService.fetchResourceWithCache(validationConfig["datafields"]);
19+
const datafieldsBielefeldString = await resourceService.fetchResourceWithCache(validationConfig["datafields-bielefeld"]);
20+
const definitionsString = await resourceService.fetchResourceWithCache(validationConfig["definitions"]);
21+
const materializationString = await resourceService.fetchResourceWithCache(validationConfig["materialization"]);
22+
const consistencyString = await resourceService.fetchResourceWithCache(validationConfig["consistency"]);
23+
24+
const profileManager = new ProfileManager();
25+
profileManager.addDatafieldsTurtle(datafieldsString)
26+
profileManager.addDatafieldsTurtle(datafieldsBielefeldString)
27+
profileManager.addDefinitionsTurtle(definitionsString);
28+
profileManager.addMaterializationTurtle(materializationString);
29+
profileManager.addConsistencyTurtle(consistencyString);
30+
31+
this.profileManagerInstance = profileManager;
32+
console.log("Profile Manager constructed.");
33+
return profileManager;
34+
})();
35+
36+
return this.constructionPromise;
37+
},
38+
39+
async initProfileManager(defaultProfileId) {
40+
const profileManager = await this.constructProfileManagerOnce();
41+
await profileManager.init();
42+
// let profileIds = JSON.parse(localStorage.getItem('userIds')) || []
43+
let existingProfileTurtle = localStorage.getItem(defaultProfileId);
44+
if (existingProfileTurtle) {
45+
profileManager.importProfileTurtle(defaultProfileId, existingProfileTurtle);
46+
} else {
47+
let profile = profileManager.getProfile(profileManager.newProfile(defaultProfileId));
48+
localStorage.setItem(defaultProfileId, await profile.toTurtle());
49+
}
50+
return profileManager;
51+
},
52+
};
53+
54+
export default profileManager;

src/ui/shared-hooks/useInitialiseApplication.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { useLanguageStore } from "@/ui/storage/useLanguageStore";
44
import { useApplicationLoadingState, useInitialisationState } from '@/ui/storage/updates';
55
import matchingEngineManager from "@/core/managers/matchingEngineManager";
66
import userManager from "@/core/managers/userManager";
7+
import profileManager from "@/core/managers/profileManager";
78

89
export const useInitialiseApplication = () => {
910
const setInitialisationState = useInitialisationState((state) => state.setInitialisationState);
@@ -20,7 +21,7 @@ export const useInitialiseApplication = () => {
2021
const validationReportStore = useValidationReportStore();
2122
const language = useLanguageStore((state) => state.language);
2223

23-
const fixedUserId = "ff:quick-check-user";
24+
const fixedUserId = "ff:quick-check-user"; // change to ff:citizen1 when switching to ProfileManager TODO
2425

2526
useEffect(() => {
2627
const initializeAppState = async () => {
@@ -39,17 +40,23 @@ export const useInitialiseApplication = () => {
3940
if (!userProfile) userManager.initialiseNewUser(fixedUserId);
4041
updateUserId(fixedUserId);
4142

43+
// Construct and init profile manager
44+
await profileManager.constructProfileManagerOnce()
45+
// await profileManager.initProfileManager(fixedUserId)
46+
// updateUserId(fixedUserId);
47+
4248
// Construct engine once (does not call .init)
4349
await matchingEngineManager.constructMatchingEngineOnce();
4450

4551
// Call init(language) once after construction
46-
lastInitializedLang.current = language; // prevent duplicate init
52+
lastInitializedLang.current = language; // prevent duplicate init
4753
await matchingEngineManager.initMatchingEngine(language);
4854

4955
// Fetch metadata and report
5056
const metadata = await matchingEngineManager.fetchMetadata(language);
5157
metadataStore.updateMetadata(metadata || "empty");
5258

59+
// It this necessary initially or only later? TODO
5360
const report = await matchingEngineManager.fetchValidationReport(fixedUserId, language);
5461
validationReportStore.updateValidationReport(report || "empty");
5562

0 commit comments

Comments
 (0)