JavaScript View

The JavaScript view provides programmatic access to various TopBraid features. When opened via Window > Show View > JavaScript, this window consist of an input field where arbitrary JavaScript code can be entered and executed by hitting enter. (To force a line break, press something like Shift-Enter). Internally, this feature uses an Oracle Nashorn engine to execute the JavaScript.

In addition to the standard JavaScript feature, this has support for the SHACL-JavaScript API and special objects as described in the following sub-sections.

TBS

The object TBS is native to TopBraid (i.e. will not work on all JavaScript platforms). It has the following functions.

TBS.function()

This function calls a given SPARQL function and returns the outcome of the function as another JavaScript RDF node.

For example TBS.function($data, "afn:localname", TermFactory.namedNode("http://example.org/ns#name")) returns the RDF literal "name".

TBC

The object TBC has the following functions:

$data

The currently open RDF graph can be accessed via $data. This has a function find(subject, predicate, object) which can be used to return a Stream of triples based on the given combination of nodes (each may be null). The stream can be walked with the next() method, which returns triples until it reaches the end.

The following example walks through all properties of the currently selected resource and adds them to the basket.

TBC.clearBasket();
var r = TBC.selected();
var s = $data.find(r, null, null);
for(var t = s.next(); t; t = s.next()) {
    TBC.addToBasket(t.predicate);
}

RDFQuery

A work-in progress library to query RDF from JavaScript is installed into the JavaScript view by default. See the file TopBraid/SHACL/rdfquery.js for details.