Skip to content

Instantly share code, notes, and snippets.

@csjx
Created April 6, 2017 15:03
Show Gist options
  • Save csjx/fb3defdfab8d805f4346bd5b3658232b to your computer and use it in GitHub Desktop.
Save csjx/fb3defdfab8d805f4346bd5b3658232b to your computer and use it in GitHub Desktop.
An example of modifying an RDF graph to test an rdflib.js problem
<!DOCTYPE html>
<html lang="en">
<head>
<title>RDF Test</title>
<script type="text/javascript" src="components/rdflib.js"></script>
<script type="text/javascript" src="components/jquery.js"></script>
<script type="text/javascript" src="components/underscore.js"></script>
<script type="text/javascript" src="rdf-modify-test.js"></script>
</head>
<body onload = "RDFTest.run()">
<span>Results are in the console.</span>
</body>
</html>
var RDFTest = {
rdfStr : function() {
var xml = [];
xml.push('<?xml version="1.0"?>\n');
xml.push('<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:n0="http://purl.org/dc/elements/1.1/" xmlns:n1="http://xmlns.com/foaf/0.1/" xmlns:terms="http://purl.org/dc/terms/" xmlns:ter="http://www.openarchives.org/ore/terms/" xmlns:cito="http://purl.org/spar/cito/">\n');
xml.push('\t<rdf:Description rdf:about="https://d1.org/cn/v2/resolve/ore.1.1">\n');
xml.push('\t\t<n0:creator>\n');
xml.push('\t\t\t<rdf:Description>\n');
xml.push('\t\t\t\t<rdf:type rdf:resource="http://purl.org/dc/terms/Agent"/>\n');
xml.push('\t\t\t\t<n1:name>Christopher Jones</n1:name>\n');
xml.push('\t\t\t</rdf:Description>\n');
xml.push('\t\t</n0:creator>\n');
xml.push('\t\t<terms:identifier>ore.1.1</terms:identifier>\n');
xml.push('\t\t<terms:modified rdf:datatype="http://www.w3.org/2001/XMLSchema#dateTime">2017-03-29T23:07:39.911Z</terms:modified>\n');
xml.push('\t\t<ter:describes rdf:resource="https://d1.org/cn/v2/resolve/ore.1.1#aggregation"/>\n');
xml.push('\t\t<rdf:type rdf:resource="http://www.openarchives.org/ore/terms/ResourceMap"/>\n');
xml.push('\t</rdf:Description>\n');
xml.push('\t<rdf:Description rdf:about="https://d1.org/cn/v2/resolve/ore.1.1#aggregation">\n');
xml.push('\t\t<ter:aggregates rdf:resource="https://d1.org/cn/v2/resolve/scimeta.1.1"/>\n');
xml.push('\t\t<ter:isDescribedBy rdf:resource="https://d1.org/cn/v2/resolve/ore.1.1"/>\n');
xml.push('\t</rdf:Description>\n');
xml.push('\t<rdf:Description rdf:about="https://d1.org/cn/v2/resolve/scimeta.1.1">\n');
xml.push('\t\t<terms:identifier>scimeta.1.1</terms:identifier>\n');
xml.push('\t\t<cito:documents rdf:resource="https://d1.org/cn/v2/resolve/scimeta.1.1"/>\n');
xml.push('\t\t<cito:isDocumentedBy rdf:resource="https://d1.org/cn/v2/resolve/scimeta.1.1"/>\n');
xml.push('\t\t<ter:isAggregatedBy rdf:resource="https://d1.org/cn/v2/resolve/ore.1.1#aggregation"/>\n');
xml.push('\t</rdf:Description>\n');
xml.push('</rdf:RDF>');
return xml.join("");
},
namespaces: {
RDF: "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
FOAF: "http://xmlns.com/foaf/0.1/",
OWL: "http://www.w3.org/2002/07/owl#",
DC: "http://purl.org/dc/elements/1.1/",
ORE: "http://www.openarchives.org/ore/terms/",
DCTERMS: "http://purl.org/dc/terms/",
CITO: "http://purl.org/spar/cito/",
XSD: "http://www.w3.org/2001/XMLSchema#"
},
oldPid: "ore.1.1",
newPid: "ore.1.2",
cnResolveUrl: "https://d1.org/cn/v2/resolve/",
run: function() {
this.rdf = $rdf;
var RDF = this.rdf.Namespace(this.namespaces.RDF),
FOAF = this.rdf.Namespace(this.namespaces.FOAF),
OWL = this.rdf.Namespace(this.namespaces.OWL),
DC = this.rdf.Namespace(this.namespaces.DC),
ORE = this.rdf.Namespace(this.namespaces.ORE),
DCTERMS = this.rdf.Namespace(this.namespaces.DCTERMS),
CITO = this.rdf.Namespace(this.namespaces.CITO),
XSD = this.rdf.Namespace(this.namespaces.XSD),
aggregatesStatements = [],
newAggregatesStatements = [],
pid;
this.store = this.rdf.graph();
this.rdf.parse(
this.rdfStr(),
this.store,
"https://d1.org/",
"application/rdf+xml");
console.log("All statements before modification:");
this.printStatements(this.store.statements);
// Find the ore:aggregates statements
aggregatesStatements =
this.store.statementsMatching(undefined, ORE("aggregates"), undefined, undefined);
// Replace old subject URIs with new URIs
_.each(aggregatesStatements, function(statement) {
// Note: remove and add() must be called for the underlying index
// in the IndexedFormula to be modified correctly
this.store.remove(statement);
statement.subject.value = this.cnResolveUrl + encodeURIComponent(this.newPid) + "#aggregation";
this.store.add(statement);
}, this);
console.log("\nAll statements after modification:");
this.printStatements(this.store.statements);
newAggregatesStatements =
this.store.statementsMatching(this.rdf.sym(this.cnResolveUrl + encodeURIComponent(this.newPid) + "#aggregation"), undefined, undefined, undefined);
console.log("\nStatements matching modified subject:");
this.printStatements(newAggregatesStatements);
},
printStatements: function(statements) {
// Print the statements for reference
console.log("-------------------------------------------------------------------");
if ( ! statements.length ) {
console.log("No statements to print.");
return;
}
_.each(statements, function(statement) {
console.log(
statement.subject.value + "\t" +
statement.predicate.value + "\t" +
statement.object.value + "\n"
);
});
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment