Skip to content

Instantly share code, notes, and snippets.

@LaurensRietveld
Last active September 21, 2018 09:34
Show Gist options
  • Save LaurensRietveld/29d14a4bc395c22f30f5 to your computer and use it in GitHub Desktop.
Save LaurensRietveld/29d14a4bc395c22f30f5 to your computer and use it in GitHub Desktop.
Fetch autocompletions from ontology XML

A simple example on how to extend the YASQE autocompletion functionality, by parsing the RDF/XML file of an ontology

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Fetch completions from ontology</title>
<link href='http://cdn.jsdelivr.net/g/[email protected](yasqe.min.css),[email protected](yasr.min.css)' rel='stylesheet' type='text/css'/>
</head>
<body>
<div id="yasqe"></div>
<div id="yasr"></div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/jquery/1.11.1/jquery.min.js'></script>
<script src='http://cdn.jsdelivr.net/yasr/2.3/yasr.bundled.min.js'></script>
<script src='http://cdn.jsdelivr.net/yasqe/2.2/yasqe.bundled.min.js'></script>
<script src="init.js"></script>
</body>
//one single function to fetch both completions. Do this, to avoid sending an ajax request once for properties, and once for objects
var extractors = [];
var fetchCompletions = function(extractCompletions) {
extractors.push(extractCompletions);
if (extractors.length == 1) {
//ok, this function has been called for both the property completions and the class completions.
//now do the ajax request
$.get("http://www.purl.org/eem#", function(xmlOntology) {
extractors.forEach(function(extractor) {
extractor(xmlOntology);
});
});
}
};
var customPropertyCompleter = function(yasqe) {
//we use several functions from the regular property autocompleter (this way, we don't have to re-define code such as determining whether we are in a valid autocompletion position)
var returnObj = {
isValidCompletionPosition: function(){return YASQE.Autocompleters.properties.isValidCompletionPosition(yasqe)},
preProcessToken: function(token) {return YASQE.Autocompleters.properties.preProcessToken(yasqe, token)},
postProcessToken: function(token, suggestedString) {return YASQE.Autocompleters.properties.postProcessToken(yasqe, token, suggestedString)}
};
//In this case we assume the properties will fit in memory. So, turn on bulk loading, which will make autocompleting a lot faster
returnObj.bulk = true;
returnObj.async = true;
//and, as everything is in memory, enable autoShowing the completions
returnObj.autoShow = true;
returnObj.persistent = "customProperties";//this will store the sparql results in the client-cache for a month.
returnObj.get = function(token, callback) {
var extractor = function(xmlOntology) {
var propertyArray = [];
$(xmlOntology).find("ObjectProperty").each(function(i, objProp) {propertyArray.push($(objProp).attr('rdf:about'))});
$(xmlOntology).find("DatatypeProperty").each(function(i, objProp) {propertyArray.push($(objProp).attr('rdf:about'))});
console.log(propertyArray);
callback(propertyArray);
};
fetchCompletions(extractor);
};
return returnObj;
};
//now register our new autocompleter
YASQE.registerAutocompleter('propOntologyCompleter', customPropertyCompleter);
//and disable the old autocompleter
YASQE.defaults.autocompleters = $.grep(YASQE.defaults.autocompleters, function(completer) {return completer != 'properties'});
//Do the same for the classes
var customClassCompleter = function(yasqe) {
var returnObj = {
isValidCompletionPosition: function(){return YASQE.Autocompleters.classes.isValidCompletionPosition(yasqe)},
preProcessToken: function(token) {return YASQE.Autocompleters.classes.preProcessToken(yasqe, token)},
postProcessToken: function(token, suggestedString) {return YASQE.Autocompleters.classes.postProcessToken(yasqe, token, suggestedString)}
};
returnObj.bulk = true;
returnObj.async = true;
returnObj.autoShow = true;
returnObj.persistent = "customClasses";
returnObj.get = function(token, callback) {
var extractor = function(xmlOntology) {
var classArray = [];
$(xmlOntology).find("Class").each(function(i, objProp) {
var classUrl = $(objProp).attr('rdf:about');
if (classUrl) classArray.push(classUrl);
});
console.log(classArray);
callback(classArray);
};
fetchCompletions(extractor);
};
return returnObj;
};
YASQE.registerAutocompleter('classOntologyCompleter', customClassCompleter);
YASQE.defaults.autocompleters = $.grep(YASQE.defaults.autocompleters, function(completer) {return completer != 'classes'});
//now do the regular stuff of instantiating YASQE. To set your endpoint, add 'endpoint': '...' to the sparql config
var yasqe = YASQE(document.getElementById("yasqe"), {
sparql: {
showQueryButton: true
},
});
var yasr = YASR(document.getElementById("yasr"), {
//this way, the URLs in the results are prettified using the defined prefixes in the query
getUsedPrefixes: yasqe.getPrefixesFromQuery
});
/**
* Set some of the hooks to link YASR and YASQE
*/
yasqe.options.sparql.callbacks.complete = yasr.setResponse;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment