diff options
Diffstat (limited to 'subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/profiler/LocalSearchProfilerAdapter.java')
-rw-r--r-- | subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/profiler/LocalSearchProfilerAdapter.java | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/profiler/LocalSearchProfilerAdapter.java b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/profiler/LocalSearchProfilerAdapter.java new file mode 100644 index 00000000..8c50c694 --- /dev/null +++ b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/profiler/LocalSearchProfilerAdapter.java | |||
@@ -0,0 +1,83 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2010-2016, Grill Balázs, IncQueryLabs Ltd. | ||
3 | * This program and the accompanying materials are made available under the | ||
4 | * terms of the Eclipse Public License v. 2.0 which is available at | ||
5 | * http://www.eclipse.org/legal/epl-v20.html. | ||
6 | * | ||
7 | * SPDX-License-Identifier: EPL-2.0 | ||
8 | *******************************************************************************/ | ||
9 | package tools.refinery.viatra.runtime.localsearch.profiler; | ||
10 | |||
11 | import java.util.HashMap; | ||
12 | import java.util.List; | ||
13 | import java.util.Map; | ||
14 | import java.util.stream.Collectors; | ||
15 | |||
16 | import tools.refinery.viatra.runtime.localsearch.MatchingFrame; | ||
17 | import tools.refinery.viatra.runtime.localsearch.matcher.ILocalSearchAdapter; | ||
18 | import tools.refinery.viatra.runtime.localsearch.matcher.LocalSearchMatcher; | ||
19 | import tools.refinery.viatra.runtime.localsearch.matcher.MatcherReference; | ||
20 | import tools.refinery.viatra.runtime.localsearch.operations.ISearchOperation; | ||
21 | import tools.refinery.viatra.runtime.localsearch.plan.SearchPlan; | ||
22 | import tools.refinery.viatra.runtime.localsearch.plan.SearchPlanExecutor; | ||
23 | |||
24 | /** | ||
25 | * This is a simple {@link ILocalSearchAdapter} which capable of counting | ||
26 | * each search operation execution then printing it in human readably form | ||
27 | * (along with the executed plans) using {@link #toString()} | ||
28 | * @author Grill Balázs | ||
29 | * @since 1.5 | ||
30 | * | ||
31 | */ | ||
32 | public class LocalSearchProfilerAdapter implements ILocalSearchAdapter { | ||
33 | |||
34 | private final Map<MatcherReference, List<SearchPlan>> planReference = new HashMap<>(); | ||
35 | |||
36 | private final Map<ISearchOperation, Integer> successfulOperationCounts = new HashMap<>(); | ||
37 | private final Map<ISearchOperation, Integer> failedOperationCounts = new HashMap<>(); | ||
38 | |||
39 | @Override | ||
40 | public void patternMatchingStarted(LocalSearchMatcher lsMatcher) { | ||
41 | MatcherReference key = new MatcherReference(lsMatcher.getPlanDescriptor().getQuery(), | ||
42 | lsMatcher.getPlanDescriptor().getAdornment()); | ||
43 | planReference.put(key, lsMatcher.getPlan().stream().map(SearchPlanExecutor::getSearchPlan).collect(Collectors.toList())); | ||
44 | } | ||
45 | |||
46 | @Override | ||
47 | public void operationExecuted(SearchPlan plan, ISearchOperation operation, MatchingFrame frame, boolean isSuccessful) { | ||
48 | Map<ISearchOperation, Integer> counts = isSuccessful ? successfulOperationCounts : failedOperationCounts; | ||
49 | counts.merge(operation, | ||
50 | /*no previous entry*/1, | ||
51 | /*increase previous value*/(oldValue, v) -> oldValue + 1); | ||
52 | } | ||
53 | |||
54 | @Override | ||
55 | public String toString() { | ||
56 | StringBuilder sb = new StringBuilder(); | ||
57 | for (java.util.Map.Entry<MatcherReference, List<SearchPlan>> entry: planReference.entrySet()){ | ||
58 | sb.append(entry.getKey()); | ||
59 | sb.append("\n"); | ||
60 | |||
61 | sb.append(entry.getValue()); | ||
62 | |||
63 | List<SearchPlan> bodies = entry.getValue(); | ||
64 | sb.append("{\n"); | ||
65 | for(int i=0;i<bodies.size();i++){ | ||
66 | sb.append("\tbody #");sb.append(i);sb.append("(\n"); | ||
67 | for(ISearchOperation operation : bodies.get(i).getOperations()){ | ||
68 | final int successCount = successfulOperationCounts.computeIfAbsent(operation, op -> 0); | ||
69 | final int failCount = failedOperationCounts.computeIfAbsent(operation, op -> 0); | ||
70 | sb.append("\t\t");sb.append(successCount); | ||
71 | sb.append("\t");sb.append(failCount); | ||
72 | sb.append("\t");sb.append(successCount + failCount); | ||
73 | sb.append("\t");sb.append(operation); | ||
74 | sb.append("\n"); | ||
75 | } | ||
76 | sb.append("\t)\n"); | ||
77 | } | ||
78 | sb.append("}\n"); | ||
79 | } | ||
80 | return sb.toString(); | ||
81 | } | ||
82 | |||
83 | } | ||