Skip to content

Commit 5119b42

Browse files
committed
feat: Add support for resources containing N3 rules
This is a hacky way to support N3 rules in the loaded resource by first removing the rules from the resource, then loading in the file and then adding the N3 rules again. The same process is applied upon editing the resource with a sparql patch request.
1 parent 0b5827d commit 5119b42

2 files changed

Lines changed: 101 additions & 3 deletions

File tree

app/controllers/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,9 @@ export default class IndexController extends Controller {
155155
return;
156156
}
157157

158+
// Remove all N3 rules from the resource.
159+
const matches = await this.model.removeN3RulesFromResource();
160+
158161
this.fields.forEach((field, i) => {
159162
field.order = i;
160163

@@ -182,6 +185,9 @@ export default class IndexController extends Controller {
182185

183186
await this.store.persist();
184187

188+
// Re-add the N3 rules to the resource.
189+
await this.model.addN3RulesToResource(matches);
190+
185191
this.success = 'Successfully saved the form definition!';
186192
event.target.disabled = false;
187193
event.target.innerText = 'Save';
@@ -453,7 +459,7 @@ export default class IndexController extends Controller {
453459

454460
this.form = this.model.loadedFormUri;
455461
this.model.configureStorageLocations(this.form);
456-
await this.model.fetchGraphs();
462+
await this.model.fetchGraphs(true);
457463

458464
this.model.loadForm();
459465

app/routes/index.js

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ export default class IndexRoute extends Route {
2727
console.log('editing form', form);
2828
this.configureStorageLocations(form);
2929

30-
await this.fetchGraphs();
30+
await this.fetchGraphs(form !== null);
3131

3232
if (form) {
3333
this.loadForm();
@@ -64,7 +64,13 @@ export default class IndexRoute extends Route {
6464
storageLocation;
6565
}
6666

67-
async fetchGraphs() {
67+
async fetchGraphs(removeN3Rules = false) {
68+
// Remove all N3 rules from the resource.
69+
let matches;
70+
if (removeN3Rules) {
71+
matches = await this.removeN3RulesFromResource();
72+
}
73+
6874
await this.store.fetchGraphForType('hydra-class', true);
6975
await this.store.fetchGraphForType('rdf-form', true);
7076
await this.store.fetchGraphForType('rdf-form-field', true);
@@ -76,6 +82,11 @@ export default class IndexRoute extends Route {
7682
await this.store.fetchGraphForType('shacl-form', true);
7783
await this.store.fetchGraphForType('shacl-form-field', true);
7884
await this.store.fetchGraphForType('shacl-form-option', true);
85+
86+
// Add N3 rules back to the resource.
87+
if (removeN3Rules) {
88+
await this.addN3RulesToResource(matches);
89+
}
7990
}
8091

8192
initiateNewRdfForm() {
@@ -116,4 +127,85 @@ export default class IndexRoute extends Route {
116127
console.log('loaded form', this.form);
117128
console.log('loaded supportedClass', this.supportedClass);
118129
}
130+
131+
async removeN3RulesFromResource() {
132+
const fetch = this.solidAuth.session.fetch;
133+
134+
const response = await fetch(this.loadedFormUri, {
135+
method: 'GET',
136+
});
137+
138+
// Get content-type.
139+
const contentType = response.headers.get('content-type');
140+
141+
// Get content.
142+
let text = await response.text();
143+
console.log(text);
144+
145+
// Match prefixes.
146+
const prefixRegex = /(@prefix|PREFIX)\s+[^:]*:\s*<[^>]*>\s*\.?\n/g;
147+
const prefixes = text.match(prefixRegex);
148+
149+
// Match N3 rules.
150+
const rulesRegex =
151+
/\{[^{}]*}\s*(=>|[^\s{}]*implies[^\s{}]*)\s*{[^{}]*}\s*\./g;
152+
const rules = text.match(rulesRegex);
153+
console.log(rules);
154+
155+
// Remove N3 rules.
156+
rules?.forEach((match) => {
157+
text = text.replace(match, '');
158+
});
159+
160+
// Save resource without N3 rules.
161+
await fetch(this.loadedFormUri, {
162+
method: 'PUT',
163+
headers: {
164+
'Content-Type': contentType,
165+
},
166+
body: text,
167+
});
168+
169+
return { rules, prefixes };
170+
}
171+
172+
async addN3RulesToResource(matches) {
173+
const { rules, prefixes } = matches;
174+
const fetch = this.solidAuth.session.fetch;
175+
176+
const response = await fetch(this.loadedFormUri, {
177+
method: 'GET',
178+
});
179+
180+
// Get content-type.
181+
const contentType = response.headers.get('content-type');
182+
183+
// Get content.
184+
let text = await response.text();
185+
186+
// Match prefixes.
187+
const prefixRegex = /(@prefix|PREFIX)\s+[^:]*:\s*<[^>]*>\s*\.?\n/g;
188+
const matchedPrefixes = text.match(prefixRegex);
189+
190+
// Add prefixes in front if not already present.
191+
prefixes?.forEach((prefix) => {
192+
if (!matchedPrefixes?.includes(prefix)) {
193+
text = prefix + text;
194+
}
195+
});
196+
197+
// Add N3 rules.
198+
rules.forEach((match) => {
199+
text += match;
200+
});
201+
202+
// Save resource with N3 rules.
203+
await fetch(this.loadedFormUri, {
204+
method: 'PUT',
205+
headers: {
206+
'Content-Type': contentType,
207+
},
208+
body: text,
209+
});
210+
}
119211
}

0 commit comments

Comments
 (0)