aboutsummaryrefslogtreecommitdiffstats
path: root/Framework/hu.bme.mit.inf.dslreasoner.logic.model/patterns/hu/bme/mit/inf/dslreasoner/logic/model/patterns/typeUtil.vql
blob: 5b38189fed01bf1b7d99172f4f7342ffb0020f99 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package hu.bme.mit.inf.dslreasoner.logic.model.patterns

import epackage "http://www.bme.hu/mit/inf/dslreasoner/logic/model/problem"
import epackage "http://www.bme.hu/mit/inf/dslreasoner/logic/model/language"

/// Basic util patterns ///

/**
 * Direct supertypes of a type.
 */
private pattern supertypeDirect(subtype : Type, supertype : Type) {
	Type.supertypes(subtype, supertype);
}

/**
 * All supertypes of a type.
 */
pattern supertypeStar(subtype: Type, supertype: Type) {
	subtype == supertype;
} or {
	find supertypeDirect+(subtype,supertype);
}

/**
 * Direct element of a type
 */
pattern typeDirectElements(type: TypeDefinition, element: DefinedElement) {
	TypeDefinition.elements(type,element);
}

/// Complex type reasoning patterns ///
//
// In a valid type system, for each element e there is exactly one type T where
// 1: T(e) - but we dont know this for type declaration
// 2: For the dynamic type D and another type T, where D(e) && D-->T, T(e) is true.
// 2e: A type hierarchy is invalid, if there is a supertype T for a dynamic type D which does no contains e:
//     D(e) && D-->T && !T(e)
// 3: There is no T' that T'->T and T'(e)
// 3e: A type hierarcy is invalid, if there is a type T for a dynamic type D, which contains e, but not subtype of T:
//    D(e) && ![T--->D] && T(e)
// 4: T is not abstract
// Such type T is called Dynamic type of e, while other types are called static types.
// 
// The following patterns checks the possible dynamic types for an element

pattern possibleDynamicType(problem: LogicProblem, dynamic:Type, element:DefinedElement)
// case 1: element is defined in at least once
{
	LogicProblem.types(problem,dynamic);
	// select a random definition
	find typeDirectElements(definition,element);
	// 2e is not true: D(e) && D-->T && !T(e)
	find supertypeStar(dynamic,definition);
	neg find dynamicTypeNotSubtypeOfADefinition(problem,element,dynamic);
	// 3e is not true: D(e) && ![T--->D] && T(e)
	neg find dynamicTypeIsSubtypeOfANonDefinition(problem,element,dynamic);
	// 4: T is not abstract
	Type.isAbstract(dynamic,false);
}	or
// case 2: element is defined is not defined
{
	LogicProblem.types(problem,dynamic);
	// there is no definition
	neg find typeDirectElements(_,element);
	// 2e is not true: D(e) && D-->T && !T(e)
	// because non of the definition contains element, the type cannot have defined supertype
	neg find typesWithDefinedSupertype(problem,dynamic);
	// 3e is not true: D(e) && ![T--->D] && T(e)
	// because there is no definition, dynamic covers all definition
}

/**
 * supertype -------> element <------- otherSupertype
 *     A                                     A
 *     |                                     |
 * wrongDynamic -----------------------------X
 */
private pattern dynamicTypeNotSubtypeOfADefinition(problem:LogicProblem, element:DefinedElement, wrongDynamic : Type) {
	LogicProblem.types(problem,wrongDynamic);
	find typeDirectElements(supertype,element);
	find supertypeStar(wrongDynamic,supertype);
	find typeDirectElements(otherSupertype,element);
	neg find supertypeStar(wrongDynamic,otherSupertype);
	//otherSupertype != wrongDynamic;
}


/**
 * supertype -------> element <---X--- otherSupertype
 *     A                                     A
 *     |                                     |
 * wrongDynamic -----------------------------+
 */
private pattern dynamicTypeIsSubtypeOfANonDefinition(problem: LogicProblem, element:DefinedElement, wrongDynamic:Type) {
	LogicProblem.types(problem,wrongDynamic);
	find typeDirectElements(supertype, element);
	find supertypeStar(wrongDynamic, supertype);
	find supertypeStar(wrongDynamic, otherSupertype);
	TypeDefinition(otherSupertype);
	neg find typeDirectElements(otherSupertype, element);
}

private pattern typesWithDefinedSupertype(problem: LogicProblem, type:TypeDeclaration) {
	LogicProblem.types(problem,type);
	find supertypeStar(type,definedSupertype);
	TypeDefinition(definedSupertype);
}

/**
 * Types that 
 */
pattern mustTypeElement(problem: LogicProblem, type:Type, element:DefinedElement) {
	LogicProblem.types(problem,type);
	TypeDefinition.elements(type,element);
} or {
	TypeDeclaration(type);
	LogicProblem.types(problem,type);
	LogicProblem.types(problem,subtype);
	TypeDefinition.elements(subtype,element);
	find supertypeStar(subtype,type);
}


/// Validation patterns ///

@Constraint(severity = "warning",key = {problem},
	message="Type system is inconsistent."
)
pattern typeSystemIsInconsistent(problem:LogicProblem) {
	find elementWithNoPossibleDynamicType(problem,_);
} or {
	find elementNotDefinedInSupertype(problem,_,_,_);
}

/**
 * An element is defined in a type, but not defined in a supertype
 */
@Constraint(severity = "warning",key = {element},
	message="An element is defined in a type, but not defined in a supertype."
)
pattern elementNotDefinedInSupertype(problem: LogicProblem,
	element: DefinedElement,
	directType:TypeDefinition,
	notDefinedIn: TypeDefinition)
{
	LogicProblem.elements(problem,element);
	find typeDirectElements(directType,element);
	find supertypeStar(directType,notDefinedIn);
	neg find typeDirectElements(notDefinedIn,element);
}

@Constraint(severity = "warning",key = {element},
	message="There is no possible dynamic type for an element."
)
pattern elementWithNoPossibleDynamicType(problem:LogicProblem, element:DefinedElement) {
	LogicProblem.elements(problem,element);
	neg find possibleDynamicType(problem,_,element);
}



@Constraint(severity = "error",key = {t},
	message="Circle in the type hierarchy"
)
pattern cyclicTypeHierarchy(t: Type) {
	find supertypeDirect+(t,t);
}