Class Helpers

java.lang.Object
laas.openrobots.ontology.helpers.Helpers

public class Helpers extends Object
This class provides various static helper methods for some common parsing and formatting tasks.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static String
    asSparqlRow(com.hp.hpl.jena.rdf.model.Statement stmt)
    Formats a statement for inclusion in a SPARQL query.
    static String
    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
    getLabel(com.hp.hpl.jena.ontology.OntResource resource, String languageCode)
     
    getType(com.hp.hpl.jena.ontology.OntResource resource)
     
    static <T, I> boolean
    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 Jena Literal.
    static <T> T
    pickRandom(Set<T> set)
     
    static String
    Protect a string by escaping the quotes and surrounding the string with quotes.
    static <K, V> Map<V,Set<K>>
    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.
    static <K, V> Map<V,Set<K>>
     
    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.
    static List<String>
    tokenize(String str, char delimiter)
    Split a string into tokens separated by the given delimiter.
    Tokenizes rules in the SWRL syntax.
    static void
    Unescapes any Java literals found in the String to a Writer.
    static String
    Unescapes any Java literals found in the String.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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 Jena Literal. The method actually returns a RDFNode because if the literal is not recognized, it falls back on a generic RDFNode. To be sure the result is actually a literal, the RDFNode.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

      public static String literalToSparqlSyntax(com.hp.hpl.jena.rdf.model.Literal lit)
      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

      public static String asSparqlRow(com.hp.hpl.jena.rdf.model.Statement stmt)
      Formats a statement for inclusion in a SPARQL query.
      Returns:
      the formatted statement.
      See Also:
    • getDateFromXSD

      public static Date getDateFromXSD(String xsdDateTime) throws ParseException
      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 by OntModel.createTypedLiteral(Calendar.getInstance()).
      Parameters:
      xsdDateTime - a XSD formatted date
      Returns:
      the corresponding Java Date object.
      Throws:
      ParseException
    • getLabel

      public static String getLabel(com.hp.hpl.jena.ontology.OntResource resource)
    • getLabel

      public static String getLabel(com.hp.hpl.jena.ontology.OntResource resource, String languageCode)
    • getId

      public static String getId(com.hp.hpl.jena.ontology.OntResource resource)
    • getType

      public static ResourceType getType(com.hp.hpl.jena.ontology.OntResource resource)
    • tokenize

      public static List<String> tokenize(String str, char delimiter)
      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

      public static Pair<List<String>,List<String>> tokenizeRule(String rule) throws InvalidRuleException
      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

      public 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.
      Type Parameters:
      K - The type of the map keys
      V - 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

      public static <K, V> Map<V,Set<K>> 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.
      Type Parameters:
      K - The type of the map keys
      V - 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

      public static <K, V> Map<V,Set<K>> reverseSetMap(Map<K,Set<V>> m)
    • implementsInterface

      public static <T, I> boolean implementsInterface(T o, Class<I> i)
      This simple method return true is a given object implements a given interface. It uses the Java reflection API.
      Parameters:
      o - Any object
      i - An interface
      Returns:
      true if the object implements the interface.
    • stringify

      public static <T> String stringify(T o)
      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 format
      type - type of the expected deserialized object.
      Returns:
      the newly created object
      Throws:
      OntologyServerException - thrown if an unparsable unicode character is encountered
      IllegalArgumentException - thrown if the string couldn't be deserialized to the expect object.
    • cleanValue

      public static String cleanValue(String value) throws OntologyServerException
      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

      public static String protectValue(String value)
      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

      public static <T> T pickRandom(Set<T> set)
    • unescapeJava

      public static String unescapeJava(String str) throws OntologyServerException

      Unescapes any Java literals found in the String. For example, it will turn a sequence of '\' and 'n' into a newline character, unless the '\' is preceded by another '\'.

      This method comes from Apache Foundation, is released under Apache License 2.0 and can be found in org.apache.commons.lang.StringEscapeUtils
      Parameters:
      str - the String 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

      public static void unescapeJava(Writer out, String str) throws IOException, OntologyServerException

      Unescapes any Java literals found in the String to a Writer.

      For example, it will turn a sequence of '\' and 'n' into a newline character, unless the '\' is preceded by another '\'.

      A null string input has no effect.

      This method comes from Apache Foundation, is released under Apache License 2.0 and can be found in org.apache.commons.lang.StringEscapeUtils
      Parameters:
      out - the Writer used to output unescaped characters
      str - the String to unescape, may be null
      Throws:
      IllegalArgumentException - if the Writer is null
      IOException - if error occurs on underlying Writer
      OntologyServerException - if a unparsable unicode character is found