aboutsummaryrefslogtreecommitdiffstats
path: root/Metrics/Metrics-Calculation/RealisticRunner/src/ca/mcgill/dslreasoner/runner/BestKRunner.java
diff options
context:
space:
mode:
Diffstat (limited to 'Metrics/Metrics-Calculation/RealisticRunner/src/ca/mcgill/dslreasoner/runner/BestKRunner.java')
-rw-r--r--Metrics/Metrics-Calculation/RealisticRunner/src/ca/mcgill/dslreasoner/runner/BestKRunner.java117
1 files changed, 117 insertions, 0 deletions
diff --git a/Metrics/Metrics-Calculation/RealisticRunner/src/ca/mcgill/dslreasoner/runner/BestKRunner.java b/Metrics/Metrics-Calculation/RealisticRunner/src/ca/mcgill/dslreasoner/runner/BestKRunner.java
new file mode 100644
index 00000000..3a2d3eff
--- /dev/null
+++ b/Metrics/Metrics-Calculation/RealisticRunner/src/ca/mcgill/dslreasoner/runner/BestKRunner.java
@@ -0,0 +1,117 @@
1package ca.mcgill.dslreasoner.runner;
2
3import java.util.ArrayList;
4import java.util.List;
5import java.util.PriorityQueue;
6import java.util.stream.Collectors;
7
8import org.eclipse.emf.ecore.EObject;
9
10import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.app.Domain;
11import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.app.EcoreMetricDistance;
12import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.app.MetricDistanceGroup;
13import ca.mcgill.ecse.dslreasoner.realistic.metrics.calculator.validation.ViolationCheck;
14
15public class BestKRunner {
16 private class ModelWithScore implements Comparable<ModelWithScore> {
17 public final EObject root;
18 public final double score;
19
20 public ModelWithScore(EObject root, double score) {
21 this.root = root;
22 this.score = score;
23 }
24
25 @Override
26 public int compareTo(ModelWithScore o) {
27 return Double.compare(o.score, this.score);
28 }
29 }
30
31 private int k = 100;
32 IGeneratorRunner runner;
33 long timeLimit;
34 EcoreMetricDistance distanceCalculator;
35 Domain d;
36 int singleRun;
37 ViolationCheck checker;
38
39 public BestKRunner(int k, IGeneratorRunner runner, long time, Domain d, int singleRun) {
40 this(k, time, d, singleRun);
41 this.runner = runner;
42 }
43
44 public BestKRunner(int k, long time, Domain d, int singleRun) {
45 this.k = k;
46 this.timeLimit = time;
47 this.d = d;
48 this.singleRun = singleRun;
49 this.distanceCalculator = new EcoreMetricDistance(d);
50 }
51
52 public void setRunner(IGeneratorRunner runner) {
53 this.runner = runner;
54 }
55
56 public List<EObject> generate() {
57 this.checker = new ViolationCheck(d);
58 PriorityQueue<ModelWithScore> queue = new PriorityQueue<ModelWithScore>();
59 long time = 0;
60
61
62 while (time < timeLimit) {
63 System.out.println(time);
64 long start = System.currentTimeMillis();
65 try {
66 List<EObject> roots =runner.runGeneration(singleRun);
67 int j = 0;
68 for (EObject root : roots) {
69 j++;
70// System.out.println(j);
71 tryAdd(queue, root);
72 }
73
74 } catch (Exception e) {
75 e.printStackTrace();
76 System.exit(1);
77 }
78
79 time += (System.currentTimeMillis() - start);
80 }
81
82 return queue.stream().map(e -> e.root).collect(Collectors.toList());
83 }
84
85 private void tryAdd(PriorityQueue<ModelWithScore> queue, EObject root) throws Exception {
86 MetricDistanceGroup g = distanceCalculator.calculateMetricDistanceKS(root);
87 // int violations = checker.calculateViolationCounts(root);
88 ModelWithScore model = new ModelWithScore(root, calculateScore(g, 0));
89
90
91 if (queue.size() < k) {
92// System.out.println("-----------------------------------------------");
93// System.out.println("MPC: " + g.getMPCDistance());
94// System.out.println("NA: " + g.getNADistance());
95// System.out.println("NT: " + g.getNodeTypeDistance());
96// System.out.println("OD: " + g.getOutDegreeDistance());
97
98 queue.offer(model);
99 } else if (model.compareTo(queue.peek()) > 0) {
100
101// System.out.println("-----------------------------------------------");
102// System.out.println("MPC: " + g.getMPCDistance());
103// System.out.println("NA: " + g.getNADistance());
104// System.out.println("NT: " + g.getNodeTypeDistance());
105// System.out.println("OD: " + g.getOutDegreeDistance());
106
107 queue.offer(model);
108 queue.poll();
109 }
110
111 }
112
113
114 private double calculateScore(MetricDistanceGroup g, int violations) {
115 return g.getMPCDistance() + g.getNADistance() + g.getNodeTypeDistance() + g.getOutDegreeDistance() + violations;
116 }
117}