aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/VIATRA-Solver/hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra/src/hu/bme/mit/inf/dslreasoner/viatrasolver/logic2viatra/TypeAnalysis.xtend
blob: 395aba85d6e9375955013a7aa668f8dd4a22a956 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra

import org.eclipse.xtend.lib.annotations.Data
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type
import java.util.List
import java.util.Map
import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation
import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem
import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine
import org.eclipse.viatra.query.runtime.emf.EMFScope
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.queries.NewElementTypeConstructorMatcher
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.queries.NewElementTypeRefinementTargetMatcher
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.queries.NewElementTypeRefinementNegativeConstraintMatcher
import java.util.ArrayList
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.queries.DontHaveDefinedSupertypeMatcher
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.TypeDeclaration
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.DefinedElement
import hu.bme.mit.inf.dslreasoner.viatrasolver.logic2viatra.queries.NewElementMayTypeNegativeConstraintMatcher

@Data class TypeRefinementPrecondition {
	Type targetType
	List<? extends Type> inhibitorTypes
}

/**
 * Collection of all possible element creation and type refinement rules for defined and undefined elements.
 */
@Data class TypeAnalysisResult {
	/////////////////
	// New elements
	/////////////////
	/**
	 * New elements can be created using with the following types.
	 */
	List<? extends Type> possibleNewDynamicTypes
	/**
	 * <b>(A,B)</b> in <b>possibleNewTypeRefinements</b>: a new element <b>x</b> can be refined to A <=> <b>x</b> has all types listed in B.
	 */
	List<TypeRefinementPrecondition> possibleNewTypeRefinements
	/**
	 * A new element may have type A if it already has types 
	 */
	Map<? extends Type,TypeRefinementPrecondition> mayNewTypePreconditions
	
	/////////////////
	// Old elements
	/////////////////
	
	/**
	 * <b>(A,B)</b> in <b>possibleNewTypeRefinements</b>: a new element <b>x</b> can be refined to A <=> <b>x</b> has all types listed in B.
	 */
	Map<DefinedElement,TypeRefinementPrecondition> possibleOldTypeRefinements
	/**
	 * A new element may have type A if it already has types 
	 */
	Map<? extends Type,Map<DefinedElement,TypeRefinementPrecondition>> mayOldTypePreconditions
}

class TypeAnalysis {
	
	def TypeAnalysisResult performTypeAnalysis(LogicProblem problem, PartialInterpretation interpretation) {
		val viatraEngine = ViatraQueryEngine.on(new EMFScope(problem))
		val negativeMatcher = NewElementTypeRefinementNegativeConstraintMatcher.on(viatraEngine)
		
		val possibleNewDynamicTypes =  NewElementTypeConstructorMatcher.on(viatraEngine).allValuesOftype
		
		val possibleNewTypeRefinementTargets = NewElementTypeRefinementTargetMatcher.on(viatraEngine).allValuesOfrefined
		val possibleNewTypeRefinements = new ArrayList
		for(possibleNewTypeRefinementTarget : possibleNewTypeRefinementTargets) {
			val inhibitorTypes = negativeMatcher.getAllValuesOfinhibitor(possibleNewTypeRefinementTarget)
			possibleNewTypeRefinements += new TypeRefinementPrecondition(
				possibleNewTypeRefinementTarget,
				inhibitorTypes.toList)
		}
		
		val possibleTypes = DontHaveDefinedSupertypeMatcher.on(viatraEngine).allValuesOftype
		val newElementMayTypeMatcher = NewElementMayTypeNegativeConstraintMatcher.on(viatraEngine)
		
		val mayNewTypePreconditions = possibleTypes.toInvertedMap[type | 
			val inhibitorTypes = newElementMayTypeMatcher.getAllValuesOfinhibitor(type as TypeDeclaration)
			return new TypeRefinementPrecondition(type,inhibitorTypes.toList)
		]
		
		return new TypeAnalysisResult(
			possibleNewDynamicTypes.toList,
			possibleNewTypeRefinements,
			mayNewTypePreconditions,
			emptyMap,
			emptyMap
		)
	}
}