Class ReasoningUtils

java.lang.Object
net.ssehub.easy.reasoning.sseReasoner.model.ReasoningUtils

public class ReasoningUtils extends Object
Utility methods for reasoning/constraint resolution.
Author:
Holger Eichelberger
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final boolean
     
    private static final net.ssehub.easy.basics.logger.EASyLoggerFactory.EASyLogger
     
    static final net.ssehub.easy.basics.pool.Pool<Set<net.ssehub.easy.varModel.model.datatypes.Compound>>
    A set pool for instances of Set<Compound>.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T, S extends T>
    void
    addAll(Collection<T> target, S[] source)
    Adds all elements from source to target.
    static void
    addRefines(net.ssehub.easy.varModel.model.datatypes.Compound cmp, Set<net.ssehub.easy.varModel.model.datatypes.Compound> result)
    Adds all refines of cmp to result.
    private static void
    collectRefines(net.ssehub.easy.varModel.model.datatypes.Compound cmp, net.ssehub.easy.varModel.model.datatypes.IDatatype exclude, boolean add, Set<net.ssehub.easy.varModel.model.datatypes.Compound> result)
    Collects all refines from cmp excluding all originating at exclude, adding or removing following compounds based on add storing the results in result.
    static Set<net.ssehub.easy.varModel.model.datatypes.Compound>
    collectRefines(net.ssehub.easy.varModel.model.datatypes.IDatatype start, net.ssehub.easy.varModel.model.datatypes.IDatatype exclude)
    Collects all refines from start excluding all originating at exclude.
    static <K, V> Map<K,V>
    copyMapNull(Map<K,V> source)
    Returns a copy of the source map.
    static int
    countSlots(net.ssehub.easy.varModel.model.datatypes.Compound cmp, String name, boolean stopGreater1)
    Counts the number of slots with given name in the refines hierarchy of cmp.
    static net.ssehub.easy.varModel.cst.ConstraintSyntaxTree
    createAsTypeCast(net.ssehub.easy.varModel.cst.ConstraintSyntaxTree exp, net.ssehub.easy.varModel.model.datatypes.IDatatype targetType)
    Creates a type cast ("asType" operation, if needed) from the type of exp to targetType applied to exp.
    static net.ssehub.easy.varModel.cst.ConstraintSyntaxTree
    createAsTypeCast(net.ssehub.easy.varModel.cst.ConstraintSyntaxTree exp, net.ssehub.easy.varModel.model.datatypes.IDatatype sourceType, net.ssehub.easy.varModel.model.datatypes.IDatatype targetType)
    Creates a type cast ("asType" operation, if needed) from sourceType to targetType applied to exp.
    static net.ssehub.easy.varModel.cst.ContainerOperationCall
    createContainerCall(net.ssehub.easy.varModel.cst.ConstraintSyntaxTree container, net.ssehub.easy.varModel.model.datatypes.Operation op, net.ssehub.easy.varModel.cst.ConstraintSyntaxTree iterEx, net.ssehub.easy.varModel.model.DecisionVariableDeclaration... decl)
    Creates a container operation call ("shortcut").
    static net.ssehub.easy.varModel.cst.ConstraintSyntaxTree
    createParentExpression(net.ssehub.easy.varModel.confModel.IDecisionVariable variable)
    Creates an expression representing the parent of variable.
    static net.ssehub.easy.varModel.model.values.Value
    createTypeValue(net.ssehub.easy.varModel.model.datatypes.IDatatype type)
    Creates a type value.
    static net.ssehub.easy.varModel.cst.ConstraintSyntaxTree
    createTypeValueConstant(net.ssehub.easy.varModel.model.datatypes.IDatatype type)
    Creates a type value constraint tree node.
    static net.ssehub.easy.varModel.cst.ConstraintSyntaxTree
    createTypeValueConstantSafe(net.ssehub.easy.varModel.model.datatypes.IDatatype type)
    Creates a type value constraint tree node and logs occurring exceptions.
    static net.ssehub.easy.varModel.cst.ConstraintSyntaxTree
    getConstraintValueExpression(net.ssehub.easy.varModel.model.values.Value value)
    Returns the expression for a constraint to be created for a constraint variable.
    static net.ssehub.easy.varModel.model.datatypes.IDatatype
    getDeepestContainedType(net.ssehub.easy.varModel.model.datatypes.Container cnt)
    Returns the deepest contained type, i.e., for a nested container the deeply nested (base) type.
    static net.ssehub.easy.varModel.model.values.Value
    getRelevantValue(net.ssehub.easy.varModel.model.AbstractVariable decl, net.ssehub.easy.varModel.confModel.IDecisionVariable var, boolean incremental)
    Returns the relevant value for reasoning.
    static <D extends net.ssehub.easy.varModel.model.values.Value>
    D
    getRelevantValue(net.ssehub.easy.varModel.model.AbstractVariable decl, net.ssehub.easy.varModel.confModel.IDecisionVariable var, boolean incremental, Class<D> filter)
    Returns the relevant value for reasoning.
    static boolean
    getUsedCompoundTypes(net.ssehub.easy.varModel.model.values.Value val, Set<net.ssehub.easy.varModel.model.datatypes.Compound> result)
    Returns the used types if value is a container value.
    static net.ssehub.easy.varModel.model.datatypes.IDatatype
    inferTypeSafe(net.ssehub.easy.varModel.cst.ConstraintSyntaxTree cst, net.ssehub.easy.varModel.model.datatypes.IDatatype dflt)
    Infers the type of cst using dflt as fallback.
    static boolean
    isNestedContainer(net.ssehub.easy.varModel.model.datatypes.IDatatype type)
    Returns whether type is a type-nested container.
    static boolean
    isOverriddenSlot(net.ssehub.easy.varModel.model.AbstractVariable decl)
    Returns whether decl is an overridden slot.
    static void
    printConstraintEvaluationResult(net.ssehub.easy.varModel.model.Constraint constraint, net.ssehub.easy.varModel.cstEvaluation.EvaluationVisitor evaluator)
    Prints a constraint evaluation result (for debugging).
    static void
    printConstraints(String text, Iterable<net.ssehub.easy.varModel.model.Constraint> constraints)
    Prints a constraints collection, each constraint including top-levle parent and system identity hashcodes [DEBUGGING].
    static void
    Method for displaying failed constraints and assignments.
    static void
    printModelElement(net.ssehub.easy.varModel.confModel.IDecisionVariable variable)
    Method for printing info about IDecisionVariable.
    static void
    printModelElements(net.ssehub.easy.varModel.confModel.Configuration config, String comment)
    Method for printing all variables, their states and values.
    static void
    printProblemPoints(Set<net.ssehub.easy.varModel.confModel.IDecisionVariable> problemVariables)
    Method for printing all problem points.
    static void
    purgeRefines(Set<net.ssehub.easy.varModel.model.datatypes.Compound> compounds, Set<net.ssehub.easy.varModel.model.datatypes.Compound> result)
    Purges all refined compounds mentioned in compounds.
    private static void
    purgeRefines(net.ssehub.easy.varModel.model.datatypes.Compound comp, Set<net.ssehub.easy.varModel.model.datatypes.Compound> result)
    Purges all refined compounds of comp from result.
    static void
    removeAll(Collection<net.ssehub.easy.varModel.model.Constraint> collection, Collection<net.ssehub.easy.varModel.model.Constraint> remove)
    Removes constraints from a constraints collection printing those constraints that have been removed.
    static <K> void
    removeAll(Map<K,?> map, Collection<K> remove)
    Removes all elements in remove as keys from map.
    static void
    setValue(net.ssehub.easy.varModel.confModel.IDecisionVariable var, net.ssehub.easy.varModel.model.Constraint value)
    Sets a constraint value.
    static String
    toIvmlString(net.ssehub.easy.varModel.cst.ConstraintSyntaxTree cst)
    Turns a constraint syntax tree into a string.
    static String
    toIvmlString(net.ssehub.easy.varModel.model.Constraint constraint)
    Turns a constraint into a string.
    static String
    toStringAttributes(net.ssehub.easy.varModel.confModel.IDecisionVariable variable)
    Method for displaying all variable attributes.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEBUG

      public static final boolean DEBUG
      See Also:
    • SET_COMPOUND_POOL

      public static final net.ssehub.easy.basics.pool.Pool<Set<net.ssehub.easy.varModel.model.datatypes.Compound>> SET_COMPOUND_POOL
      A set pool for instances of Set<Compound>.
    • LOGGER

      private static final net.ssehub.easy.basics.logger.EASyLoggerFactory.EASyLogger LOGGER
  • Constructor Details

    • ReasoningUtils

      public ReasoningUtils()
  • Method Details

    • inferTypeSafe

      public static net.ssehub.easy.varModel.model.datatypes.IDatatype inferTypeSafe(net.ssehub.easy.varModel.cst.ConstraintSyntaxTree cst, net.ssehub.easy.varModel.model.datatypes.IDatatype dflt)
      Infers the type of cst using dflt as fallback.
      Parameters:
      cst - the expression to infer the type for
      dflt - the default
      Returns:
      the type of the expression or dflt in case of failures
    • createTypeValue

      public static net.ssehub.easy.varModel.model.values.Value createTypeValue(net.ssehub.easy.varModel.model.datatypes.IDatatype type) throws net.ssehub.easy.varModel.model.values.ValueDoesNotMatchTypeException
      Creates a type value.
      Parameters:
      type - the datatype
      Returns:
      the constant value
      Throws:
      net.ssehub.easy.varModel.model.values.ValueDoesNotMatchTypeException - if the value cannot be created
    • createTypeValueConstant

      public static net.ssehub.easy.varModel.cst.ConstraintSyntaxTree createTypeValueConstant(net.ssehub.easy.varModel.model.datatypes.IDatatype type) throws net.ssehub.easy.varModel.model.values.ValueDoesNotMatchTypeException
      Creates a type value constraint tree node.
      Parameters:
      type - the datatype
      Returns:
      the constant value constraint tree node
      Throws:
      net.ssehub.easy.varModel.model.values.ValueDoesNotMatchTypeException - if the value cannot be created
    • setValue

      public static void setValue(net.ssehub.easy.varModel.confModel.IDecisionVariable var, net.ssehub.easy.varModel.model.Constraint value)
      Sets a constraint value.
      Parameters:
      var - the variable to set the value on.
      value - the value to set
    • createTypeValueConstantSafe

      public static net.ssehub.easy.varModel.cst.ConstraintSyntaxTree createTypeValueConstantSafe(net.ssehub.easy.varModel.model.datatypes.IDatatype type)
      Creates a type value constraint tree node and logs occurring exceptions.
      Parameters:
      type - the datatype
      Returns:
      the constant value constraint tree node
    • createAsTypeCast

      public static net.ssehub.easy.varModel.cst.ConstraintSyntaxTree createAsTypeCast(net.ssehub.easy.varModel.cst.ConstraintSyntaxTree exp, net.ssehub.easy.varModel.model.datatypes.IDatatype targetType)
      Creates a type cast ("asType" operation, if needed) from the type of exp to targetType applied to exp.
      Parameters:
      exp - the expression to apply the type cast operation to
      targetType - the target type
      Returns:
      the resulting expression
    • createAsTypeCast

      public static net.ssehub.easy.varModel.cst.ConstraintSyntaxTree createAsTypeCast(net.ssehub.easy.varModel.cst.ConstraintSyntaxTree exp, net.ssehub.easy.varModel.model.datatypes.IDatatype sourceType, net.ssehub.easy.varModel.model.datatypes.IDatatype targetType)
      Creates a type cast ("asType" operation, if needed) from sourceType to targetType applied to exp.
      Parameters:
      exp - the expression to apply the type cast operation to
      sourceType - the source type
      targetType - the target type (nothing happens if targetType is null or sourceType is identical to targetType)
      Returns:
      the resulting expression
    • printProblemPoints

      public static void printProblemPoints(Set<net.ssehub.easy.varModel.confModel.IDecisionVariable> problemVariables)
      Method for printing all problem points.
      Parameters:
      problemVariables - the variables to print
    • toStringAttributes

      public static String toStringAttributes(net.ssehub.easy.varModel.confModel.IDecisionVariable variable)
      Method for displaying all variable attributes.
      Parameters:
      variable - variable
      Returns:
      String of all attributes of the variable.
    • printModelElement

      public static void printModelElement(net.ssehub.easy.varModel.confModel.IDecisionVariable variable)
      Method for printing info about IDecisionVariable.
      Parameters:
      variable - Variable to be printed out.
    • toIvmlString

      public static String toIvmlString(net.ssehub.easy.varModel.model.Constraint constraint)
      Turns a constraint into a string.
      Parameters:
      constraint - the constraint
      Returns:
      the representing string
    • toIvmlString

      public static String toIvmlString(net.ssehub.easy.varModel.cst.ConstraintSyntaxTree cst)
      Turns a constraint syntax tree into a string.
      Parameters:
      cst - may be null, print "-" then
      Returns:
      the string representation
    • printModelElements

      public static void printModelElements(net.ssehub.easy.varModel.confModel.Configuration config, String comment)
      Method for printing all variables, their states and values.
      Parameters:
      config - Configuration to work with.
      comment - Comment for printing.
    • printConstraints

      public static void printConstraints(String text, Iterable<net.ssehub.easy.varModel.model.Constraint> constraints)
      Prints a constraints collection, each constraint including top-levle parent and system identity hashcodes [DEBUGGING].
      Parameters:
      text - text to be printed before (may be null, ignored then)
      constraints - the constraints to be printed
    • printFailedElements

      public static void printFailedElements(FailedElements failedElements)
      Method for displaying failed constraints and assignments.
      Parameters:
      failedElements - the failed elements
    • createContainerCall

      public static net.ssehub.easy.varModel.cst.ContainerOperationCall createContainerCall(net.ssehub.easy.varModel.cst.ConstraintSyntaxTree container, net.ssehub.easy.varModel.model.datatypes.Operation op, net.ssehub.easy.varModel.cst.ConstraintSyntaxTree iterEx, net.ssehub.easy.varModel.model.DecisionVariableDeclaration... decl)
      Creates a container operation call ("shortcut").
      Parameters:
      container - the container to operate on
      op - the operation
      iterEx - the iterator expression
      decl - the declarators
      Returns:
      the created call
    • isOverriddenSlot

      public static boolean isOverriddenSlot(net.ssehub.easy.varModel.model.AbstractVariable decl)
      Returns whether decl is an overridden slot.
      Parameters:
      decl - the declaration of the slot to search for
      Returns:
      true if overridden, false else
    • countSlots

      public static int countSlots(net.ssehub.easy.varModel.model.datatypes.Compound cmp, String name, boolean stopGreater1)
      Counts the number of slots with given name in the refines hierarchy of cmp.
      Parameters:
      cmp - the compound to start searching
      name - the name of the slot to search for
      stopGreater1 - stop searching if we have more than one matching slot
      Returns:
      the number of slots
    • isNestedContainer

      public static boolean isNestedContainer(net.ssehub.easy.varModel.model.datatypes.IDatatype type)
      Returns whether type is a type-nested container.
      Parameters:
      type - the type
      Returns:
      true for a nested collection, false else
    • getRelevantValue

      public static net.ssehub.easy.varModel.model.values.Value getRelevantValue(net.ssehub.easy.varModel.model.AbstractVariable decl, net.ssehub.easy.varModel.confModel.IDecisionVariable var, boolean incremental)
      Returns the relevant value for reasoning.
      Parameters:
      decl - the declaration
      var - the decision variable
      incremental - are we in incremental mode
      Returns:
      the value, may be null
    • getRelevantValue

      public static <D extends net.ssehub.easy.varModel.model.values.Value> D getRelevantValue(net.ssehub.easy.varModel.model.AbstractVariable decl, net.ssehub.easy.varModel.confModel.IDecisionVariable var, boolean incremental, Class<D> filter)
      Returns the relevant value for reasoning.
      Type Parameters:
      D - the value type
      Parameters:
      decl - the declaration
      var - the decision variable
      incremental - are we in incremental mode
      filter - in case that only a value of the specific type shall be returned
      Returns:
      the value, may be null
    • addRefines

      public static void addRefines(net.ssehub.easy.varModel.model.datatypes.Compound cmp, Set<net.ssehub.easy.varModel.model.datatypes.Compound> result)
      Adds all refines of cmp to result.
      Parameters:
      cmp - the compound to derive the refined compound hierarchy from
      result - the set of refined compounds
    • getUsedCompoundTypes

      public static boolean getUsedCompoundTypes(net.ssehub.easy.varModel.model.values.Value val, Set<net.ssehub.easy.varModel.model.datatypes.Compound> result)
      Returns the used types if value is a container value.
      Parameters:
      val - the value
      result - the result set to be modified as a side effect
      Returns:
      true if val is a container value, false else
    • purgeRefines

      public static void purgeRefines(Set<net.ssehub.easy.varModel.model.datatypes.Compound> compounds, Set<net.ssehub.easy.varModel.model.datatypes.Compound> result)
      Purges all refined compounds mentioned in compounds.
      Parameters:
      compounds - the compounds to purge
      result - the purged compounds (to be modified as a side effect)
    • purgeRefines

      private static void purgeRefines(net.ssehub.easy.varModel.model.datatypes.Compound comp, Set<net.ssehub.easy.varModel.model.datatypes.Compound> result)
      Purges all refined compounds of comp from result.
      Parameters:
      comp - the compound to purge the refined ones for
      result - modified as a side effect
    • addAll

      public static <T, S extends T> void addAll(Collection<T> target, S[] source)
      Adds all elements from source to target.
      Type Parameters:
      T - the target type
      S - the source type, a subtype of <T>
      Parameters:
      target - the target collection
      source - the source array (may be null, then nothing happens)
    • getDeepestContainedType

      public static net.ssehub.easy.varModel.model.datatypes.IDatatype getDeepestContainedType(net.ssehub.easy.varModel.model.datatypes.Container cnt)
      Returns the deepest contained type, i.e., for a nested container the deeply nested (base) type.
      Parameters:
      cnt - the container type
      Returns:
      the deeply nested type
    • printConstraintEvaluationResult

      public static void printConstraintEvaluationResult(net.ssehub.easy.varModel.model.Constraint constraint, net.ssehub.easy.varModel.cstEvaluation.EvaluationVisitor evaluator)
      Prints a constraint evaluation result (for debugging).
      Parameters:
      constraint - the constraint
      evaluator - the evaluator
    • getConstraintValueExpression

      public static net.ssehub.easy.varModel.cst.ConstraintSyntaxTree getConstraintValueExpression(net.ssehub.easy.varModel.model.values.Value value)
      Returns the expression for a constraint to be created for a constraint variable.
      Parameters:
      value - the value to create the constraint for
      Returns:
      the constraint expression, may be null
    • removeAll

      public static void removeAll(Collection<net.ssehub.easy.varModel.model.Constraint> collection, Collection<net.ssehub.easy.varModel.model.Constraint> remove)
      Removes constraints from a constraints collection printing those constraints that have been removed. [debugging]
      Parameters:
      collection - the collections to remove the constraints from
      remove - the constraints to remove
    • removeAll

      public static <K> void removeAll(Map<K,?> map, Collection<K> remove)
      Removes all elements in remove as keys from map.
      Type Parameters:
      K - the key type
      Parameters:
      map - the map to remove from
      remove - the elements to remove
    • collectRefines

      public static Set<net.ssehub.easy.varModel.model.datatypes.Compound> collectRefines(net.ssehub.easy.varModel.model.datatypes.IDatatype start, net.ssehub.easy.varModel.model.datatypes.IDatatype exclude)
      Collects all refines from start excluding all originating at exclude.
      Parameters:
      start - the start type (an empty set will be returned if start is not a Compound)
      exclude - an optional type excluding the entire refinement hierarchy originating at exclude if exclude is a compound, ignored if null
      Returns:
      the set of refined compounds, must be released via SET_COMPOUND_POOL
    • collectRefines

      private static void collectRefines(net.ssehub.easy.varModel.model.datatypes.Compound cmp, net.ssehub.easy.varModel.model.datatypes.IDatatype exclude, boolean add, Set<net.ssehub.easy.varModel.model.datatypes.Compound> result)
      Collects all refines from cmp excluding all originating at exclude, adding or removing following compounds based on add storing the results in result.
      Parameters:
      cmp - the type to collect for
      exclude - an optional type excluding the entire refinement hierarchy originating at exclude if exclude is a compound, ignored if null
      add - whether datatypes shall be added or removed from result.
      result - incrementally built up result, may be modified as a side effect
    • createParentExpression

      public static net.ssehub.easy.varModel.cst.ConstraintSyntaxTree createParentExpression(net.ssehub.easy.varModel.confModel.IDecisionVariable variable)
      Creates an expression representing the parent of variable.
      Parameters:
      variable - the variable
      Returns:
      the expression, null if no expression can be constructed
    • copyMapNull

      public static <K, V> Map<K,V> copyMapNull(Map<K,V> source)
      Returns a copy of the source map. Turns an empty map into null.
      Type Parameters:
      K - the key
      V - the value
      Parameters:
      source - the source map, may be null
      Returns:
      the copy or null if there was no or just an empty source