Class ReasoningUtils
- java.lang.Object
-
- net.ssehub.easy.reasoning.sseReasoner.model.ReasoningUtils
-
public class ReasoningUtils extends java.lang.ObjectUtility methods for reasoning/constraint resolution. Introduced to clean upResolver.- Author:
- Holger Eichelberger
-
-
Field Summary
Fields Modifier and Type Field Description static booleanDEBUGprivate static EASyLoggerFactory.EASyLoggerLOGGERstatic Pool<java.util.Set<Compound>>SET_COMPOUND_POOLA set pool for instances ofSet.
-
Constructor Summary
Constructors Constructor Description ReasoningUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static <T,S extends T>
voidaddAll(java.util.Collection<T> target, S[] source)Adds all elements fromsourcetotarget.static voidaddRefines(Compound cmp, java.util.Set<Compound> result)Adds all refines ofcmptoresult.private static voidcollectRefines(Compound cmp, IDatatype exclude, boolean add, java.util.Set<Compound> result)Collects all refines fromcmpexcluding all originating atexclude, adding or removing following compounds based onaddstoring the results inresult.static java.util.Set<Compound>collectRefines(IDatatype start, IDatatype exclude)Collects all refines fromstartexcluding all originating atexclude.static <K,V>
java.util.Map<K,V>copyMapNull(java.util.Map<K,V> source)Returns a copy of thesourcemap.static intcountSlots(Compound cmp, java.lang.String name, boolean stopGreater1)Counts the number of slots with givennamein the refines hierarchy ofcmp.static ConstraintSyntaxTreecreateAsTypeCast(ConstraintSyntaxTree exp, IDatatype targetType)Creates a type cast ("asType" operation, if needed) from the type ofexptotargetTypeapplied toexp.static ConstraintSyntaxTreecreateAsTypeCast(ConstraintSyntaxTree exp, IDatatype sourceType, IDatatype targetType)Creates a type cast ("asType" operation, if needed) fromsourceTypetotargetTypeapplied toexp.static ContainerOperationCallcreateContainerCall(ConstraintSyntaxTree container, Operation op, ConstraintSyntaxTree iterEx, DecisionVariableDeclaration... decl)Creates a container operation call ("shortcut").static ConstraintSyntaxTreecreateParentExpression(IDecisionVariable variable)Creates an expression representing the parent ofvariable.static ValuecreateTypeValue(IDatatype type)Creates a type value.static ConstraintSyntaxTreecreateTypeValueConstant(IDatatype type)Creates a type value constraint tree node.static ConstraintSyntaxTreecreateTypeValueConstantSafe(IDatatype type)Creates a type value constraint tree node and logs occurring exceptions.static ConstraintSyntaxTreegetConstraintValueExpression(Value value)Returns the expression for a constraint to be created for a constraint variable.static IDatatypegetDeepestContainedType(Container cnt)Returns the deepest contained type, i.e., for a nested container the deeply nested (base) type.static ValuegetRelevantValue(AbstractVariable decl, IDecisionVariable var, boolean incremental)Returns the relevant value for reasoning.static <D extends Value>
DgetRelevantValue(AbstractVariable decl, IDecisionVariable var, boolean incremental, java.lang.Class<D> filter)Returns the relevant value for reasoning.static booleangetUsedCompoundTypes(Value val, java.util.Set<Compound> result)Returns the used types ifvalueis a container value.static IDatatypeinferTypeSafe(ConstraintSyntaxTree cst, IDatatype dflt)Infers the type ofcstusingdfltas fallback.static booleanisNestedContainer(IDatatype type)Returns whethertypeis a type-nested container.static booleanisOverriddenSlot(AbstractVariable decl)Returns whetherdeclis an overridden slot.static voidprintConstraintEvaluationResult(Constraint constraint, EvaluationVisitor evaluator)Prints a constraint evaluation result (for debugging).static voidprintConstraints(java.lang.String text, java.lang.Iterable<Constraint> constraints)Prints a constraints collection, each constraint including top-levle parent and system identity hashcodes [DEBUGGING].static voidprintFailedElements(FailedElements failedElements)Method for displaying failed constraints and assignments.static voidprintModelElement(IDecisionVariable variable)Method for printing info aboutIDecisionVariable.static voidprintModelElements(Configuration config, java.lang.String comment)Method for printing all variables, their states and values.static voidprintProblemPoints(java.util.Set<IDecisionVariable> problemVariables)Method for printing all problem points.static voidpurgeRefines(java.util.Set<Compound> compounds, java.util.Set<Compound> result)Purges all refined compounds mentioned incompounds.private static voidpurgeRefines(Compound comp, java.util.Set<Compound> result)Purges all refined compounds ofcompfromresult.static voidremoveAll(java.util.Collection<Constraint> collection, java.util.Collection<Constraint> remove)Removes constraints from a constraints collection printing those constraints that have been removed.static <K> voidremoveAll(java.util.Map<K,?> map, java.util.Collection<K> remove)Removes all elements inremoveas keys frommap.static voidsetValue(IDecisionVariable var, Constraint value)Sets a constraint value.static java.lang.StringtoIvmlString(ConstraintSyntaxTree cst)Turns a constraint syntax tree into a string.static java.lang.StringtoIvmlString(Constraint constraint)Turns a constraint into a string.static java.lang.StringtoStringAttributes(IDecisionVariable variable)Method for displaying all variable attributes.
-
-
-
Field Detail
-
DEBUG
public static final boolean DEBUG
- See Also:
- Constant Field Values
-
SET_COMPOUND_POOL
public static final Pool<java.util.Set<Compound>> SET_COMPOUND_POOL
A set pool for instances ofSet.
-
LOGGER
private static final EASyLoggerFactory.EASyLogger LOGGER
-
-
Method Detail
-
inferTypeSafe
public static IDatatype inferTypeSafe(ConstraintSyntaxTree cst, IDatatype dflt)
Infers the type ofcstusingdfltas fallback.- Parameters:
cst- the expression to infer the type fordflt- the default- Returns:
- the type of the expression or
dfltin case of failures
-
createTypeValue
public static Value createTypeValue(IDatatype type) throws ValueDoesNotMatchTypeException
Creates a type value.- Parameters:
type- the datatype- Returns:
- the constant value
- Throws:
ValueDoesNotMatchTypeException- if the value cannot be created
-
createTypeValueConstant
public static ConstraintSyntaxTree createTypeValueConstant(IDatatype type) throws ValueDoesNotMatchTypeException
Creates a type value constraint tree node.- Parameters:
type- the datatype- Returns:
- the constant value constraint tree node
- Throws:
ValueDoesNotMatchTypeException- if the value cannot be created
-
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 ofexptotargetTypeapplied toexp.- Parameters:
exp- the expression to apply the type cast operation totargetType- 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) fromsourceTypetotargetTypeapplied toexp.- Parameters:
exp- the expression to apply the type cast operation tosourceType- the source typetargetType- the target type (nothing happens iftargetTypeis null orsourceTypeis identical totargetType)- 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 aboutIDecisionVariable.- 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
-
createContainerCall
public static ContainerOperationCall createContainerCall(ConstraintSyntaxTree container, Operation op, ConstraintSyntaxTree iterEx, DecisionVariableDeclaration... decl)
Creates a container operation call ("shortcut").- Parameters:
container- the container to operate onop- the operationiterEx- the iterator expressiondecl- the declarators- Returns:
- the created call
-
isOverriddenSlot
public static boolean isOverriddenSlot(AbstractVariable decl)
Returns whetherdeclis an overridden slot.- Parameters:
decl- the declaration of the slot to search for- Returns:
trueif overridden,falseelse
-
countSlots
public static int countSlots(Compound cmp, java.lang.String name, boolean stopGreater1)
Counts the number of slots with givennamein the refines hierarchy ofcmp.- Parameters:
cmp- the compound to start searchingname- the name of the slot to search forstopGreater1- stop searching if we have more than one matching slot- Returns:
- the number of slots
-
isNestedContainer
public static boolean isNestedContainer(IDatatype type)
Returns whethertypeis a type-nested container.- Parameters:
type- the type- Returns:
truefor 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 declarationvar- the decision variableincremental- 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 declarationvar- the decision variableincremental- are we in incremental modefilter- 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 ofcmptoresult.- Parameters:
cmp- the compound to derive the refined compound hierarchy fromresult- the set of refined compounds
-
getUsedCompoundTypes
public static boolean getUsedCompoundTypes(Value val, java.util.Set<Compound> result)
Returns the used types ifvalueis a container value.- Parameters:
val- the valueresult- the result set to be modified as a side effect- Returns:
trueifvalis a container value,falseelse
-
purgeRefines
public static void purgeRefines(java.util.Set<Compound> compounds, java.util.Set<Compound> result)
Purges all refined compounds mentioned incompounds.- Parameters:
compounds- the compounds to purgeresult- 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 ofcompfromresult.- Parameters:
comp- the compound to purge the refined ones forresult- modified as a side effect
-
addAll
public static <T,S extends T> void addAll(java.util.Collection<T> target, S[] source)Adds all elements fromsourcetotarget.- Type Parameters:
T- the target typeS- the source type, a subtype of<T>- Parameters:
target- the target collectionsource- 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 constraintevaluator- 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 fromremove- the constraints to remove
-
removeAll
public static <K> void removeAll(java.util.Map<K,?> map, java.util.Collection<K> remove)Removes all elements inremoveas keys frommap.- Type Parameters:
K- the key type- Parameters:
map- the map to remove fromremove- the elements to remove
-
collectRefines
public static java.util.Set<Compound> collectRefines(IDatatype start, IDatatype exclude)
Collects all refines fromstartexcluding all originating atexclude.- Parameters:
start- the start type (an empty set will be returned ifstartis not aCompound)exclude- an optional type excluding the entire refinement hierarchy originating atexcludeifexcludeis 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 fromcmpexcluding all originating atexclude, adding or removing following compounds based onaddstoring the results inresult.- Parameters:
cmp- the type to collect forexclude- an optional type excluding the entire refinement hierarchy originating atexcludeifexcludeis a compound, ignored if nulladd- whether datatypes shall be added or removed fromresult.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 ofvariable.- 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 thesourcemap. Turns an empty map intonull.- Type Parameters:
K- the keyV- the value- Parameters:
source- the source map, may be null- Returns:
- the copy or null if there was no or just an empty
source
-
-