Package laas.openrobots.ontology.helpers
Class Helpers
java.lang.Object
laas.openrobots.ontology.helpers.Helpers
This class provides various static helper methods for some common parsing and formatting tasks.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
asSparqlRow
(com.hp.hpl.jena.rdf.model.Statement stmt) Formats a statement for inclusion in a SPARQL query.static String
cleanValue
(String value) Remove leading and trailing quotes and whitespace if needed from a string and unescape escaped sequences.static <T> T
deserialize
(String val, Class<T> type) Deserializes a string received from a client to a Java native object.static Date
getDateFromXSD
(String xsdDateTime) Returns a Java Date object from its XML Schema Dataype (XSD) representation in the GMT timezone.static String
getId
(com.hp.hpl.jena.ontology.OntResource resource) static String
getLabel
(com.hp.hpl.jena.ontology.OntResource resource) static String
static ResourceType
getType
(com.hp.hpl.jena.ontology.OntResource resource) static <T,
I> boolean implementsInterface
(T o, Class<I> i) This simple method return true is a given object implements a given interface.static String
literalToSparqlSyntax
(com.hp.hpl.jena.rdf.model.Literal lit) Formats a literal to a SPARQL-compatible string.static com.hp.hpl.jena.rdf.model.RDFNode
parseLiteral
(String lex, com.hp.hpl.jena.rdf.model.impl.ModelCom model) Parse a SPARQL string representing a literal to an actual JenaLiteral
.static <T> T
pickRandom
(Set<T> set) static String
protectValue
(String value) Protect a string by escaping the quotes and surrounding the string with quotes.reverseMap
(Map<K, V> m) Creates a new map, using the values of the initial one as keys for the new one, and keys of the initial one as a set of values for the new one.reverseSetMap
(Map<K, Set<V>> m) static <K,
V> List <K> sortByValue
(Map<K, V> m) This static method takes a map and return the list of the key sorted by their values in ascending order.static <T> String
stringify
(T o) Convert primitive and collection objects to a JSON-like string.Split a string into tokens separated by the given delimiter.tokenizeRule
(String rule) Tokenizes rules in the SWRL syntax.static void
unescapeJava
(Writer out, String str) Unescapes any Java literals found in theString
to aWriter
.static String
unescapeJava
(String str) Unescapes any Java literals found in theString
.
-
Constructor Details
-
Helpers
public Helpers()
-
-
Method Details
-
parseLiteral
public static com.hp.hpl.jena.rdf.model.RDFNode parseLiteral(String lex, com.hp.hpl.jena.rdf.model.impl.ModelCom model) throws IllegalStatementException Parse a SPARQL string representing a literal to an actual JenaLiteral
. The method actually returns aRDFNode
because if the literal is not recognized, it falls back on a generic RDFNode. To be sure the result is actually a literal, theRDFNode.isLiteral()
method can be used.- Parameters:
lex
- the string representing the literal.model
- the model linked to the ontology in which the literal is to be created- Returns:
- a RDFNode holding the literal
- Throws:
IllegalStatementException
- See Also:
-
literalToSparqlSyntax
Formats a literal to a SPARQL-compatible string.- Parameters:
lit
- a literal to be formatted- Returns:
- a string representing the literal with SPARQL syntax.
- See Also:
-
asSparqlRow
Formats a statement for inclusion in a SPARQL query.- Returns:
- the formatted statement.
- See Also:
-
getDateFromXSD
Returns a Java Date object from its XML Schema Dataype (XSD) representation in the GMT timezone. Note: the reverse operation (Date to XSD) can be achieved byOntModel.createTypedLiteral(Calendar.getInstance())
.- Parameters:
xsdDateTime
- a XSD formatted date- Returns:
- the corresponding Java Date object.
- Throws:
ParseException
-
getLabel
-
getLabel
-
getId
-
getType
-
tokenize
Split a string into tokens separated by the given delimiter. It protects quoted strings and arrays delimited by [] or {}.- Parameters:
str
- A string to tokenize.- Returns:
- A list of tokens
-
tokenizeRule
Tokenizes rules in the SWRL syntax. For instance: Male(?y), hasParent(?x, ?y) -> hasFather(?x, ?y) would be converted into 2 lists: ['Male(?y)', 'hasParent(?x, ?y)'] and ['hasFather(?x, ?y)']- Parameters:
rule
- the literal form of the rule, using the SWRL syntax- Returns:
- a pair of list of string: the first list is the body of the rule, the second list is the head. Each atom is left in its literal form
- Throws:
InvalidRuleException
-
sortByValue
This static method takes a map and return the list of the key sorted by their values in ascending order.- Type Parameters:
K
- The type of the map keysV
- The type of the map values- Parameters:
m
- The map whose key must by sorted- Returns:
- A list of key, sorted by their values in ascending order.
-
reverseMap
Creates a new map, using the values of the initial one as keys for the new one, and keys of the initial one as a set of values for the new one.- Type Parameters:
K
- The type of the map keysV
- The type of the map values- Parameters:
m
- The initial map.- Returns:
- A new map built by inversing the keys and values of the initial one. For each new key, a set is built for all the values (ie, the former keys).
-
reverseSetMap
-
implementsInterface
This simple method return true is a given object implements a given interface. It uses the Java reflection API.- Parameters:
o
- Any objecti
- An interface- Returns:
- true if the object implements the interface.
-
stringify
Convert primitive and collection objects to a JSON-like string. The conversion should be correct in standard cases, but it doesn't handle corner cases.- Parameters:
o
- The object to convert- Returns:
- A JSON-like string representating the object.
- See Also:
-
deserialize
public static <T> T deserialize(String val, Class<T> type) throws OntologyServerException, IllegalArgumentException Deserializes a string received from a client to a Java native object.- Type Parameters:
T
- type of the expected deserialized object.- Parameters:
val
- the input string, in a JSON-like formattype
- type of the expected deserialized object.- Returns:
- the newly created object
- Throws:
OntologyServerException
- thrown if an unparsable unicode character is encounteredIllegalArgumentException
- thrown if the string couldn't be deserialized to the expect object.
-
cleanValue
Remove leading and trailing quotes and whitespace if needed from a string and unescape escaped sequences.- Parameters:
value
- the string to clean.- Returns:
- The same, unescaped string with quotes and whitespaces trimmed.
- Throws:
OntologyServerException
- if a unparsable unicode character is found
-
protectValue
Protect a string by escaping the quotes and surrounding the string with quotes.- Parameters:
value
- The string to protect.- Returns:
- The same string between double quotes and with inside quotes escaped.
-
pickRandom
-
unescapeJava
Unescapes any Java literals found in the
This method comes from Apache Foundation, is released under Apache License 2.0 and can be found in org.apache.commons.lang.StringEscapeUtilsString
. For example, it will turn a sequence of'\'
and'n'
into a newline character, unless the'\'
is preceded by another'\'
.- Parameters:
str
- theString
to unescape, may be null- Returns:
- a new unescaped
String
,null
if null string input - Throws:
OntologyServerException
- if a unparsable unicode character is found
-
unescapeJava
Unescapes any Java literals found in the
String
to aWriter
.For example, it will turn a sequence of
'\'
and'n'
into a newline character, unless the'\'
is preceded by another'\'
.A
This method comes from Apache Foundation, is released under Apache License 2.0 and can be found in org.apache.commons.lang.StringEscapeUtilsnull
string input has no effect.- Parameters:
out
- theWriter
used to output unescaped charactersstr
- theString
to unescape, may be null- Throws:
IllegalArgumentException
- if the Writer isnull
IOException
- if error occurs on underlying WriterOntologyServerException
- if a unparsable unicode character is found
-