diff --git a/index.html b/index.html index fdfdb088b5c1a56648ea5eaefff03132a2d04a66..39391e00c300d177ea3469357ff50f65aeed8961 100644 --- a/index.html +++ b/index.html @@ -28,6 +28,7 @@ <script type="text/javascript" src="js/authoring/formatSelections.js"></script> <script type="text/javascript" src="js/authoring/dictionaries.js"></script> + <script type="text/javascript" src="js/authoring/utils.js"></script> <!-- models --> <script type="text/javascript" src="js/authoring/models/xmlParser.js"></script> diff --git a/js/authoring/controllers/saveScenarioController.js b/js/authoring/controllers/saveScenarioController.js index bfe9702419ff248f07c47fead93ded1818702d29..87a37f2112abb42d2099750638c613f12fd5939b 100644 --- a/js/authoring/controllers/saveScenarioController.js +++ b/js/authoring/controllers/saveScenarioController.js @@ -11,18 +11,22 @@ $(function() { * Function saves current open scenario as a JSON file. */ function showSaveScenario() { - var saveScenarioElement = $("#saveScenario"); // get current scenario name var currentScenario = $("#lname")[0].innerHTML; - var json; + if (currentScenario == "") { + alert("Sie müssen erst ein Szenario erstellen, bevor Sie es speichern können."); + return false; + } + var jsonFile = null; // find current scenario in all scenarios - json = JSON.stringify(authorSystemContent.getScenario(currentScenario)); + var json = authorSystemContent.getScenario(currentScenario); + var jsonLD = JSON.stringify(json ? json.getABoxJSONLD() : {}); // set blob with JSON data - var data = new Blob([json], {type: "text/json;charset=utf8"}); + var data = new Blob([jsonLD], {type: "text/json;charset=utf8"}); // if file will be replaced by another one --> avoid memory leak if (jsonFile !== null) { @@ -31,12 +35,13 @@ function showSaveScenario() { // set JSON file jsonFile = window.URL.createObjectURL(data); + var saveScenarioElement = $("#saveScenario"); // change file name to current scenario name saveScenarioElement.children("a")[0].download = currentScenario + ".json"; - // add link and open download view saveScenarioElement.children("a")[0].href = jsonFile; + // show json in new window /*var url = "data:text/json;charset=utf8," + encodeURIComponent(JSON.stringify(myAuthorSystem)); window.open(url, "_blank"); diff --git a/js/authoring/models/aBoxJSONLDModel.js b/js/authoring/models/aBoxJSONLDModel.js index f02410c3fa2ae243b963a5d1970d68bde05058da..0721fd07b975134327d72591da152f35e3c5df54 100644 --- a/js/authoring/models/aBoxJSONLDModel.js +++ b/js/authoring/models/aBoxJSONLDModel.js @@ -7,11 +7,12 @@ // formatted and validated with the help of: http://json-ld.org/playground/ function ABoxJSONLD() { - // just a placeholder + // a little template for the graph (see below) this._typeId = {"@type": "@id"}; - // define the ontology's namespace which will remain just like this + // the actual A-Box this._jsonLD = { + // define the ontology's namespace which will remain just like this "@context": { "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#", "rdfs": "http://www.w3.org/2000/01/rdf-schema#", @@ -130,6 +131,7 @@ function ABoxJSONLD() { "@type": "rdfs:Literal" } }, + // and the actual content (a list of all JSON-LD individuals) "@graph": [ { "@id": "http://motivate-project.de/ontology/abox.owl", @@ -142,6 +144,12 @@ function ABoxJSONLD() { return this; } +// getters + +ABoxJSONLD.prototype.getABoxJSONLD = function() { + return this._jsonLD; +}; + ABoxJSONLD.prototype.getContext = function () { return this._jsonLD["@context"]; }; @@ -161,6 +169,7 @@ ABoxJSONLD.prototype.containsIndividual = function (namedIndividualJSONLD) { for (var i in graph) { var individual = graph[i]; + // make sure both have the same keys if (Object.keys(individual) != Object.keys(namedIndividualJSONLD)) continue; diff --git a/js/authoring/models/contextInfoModel.js b/js/authoring/models/contextInfoModel.js index d796c871469c5e76c5631552331de95c9ddfb0cc..2bbe4426851065028adb02339dc5253e1ab1ed15 100644 --- a/js/authoring/models/contextInfoModel.js +++ b/js/authoring/models/contextInfoModel.js @@ -140,11 +140,9 @@ ContextInformation.prototype.resetAllValues = function() { /***** JSON-LD formatting *****/ -ContextInformation.prototype.getJSONLDGraph = function () { +ContextInformation.prototype.getJSONLD = function () { if (this._chosenValue == "") return false; - var graphJSONLD = []; - // for this context information, create a new JSON-LD named individual object var contextInfoJSONLD = { "@id" : "abox:ContextInfo"+uuid4(), @@ -155,20 +153,22 @@ ContextInformation.prototype.getJSONLDGraph = function () { }; // if this context information has parameters, get their JSON-LD named individual objects - var parameterJSONLDArray = []; + var parameterJSONLDList = []; + var parameterReferenceJSONLDList = []; for (var i in this._parameters) { var parameterJSONLD = this._parameters[i].getJSONLD(); // add references to these named individuals to the context information individual - parameterJSONLDArray.push( {"@id" : parameterJSONLD["@id"]} ); + parameterReferenceJSONLDList.push( {"@id" : parameterJSONLD["@id"]} ); // add each parameter individual to the partial ontology graph - graphJSONLD.push(parameterJSONLD); + parameterJSONLDList.push(parameterJSONLD); + } + if (parameterReferenceJSONLDList.length == 1) + contextInfoJSONLD["kno:hasContextInformationParameter"] = parameterReferenceJSONLDList[0]; + else if (parameterReferenceJSONLDList.length > 1) + contextInfoJSONLD["kno:hasContextInformationParameter"] = parameterReferenceJSONLDList; + + return { + contextInfoJSONLD : contextInfoJSONLD, + parameterJSONLD : parameterJSONLDList } - if (parameterJSONLDArray.length == 1) - contextInfoJSONLD["kno:hasContextInformationParameter"] = parameterJSONLDArray[0]; - else if (parameterJSONLDArray.length > 1) - contextInfoJSONLD["kno:hasContextInformationParameter"] = parameterJSONLDArray; - - graphJSONLD.push(contextInfoJSONLD); - - return graphJSONLD; }; \ No newline at end of file diff --git a/js/authoring/models/scenarioModel.js b/js/authoring/models/scenarioModel.js index c884bac1df67a0007d44e350b53ee6232591e7dd..18fc8dc3109203bee2c8ce6e35eb36e9397b60d5 100644 --- a/js/authoring/models/scenarioModel.js +++ b/js/authoring/models/scenarioModel.js @@ -79,7 +79,6 @@ Scenario.prototype.removeConnection = function(connId) { } }; - /** * Goes through all units of this scenario and gets (copies of) their associated context information items. * Chosen values will be reset to "". @@ -114,4 +113,29 @@ Scenario.prototype.hasContext = function() { for (var i in this._units) if (this._units[i].getContextData().length != 0) return true; return false; +}; + + +/************* JSON-LD formatting *************/ + +/** + * Create an A-Box in JSON-LD for this scenario. + * @returns {ABoxJSONLD} The A-Box. + */ +Scenario.prototype.getABoxJSONLD = function() { + var aBoxJSONLD = new ABoxJSONLD(); + + // for each unit ... + for (var i in this._units) { + // ... get its JSON-LD (a list of JSON-LD named individuals) + var unitJSONLDGraph = this._units[i].getJSONLDGraph(); + // ... add all unique items (individuals) in this list to A-Box graph + for (var j in unitJSONLDGraph) { + var unitJSONLD = unitJSONLDGraph[j]; + // prevent unnecessary duplicates + if (!aBoxJSONLD.containsIndividual(unitJSONLD)) + aBoxJSONLD.addToGraph(unitJSONLD); + } + } + return aBoxJSONLD.getABoxJSONLD(); }; \ No newline at end of file diff --git a/js/authoring/models/unitModel.js b/js/authoring/models/unitModel.js index d6b7604a0f2ab44d7d2b42073020a3b22e24141a..db0b9655686f2581aeda68bd809f5d5a613041bb 100644 --- a/js/authoring/models/unitModel.js +++ b/js/authoring/models/unitModel.js @@ -80,18 +80,51 @@ Unit.prototype.removeContextInfoByIndex = function(index) { }; - +/***** JSON-LD formatting *****/ /** - * The following function and comment are taken from: - * https://github.com/University-of-Potsdam-MM/UP.App/blob/bdcd669ae4a75e4666b4bf7c0750a94262e9d5c1/www/js/lib/utils.js - * (courtesy of Alexander Kiy) - * - * Generates a uuid v4. Code is taken from broofas answer in http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript + * Produce a sub-graph for this unit (array of JSON-LD objects) to be added to JSON-LD + * NOTE: Metadata are not yet included in JSON-LD since a matching of this._metadata and T-Box predicates is needed first. + * @returns {Array} A sub-graph (array) */ -var uuid4 = function() { - return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { - var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); - return v.toString(16); - }); +Unit.prototype.getJSONLDGraph = function () { + // the sub-graph to be returned + var graphJSONLD = []; + + // for this unit, create a new JSON-LD named individual object + var unitJSONLD = { + "@id" : "abox:Unit"+uuid4(), + "@type" : [ "kno:LearningUnit", "owl:NamedIndividual" ], + "kno:hasLID" : this._uuid, + "kno:hasLogicalOperator" : (this._sat == "all") ? "AND" : "OR", + // TODO: all relations!! + "kno:hasPrerequisite" : [ ] + // TODO: metadata according to T-Box + }; + + // if this unit has context information , get their JSON-LD named individual objects + var contextReferenceJSONLDList = []; + for (var i in this._contextData) { + // get a JSON-LD-containing object for each context item + var contextJSONLD = this._contextData[i].getJSONLD(); + + // add references to these named JSON-LD individuals to the unit individual + contextReferenceJSONLDList.push( {"@id" : contextJSONLD.contextInfoJSONLD["@id"]} ); + + // add the context info JSON-LD individual to the partial ontology graph + graphJSONLD.push(contextJSONLD.contextInfoJSONLD); + // add each parameter JSON-LD individual to the partial ontology graph + for (var j in contextJSONLD.parameterJSONLD) + graphJSONLD.push(contextJSONLD.parameterJSONLD[j]); + } + // add context reference(s) to unit JSON-LD individual, if existent + if (contextReferenceJSONLDList.length == 1) + unitJSONLD["kno:hasMeasurableContextInformation"] = contextReferenceJSONLDList[0]; + else if (contextReferenceJSONLDList.length > 1) + unitJSONLD["kno:hasMeasurableContextInformation"] = contextReferenceJSONLDList; + + // add the unit's JSON-LD individual + graphJSONLD.push(unitJSONLD); + + return graphJSONLD; }; \ No newline at end of file diff --git a/js/authoring/utils.js b/js/authoring/utils.js new file mode 100644 index 0000000000000000000000000000000000000000..9e025d2c49ee1f463b44186e5532a1bdb09a59f8 --- /dev/null +++ b/js/authoring/utils.js @@ -0,0 +1,19 @@ +/** + * Created by elis on 07.01.2016. + */ + + + +/** + * The following function and comment are taken from: + * https://github.com/University-of-Potsdam-MM/UP.App/blob/bdcd669ae4a75e4666b4bf7c0750a94262e9d5c1/www/js/lib/utils.js + * (courtesy of Alexander Kiy) + * + * Generates a uuid v4. Code is taken from broofas answer in http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript + */ +var uuid4 = function() { + return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { + var r = Math.random()*16|0, v = c == 'x' ? r : (r&0x3|0x8); + return v.toString(16); + }); +}; \ No newline at end of file diff --git a/js/main.js b/js/main.js index 85802204aa92bcc1e1c4c5c99d8525310c7b8816..21847df4a787fd19bb3971a82f535ab7e7cf9be4 100644 --- a/js/main.js +++ b/js/main.js @@ -13,7 +13,6 @@ var contextList = new ContextInfoList(); // will be deleted once refactoring is complete //var myAuthorSystem = []; var authorSystemContent = new AuthorSystemContent(); -var aBoxJSONLD = new ABoxJSONLD(); // reloading var loadedData;