diff options
Diffstat (limited to 'subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/MatchingFrame.java')
-rw-r--r-- | subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/MatchingFrame.java | 122 |
1 files changed, 122 insertions, 0 deletions
diff --git a/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/MatchingFrame.java b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/MatchingFrame.java new file mode 100644 index 00000000..9caf32bb --- /dev/null +++ b/subprojects/viatra-runtime-localsearch/src/main/java/tools/refinery/viatra/runtime/localsearch/MatchingFrame.java | |||
@@ -0,0 +1,122 @@ | |||
1 | /******************************************************************************* | ||
2 | * Copyright (c) 2004-2008 Akos Horvath, Gergely Varro Zoltan Ujhelyi 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.localsearch; | ||
11 | |||
12 | import java.util.Arrays; | ||
13 | import java.util.stream.Collectors; | ||
14 | |||
15 | import org.eclipse.emf.ecore.EObject; | ||
16 | import org.eclipse.emf.ecore.EStructuralFeature; | ||
17 | import tools.refinery.viatra.runtime.matchers.tuple.IModifiableTuple; | ||
18 | import tools.refinery.viatra.runtime.matchers.tuple.VolatileTuple; | ||
19 | import tools.refinery.viatra.runtime.matchers.util.Preconditions; | ||
20 | |||
21 | /** | ||
22 | * A MatchingFrame is a Volatile Tuple implementation used by the local search engine internally. | ||
23 | */ | ||
24 | public class MatchingFrame extends VolatileTuple implements IModifiableTuple { | ||
25 | |||
26 | /** | ||
27 | * The array that physically holds the values. | ||
28 | */ | ||
29 | private Object[] frame; | ||
30 | |||
31 | /** | ||
32 | * @since 1.7 | ||
33 | */ | ||
34 | public MatchingFrame(int frameSize) { | ||
35 | this.frame = new Object[frameSize]; | ||
36 | } | ||
37 | |||
38 | /** | ||
39 | * Creates a copy of another matching frame; the two frames can be updated separately | ||
40 | * @param other | ||
41 | * @since 1.7 | ||
42 | */ | ||
43 | public MatchingFrame(MatchingFrame other) { | ||
44 | this.frame = Arrays.copyOf(other.frame, other.frame.length); | ||
45 | } | ||
46 | |||
47 | |||
48 | |||
49 | /** | ||
50 | * Returns the value stored inside the matching frame. | ||
51 | * | ||
52 | * @param position | ||
53 | * @return the element stored in the selected position in the frame, or null if it is not yet set | ||
54 | * @throws IndexOutOfBoundsException | ||
55 | * if position is negative | ||
56 | * @throws IllegalArgumentException | ||
57 | * if the position is larger then the length of the frame | ||
58 | */ | ||
59 | public Object getValue(int position) { | ||
60 | Preconditions.checkElementIndex(position, frame.length); | ||
61 | return frame[position]; | ||
62 | } | ||
63 | |||
64 | /** | ||
65 | * Sets the value of the variable at the given position. For internal use in LS matching only. | ||
66 | * | ||
67 | * @param position the position of the variable within the frame | ||
68 | * @param value the value to be set for the variable | ||
69 | */ | ||
70 | public void setValue(int position, Object value) { | ||
71 | Preconditions.checkElementIndex(position, frame.length); | ||
72 | frame[position] = value; | ||
73 | } | ||
74 | |||
75 | public boolean testAndSetValue(Integer position, Object value) { | ||
76 | Preconditions.checkElementIndex(position, frame.length); | ||
77 | if (frame[position] == null) { | ||
78 | frame[position] = value; | ||
79 | return true; | ||
80 | } else { | ||
81 | return frame[position].equals(value); | ||
82 | } | ||
83 | } | ||
84 | |||
85 | @Override | ||
86 | public String toString() { | ||
87 | return Arrays.stream(frame).map(this::stringRepresentation).collect(Collectors.joining(", ", "[", "]")); | ||
88 | } | ||
89 | |||
90 | private String stringRepresentation(Object obj) { | ||
91 | if (obj == null) { | ||
92 | return "_"; | ||
93 | } else if (obj instanceof EObject) { | ||
94 | EObject eObject = (EObject) obj; | ||
95 | final EStructuralFeature feature = eObject.eClass().getEStructuralFeature("identifier"); | ||
96 | if (feature != null) { | ||
97 | return String.format("%s : %s", eObject.eGet(feature), eObject.eClass().getName()); | ||
98 | } | ||
99 | } | ||
100 | return obj.toString(); | ||
101 | } | ||
102 | |||
103 | @Override | ||
104 | public int getSize() { | ||
105 | return frame.length; | ||
106 | } | ||
107 | |||
108 | @Override | ||
109 | public Object get(int index) { | ||
110 | return getValue(index); | ||
111 | } | ||
112 | |||
113 | @Override | ||
114 | public Object[] getElements() { | ||
115 | return Arrays.copyOf(frame, frame.length); | ||
116 | } | ||
117 | |||
118 | @Override | ||
119 | public void set(int index, Object value) { | ||
120 | frame[index] = value; | ||
121 | } | ||
122 | } | ||