Skip to content

Commit

Permalink
added list syntax support
Browse files Browse the repository at this point in the history
  • Loading branch information
kianschmalenbach committed Sep 27, 2020
1 parent 68c6113 commit 68c877d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
24 changes: 18 additions & 6 deletions src/app/serializer.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,22 @@ function serializeTriples(store, port = null) {
while (objectIndex < predicate.objects.length) {
const object = predicate.objects[objectIndex];
const objectIndent = predicateIndent + predicate.resource.representationLength + 1;
const objectWrapper = getObjectWrapper(object, objectIndent);
if (objectIndex > 0)
triple.appendChild(document.createTextNode(getIndent(objectIndent)));
triple.appendChild(objectWrapper);
const list = object.getList();
if (list !== null) {
if (objectIndex > 0)
triple.appendChild(document.createTextNode(getIndent(objectIndent)));
triple.appendChild(document.createTextNode("( "));
for (const i in list) {
triple.appendChild(getObjectWrapper(list[i], 0, true));
triple.appendChild(document.createTextNode(" "));
}
triple.appendChild(document.createTextNode(")"));
} else {
const objectWrapper = getObjectWrapper(object, objectIndent);
if (objectIndex > 0)
triple.appendChild(document.createTextNode(getIndent(objectIndent)));
triple.appendChild(objectWrapper);
}
triple.appendChild(document.createTextNode(" "));
objectIndex++;
if (objectIndex < predicate.objects.length) {
Expand Down Expand Up @@ -117,12 +129,12 @@ function serializeTriples(store, port = null) {
return predicateWrapper;
}

function getObjectWrapper(object, indent) {
function getObjectWrapper(object, indent, list = false) {
if (object.resource.html === null)
object.resource.createHtml();
const objectWrapper = document.createElement("span");
objectWrapper.setAttribute("class", "object");
if (object.equivalentSubject !== null) {
if (!list && object.equivalentSubject !== null) {
objectWrapper.appendChild(document.createTextNode("[ "));
objectWrapper.appendChild(document.createElement("br"));
objectWrapper.appendChild(document.createTextNode(getIndent(indent + 2)));
Expand Down
35 changes: 35 additions & 0 deletions src/bdo/triplestore.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
const Resource = require("./resource");
const commonPrefixSource = "https://prefix.cc/popular/all.file.json";
const listURIs = {
first: "http://www.w3.org/1999/02/22-rdf-syntax-ns#first",
rest: "http://www.w3.org/1999/02/22-rdf-syntax-ns#rest",
nil: "http://www.w3.org/1999/02/22-rdf-syntax-ns#nil"
}
const commonPrefixes = [];

class Triplestore {
Expand Down Expand Up @@ -157,6 +162,29 @@ class Subject extends TripleConstituent {
else
return {item: new Subject(resource), isNew: true};
}

getList() {
if (this.predicates.length !== 2)
return null;
const firstP = this.predicates.find(
element => element.resource instanceof Resource.URI && element.resource.value === listURIs.first
);
if (!firstP || firstP.objects.length !== 1)
return null;
const restP = this.predicates.find(
element => element.resource instanceof Resource.URI && element.resource.value === listURIs.rest
);
if (!restP || restP.objects.length !== 1)
return null;
const rest = restP.objects[0];
if (rest.resource instanceof Resource.URI && rest.resource.value === listURIs.nil)
return [firstP.objects[0]];
const list = rest.getList();
if (list === null)
return null;
list.splice(0, 0, firstP.objects[0]);
return list;
}
}

class Predicate extends TripleConstituent {
Expand Down Expand Up @@ -185,6 +213,13 @@ class Object extends TripleConstituent {
setEquivalentSubject(subject) {
this.equivalentSubject = subject;
}

getList() {
if (this.equivalentSubject !== null)
return this.equivalentSubject.getList();
else
return null;
}
}

function getCommonPrefixes() {
Expand Down

0 comments on commit 68c877d

Please sign in to comment.