Class ExpressionTrees

java.lang.Object
org.cyclops.integrateddynamics.core.evaluate.expression.tree.ExpressionTrees

public final class ExpressionTrees extends Object
Helpers for expression trees: serialization, path navigation and queries.
  • Field Details

    • MAX_DEPTH

      public static final int MAX_DEPTH
      Maximum tree depth accepted when deserializing, to protect against malicious or corrupt data.
      See Also:
    • MAX_PARAMETER_NAME_LENGTH

      public static final int MAX_PARAMETER_NAME_LENGTH
      Maximum length of a parameter name.
      See Also:
  • Method Details

    • serialize

      public static net.minecraft.nbt.CompoundTag serialize(ValueDeseralizationContext context, Expression expression)
    • serialize

      public static void serialize(net.minecraft.world.level.storage.ValueOutput output, Expression expression)
    • deserialize

      public static Expression deserialize(ValueDeseralizationContext context, net.minecraft.nbt.CompoundTag tag) throws EvaluationException
      Throws:
      EvaluationException
    • deserialize

      public static Expression deserialize(net.minecraft.world.level.storage.ValueInput input) throws EvaluationException
      Throws:
      EvaluationException
    • serializeNode

      public static void serializeNode(net.minecraft.world.level.storage.ValueOutput output, ExpressionNode node)
    • deserializeNode

      public static ExpressionNode deserializeNode(net.minecraft.world.level.storage.ValueInput input, int depth, int parameterCount) throws EvaluationException
      Throws:
      EvaluationException
    • serializeValue

      public static net.minecraft.nbt.CompoundTag serializeValue(ValueDeseralizationContext context, IValue value)
      Parameters:
      context - For serializing the value.
      value - A value.
      Returns:
      The value as its type writes it.
    • deserializeValue

      public static <T extends IValue> T deserializeValue(ValueDeseralizationContext context, IValueType<T> valueType, net.minecraft.nbt.CompoundTag tag)
      Parameters:
      context - For deserializing the value.
      valueType - The type of the value.
      tag - The value as its type writes it.
      Returns:
      The value.
      Throws:
      RuntimeException - If the tag is not a value of the type.
    • asRoot

      public static ExpressionNode.Operator asRoot(ExpressionNode node)
      The root of an expression is an operator, so a value, parameter, reference or empty slot as a whole expression is the input of the identity operator, which is not written in its text.
      Parameters:
      node - A whole expression.
      Returns:
      The root for it.
    • getRootValue

      @Nullable public static ExpressionNode getRootValue(ExpressionNode root)
      Parameters:
      root - The root of an expression.
      Returns:
      The input of the identity operator if the root was made by asRoot(ExpressionNode) for a node that is not an operator, which is the whole expression in its text, otherwise null.
    • get

      @Nullable public static ExpressionNode get(ExpressionNode root, int[] path)
      Parameters:
      root - A root node.
      path - A path of child indexes.
      Returns:
      The node at the given path, or null if the path is invalid.
    • replace

      public static ExpressionNode replace(ExpressionNode root, int[] path, ExpressionNode replacement)
      Parameters:
      root - A root node.
      path - A valid path of child indexes.
      replacement - The node to put at the given path.
      Returns:
      A new root node.
    • isOperatorPath

      public static boolean isOperatorPath(ExpressionNode root, int[] path)
      Parameters:
      root - A root node.
      path - A path.
      Returns:
      If the path points to an operator node.
    • append

      public static int[] append(int[] path, int index)
    • parent

      public static int[] parent(int[] path)
    • countEmpty

      public static int countEmpty(ExpressionNode node)
    • depth

      public static int depth(ExpressionNode node)
      Parameters:
      node - A node.
      Returns:
      The depth, where a leaf has depth 0.
    • countNodes

      public static int countNodes(ExpressionNode node)
    • countOperators

      public static int countOperators(ExpressionNode node)
      Parameters:
      node - A node.
      Returns:
      The number of operator nodes in it, not counting operators used as values.
    • getRefIds

      public static it.unimi.dsi.fastutil.ints.IntList getRefIds(ExpressionNode node)
      Parameters:
      node - A node.
      Returns:
      All referenced variable ids, in order of first occurrence.
    • containsParam

      public static boolean containsParam(ExpressionNode node, int index)
    • removeParam

      public static ExpressionNode removeParam(ExpressionNode node, int removedIndex)
      Parameters:
      node - A node.
      removedIndex - A removed parameter index.
      Returns:
      The node where params referring to the removed index become empty, and higher indexes shift down.
    • isPlainOperator

      public static boolean isPlainOperator(Expression expression)
      Check if the given expression can be stored as a plain operator variable, which is the case for a single operator without parameters and only variable references as inputs.
      Parameters:
      expression - An expression.
      Returns:
      If it is equivalent to a plain operator variable.
    • getPlainOperatorVariableIds

      public static int[] getPlainOperatorVariableIds(Expression expression)
      Parameters:
      expression - A plain operator expression, see isPlainOperator(Expression).
      Returns:
      The referenced variable ids.
    • fromPlainOperator

      public static Expression fromPlainOperator(IOperator operator, int[] variableIds)
      Parameters:
      operator - An operator.
      variableIds - Variable ids.
      Returns:
      An expression for a plain operator variable.