Class JSONLDHierarchicalProcessor

java.lang.Object
org.eclipse.rdf4j.rio.jsonld.JSONLDHierarchicalProcessor

public class JSONLDHierarchicalProcessor extends Object
Converts a JSON-LD object to a hierarchical form
  • Field Details

  • Constructor Details

    • JSONLDHierarchicalProcessor

      public JSONLDHierarchicalProcessor()
  • Method Details

    • fromJsonLdObject

      public static Object fromJsonLdObject(Object jsonLdObject)
      Converts a JSON-LD object to a hierarchical JSON-LD object
      Parameters:
      jsonLdObject - JSON-LD object to be converted. Gets modified during processing
      Returns:
      hierarchical JSON-LD object
    • expandInDepth

      private static Object expandInDepth(Object input)
      Expands the JSON-LD object to a hierarchical shape.

      As the first level of nodes in the object can be either a triple or a whole graph we first expand the graph nodes and after that we expand the default graph.

      The different graphs are processed independently to keep them in insulation.

      Parameters:
      input - the JSON-LD object. Gets modified during processing.
      Returns:
    • expandContextInDepth

      private static Object expandContextInDepth(Object input)
      Transforms a JSON-LD object to a more human-readable hierarchical form.

      The steps performed are

      1. Take all triples which will take part of the processing and add them to a separate map
      2. Create a separate list the triples sorted by number of predicates in the descending order
      3. Select a root to start
      4. Take this root from the graph and start a DFS traversing. For each traversed node
        1. Mark this node as visited
        2. Find all sub-nodes (effectively objects in triples in which the current node is subject)
        3. Expand the sub-nodes (replace them with their full version) and add them to the traversing if the following conditions are met
          • sub-node is IRI or BlankNode
          • sub-node has not been expanded already in the current path
          • sub-node is not the same as it's parent
      5. If the visited list shows there are still unvisited nodes choose a new root from the list of sorted nodes and start another traversal
      Parameters:
      input - JSON-LD object. Gets modified during processing
      Returns:
      the hierarchical JSON-LD object
    • getNextRoot

      private static String getNextRoot(Set<String> visited, List<String> sortedNodes)
      Returns the next node to be a root. Chooses the first non-visited node from the sortedNodes list.
      Parameters:
      visited - contains the visited nodes so-far
      sortedNodes - contains the nodes in a specific order. This list will get modified by the method!
      Returns:
      root for the next tree
    • getNodesOrder

      private static List<String> getNodesOrder(Map<String,Object> graph)
      Returns the nodes in a JSON-LD object ordered by the number of their subnodes (predicates in which the nodes are subjects)
      Parameters:
      graph - JSON-LD object
      Returns:
      List with the nodes' ids