aboutsummaryrefslogtreecommitdiffstats
path: root/Solvers/Vampire-Solver/ca.mcgill.ecse.dslreasoner.vampire.reasoner/src/ca/mcgill/ecse/dslreasoner/vampire/reasoner/VampireSolver.xtend
blob: 4b8b10a96054b35b877424847812985d7a2e405a (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package ca.mcgill.ecse.dslreasoner.vampire.reasoner

import ca.mcgill.ecse.dslreasoner.VampireLanguageStandaloneSetup
import ca.mcgill.ecse.dslreasoner.VampireLanguageStandaloneSetupGenerated
import ca.mcgill.ecse.dslreasoner.vampire.reasoner.builder.Logic2VampireLanguageMapper
import ca.mcgill.ecse.dslreasoner.vampire.reasoner.builder.Logic2VampireLanguageMapperTrace
import ca.mcgill.ecse.dslreasoner.vampire.reasoner.builder.Logic2VampireLanguageMapper_Support
import ca.mcgill.ecse.dslreasoner.vampire.reasoner.builder.MonitoredVampireSolution
import ca.mcgill.ecse.dslreasoner.vampire.reasoner.builder.Vampire2LogicMapper
import ca.mcgill.ecse.dslreasoner.vampire.reasoner.builder.VampireHandler
import ca.mcgill.ecse.dslreasoner.vampire.reasoner.builder.VampireModelInterpretation
import ca.mcgill.ecse.dslreasoner.vampireLanguage.VampireLanguageFactory
import ca.mcgill.ecse.dslreasoner.vampireLanguage.VampireLanguagePackage
import ca.mcgill.ecse.dslreasoner.vampireLanguage.VampireModel
import hu.bme.mit.inf.dslreasoner.logic.model.builder.DocumentationLevel
import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicReasoner
import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicReasonerException
import hu.bme.mit.inf.dslreasoner.logic.model.builder.LogicSolverConfiguration
import hu.bme.mit.inf.dslreasoner.logic.model.logicproblem.LogicProblem
import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.LogicresultFactory
import hu.bme.mit.inf.dslreasoner.logic.model.logicresult.ModelResult
import hu.bme.mit.inf.dslreasoner.workspace.ReasonerWorkspace
import ca.mcgill.ecse.dslreasoner.vampireLanguage.impl.VLSFiniteModelImpl

class VampireSolver extends LogicReasoner {

	new() {
		VampireLanguagePackage.eINSTANCE.eClass
		val x = new VampireLanguageStandaloneSetupGenerated
		VampireLanguageStandaloneSetup::doSetup()
	}

	val Logic2VampireLanguageMapper forwardMapper = new Logic2VampireLanguageMapper
	val Vampire2LogicMapper backwardMapper = new Vampire2LogicMapper
	val VampireHandler handler = new VampireHandler
	val Logic2VampireLanguageMapper_Support support = new Logic2VampireLanguageMapper_Support
	val extension LogicresultFactory resultFactory = LogicresultFactory.eINSTANCE
	private val extension VampireLanguageFactory factory = VampireLanguageFactory.eINSTANCE

//	var fileName = "problem.tptp"
//	def solve(LogicProblem problem, LogicSolverConfiguration config, ReasonerWorkspace workspace, String location) {
//		fileName = location + fileName
//		solve(problem, config, workspace)
//	}
	override solve(LogicProblem problem, LogicSolverConfiguration config,
		ReasonerWorkspace workspace) throws LogicReasonerException {
		val vampireConfig = config.asConfig
		var fileName = "problem_" + vampireConfig.typeScopes.minNewElements + "-" +
			vampireConfig.typeScopes.maxNewElements + ".tptp"

		// Start: Logic -> Vampire mapping
		val transformationStart = System.currentTimeMillis
		// TODO
		val result = forwardMapper.transformProblem(problem, vampireConfig)
		val transformationTime = System.currentTimeMillis - transformationStart

		val vampireProblem = result.output
		val forwardTrace = result.trace

		var String fileURI = null;
		var String vampireCode = null;
		vampireCode = workspace.writeModelToString(vampireProblem, fileName)

		val writeFile = (
			vampireConfig.documentationLevel === DocumentationLevel::NORMAL ||
			vampireConfig.documentationLevel === DocumentationLevel::FULL)
		if (writeFile) {
			fileURI = workspace.writeModel(vampireProblem, fileName).toFileString
		}

//		Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("tptp", new VampireLanguageFactoryImpl)
////		val Resource resource = Resource.
////		Resource.getResource(wsURI+"problem.tptp") as Resource
////		resource
//		val model = workspace.readModel(VampireModel, "problem.tptp").eResource.contents
//		println(model)
		// Finish: Logic -> Vampire mapping
		if (vampireConfig.genModel) {
			if (vampireConfig.server) {
				val form = support.makeForm(vampireCode, vampireConfig.solver, vampireConfig.runtimeLimit)
				var response = newArrayList
				var ind = 0
				var done = false
				print(" ")
				while (!done) {
//					print("(x)")
					done = false
					response = support.sendPost(form)

					var responseFound = false
					ind = 0
					while (!responseFound && ind<response.length) {
						var line = response.get(ind)
//						println(line)
						if (line.length >= 5 && line.substring(0, 5) == "ERROR") {
							done = false
							responseFound = true
						} else {
							if (line == "</PRE>" && response.get(ind-1) != "<PRE>") {
								done = true
								responseFound = true
							}
						}
						ind++
					}
					if (!done) println("(Server call failed. Trying again...)")
				}
				val satRaw = response.get(ind - 3)
				val modRaw = response.get(ind - 2)

				val sat = newArrayList(satRaw.split(" "))
				val mod = newArrayList(modRaw.split(" "))

				val satOut = sat.get(1)
				val modOut = mod.get(1)
				val satTime = sat.get(2)
				val modTime = mod.get(2)

				println()
				println(sat)
				println(mod)
				
				return createUndecidableResult => [
					it.statistics = createStatistics => [
						it.transformationTime = transformationTime as int
						it.entries += createStringStatisticEntry => [
							it.name = "satOut"
							it.value = satOut
						]
						it.entries += createStringStatisticEntry => [
							it.name = "satTime"
							it.value = satTime
						]
						it.entries += createStringStatisticEntry => [
							it.name = "modOut"
							it.value = modOut
						]
						it.entries += createStringStatisticEntry => [
							it.name = "modTime"
							it.value = modTime
						]

					]
				]
					
				/*
				 * TODO
				return createModelResult => [
					it.problem = null
					it.representation += createVampireModel => []//TODO Add something here
					it.trace = trace
					it.statistics = createStatistics => [
						it.transformationTime = transformationTime as int
						it.entries += createStringStatisticEntry => [
							it.name = "satOut"
							it.value = satOut
						]
						it.entries += createStringStatisticEntry => [
							it.name = "satTime"
							it.value = satTime
						]
						it.entries += createStringStatisticEntry => [
							it.name = "modOut"
							it.value = modOut
						]
						it.entries += createStringStatisticEntry => [
							it.name = "modTime"
							it.value = modTime
						]

					]
				]
				*/

//				return newArrayList(line1, line2)
			} else {

				// Start: Solving .tptp problem
				println()
				val MonitoredVampireSolution vampSol = handler.callSolver(vampireProblem, workspace, vampireConfig)
				// Finish: Solving .tptp problem
				// Start: Vampire -> Logic mapping
//				val backTransformationStart = System.currentTimeMillis
//				// Backwards Mapper
//				val logicResult = backwardMapper.transformOutput(problem,
//					vampireConfig.solutionScope.numberOfRequiredSolution, vampSol, forwardTrace, transformationTime)
//
//				val backTransformationTime = System.currentTimeMillis - backTransformationStart
				// Finish: Vampire -> Logic Mapping
//		print(vampSol.generatedModel.tfformulas.size)

//				return logicResult // currently only a ModelResult

//				var model = vampSol.generatedModel.confirmations.filter[class == VLSFiniteModelImpl]
//				
				var modOut = "no"
				if(vampSol.finiteModelGenerated){
					modOut = "FiniteModel"
				}
				
				val realModOut=modOut
				
				return createUndecidableResult => [
					it.statistics = createStatistics => [
						it.transformationTime = transformationTime as int
						it.entries += createStringStatisticEntry => [
							it.name = "satOut"
							it.value = "-"
						]
						it.entries += createStringStatisticEntry => [
							it.name = "satTime"
							it.value = "-"
						]
						it.entries += createStringStatisticEntry => [
							it.name = "modOut"
							it.value = realModOut
						]
						it.entries += createStringStatisticEntry => [
							it.name = "modTime"
							it.value = (vampSol.solverTime/1000.0).toString
						]

					]
				]
				
				/*
				return createModelResult => [
					it.problem = null
					it.representation += createVampireModel => []
					it.trace = trace
					it.statistics = createStatistics => [
						it.transformationTime = transformationTime as int
						it.entries += createStringStatisticEntry => [
							it.name = "satOut"
							it.value = "-"
						]
						it.entries += createStringStatisticEntry => [
							it.name = "satTime"
							it.value = "-"
						]
						it.entries += createStringStatisticEntry => [
							it.name = "modOut"
							it.value = realModOut
						]
						it.entries += createStringStatisticEntry => [
							it.name = "modTime"
							it.value = (vampSol.solverTime/1000.0).toString
						]

					]
				]
				*/
			}
		}
//		return backwardMapper.transformOutput(problem, vampireConfig.solutionScope.numberOfRequiredSolution,
//			new MonitoredVampireSolution(-1, null), forwardTrace, transformationTime)
	}

	def asConfig(LogicSolverConfiguration configuration) {
		if (configuration instanceof VampireSolverConfiguration) {
			return configuration
		} else {
			throw new IllegalArgumentException('''The configuration have to be an «VampireSolverConfiguration.simpleName»!''')
		}
	}

//	/*
//	 * not for now
//	 * 
	override getInterpretations(ModelResult modelResult) {
//		 val answers = (modelResult.representation as MonitoredAlloySolution).aswers.map[key]
		val sols = modelResult.representation // as List<A4Solution>
		// val res = answers.map 
		sols.map [
			new VampireModelInterpretation(
//				forwardMapper.typeMapper.typeInterpreter,
				it as VampireModel,
//				forwardMapper,
				modelResult.trace as Logic2VampireLanguageMapperTrace
			)
		]
	}
//	*/
}