diff options
Diffstat (limited to 'Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/app')
3 files changed, 201 insertions, 0 deletions
diff --git a/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/app/Domain.xtend b/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/app/Domain.xtend new file mode 100644 index 00000000..8351e96b --- /dev/null +++ b/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/app/Domain.xtend | |||
@@ -0,0 +1,5 @@ | |||
1 | package ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.app | ||
2 | |||
3 | enum Domain{ | ||
4 | Yakinduum | ||
5 | } \ No newline at end of file | ||
diff --git a/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/app/Main.xtend b/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/app/Main.xtend new file mode 100644 index 00000000..cf871ead --- /dev/null +++ b/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/app/Main.xtend | |||
@@ -0,0 +1,72 @@ | |||
1 | package ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.app | ||
2 | |||
3 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.graph.EMFGraph | ||
4 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.io.CsvFileWriter | ||
5 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.io.GraphReader | ||
6 | import hu.bme.mit.inf.dslreasoner.domains.yakindu.sgraph.yakindumm.impl.YakindummPackageImpl | ||
7 | import java.util.ArrayList | ||
8 | |||
9 | //import yakindumm2.impl.Yakindumm2PackageImpl | ||
10 | |||
11 | class Main { | ||
12 | static class RWInformation{ | ||
13 | public var String inputFolder; | ||
14 | public var String outputFolder; | ||
15 | public var int numRuns; | ||
16 | |||
17 | new(String inputFolder, String outputFolder, int numRuns){ | ||
18 | this.inputFolder = inputFolder; | ||
19 | this.outputFolder = outputFolder; | ||
20 | this.numRuns = numRuns; | ||
21 | } | ||
22 | } | ||
23 | |||
24 | def static void main(String[] args){ | ||
25 | //init model | ||
26 | YakindummPackageImpl.eINSTANCE.eClass; | ||
27 | // Yakindumm2PackageImpl.eINSTANCE.eClass; | ||
28 | //val infos = initData(); | ||
29 | |||
30 | println("Start Reading Models..."); | ||
31 | var reader = new GraphReader(YakindummPackageImpl.eINSTANCE); | ||
32 | // for(info : infos){ | ||
33 | // calculateAllModels(info.inputFolder, info.outputFolder,info.numRuns, reader); | ||
34 | // } | ||
35 | |||
36 | //human input has different package declaration | ||
37 | // reader = new GraphReader(Yakindumm2PackageImpl.eINSTANCE); | ||
38 | val human = new RWInformation("inputs/Random/", "outputs/", 1); | ||
39 | calculateAllModels(human.inputFolder, human.outputFolder,human.numRuns, reader); | ||
40 | |||
41 | |||
42 | println("finished"); | ||
43 | } | ||
44 | |||
45 | static def initData(){ | ||
46 | val infos = new ArrayList<RWInformation>(); | ||
47 | infos.add(new RWInformation("inputs/alloyInput/models/", "../plot/statistics/alloyOutput/", 20)); | ||
48 | infos.add(new RWInformation("inputs/randomInput/models/", "../plot/statistics/randomOutput/", 20)); | ||
49 | infos.add(new RWInformation("inputs/viatraInput30/", "../plot/statistics/viatraOutput30/", 20)); | ||
50 | infos.add(new RWInformation("inputs/viatraInput100/", "../plot/statistics/viatraOutput100/", 10)); | ||
51 | return infos; | ||
52 | } | ||
53 | |||
54 | static def calculateAllModels(String inputFolder, String outputFolder, int numRuns, GraphReader reader){ | ||
55 | for(var i = 1; i <= numRuns; i++){ | ||
56 | val models = new ArrayList<EMFGraph>(); | ||
57 | models.addAll(reader.readModels(inputFolder + "run" + i)); | ||
58 | for(model : models){ | ||
59 | calculateAndOutputMetrics(model, YakindummPackageImpl.eNAME, outputFolder+model.name+"_run_"+i+".csv"); | ||
60 | } | ||
61 | } | ||
62 | println("output results Ended for: " + outputFolder); | ||
63 | |||
64 | |||
65 | } | ||
66 | |||
67 | static def calculateAndOutputMetrics(EMFGraph model, String metaModel, String fileName){ | ||
68 | //println("evaluating for " + model.name); | ||
69 | model.metaModel = metaModel; | ||
70 | CsvFileWriter.write(model.evaluateAllMetrics(), fileName); | ||
71 | } | ||
72 | } \ No newline at end of file | ||
diff --git a/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/app/PartialInterpretationMetric.xtend b/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/app/PartialInterpretationMetric.xtend new file mode 100644 index 00000000..cdd06027 --- /dev/null +++ b/Metrics/Metrics-Calculation/ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator/src/ca/mcgill/ecse/dslreasoner/realistic/metrics/calculator/app/PartialInterpretationMetric.xtend | |||
@@ -0,0 +1,124 @@ | |||
1 | package ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.app | ||
2 | |||
3 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.distance.KSDistance | ||
4 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.graph.PartialInterpretationGraph | ||
5 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.io.CsvFileWriter | ||
6 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.Metric | ||
7 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.MultiplexParticipationCoefficientMetric | ||
8 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.NodeActivityMetric | ||
9 | import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.metrics.OutDegreeMetric | ||
10 | import hu.bme.mit.inf.dslreasoner.viatrasolver.partialinterpretationlanguage.partialinterpretation.PartialInterpretation | ||
11 | import java.io.File | ||
12 | import java.io.FileNotFoundException | ||
13 | import java.io.PrintWriter | ||
14 | import java.util.ArrayList | ||
15 | import java.util.List | ||
16 | import org.eclipse.emf.ecore.util.EcoreUtil | ||
17 | import org.eclipse.viatra.dse.api.Solution | ||
18 | |||
19 | class PartialInterpretationMetric { | ||
20 | var static state = 0; | ||
21 | var static KSDistance ks; | ||
22 | |||
23 | def static void initPaths(){ | ||
24 | new File("debug/metric/").mkdir(); | ||
25 | new File("debug/metric/trajectories/").mkdir(); | ||
26 | ks = new KSDistance(Domain.Yakinduum); | ||
27 | } | ||
28 | |||
29 | def static MetricDistanceGroup calculateMetricDistance(PartialInterpretation partial){ | ||
30 | val metrics = new ArrayList<Metric>(); | ||
31 | metrics.add(new OutDegreeMetric()); | ||
32 | metrics.add(new NodeActivityMetric()); | ||
33 | metrics.add(new MultiplexParticipationCoefficientMetric()); | ||
34 | |||
35 | val metricCalculator = new PartialInterpretationGraph(partial, metrics, null); | ||
36 | var metricSamples = metricCalculator.evaluateAllMetricsToSamples(); | ||
37 | |||
38 | var mpc = ks.mpcDistance(metricSamples.mpcSamples); | ||
39 | var na = ks.naDistance(metricSamples.naSamples); | ||
40 | var outDegree = ks.outDegreeDistance(metricSamples.outDegreeSamples); | ||
41 | |||
42 | return new MetricDistanceGroup(mpc, na, outDegree); | ||
43 | } | ||
44 | |||
45 | // calculate the metrics for a state | ||
46 | def static void calculateMetric(PartialInterpretation partial, String path, String currentStateId, Integer counter){ | ||
47 | val metrics = new ArrayList<Metric>(); | ||
48 | metrics.add(new OutDegreeMetric()); | ||
49 | metrics.add(new NodeActivityMetric()); | ||
50 | metrics.add(new MultiplexParticipationCoefficientMetric()); | ||
51 | |||
52 | //make dir since the folder can be none existing | ||
53 | new File(path).mkdir(); | ||
54 | val filename = path + "/state_"+currentStateId+"-"+counter+".csv"; | ||
55 | state++; | ||
56 | val metricCalculator = new PartialInterpretationGraph(partial, metrics, currentStateId); | ||
57 | |||
58 | CsvFileWriter.write(metricCalculator.evaluateAllMetrics(), filename); | ||
59 | } | ||
60 | |||
61 | def static void outputTrajectories(PartialInterpretation empty, List<Solution> solutions){ | ||
62 | for(solution : solutions){ | ||
63 | //need to copy the empty solution because the transition directly worked on the graph | ||
64 | val emptySolutionCopy = EcoreUtil.copy(empty) | ||
65 | val trajectory = solution.shortestTrajectory; | ||
66 | trajectory.modelWithEditingDomain = emptySolutionCopy | ||
67 | |||
68 | // state codes that will record the trajectory | ||
69 | val stateCodes = newArrayList() | ||
70 | |||
71 | var counter = 0 | ||
72 | //transform and record the state codes for each state | ||
73 | while(trajectory.doNextTransformation){ | ||
74 | //println(trajectory.stateCoder.createStateCode) | ||
75 | val stateId = trajectory.stateCoder.createStateCode.toString | ||
76 | val interpretation = trajectory.getModel(); | ||
77 | //calculate metrics of current state | ||
78 | calculateMetric(interpretation as PartialInterpretation, "debug/metric/output", stateId, counter) | ||
79 | stateCodes.add(stateId) | ||
80 | counter++ | ||
81 | } | ||
82 | |||
83 | |||
84 | //output the trajectory | ||
85 | try{ | ||
86 | new File("debug/metric/trajectories/").mkdir(); | ||
87 | val path = "debug/metric/trajectories/trajectory"+trajectory.stateCoder.createStateCode.toString+".csv" | ||
88 | val PrintWriter writer = new PrintWriter(new File(path)) | ||
89 | val output = new StringBuilder | ||
90 | for(stateCode : stateCodes){ | ||
91 | output.append(stateCode+'\n') | ||
92 | } | ||
93 | writer.write(output.toString()) | ||
94 | writer.close() | ||
95 | }catch(FileNotFoundException e) { | ||
96 | e.printStackTrace() | ||
97 | } | ||
98 | } | ||
99 | } | ||
100 | } | ||
101 | |||
102 | class MetricDistanceGroup{ | ||
103 | var double mpcDistance; | ||
104 | var double naDistance; | ||
105 | var double outDegreeDistance; | ||
106 | |||
107 | new(double mpcDistance, double naDistance, double outDegreeDistance){ | ||
108 | this.mpcDistance = mpcDistance; | ||
109 | this.naDistance = naDistance; | ||
110 | this.outDegreeDistance = outDegreeDistance; | ||
111 | } | ||
112 | |||
113 | def double getMPCDistance(){ | ||
114 | return this.mpcDistance | ||
115 | } | ||
116 | |||
117 | def double getNADistance(){ | ||
118 | return this.naDistance | ||
119 | } | ||
120 | |||
121 | def double getOutDegreeDistance(){ | ||
122 | return this.outDegreeDistance | ||
123 | } | ||
124 | } \ No newline at end of file | ||