The AllegroGraph server can be scripted using Common Lisp or JavaScript. The easiest way to get started with Javascript is to open a repository in WebView and select 'Utilities->Scripts' from the navigation bar.
This document describes the programming interface available to JavaScript programs running in an AllegroGraph server.
OverviewThe simplest way to run scripts is to use the /eval
HTTP service, using a text/javascript
content-type for your request. This will cause the body of the request to be evaluated as JavaScript. If an error is raised, it will be returned as an HTTP error. If not, the result of evaluating the code is returned. Usually, you'll want to specify that you accept application/json
responses, and receive the result in that form. If your code returns a cursor, you can also specify one of the formats suitable for that kind of cursor (like application/trix
for triple cursors, or application/sparql-results+xml
for SPARQL SELECT cursors).
It is also possible to define custom HTTP services using JavaScript. Here, you'll probably want to put your code in a server-side script file, with a .js
extension, and use the x-scripts
header to ensure your service is available. See the server.defineService
API function described below.
Note that, unless you are using a session, each /eval
request will run in its own environment, and thus not see variables and state created by previous requests.
The Part
function, when given a string, tries to parse it using N-Triples syntax, and returns a part value. This constructor has the following properties, used for constructing parts in other ways:
literal(string [, language])
literalTyped(string, type)
resource(string [, namespace])
fromInt(int)
, fromUnsignedInt(int)
, fromLong(long)
, fromUnsignedLong(long)
fromFloat(float)
, fromDouble(double)
fromSeconds(seconds)
fromDate(milliseconds)
fromSeconds
, but divides its argument by 1000, and uses 1970 as reference point. This can be used to convert JavaScript Date
objects to parts, since those (when converted to number, which this function implicitly does) will return an amount of milliseconds since 1970.
Part values themselves have the following properties:
toString()
value
type
"literal"
or "resource"
, which identifies the type of the part.
language
null
if no language is specified.
datatype
null
if no language is specified.
Triple
objects, as returned by, for example, store.getTriples
, have object
, predicate
, subject
, and graph
properties, which will return parts, and an id
property, which will return an integer.
The top-level store
variable provides an interface to the current triple store (as determined by the URL from which the request is made). It is an instance of the the Store
type, and has the following properties:
name
getTriples(subject, predicate, object, graph)
Part
s or arrays of two Part
s. The latter case is used to specify range queries. Note that only one range query can be specified at a time.
getTriplesArray(subject, predicate, object, graph [, limit])
limit
is given, it provides a maximum length for the returned array.
deleteTriples(subject, predicate, object, graph)
getTriples
, null and undefined count as wildcards and arrays can be used to specify a range query.
addTriple(subject, predicate, object [, graph])
size
commit()
, rollback()
newBlankNode()
runSparql(query)
runProlog(query)
textIndices
createTextIndex(name [, options])
options
, if given, is an object containing options for the store. The properties predicates
, indexFields
, indexResources
, indexLitrals
, minimumWordSize
, stopWords
, and wordFilters
are supported, and accept values similar to those in the Lisp API.
dropTextIndex(name)
textSearch(query [, index])
index
is left off) for the given query string. Returns a triple cursor.
indices
addIndex(type)
posgi
-style specifier.
dropIndex(type)
Cursors have the following properties:
next()
null
if the cursor is exhausted. For triple cursors, this will return a triple. For row cursors, you get an array of parts.
close()
forEach(func)
func
on each row in the cursor.
collect([limit])
limit
is given, it limits the amount of rows that are collected.
count()
names
There is a toplevel namespaces
object with these methods:
collect()
{name, uri}
objects representing the namespaces.
register(name, uri)
unregister(name)
clear()
reset()
lookup(name)
null
if the namespace is not defined.
The GeoType
constructor provides an interface to geospatial encodings. Instances are created through these methods:
cartesian(store, xmin, xmax, ymin, ymax, strip-width)
spherical(store, strip-width [, unit])
unit
can be one of degree, radian, km, or mile, and defaults to degree.
GeoType
instances have these properties:
datatype
encode(x, y)
x
takes the longitude, and y
the latitude.
All graph operations are modeled around 'generators', which, conceptually, are functions that take a node, and produce a set of 'neighbors', by some definition of neighbor. The Generator
constructor can be called in two ways. In both, it takes a store as its first argument. When its second argument is a function, it wraps that function as a generator. The function should take a single part as an argument, and return an array of parts. When the second argument is not a function, the signature of the constructor is (store, objects, subjects, undirected)
, where each of the last tree arguments can be null
, a predicate part, or an array of predicates. The set of predicates indicated by the objects
parameter causes relations with those predicates to be followed from the starting node to any objects. The subjects
parameter does the reverseâit follows relations from object to subject. Finally, the undirected
parameter causes both of these to happen for the given predicates.
Generator instances have the following methods:
asMatrix(group, maxdepth)
group
parameter should be an array of parts, and maxdepth
an integer, indicating how many 'jumps' (starting from this group) should be pre-computed. The return value is again an instance of Generator
, and supports all the methods listed below.
breadthFirstPath(from, to [, maxdepth])
depthFirstPath(from, to [, maxdepth])
bidirectionalPath(from, to [, maxdepth])
maxdepth
jumps (or not giving up if no maximum depth is given). The return value will be an array of parts, or null
if no path was found.
breadthFirstPaths(from, to [, maxdepth])
bidirectionalPaths(from, to [, maxdepth])
neighbors(node [, maxdepth])
maxdepth
jumps from node
. If not given, maxdepth
defaults to 1.
cliques(node [, minsize [, maxsize]])
node
is part of. minsize
defaults to 3.
isClique(nodes)
Hash tables specialized for UPIs are exposed through the UPIHash
constructor. These are much faster than using regular JavaScript objects to keep a mapping on triple parts. The constructor takes an optional size argument. Instances have the following methods:
set(key, value)
value
under the part given as key
.
get(key)
del(key)
keys()
forEach(func)
func
with (key, value)
arguments for every entry in the table.
The server
variable exposes a method defineService(method, name, func)
which can be used to define a JavaScript custom service. method
must be a string (one of "get"
, "post"
, "put"
, and "delete"
), or an array of such strings. name
is a string that will name the service, and finally, func
must be a JavaScript function of one argument.
The service will be exposed at /catalogs/CATALOG/repositories/REPO/custom/NAME
and /repositories/REPO/custom/NAME
.
When the service is called, this function will be applied to a request object, and its return value will be returned to the user, using the same content-negotiation process that was used for /eval
(as described before).
The request object can be used to get information about the request. It has the following properties:
url
method
param(name)
application/x-www-form-urlencoded
. Returns null
if the parameter was not given.
paramArray(name)
param
, but returns an array, allowing you to see whether a parameter has been given multiple times.
params
body
null
if no body was given.
header(name)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4