aboutsummaryrefslogtreecommitdiffstats
path: root/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/io/GraphReader.xtend
blob: 491501b0c13088b15101e45ee6049162aa1cb541 (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
package ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.io;

import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.graph.EMFGraph
import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.EdgeTypeMetric
import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.Metric
import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.MultiplexParticipationCoefficientMetric
import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.NodeActivityMetric
import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.NodeTypeMetric
import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.OutDegreeMetric
import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.TypedOutDegree
import java.io.File
import java.io.FileNotFoundException
import java.util.ArrayList
import java.util.List
import org.eclipse.emf.common.util.URI
import org.eclipse.emf.ecore.EObject
import org.eclipse.emf.ecore.EPackage
import org.eclipse.emf.ecore.EReference
import org.eclipse.emf.ecore.resource.Resource
import org.eclipse.emf.ecore.resource.ResourceSet
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl
import org.eclipse.emf.ecore.xmi.impl.XMIResourceFactoryImpl

class GraphReader{
	val ResourceSet resSet = new ResourceSetImpl();
	val referenceTypes = new ArrayList<EReference>();
	var String suffix;
	
	new(EPackage metaModel, String suffix) {
		Resource.Factory.Registry.INSTANCE.extensionToFactoryMap.put("*",new XMIResourceFactoryImpl)
		this.suffix = suffix;
		
		//find all reference types in the meta model
		metaModel.eAllContents.forEach[
			if(it instanceof EReference){
				referenceTypes.add(it);
			}
		]
	}
	
	def List<EMFGraph> readModels(String path){
		val dir = new File(path);
		if(!dir.isDirectory){
			throw new Exception("expecting a directory");
		}
		
		val graphs = new ArrayList<EMFGraph>();
		
		val metrics = new ArrayList<Metric>();
		metrics.add(new OutDegreeMetric());
		metrics.add(new NodeActivityMetric());
		metrics.add(new MultiplexParticipationCoefficientMetric());
		metrics.add(new TypedOutDegree());		
		metrics.add(new NodeTypeMetric());
		metrics.add(new EdgeTypeMetric());
		
		//check all files in the directory with suffix
		for(String name : dir.list.filter[it| it.endsWith(suffix)]){
			val file = new File(name);
			val roots = readModel(EObject, path,  file.name);			
			//add a list of metrics
			val g = new EMFGraph();
			for(root : roots){
				g.init(root, metrics, name.replaceFirst(suffix, ""), referenceTypes);
			}
			
			graphs.add(g);
		}
		
		return graphs;
	}
	
	def EMFGraph readModel(String path, String filename){
		val metrics = new ArrayList<Metric>();
		metrics.add(new OutDegreeMetric());
		metrics.add(new NodeActivityMetric());
		metrics.add(new MultiplexParticipationCoefficientMetric());
		metrics.add(new TypedOutDegree());		
		metrics.add(new NodeTypeMetric());
		metrics.add(new EdgeTypeMetric());
		
		val file = new File(filename);
		val roots = readModel(EObject, path,  file.name);			
		//add a list of metrics
		val g = new EMFGraph();
		for(root : roots){
			g.init(root, metrics, filename.replaceFirst(suffix, ""), referenceTypes);
		}
		return g
	}
	
	def <RootType extends EObject> List<RootType> readModel(Class<RootType> type, String path, String name) {
		try {
			val resource = resSet.getResource(getURI(path, name),true);
			if(resource === null) throw new FileNotFoundException(getURI(path, name).toString)
			else {		
				return resource.contents as List<RootType>
			}
		} catch(Exception e) {
			e.printStackTrace();
			throw new Exception(getURI(path, name).toString());
		}
	}
	
	def static getURI(String path, String name) {
		URI.createFileURI(path + "/"  + name)
	}
}