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