Class GraphModel

java.lang.Object
org.praxislive.project.GraphModel

public final class GraphModel extends Object
Model for graph and subgraph scripts, encompassing the element tree and related information.

A graph model can be parsed from a script (eg. contents of .pxr / .pxg file), or created from the serialization data returned from SerializableProtocol. A graph model can also be written back out to a script for execution or saving to a file.

A graph model, and the underlying tree, are immutable. Transformative methods return a new model instance.

  • Method Details

    • root

      public GraphElement.Root root()
      Access the root of the element tree. For a subgraph, the root will be synthetic.
      Returns:
      root element
    • context

      public Optional<URI> context()
      Access the optional context (eg. working dir) for resolving relative file values.
      Returns:
      optional context
    • withContext

      public GraphModel withContext(URI context)
      Create a new graph model based on this one, with a different context. The context is used to relativize resources when writing. Use null to create a model without context.
      Parameters:
      context - new resource context
      Returns:
      new graph model
    • withRename

      public GraphModel withRename(String id)
      Create a new graph model based on this one, with a renamed root. All other elements are kept the same. This method will throw an exception if called on a (subgraph) model with a synthetic root.
      Parameters:
      id - new root ID
      Returns:
      new graph model
    • withTransform

      public GraphModel withTransform(Consumer<GraphBuilder.Root> transform)
      Create a new graph model based on this one after applying the provided transform function. The builder passed into the transform will be pre-configured with all the elements of this model.
      Parameters:
      transform - transforming builder consumer
      Returns:
      new graph model
    • write

      public void write(Appendable target) throws IOException
      Write the model as a script to the given target.
      Parameters:
      target - write destination
      Throws:
      IOException
    • writeToString

      public String writeToString()
      Write the graph model to a String. This is shorthand for passing in a StringBuilder to write(java.lang.Appendable).

      The output of this method is suitable for parsing back into a model, as distinct from the output of toString().

      Returns:
      model as script
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • fromSerializedComponent

      public static GraphModel fromSerializedComponent(String componentID, org.praxislive.core.types.PMap data)
      Create a graph model from the serialization data of a single component. The data should be in the format specified by SerializableProtocol. The graph model will consist of a synthetic root with a single child component element with the given ID and data.
      Parameters:
      componentID - id of the component
      data - serialization data
      Returns:
      created graph model
    • fromSerializedRoot

      public static GraphModel fromSerializedRoot(String rootID, org.praxislive.core.types.PMap data)
      Create a graph model from the serialization data of a complete root. The data should be in the format specified by SerializableProtocol.
      Parameters:
      rootID - if of the root
      data - serialization data
      Returns:
      created graph model
    • fromSerializedSubgraph

      public static GraphModel fromSerializedSubgraph(org.praxislive.core.types.PMap data)
      Create a graph model from the serialization data of a container. The data should be in the format specified by SerializableProtocol. The graph model will consist of a synthetic root with all the children and connections of the container. Properties of the container itself will be ignored.
      Parameters:
      data - container serialization data
      Returns:
      created graph model
    • fromSerializedSubgraph

      public static GraphModel fromSerializedSubgraph(org.praxislive.core.types.PMap data, Predicate<String> filter)
      Create a graph model from the serialization data of a container. The data should be in the format specified by SerializableProtocol. The graph model will consist of a synthetic root with all the children of the container that pass the given child ID filter. Connections will be filtered to those between included components. Properties of the container itself will be ignored.
      Parameters:
      data - container serialization data
      filter - child ID filter
      Returns:
      created graph model
    • of

      public static GraphModel of(GraphElement.Root root)
      Create a graph model of the provided root element.
      Parameters:
      root - root element
      Returns:
      created graph model
    • of

      public static GraphModel of(GraphElement.Root root, URI context)
      Create a graph model of the provided root element and context.
      Parameters:
      root - root element
      context - resource context
      Returns:
      created graph model
    • parse

      public static GraphModel parse(String graph) throws ParseException
      Parse the given graph script into a graph model. The script must be a valid full root graph.
      Parameters:
      graph - graph script
      Returns:
      created graph model
      Throws:
      ParseException - if the graph is invalid
    • parse

      public static GraphModel parse(URI context, String graph) throws ParseException
      Parse the given graph script into a graph model. Relative resources will be resolved against the provided context. The script must be a full root graph.
      Parameters:
      context - resource context
      graph - graph script
      Returns:
      created graph model
      Throws:
      ParseException - if the graph is invalid
    • parseSubgraph

      public static GraphModel parseSubgraph(String graph) throws ParseException
      Parse the given subgraph script into a graph model. The script must be a valid subgraph script.
      Parameters:
      graph - subgraph script
      Returns:
      created graph model
      Throws:
      ParseException - if the subgraph is invalid
    • parseSubgraph

      public static GraphModel parseSubgraph(URI context, String graph) throws ParseException
      Parse the given subgraph script into a graph model. Relative resources will be resolved against the provided context. The script must be a valid subgraph script.
      Parameters:
      context - resource context
      graph - subgraph script
      Returns:
      created graph model
      Throws:
      ParseException - if the subgraph is invalid