概要
blocklyでobjectを動かす。
bdpediaからsparqlで取得してみる。
stageを書く。
function dbpedia(query) {
var endpoint = "http://ja.dbpedia.org/sparql";
query = "SELECT DISTINCT * WHERE { dbpedia-ja:" + query + " dbpedia-owl:abstract ?abstract .}"
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyState == 4 && xhr.status == 200)
{
var json = JSON.parse(xhr.responseText);
alert(json.results.bindings[0].abstract.value)
}
};
xhr.open("POST", endpoint, true);
xhr.setRequestHeader("Accept", "application/sparql-results+json");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send("query=" + encodeURIComponent(query));
}
objectを書く。
Blockly.Blocks.dbpedia = {
init: function() {
this.jsonInit({
message0: "dbpedia %1",
args0: [{
type: "input_value",
name: "TEXT"
}],
previousStatement: null,
nextStatement: null,
style: "text_blocks",
tooltip: Blockly.Msg.TEXT_PRINT_TOOLTIP,
helpUrl: Blockly.Msg.TEXT_PRINT_HELPURL
})
}};
Blockly.JavaScript.dbpedia = function(a) {
return "dbpedia(" + (Blockly.JavaScript.valueToCode(a, "TEXT", Blockly.JavaScript.ORDER_NONE) || "''") + ");\n"
};
フローを書く。
成果物
以上。