diff options
Diffstat (limited to 'subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/construction/basiclinear/OrderingHeuristics.java')
-rw-r--r-- | subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/construction/basiclinear/OrderingHeuristics.java | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/construction/basiclinear/OrderingHeuristics.java b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/construction/basiclinear/OrderingHeuristics.java new file mode 100644 index 00000000..2b4e8890 --- /dev/null +++ b/subprojects/viatra-runtime-rete/src/main/java/tools/refinery/viatra/runtime/rete/construction/basiclinear/OrderingHeuristics.java | |||
@@ -0,0 +1,90 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2004-2010 Gabor Bergmann and Daniel Varro | ||
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 | |||
10 | package tools.refinery.viatra.runtime.rete.construction.basiclinear; | ||
11 | |||
12 | import java.util.Comparator; | ||
13 | import java.util.Set; | ||
14 | |||
15 | import tools.refinery.viatra.runtime.matchers.context.IQueryMetaContext; | ||
16 | import tools.refinery.viatra.runtime.matchers.planning.SubPlan; | ||
17 | import tools.refinery.viatra.runtime.matchers.psystem.DeferredPConstraint; | ||
18 | import tools.refinery.viatra.runtime.matchers.psystem.EnumerablePConstraint; | ||
19 | import tools.refinery.viatra.runtime.matchers.psystem.PConstraint; | ||
20 | import tools.refinery.viatra.runtime.matchers.psystem.PVariable; | ||
21 | import tools.refinery.viatra.runtime.matchers.psystem.basicenumerables.ConstantValue; | ||
22 | import tools.refinery.viatra.runtime.matchers.util.CollectionsFactory; | ||
23 | import tools.refinery.viatra.runtime.rete.util.OrderingCompareAgent; | ||
24 | |||
25 | /** | ||
26 | * @author Gabor Bergmann | ||
27 | * | ||
28 | */ | ||
29 | public class OrderingHeuristics implements Comparator<PConstraint> { | ||
30 | private SubPlan plan; | ||
31 | private IQueryMetaContext context; | ||
32 | |||
33 | public OrderingHeuristics(SubPlan plan, IQueryMetaContext context) { | ||
34 | super(); | ||
35 | this.plan = plan; | ||
36 | this.context = context; | ||
37 | } | ||
38 | |||
39 | @Override | ||
40 | public int compare(PConstraint o1, PConstraint o2) { | ||
41 | return new OrderingCompareAgent<PConstraint>(o1, o2) { | ||
42 | @Override | ||
43 | protected void doCompare() { | ||
44 | boolean temp = consider(preferTrue(isConstant(a), isConstant(b))) | ||
45 | && consider(preferTrue(isReady(a), isReady(b))); | ||
46 | if (!temp) | ||
47 | return; | ||
48 | |||
49 | Set<PVariable> bound1 = boundVariables(a); | ||
50 | Set<PVariable> bound2 = boundVariables(b); | ||
51 | swallowBoolean(temp && consider(preferTrue(isBound(a, bound1), isBound(b, bound2))) | ||
52 | && consider(preferMore(degreeBound(a, bound1), degreeBound(b, bound2))) | ||
53 | && consider(preferLess(degreeFree(a, bound1), degreeFree(b, bound2))) | ||
54 | |||
55 | // tie breaking | ||
56 | && consider(preferLess(a.getMonotonousID(), b.getMonotonousID())) // this is hopefully deterministic | ||
57 | && consider(preferLess(System.identityHashCode(a), System.identityHashCode(b)))); | ||
58 | } | ||
59 | }.compare(); | ||
60 | } | ||
61 | |||
62 | boolean isConstant(PConstraint o) { | ||
63 | return (o instanceof ConstantValue); | ||
64 | } | ||
65 | |||
66 | boolean isReady(PConstraint o) { | ||
67 | return (o instanceof EnumerablePConstraint) | ||
68 | || (o instanceof DeferredPConstraint && ((DeferredPConstraint) o) | ||
69 | .isReadyAt(plan, context)); | ||
70 | } | ||
71 | |||
72 | Set<PVariable> boundVariables(PConstraint o) { | ||
73 | Set<PVariable> boundVariables = CollectionsFactory.createSet(o.getAffectedVariables()); | ||
74 | boundVariables.retainAll(plan.getVisibleVariables()); | ||
75 | return boundVariables; | ||
76 | } | ||
77 | |||
78 | boolean isBound(PConstraint o, Set<PVariable> boundVariables) { | ||
79 | return boundVariables.size() == o.getAffectedVariables().size(); | ||
80 | } | ||
81 | |||
82 | int degreeBound(PConstraint o, Set<PVariable> boundVariables) { | ||
83 | return boundVariables.size(); | ||
84 | } | ||
85 | |||
86 | int degreeFree(PConstraint o, Set<PVariable> boundVariables) { | ||
87 | return o.getAffectedVariables().size() - boundVariables.size(); | ||
88 | } | ||
89 | |||
90 | } | ||