aboutsummaryrefslogtreecommitdiffstats
path: root/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/validation/ConstraintCollection.xtend
blob: 685c583692ea499b83c644ca7a86c189e49081bd (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
package ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.validation

import java.util.ArrayList
import java.util.HashMap
import java.util.List
import java.util.Map
import org.eclipse.emf.common.notify.Notifier
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.viatra.addon.validation.core.api.IConstraintSpecification
import org.eclipse.viatra.query.runtime.api.ViatraQueryEngine
import org.eclipse.viatra.query.runtime.api.impl.BaseGeneratedPatternGroup
import org.eclipse.viatra.query.runtime.emf.EMFScope

class ConstraintCollection{
	val constraints = new ArrayList<IConstraintSpecification>();
	var BaseGeneratedPatternGroup patterns;
	var List<Notifier> resources = new ArrayList<Notifier>(); 
	
	
	new(List<IConstraintSpecification> constraints, List<String> uris, BaseGeneratedPatternGroup patterns){
		this.constraints.addAll(constraints);
		this.patterns = patterns;
		setURIs(uris);
	}
	
	new(List<IConstraintSpecification> constraints, BaseGeneratedPatternGroup patterns){
		this.constraints.addAll(constraints);
		this.patterns = patterns;
	}
	
	def addModel(Notifier n ){
		resources.add(n);
	}
	
	def setURIs(List<String> uris){
		val resSet = new ResourceSetImpl();
		
		for(uri : uris){
			var resource = resSet.getResource(URI.createURI(uri), true);
			resources.add(resource);
		}
	
		println('reading model finished')
	}
	
	def List<Integer> calculateViolations(){
		var results = new ArrayList<Integer>();
		
		for(resource : resources){
			val engine = initEngine(resource);
			var matches = constraints.stream.mapToInt([ele| ele.querySpecification.getMatcher(engine).countMatches]).sum();
			results.add(matches);
		}
		
		return results;
	}
	
	def ArrayList<Map<String, Integer>> calculateViolationMaps(){
		val result = new ArrayList<Map<String, Integer>>()
		
		for(resource : resources){
			val map = new HashMap<String, Integer>();
			val engine = initEngine(resource);
			constraints.forEach[
				var count = it.querySpecification.getMatcher(engine).countMatches;
				map.put(it.querySpecification.simpleName, count);
			];
			result.add(map);
		}
		return result;
	}
	
	private def initEngine(Notifier r){
		var engine = ViatraQueryEngine.on(new EMFScope(r));
		//init patterns with the new engine
		patterns.prepare(engine);
		return engine;
	}
}