class WOQLQuery()
def __init__(query=None, graph="schema")
defines the internal functions of the woql query object - the language API is defined in WOQLQuery
Arguments:
query
(dict
): json-ld query for initialisationgraph
(str
): graph that this query is appled to, default to be schema/main
def __add__(other)
Creates a logical AND with the argument passed, for WOQLQueries.
Arguments:
other
(WOQLQuery object
): None
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def __and__(other)
Creates a logical AND with the argument passed, for WOQLQueries.
Arguments:
other
(WOQLQuery object
): None
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def __or__(other)
Creates a logical OR with the argument passed, for WOQLQueries.
Arguments:
other
(WOQLQuery object
): None
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def __invert__()
Creates a logical Not with the argument passed, for WOQLQueries.
Arguments:
other
(WOQLQuery object
): None
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def execute(client, commit_msg=None, file_dict=None)
Executes the query using the passed client to connect to a server
Arguments:
client
(Client object
): client that provide connection to the database for the query to execute.commit_msg
(str
): optional, commit message for this query. Recommended for query that carrries an update.file_dict
(``): File dictionary to be associated with post name => filename, for multipart POST
def to_json()
Dumps the JSON-LD format of the query in a json string
def from_json(input_json)
Set a query from a JSON-LD json string
def to_dict()
Give the dictionary that represents the query in JSON-LD format.
def from_dict(dictdata)
Set a query from a dictionary that represents the query in JSON-LD format.
def load_vocabulary(client)
Queries the schema graph and loads all the ids found there as vocabulary that can be used without prefixes ignoring blank node ids
def using(collection, subq=None)
Use a specific data product for the enclosed query
Arguments:
collection
(str
): the name of the data product
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def comment(comment, subq=None)
Adds a text comment to a query - can also be used to wrap any part of a query to turn it off
Arguments:
comment
(str
): text comment
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def select(*args)
Filters the query so that only the variables included in [V1...Vn] are returned in the bindings
Arguments:
args
: only these variables are returned
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def distinct(*args)
Ensures that the solutions for the variables [V1...Vn] are distinct
Arguments:
args
: The variables to make distinct with the final argument being a query.
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def woql_and(*args)
Creates a logical AND of the arguments
Commonly used to combine WOQLQueries.
Arguments:
args
(WOQLQuery objects
): None
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def woql_or(*args)
Creates a logical OR of the arguments
Arguments:
args
(WOQLQuery objects
): None
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def woql_from(graph, query=None)
Specifies the database URL that will be the default database for the enclosed query
Arguments:
graph
(str
): url of the databasequery
(WOQLQuery object
): None
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def into(graph_descriptor, query)
Sets the current output graph for writing output to.
Arguments:
graph_descriptor
(str
): output graphquery
(WOQLQuery object
): None
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def triple(sub, pred, obj, opt=False)
Creates a triple pattern matching rule for the triple [S, P, O] (Subject, Predicate, Object)
Arguments:
sub
(str
): Subject, has to be a node (URI)pred
(str
): Predicate, can be variable (prefix with "v:") or nodeobj
(str
): Object, can be variable or node or valueopt
(bool
): weather or not this triple is optional, default to be False
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def added_triple(sub, pred, obj, opt=False)
Creates a triple pattern matching rule for the triple [S, P, O] (Subject, Predicate, Object) added to the current commit.
Arguments:
sub
(str
): Subjectpred
(str
): Predicateobj
(str
): Objectopt
(bool
): weather or not this triple is optional, default to be False
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def removed_triple(sub, pred, obj, opt=False)
Creates a triple pattern matching rule for the triple [S, P, O] (Subject, Predicate, Object) added to the current commit.
Arguments:
sub
(str
): Subjectpred
(str
): Predicateobj
(str
): Objectopt
(bool
): weather or not this triple is optional, default to be False
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def quad(sub, pred, obj, graph, opt=False)
Creates a pattern matching rule for the quad [S, P, O, G] (Subject, Predicate, Object, Graph)
Arguments:
sub
(str
): Subjectpre
(str
): Predicateobj
(str
): Objectgra
(str
): Graphopt
(bool
): weather or not this quad is optional, default to be False
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def added_quad(sub, pred, obj, graph, opt=False)
Creates a pattern matching rule for the quad [S, P, O, G] (Subject, Predicate, Object, Graph) added to the current commit.
Arguments:
sub
(str
): Subjectpre
(str
): Predicateobj
(str
): Objectgra
(str
): Graphopt
(bool
): weather or not this quad is optional, default to be False
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def removed_quad(sub, pred, obj, graph, opt=False)
Creates a pattern matching rule for the quad [S, P, O, G] (Subject, Predicate, Object, Graph) added to the current commit.
Arguments:
sub
(str
): Subjectpre
(str
): Predicateobj
(str
): Objectgra
(str
): Graphopt
(bool
): weather or not this quad is optional, default to be False
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def string(input_str)
Transforms the given string into the proper json-ld form
Arguments:
input_str
(str
): the given input string
Returns:
dict
:
def boolean(input_bool)
Transforms the given bool object into the proper json-ld form
Arguments:
input_bool
(bool
): the given input string
Returns:
dict
:
def datetime(input_obj)
Transforms the given datetime object into the proper json-ld form
Arguments:
input_obj
(str
): the given input dateTime object
Returns:
dict
:
def sub(parent, child)
Returns true if child is a sub-class of parent, according to the current DB schema
Arguments:
parent
(str
): the parent class to be checkedchild
(str
): the child class to be checked
Returns:
bool
:
def eq(left, right)
Matches if a is equal to b
Arguments:
left
(str
): object in the graphright
(str
): object in the graph
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def update_document(docjson, json_or_iri=None)
Update a document in the database
Arguments:
docjson
(JSON
): object to be updatedjson_or_iri
(str
): the output ID, or a JSON to compare the ID against
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def insert_document(docjson, json_or_iri=None)
Insert a document into the database
Arguments:
docjson
(JSON
): object to be insertedjson_or_iri
(str
): the output ID, or a JSON to compare the ID against
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def delete_document(json_or_iri)
Delete a document into the database
Arguments:
docjson
(JSON
): object to be deletedjson_or_iri
(str
): the output ID, or a JSON to compare the ID against
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def read_document(iri, output_var)
Read a document from the database
query = (WOQLQuery().triple('v:TermId', 'rdf:type', '@schema:Term') & WOQLQuery().triple('v:TermCountId','term','v:TermId') & WOQLQuery().triple('v:DocumentId', 'terms', 'v:TermCountId') & WOQLQuery().read_document('v:TermId','v:TermDoc'))
def get(as_vars, query_resource=None)
Takes an as structure
def put(as_vars, query, query_resource=None)
Takes an array of variables, an optional array of column names
def file(fpath, opts=None)
Provides details of a file source in a JSON format that includes a URL property
Arguments:
fpath
(dict
): file data source in a JSON formatopts
(input options
): optional
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def once(query=None)
Obtains only one result from subquery
Arguments:
query
(WOQLQuery object
): NoneReturns
: None----------
: NoneWOQLQuery object
: query object that can be chained and/or executed
def remote(uri, opts=None)
Provides details of a remote data source in a JSON format that includes a URL property
Arguments:
uri
(str
): remote data sourceopts
(input options
): optional
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def delete_triple(subject, predicate, object_or_literal)
Deletes any triples that match the rule [subject, predicate, object]
Arguments:
subject
(str
): Subjectpredicate
(str
): Predicateobject_or_literal
(str
): Object or Literal
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def add_triple(subject, predicate, object_or_literal)
Adds triples according to the the pattern [subject, predicate, object]
Arguments:
subject
(str
): Subjectpredicate
(str
): Predicateobject_or_literal
(str
): Object or Literal
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def delete_quad(subject, predicate, object_or_literal, graph=None)
Deletes any quads that match the rule [subject, predicate, object, graph]
Arguments:
subject
(str
): Subjectpredicate
(str
): Predicateobject_or_literal
(str
): Object or Literalgraph
(str
): Graph
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def add_quad(subject, predicate, object_or_literal, graph)
Adds quads according to the pattern [subject, predicate, object, graph]
Arguments:
subject
(str
): Subjectpredicate
(str
): Predicateobject_or_literal
(str
): Object or Literalgraph
(str
): Graph
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def trim(untrimmed, trimmed)
A trimmed version of untrimmed (with leading and trailing whitespace removed) is copied into trimmed
Arguments:
untrimmed
(str
): original stringtrimmed
(str
): WOQL varible storing the result string
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def eval(arith, res)
Evaluates the Arithmetic Expression Arith and copies the output to variable V
Arguments:
arith
(WOQLQuery or dict
): query or JSON-LD representing the queryres
(str
): output variable
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def plus(*args)
Adds numbers N1...Nn together
Arguments:
args
(int or float
): numbers to add together
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def minus(*args)
Adds numbers N1...Nn together
Arguments:
args
(int or float
): numbers to add together
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def times(*args)
Multiplies numbers N1...Nn together
Arguments:
args
(int or float
): numbers to be multiplied
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def divide(*args)
Divides numbers N1...Nn by each other left, to right precedence
Arguments:
args
(int or float
): numbers to be divided
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def div(*args)
Division - integer division - args are divided left to right
Arguments:
args
(int or float
): numbers for division
Returns:
WOQLQuery
: query object that can be chained and/or execute
def exp(first, second)
Raises A to the power of B
Arguments:
first
(int or float
): base numbersecond
(int or float
): power of
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def floor(user_input)
The floor function of a real number x denotes the greatest integer less than or equal to x.
Arguments:
user_input
(int or float
): number whose floor needs to be calculated
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def isa(element, of_type)
Matches if element is a member of a certain type, according to the current state of the DB
Arguments:
element
(str
): element to be checkedof_type
(str
): type to be checked
Returns:
bool
:
def like(left, right, dist)
Matches left string to right string with a distance
Arguments:
left
(str
): first string to compareright
(str
): second string to comparedist
(str
): Hamming distance between left and right
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def less(left, right)
Compares the value of v1 against v2 and returns true if v1 is less than v2
Arguments:
left
(str
): first variable to compareright
(str
): second variable to compare
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def greater(left, right)
Compares the value of v1 against v2 and returns true if v1 is greater than v2
Arguments:
left
(str
): first variable to compareright
(str
): second variable to compare
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def opt(query=None)
The Query in the Optional argument is specified as optional
Arguments:
query
(WOQLQuery object
): None
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def unique(prefix, key_list, uri)
Generates an ID for a node as a function of the passed VariableList with a specific prefix (URL base)(A.K.A Hashing) If the values of the passed variables are the same, the output will be the same
Arguments:
prefix
(str
): prefix for the idkey_list
(str
): variable to generate id foruri
(str
): the variable to hold the id
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def idgen(prefix, input_var_list, output_var)
Generates an ID for a node as a function of the passed VariableList with a specific prefix (URL base). If the values of the passed variables are the same, the output will be the same
Arguments:
prefix
(str
): prefix for the idinput_var_list
(str or list
): variable to generate id foroutput_var
(str
): the variable to hold the id
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def random_idgen(prefix, key_list, uri)
Randomly generates an ID and appends to the end of the key_list.
Arguments:
prefix
(str
): prefix for the idkey_list
(str
): variable to generate id foruri
(str
): the variable to hold the id
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def upper(left, right)
Changes a string to upper-case - input is in left, output in right
Arguments:
left
(str
): input stringright
(str
): stores output
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def lower(left, right)
Changes a string to lower-case - input is in u, output in l
Arguments:
left
(str
): input stringright
(str
): stores output
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def pad(user_input, pad, length, output)
Pads out the string input to be exactly len long by appending the pad character pad to form output
Arguments:
user_input
(str
): input stringpad
(str
): padding character(s)length
(int
): length to padoutput
(str
): stores output
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def split(user_input, glue, output)
Splits a variable apart (input) into a list of variables (output) by separating the strings together with separator
Arguments:
user_input
(str
): input string or WOQL variable "v:"glue
(str
): character string to separate string into listoutput
(str
): WOQL variable that stores output list
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def dot(document, field, value)
Iterates through a list and returns a value for each member
Arguments:
dictionary
: a WOQL dictionary or variable representing a dictionaryfield
(str
): a string representing the field or key to access the dictionaryvalue
: a WOQL value representing the result
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def member(member, mem_list)
Iterates through a list and returns a value for each member
Arguments:
member
(str
): a WOQL variable representing an element of the listmem_list
(str
): a WOQL list variable
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def concat(concat_list, result)
Concatenates the list of variables into a string and saves the result in v
Arguments:
concat_list
(list
): list of variables to concatenateresult
(str
): saves the results
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def join(user_input, glue, output)
Joins a list variable together (input) into a string variable (output) by glueing the strings together with glue
Arguments:
user_input
(list
): a list of variablesglue
(str
): jioining character(s)output
(str
): variable that sotres output
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def sum(user_input, output)
Joins a list variable containing numbers together (input) into a single number
containing the sum.
Arguments:
user_input
(list
): a variable containing a list of numbersoutput
(str
): a variable that stores the output
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def start(start, query=None)
Specifies that the start of the query returned
Arguments:
start
(int
): index of the frist result got returnedquery
(WOQLQuery object
): None
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def limit(limit, query=None)
Specifies that only the first Number of rows will be returned
Arguments:
limit
(int
): number of maximum results returnedquery
(WOQLQuery object
): None
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def regexp(pattern, reg_str, reg_list)
Regular Expression Call
pattern is a regex pattern (.) using normal regular expression syntax, the only unusual thing is that special characters have to be escaped twice, s is the string to be matched and m is a list of matches: e.g. WOQLQuery().regexp("(.).", "hello", ["v:All", "v:Sub"])
Arguments:
pattern
(str
): regex patternreg_str
(str
): string to be matchedreg_list
(str or list or dict
): store list of matches
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def length(var_list, var_len)
Length
Calculates the length of a list
Arguments:
var_list
(list
): list of elementsvar_len
(num
): number of eleemnts
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def woql_not(query=None)
Creates a logical NOT of the arguments
Arguments:
query
(WOQLQuery object
): NoneReturns
: None----------
: NoneWOQLQuery object
: query object that can be chained and/or executed
def immediately(query=None)
Immediately runs side-effects without backtracking
Arguments:
query
(WOQLQuery object
): NoneReturns
: None----------
: NoneWOQLQuery object
: query object that can be chained and/or executed
def count(countvar, query=None)
Counds the number of solutions in the given query
Arguments:
result
(A variable or non-negative integer with the count
): Nonequery
(The query from which to count the number of results
): NoneReturns
: None----------
: NoneWOQLQuery object
: query object that can be chained and/or executed
def cast(val, user_type, result, literal_type=None)
Changes the type of va to type and saves the return in vb
Arguments:
val
(str
): original variableuser_type
(str
): type to be changedresult
(str
): save the return variableliteral_type
(str
): literal type ofval
, can be used to treatval
as a literal rather than an object or variable in the WOQL query. If literal type is "owl:Thing" or "node",val
will be treated as object in the graph
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def type_of(value, vtype)
Sets the given value and type for cursor.
Arguments:
value
(str
): Value which needs to be setvtype
(type
): Type which needs to be set
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def order_by(*args, order="asc")
Orders the results by the list of variables including in gvarlist, asc_or_desc is a WOQL.asc or WOQ.desc list of variables
Arguments:
gvarlist
(list or dict of WOQLQuery().asc or WOQLQuery().desc objects
): Nonequery
(WOQLQuery object
): None
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def group_by(group_vars, template, output, groupquery=None)
Groups the results of groupquery together by the list of variables group_vars, using the variable template as a grouping and saves the result into variable output.
Arguments:
group_vars
(list or str or Var object
): list of variables to grouptemplate
(dict or list or str
): template of data to group with free variable(s)output
(str
): output variablegroupquery
(dict
): None
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def true()
Sets true for cursor type.
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def path(subject, pattern, obj, path=None)
Create a path object constructed by the rules specified with pattern.
Arguments:
subject
(str
): a woql subject, the node that the path startedpattern
(str
): a pattern which specified the edges the path is consisted of. It uses pattern construction syntax such as:
- '(scm:edge1, scm:edge2)+' for repeated pattern,
- 'scm:edge1|scm:edge2' for 'or' pattern,
- '<scm:edge' for reverse pattern, and
- '(scm:edge1)[n,m] for pattern between n and m times'
obj
(str
): a woql object, the node that the path endedpath
(str
): output variable
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def size(graph, size)
Sets the given graph and size for cursor.
Arguments:
graph
(Graph which needs to be set as resource
): Nonesize
(Size which needs to be set
): None
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def triple_count(graph, triple_count)
Sets the given triple count and size for cursor.
Arguments:
graph
(Graph which needs to be set as resource
): Nonetriple_count
(Triple count which needs to be set
): None
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def star(graph=None, subj=None, pred=None, obj=None)
Selects everything as triples in the graph identified by GraphIRI into variables Subj, Pred, Obj - by default they are "v:Subject", "v:Predicate", "v:Object"
Arguments:
GraphIRI
(str
): graphIRISubj
(str
): target subjectPred
(str
): target predicateObj
(str
): target object
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def all(subj=None, pred=None, obj=None, graph=None)
Calls the star method and returns the result of the same.
Arguments:
subj
(str
): target subjectpred
(str
): target predicateobj
(str
): target objectgraph
(str
): graphIRI
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def graph(g)
Used to specify that the rest of the query should use the graph g in calls to add_quad, quad, etc
Arguments:
g
(str
): target graph
Returns:
WOQLQuery object
: query object that can be chained and/or execute
def vars(*args)
Generate variables to be used in WOQLQueries
Arguments:
args
: string arguments
Returns:
tuple/string
: args prefixed with "v:"
def variables(*args)
Generate variables to be used in WOQLQueries
Arguments:
args
: string arguments
Returns:
tuple/string
: args prefixed with "v:"