Class ReasoningUtils


  • public class ReasoningUtils
    extends java.lang.Object
    Utility methods for reasoning/constraint resolution. Introduced to clean up Resolver.
    Author:
    Holger Eichelberger
    • Constructor Detail

      • ReasoningUtils

        public ReasoningUtils()
    • Method Detail

      • inferTypeSafe

        public static IDatatype inferTypeSafe​(ConstraintSyntaxTree cst,
                                              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
      • setValue

        public static void setValue​(IDecisionVariable var,
                                    Constraint value)
        Sets a constraint value.
        Parameters:
        var - the variable to set the value on.
        value - the value to set
      • createTypeValueConstantSafe

        public static ConstraintSyntaxTree createTypeValueConstantSafe​(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 ConstraintSyntaxTree createAsTypeCast​(ConstraintSyntaxTree exp,
                                                            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 ConstraintSyntaxTree createAsTypeCast​(ConstraintSyntaxTree exp,
                                                            IDatatype sourceType,
                                                            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​(java.util.Set<IDecisionVariable> problemVariables)
        Method for printing all problem points.
        Parameters:
        problemVariables - the variables to print
      • toStringAttributes

        public static java.lang.String toStringAttributes​(IDecisionVariable variable)
        Method for displaying all variable attributes.
        Parameters:
        variable - variable
        Returns:
        String of all attributes of the variable.
      • printModelElement

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

        public static java.lang.String toIvmlString​(Constraint constraint)
        Turns a constraint into a string.
        Parameters:
        constraint - the constraint
        Returns:
        the representing string
      • toIvmlString

        public static java.lang.String toIvmlString​(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​(Configuration config,
                                              java.lang.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​(java.lang.String text,
                                            java.lang.Iterable<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
      • isOverriddenSlot

        public static boolean isOverriddenSlot​(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​(Compound cmp,
                                     java.lang.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​(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 Value getRelevantValue​(AbstractVariable decl,
                                             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 Value> D getRelevantValue​(AbstractVariable decl,
                                                           IDecisionVariable var,
                                                           boolean incremental,
                                                           java.lang.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​(Compound cmp,
                                      java.util.Set<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​(Value val,
                                                   java.util.Set<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​(java.util.Set<Compound> compounds,
                                        java.util.Set<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​(Compound comp,
                                         java.util.Set<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​(java.util.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 IDatatype getDeepestContainedType​(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​(Constraint constraint,
                                                           EvaluationVisitor evaluator)
        Prints a constraint evaluation result (for debugging).
        Parameters:
        constraint - the constraint
        evaluator - the evaluator
      • getConstraintValueExpression

        public static ConstraintSyntaxTree getConstraintValueExpression​(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​(java.util.Collection<Constraint> collection,
                                     java.util.Collection<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​(java.util.Map<K,​?> map,
                                         java.util.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 java.util.Set<Compound> collectRefines​(IDatatype start,
                                                             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​(Compound cmp,
                                           IDatatype exclude,
                                           boolean add,
                                           java.util.Set<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 ConstraintSyntaxTree createParentExpression​(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> java.util.Map<K,​V> copyMapNull​(java.util.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