aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/Vampire-Solver/ca.mcgill.ecse.dslreasoner.vampire.reasoner/src/ca/mcgill/ecse/dslreasoner/vampire/reasoner/builder/Logic2VampireLanguageMapper_ScopeMapper.xtend
blob: c50aa7702122abe406f1b773c0b5741b51b24783 (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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package ca.mcgill.ecse.dslreasoner.vampire.reasoner.builder

import ca.mcgill.ecse.dslreasoner.vampire.reasoner.VampireSolverConfiguration
import ca.mcgill.ecse.dslreasoner.vampireLanguage.VLSTerm
import ca.mcgill.ecse.dslreasoner.vampireLanguage.VLSVariable
import ca.mcgill.ecse.dslreasoner.vampireLanguage.VampireLanguageFactory
import hu.bme.mit.inf.dslreasoner.logic.model.logiclanguage.Type
import java.util.ArrayList
import java.util.HashMap
import java.util.Map

import static extension hu.bme.mit.inf.dslreasoner.util.CollectionsUtil.*

class Logic2VampireLanguageMapper_ScopeMapper {
	private val extension VampireLanguageFactory factory = VampireLanguageFactory.eINSTANCE
	private val Logic2VampireLanguageMapper_Support support = new Logic2VampireLanguageMapper_Support
	val Logic2VampireLanguageMapper base
	private val VLSVariable variable = createVLSVariable => [it.name = "A"]

	public new(Logic2VampireLanguageMapper base) {
		this.base = base
	}

	def dispatch public void transformScope(VampireSolverConfiguration config, Logic2VampireLanguageMapperTrace trace) {
		val ABSOLUTE_MIN = 0
		val ABSOLUTE_MAX = Integer.MAX_VALUE

//		TODO HANDLE ERRORS RELATED TO MAX > MIN
//		TODO HANDLE ERROR RELATED TO SUM(MIN TYPES)+1(for root) > MAX OBJECTS
//		TODO HANDLE 
		// 1. make a list of constants equaling the min number of specified objects
		//These numbers do not include enums or initial model elements
		
		val GLOBAL_MIN = config.typeScopes.minNewElements
		val GLOBAL_MAX = config.typeScopes.maxNewElements

		val localInstances = newArrayList

		val consistant = GLOBAL_MAX > GLOBAL_MIN

		// Handling Minimum_General
		if (GLOBAL_MIN != ABSOLUTE_MIN) {
			getInstanceConstants(GLOBAL_MIN, 0, localInstances, trace, true, !consistant)
			if (consistant) {
				for (i : trace.uniqueInstances) {
					localInstances.add(support.duplicate(i))
				}
				makeFofFormula(localInstances, trace, true, null)
			} else {
				makeFofFormula(trace.uniqueInstances as ArrayList, trace, true, null)
			}
		}

		// Handling Maximum_General
		if (GLOBAL_MAX != ABSOLUTE_MAX) {
			getInstanceConstants(GLOBAL_MAX, 0, localInstances, trace, true, consistant)
			if (consistant) {
				makeFofFormula(trace.uniqueInstances as ArrayList, trace, false, null)
			} else {
				makeFofFormula(localInstances, trace, false, null)
			}
		}

		// Handling Minimum_Specific
		var i = 1
		var minNum = -1
		var Map<Type, Integer> startPoints = new HashMap
		for (t : config.typeScopes.minNewElementsByType.keySet) {
			minNum = t.lookup(config.typeScopes.minNewElementsByType)
			if (minNum != 0) {
				getInstanceConstants(i + minNum, i, localInstances, trace, true, false)
				startPoints.put(t, i)
				i += minNum
				makeFofFormula(localInstances, trace, true, t)
			}
		}

		// TODO: calc sum of mins, compare to current value of i
		// Handling Maximum_Specific
		for (t : config.typeScopes.maxNewElementsByType.keySet) {
			var maxNum = t.lookup(config.typeScopes.maxNewElementsByType)
			minNum = t.lookup(config.typeScopes.minNewElementsByType)
			var startpoint = t.lookup(startPoints)
			if (minNum != 0) {
				getInstanceConstants(startpoint + minNum, startpoint, localInstances, trace, true, false)
			}
			if (maxNum != minNum) {
				var instEndInd = Math.min(GLOBAL_MAX, i + maxNum - minNum)
				getInstanceConstants(instEndInd, i, localInstances, trace, false, false)
				makeFofFormula(localInstances, trace, false, t)
			}
		}

// 3. Specify uniqueness of elements
		// TEMP
		val DUPLICATES = config.uniquenessDuplicates

		val numInst = trace.uniqueInstances.length
		var ind = 1
		if (numInst != 0) {
			if (DUPLICATES) {
				// W/ DUPLICATES
				for (e : trace.uniqueInstances) {
					val x = ind
					val uniqueness = createVLSFofFormula => [
						it.name = support.toIDMultiple("t_uniqueness", e.name)
						it.fofRole = "axiom"
						it.fofFormula = support.establishUniqueness(trace.uniqueInstances, e)
					]
					trace.specification.formulas += uniqueness
					ind++
				}
			} else {
				// W/O DUPLICATES
				for (e : trace.uniqueInstances.subList(0, numInst - 1)) {
					val x = ind
					val uniqueness = createVLSFofFormula => [
						it.name = support.toIDMultiple("t_uniqueness", e.name)
						it.fofRole = "axiom"
						it.fofFormula = support.establishUniqueness(trace.uniqueInstances.subList(x, numInst), e)
					]
					trace.specification.formulas += uniqueness
					ind++
				}
			}
		}
	}

	def protected void getInstanceConstants(int endInd, int startInd, ArrayList list,
		Logic2VampireLanguageMapperTrace trace, boolean clear, boolean addToTrace) {
		if (clear) {
			list.clear
		}
		for (var i = startInd; i < endInd; i++) {
			val num = i + 1
			val cst = createVLSConstant => [
				it.name = "o" + num
			]
			if (addToTrace) {
				trace.uniqueInstances.add(cst)
			}
			list.add(cst)
		}
	}

	def protected void makeFofFormula(ArrayList list, Logic2VampireLanguageMapperTrace trace, boolean minimum,
		Type type) {
		var nm = ""
		var VLSTerm tm = null
		if (type === null) {
			nm = "object"
			tm = support.topLevelTypeFunc
		} else {
			nm = type.lookup(trace.type2Predicate).constant.toString
			tm = createVLSAnd => [
				it.left = support.duplicate(type.lookup(trace.type2Predicate))
				it.right = support.topLevelTypeFunc
			]
//			tm = support.duplicate(type.lookup(trace.type2Predicate))
		}
		val name = nm
		val term = tm

		val cstDec = createVLSFofFormula => [
			it.name = support.toIDMultiple("typeScope", if(minimum) "min" else "max", name)
			it.fofRole = "axiom"
			it.fofFormula = createVLSUniversalQuantifier => [
				it.variables += support.duplicate(variable)
				// check below
				it.operand = createVLSImplies => [
					if (minimum) {
						it.left = support.unfoldOr(list.map [ i |
							createVLSEquality => [
								it.left = createVLSVariable => [it.name = variable.name]
								it.right = i
							]
						])
						it.right = term
					} else {
						it.left = term
						it.right = support.unfoldOr(list.map [ i |
							createVLSEquality => [
								it.left = createVLSVariable => [it.name = variable.name]
								it.right = i
							]
						])

					}
				]
			]
		]
		trace.specification.formulas += cstDec
	}

}