From 9b8236dd7499510577c32f0d87dfd52d34f6b023 Mon Sep 17 00:00:00 2001 From: Kristóf Marussy Date: Sat, 2 Oct 2021 20:51:58 +0200 Subject: chore: fix Java 17 Sonar warnings Had to disable code coverage measurement in the Quality Gate, we should switch it on again once we have a complete test suite. --- .../viatra/solver/data/map/internal/HashClash.java | 18 ++ .../solver/data/map/internal/ImmutableNode.java | 255 ++++++++++----------- .../solver/data/map/internal/MapDiffCursor.java | 125 +++++----- .../solver/data/map/internal/MutableNode.java | 45 ++-- .../viatra/solver/data/model/ModelStoreImpl.java | 26 +-- .../data/model/representation/TruthValue.java | 47 ++-- .../solver/data/query/building/PredicateAtom.java | 19 +- .../solver/data/query/building/RelationAtom.java | 16 +- .../query/internal/RelationalRuntimeContext.java | 4 +- 9 files changed, 299 insertions(+), 256 deletions(-) create mode 100644 store/src/main/java/org/eclipse/viatra/solver/data/map/internal/HashClash.java (limited to 'store/src/main') diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/map/internal/HashClash.java b/store/src/main/java/org/eclipse/viatra/solver/data/map/internal/HashClash.java new file mode 100644 index 00000000..c70fb8b8 --- /dev/null +++ b/store/src/main/java/org/eclipse/viatra/solver/data/map/internal/HashClash.java @@ -0,0 +1,18 @@ +package org.eclipse.viatra.solver.data.map.internal; + +enum HashClash { + /** + * Not stuck. + */ + NONE, + + /** + * Clashed, next we should return the key of cursor 1. + */ + STUCK_CURSOR_1, + + /** + * Clashed, next we should return the key of cursor 2. + */ + STUCK_CURSOR_2 +} diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/map/internal/ImmutableNode.java b/store/src/main/java/org/eclipse/viatra/solver/data/map/internal/ImmutableNode.java index 04a9b19a..b507763f 100644 --- a/store/src/main/java/org/eclipse/viatra/solver/data/map/internal/ImmutableNode.java +++ b/store/src/main/java/org/eclipse/viatra/solver/data/map/internal/ImmutableNode.java @@ -15,15 +15,16 @@ public class ImmutableNode extends Node { */ final int nodeMap; /** - * Stores Keys, Values, and subnodes. Structure: (K,V)*,NODE; NODES are stored backwards. + * Stores Keys, Values, and subnodes. Structure: (K,V)*,NODE; NODES are stored + * backwards. */ final Object[] content; - + /** * Hash code derived from immutable hash code */ final int precalculatedHash; - + private ImmutableNode(int dataMap, int nodeMap, Object[] content, int precalculatedHash) { super(); this.dataMap = dataMap; @@ -41,83 +42,87 @@ public class ImmutableNode extends Node { * available. * @return an immutable version of the input node. */ - @SuppressWarnings("unchecked") - static ImmutableNode constructImmutable(MutableNode node, Map, ImmutableNode> cache) { + static ImmutableNode constructImmutable(MutableNode node, + Map, ImmutableNode> cache) { // 1. try to return from cache - if(cache != null) { + if (cache != null) { ImmutableNode cachedResult = cache.get(node); - if(cachedResult != null) { + if (cachedResult != null) { // 1.1 Already cached, return from cache. return cachedResult; } } - + // 2. otherwise construct a new ImmutableNode int size = 0; - for(int i = 0; i subnode = (Node) node.content[i*2+1]; - if(subnode != null) { + @SuppressWarnings("unchecked") + var subnode = (Node) node.content[i * 2 + 1]; + if (subnode != null) { ImmutableNode immutableSubnode = subnode.toImmutable(cache); - resultNodeMap |=bitposition; - resultContent[size-1-nodes] = immutableSubnode; + resultNodeMap |= bitposition; + resultContent[size - 1 - nodes] = immutableSubnode; nodes++; } } - bitposition<<=1; + bitposition <<= 1; } final int resultHash = node.hashCode(); - ImmutableNode newImmutable = new ImmutableNode<>(resultDataMap, resultNodeMap, resultContent, resultHash); - + var newImmutable = new ImmutableNode(resultDataMap, resultNodeMap, resultContent, resultHash); + // 3. save new immutable. - if(cache != null) { + if (cache != null) { cache.put(newImmutable, newImmutable); } return newImmutable; } - + private int index(int bitmap, int bitpos) { - return Integer.bitCount(bitmap & (bitpos-1)); + return Integer.bitCount(bitmap & (bitpos - 1)); } - - @SuppressWarnings("unchecked") + @Override public V getValue(K key, ContinousHashProvider hashProvider, V defaultValue, int hash, int depth) { - int selectedHashFragment = hashFragment(hash,shiftDepth(depth)); + int selectedHashFragment = hashFragment(hash, shiftDepth(depth)); int bitposition = 1 << selectedHashFragment; // If the key is stored as a data - if((dataMap & bitposition) != 0) { - int keyIndex = 2*index(dataMap, bitposition); + if ((dataMap & bitposition) != 0) { + int keyIndex = 2 * index(dataMap, bitposition); + @SuppressWarnings("unchecked") K keyCandidate = (K) content[keyIndex]; - if(keyCandidate.equals(key)) { - return (V) content[keyIndex+1]; + if (keyCandidate.equals(key)) { + @SuppressWarnings("unchecked") + V value = (V) content[keyIndex + 1]; + return value; } else { return defaultValue; } } // the key is stored as a node - else if((nodeMap & bitposition) != 0) { - int keyIndex = content.length-1-index(nodeMap, bitposition); - ImmutableNode subNode = (ImmutableNode) content[keyIndex]; - int newDepth = depth+1; + else if ((nodeMap & bitposition) != 0) { + int keyIndex = content.length - 1 - index(nodeMap, bitposition); + @SuppressWarnings("unchecked") + var subNode = (ImmutableNode) content[keyIndex]; + int newDepth = depth + 1; int newHash = newHash(hashProvider, key, hash, newDepth); return subNode.getValue(key, hashProvider, defaultValue, newHash, newDepth); } @@ -126,21 +131,22 @@ public class ImmutableNode extends Node { return defaultValue; } } - - @SuppressWarnings("unchecked") + @Override - public Node putValue(K key, V value, OldValueBox oldValue, ContinousHashProvider hashProvider, V defaultValue, int hash, int depth) { - int selectedHashFragment = hashFragment(hash,shiftDepth(depth)); + public Node putValue(K key, V value, OldValueBox oldValue, ContinousHashProvider hashProvider, + V defaultValue, int hash, int depth) { + int selectedHashFragment = hashFragment(hash, shiftDepth(depth)); int bitposition = 1 << selectedHashFragment; - if((dataMap & bitposition) != 0) { - int keyIndex = 2*index(dataMap, bitposition); + if ((dataMap & bitposition) != 0) { + int keyIndex = 2 * index(dataMap, bitposition); + @SuppressWarnings("unchecked") K keyCandidate = (K) content[keyIndex]; - if(keyCandidate.equals(key)) { - if(value == defaultValue) { + if (keyCandidate.equals(key)) { + if (value == defaultValue) { // delete MutableNode mutable = this.toMutable(); - return mutable.removeEntry(selectedHashFragment,oldValue); - } else if(value == content[keyIndex+1]) { + return mutable.removeEntry(selectedHashFragment, oldValue); + } else if (value == content[keyIndex + 1]) { // dont change oldValue.setOldValue(value); return this; @@ -150,7 +156,7 @@ public class ImmutableNode extends Node { return mutable.updateValue(value, oldValue, selectedHashFragment); } } else { - if(value == defaultValue) { + if (value == defaultValue) { // dont change oldValue.setOldValue(defaultValue); return this; @@ -160,19 +166,19 @@ public class ImmutableNode extends Node { return mutable.putValue(key, value, oldValue, hashProvider, defaultValue, hash, depth); } } - } else if((nodeMap & bitposition)!=0) { - - int keyIndex = content.length-1-index(nodeMap, bitposition); - ImmutableNode subNode = (ImmutableNode) content[keyIndex]; - int newDepth = depth+1; + } else if ((nodeMap & bitposition) != 0) { + int keyIndex = content.length - 1 - index(nodeMap, bitposition); + @SuppressWarnings("unchecked") + var subNode = (ImmutableNode) content[keyIndex]; + int newDepth = depth + 1; int newHash = newHash(hashProvider, key, hash, newDepth); - Node newsubNode = subNode.putValue(key, value, oldValue, hashProvider, defaultValue, newHash, newDepth); - - if(subNode == newsubNode) { + var newsubNode = subNode.putValue(key, value, oldValue, hashProvider, defaultValue, newHash, newDepth); + + if (subNode == newsubNode) { // nothing changed return this; } else { - MutableNode mutable = toMutable(); + MutableNode mutable = toMutable(); return mutable.updateWithSubNode(selectedHashFragment, newsubNode, value.equals(defaultValue)); } } else { @@ -181,59 +187,56 @@ public class ImmutableNode extends Node { return mutable.putValue(key, value, oldValue, hashProvider, defaultValue, hash, depth); } } - - - @SuppressWarnings("unchecked") + @Override public long getSize() { int result = Integer.bitCount(this.dataMap); - for(int subnodeIndex = 0; subnodeIndex < Integer.bitCount(this.nodeMap); subnodeIndex++) { - ImmutableNode subnode = - (ImmutableNode) this.content[this.content.length-1-subnodeIndex]; + for (int subnodeIndex = 0; subnodeIndex < Integer.bitCount(this.nodeMap); subnodeIndex++) { + @SuppressWarnings("unchecked") + var subnode = (ImmutableNode) this.content[this.content.length - 1 - subnodeIndex]; result += subnode.getSize(); } return result; } - + @Override - protected MutableNode toMutable() { + protected MutableNode toMutable() { return new MutableNode<>(this); } - + @Override - public ImmutableNode toImmutable( - Map, ImmutableNode> cache) { + public ImmutableNode toImmutable(Map, ImmutableNode> cache) { return this; } - + @Override protected MutableNode isMutable() { return null; } - + @SuppressWarnings("unchecked") @Override boolean moveToNext(MapCursor cursor) { // 1. try to move to data int datas = Integer.bitCount(this.dataMap); - if(cursor.dataIndex != MapCursor.INDEX_FINISH) { + if (cursor.dataIndex != MapCursor.INDEX_FINISH) { int newDataIndex = cursor.dataIndex + 1; - if(newDataIndex < datas) { + if (newDataIndex < datas) { cursor.dataIndex = newDataIndex; - cursor.key = (K) this.content[newDataIndex*2]; - cursor.value = (V) this.content[newDataIndex*2+1]; + cursor.key = (K) this.content[newDataIndex * 2]; + cursor.value = (V) this.content[newDataIndex * 2 + 1]; return true; } else { cursor.dataIndex = MapCursor.INDEX_FINISH; } } - + // 2. look inside the subnodes int nodes = Integer.bitCount(this.nodeMap); int newNodeIndex = cursor.nodeIndexStack.peek() + 1; - if(newNodeIndex < nodes) { + if (newNodeIndex < nodes) { // 2.1 found next subnode, move down to the subnode - Node subnode = (Node) this.content[this.content.length-1-newNodeIndex]; + Node subnode = (Node) this.content[this.content.length - 1 - newNodeIndex]; cursor.dataIndex = MapCursor.INDEX_START; cursor.nodeIndexStack.pop(); cursor.nodeIndexStack.push(newNodeIndex); @@ -244,7 +247,7 @@ public class ImmutableNode extends Node { // 3. no subnode found, move up cursor.nodeStack.pop(); cursor.nodeIndexStack.pop(); - if(!cursor.nodeStack.isEmpty()) { + if (!cursor.nodeStack.isEmpty()) { Node supernode = cursor.nodeStack.peek(); return supernode.moveToNext(cursor); } else { @@ -254,69 +257,69 @@ public class ImmutableNode extends Node { } } } - + @Override public void prettyPrint(StringBuilder builder, int depth, int code) { - for(int i = 0; i=0) { + if (code >= 0) { builder.append(code); builder.append(":"); } builder.append("Immutable("); boolean hadContent = false; int dataMask = 1; - for(int i = 0; i["); - builder.append(content[2*index(dataMap, dataMask)+1].toString()); + builder.append(content[2 * index(dataMap, dataMask) + 1].toString()); builder.append("]"); hadContent = true; } - dataMask<<=1; + dataMask <<= 1; } builder.append(")"); int nodeMask = 1; - for(int i = 0; i subNode = (Node) content[content.length-1-index(nodeMap, nodeMask)]; + Node subNode = (Node) content[content.length - 1 - index(nodeMap, nodeMask)]; builder.append("\n"); - subNode.prettyPrint(builder, depth+1, i); + subNode.prettyPrint(builder, depth + 1, i); } - nodeMask<<=1; + nodeMask <<= 1; } } - - @SuppressWarnings("unchecked") + @Override public void checkIntegrity(ContinousHashProvider hashProvider, V defaultValue, int depth) { - if(depth>0) { + if (depth > 0) { boolean orphaned = Integer.bitCount(dataMap) == 1 && nodeMap == 0; - if(orphaned) { - throw new IllegalStateException("Orphaned node! " + dataMap + ": " + content[0]); + if (orphaned) { + throw new IllegalStateException("Orphaned node! " + dataMap + ": " + content[0]); } } // check the place of data - + // check subnodes - for(int i = 0; i subnode = (Node) this.content[this.content.length-1-i]; - if(! (subnode instanceof ImmutableNode)) { + for (int i = 0; i < Integer.bitCount(nodeMap); i++) { + @SuppressWarnings("unchecked") + var subnode = (Node) this.content[this.content.length - 1 - i]; + if (!(subnode instanceof ImmutableNode)) { throw new IllegalStateException("Immutable node contains mutable subnodes!"); } else { - subnode.checkIntegrity(hashProvider, defaultValue, depth+1); + subnode.checkIntegrity(hashProvider, defaultValue, depth + 1); } } } - + @Override public int hashCode() { return this.precalculatedHash; @@ -328,43 +331,39 @@ public class ImmutableNode extends Node { return true; if (obj == null) return false; - if (obj instanceof ImmutableNode) { - ImmutableNode other = (ImmutableNode) obj; - if (precalculatedHash != other.precalculatedHash || dataMap != other.dataMap || nodeMap != other.nodeMap || !Arrays.deepEquals(content, other.content)) - return false; - else return true; - } else if(obj instanceof MutableNode) { - return ImmutableNode.compareImmutableMutable(this, (MutableNode) obj); + if (obj instanceof ImmutableNode other) { + return precalculatedHash == other.precalculatedHash && dataMap == other.dataMap && nodeMap == other.nodeMap + && Arrays.deepEquals(content, other.content); + } else if (obj instanceof MutableNode mutableObj) { + return ImmutableNode.compareImmutableMutable(this, mutableObj); } else { return false; } } - public static boolean compareImmutableMutable( - ImmutableNode immutable, - MutableNode mutable) - { + + public static boolean compareImmutableMutable(ImmutableNode immutable, MutableNode mutable) { int datas = 0; int nodes = 0; final int immutableLength = immutable.content.length; - for(int i = 0; i mutableSubnode = (Node) mutable.content[i*2+1]; - if(mutableSubnode != null) { - if(datas*2+nodes+1 <= immutableLength) { - Object immutableSubnode = immutable.content[immutableLength-1-nodes]; - if(!mutableSubnode.equals(immutableSubnode)) { + var mutableSubnode = (Node) mutable.content[i * 2 + 1]; + if (mutableSubnode != null) { + if (datas * 2 + nodes + 1 <= immutableLength) { + Object immutableSubnode = immutable.content[immutableLength - 1 - nodes]; + if (!mutableSubnode.equals(immutableSubnode)) { return false; } nodes++; diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/map/internal/MapDiffCursor.java b/store/src/main/java/org/eclipse/viatra/solver/data/map/internal/MapDiffCursor.java index e97e4aa1..35d20539 100644 --- a/store/src/main/java/org/eclipse/viatra/solver/data/map/internal/MapDiffCursor.java +++ b/store/src/main/java/org/eclipse/viatra/solver/data/map/internal/MapDiffCursor.java @@ -1,7 +1,6 @@ package org.eclipse.viatra.solver.data.map.internal; import java.util.List; -import java.util.stream.Collectors; import java.util.stream.Stream; import org.eclipse.viatra.solver.data.map.ContinousHashProvider; @@ -11,110 +10,109 @@ import org.eclipse.viatra.solver.data.map.VersionedMap; /** * A cursor representing the difference between two states of a map. + * * @author Oszkar Semerath * */ -public class MapDiffCursor implements DiffCursor, Cursor{ +public class MapDiffCursor implements DiffCursor, Cursor { /** * Default value representing missing elements. */ private V defaultValue; - private MapCursor cursor1; - private MapCursor cursor2; + private MapCursor cursor1; + private MapCursor cursor2; private ContinousHashProvider hashProvider; - + // Values private K key; private V fromValue; private V toValue; - + // State /** - * Positive number if cursor 1 is behind, negative number if cursor 2 is behind, and 0 if they are at the same position. + * Positive number if cursor 1 is behind, negative number if cursor 2 is behind, + * and 0 if they are at the same position. */ private int cursorRelation; - /** - * Denotes a state when two cursors are in the same position, but they contain different keys. - * Possible values: - *
    - *
  • 0: not stuck
  • - *
  • 1: hashClash, next it should return the key of cursor 1.
  • - *
  • 2: hashClash, next it should return the key of cursor 2.
  • - *
- */ - private int hashClash=0; - - public MapDiffCursor(ContinousHashProvider hashProvider, V defaultValue, Cursor cursor1, Cursor cursor2) { + private HashClash hashClash = HashClash.NONE; + + public MapDiffCursor(ContinousHashProvider hashProvider, V defaultValue, Cursor cursor1, + Cursor cursor2) { super(); this.hashProvider = hashProvider; this.defaultValue = defaultValue; this.cursor1 = (MapCursor) cursor1; this.cursor2 = (MapCursor) cursor2; } + @Override public K getKey() { return key; } + @Override public V getFromValue() { return fromValue; } + @Override public V getToValue() { return toValue; } + @Override public V getValue() { return getToValue(); } + public boolean isTerminated() { return cursor1.isTerminated() && cursor2.isTerminated(); } + @Override public boolean isDirty() { return this.cursor1.isDirty() || this.cursor2.isDirty(); } + @Override public List> getDependingMaps() { - return Stream.concat( - cursor1.getDependingMaps().stream(), - cursor2.getDependingMaps().stream() - ).collect(Collectors.toList()); + return Stream.concat(cursor1.getDependingMaps().stream(), cursor2.getDependingMaps().stream()).toList(); } - + protected void updateState() { - if(!isTerminated()) { + if (!isTerminated()) { this.cursorRelation = MapCursor.compare(cursor1, cursor2); - if(cursorRelation > 0 || cursor2.isTerminated()) { + if (cursorRelation > 0 || cursor2.isTerminated()) { this.key = cursor1.getKey(); this.fromValue = cursor1.getValue(); this.toValue = defaultValue; - } else if(cursorRelation < 0 || cursor1.isTerminated()){ + } else if (cursorRelation < 0 || cursor1.isTerminated()) { this.key = cursor2.getKey(); this.fromValue = defaultValue; this.toValue = cursor1.getValue(); } else { // cursor1 = cursor2 - if(cursor1.getKey().equals(cursor2.getKey())) { + if (cursor1.getKey().equals(cursor2.getKey())) { this.key = cursor1.getKey(); this.fromValue = cursor1.getValue(); this.toValue = defaultValue; } else { - resolveHashClash1(); + resolveHashClashWithFirstEntry(); } } } } - protected void resolveHashClash1() { + + protected void resolveHashClashWithFirstEntry() { int compareResult = this.hashProvider.compare(cursor1.key, cursor2.key); - if(compareResult<0) { - this.hashClash = 2; + if (compareResult < 0) { + this.hashClash = HashClash.STUCK_CURSOR_2; this.cursorRelation = 0; this.key = cursor1.key; this.fromValue = cursor1.value; this.toValue = defaultValue; - } else if(compareResult>0) { - this.hashClash = 1; + } else if (compareResult > 0) { + this.hashClash = HashClash.STUCK_CURSOR_1; this.cursorRelation = 0; this.key = cursor2.key; this.fromValue = defaultValue; @@ -123,40 +121,47 @@ public class MapDiffCursor implements DiffCursor, Cursor{ throw new IllegalArgumentException("Inconsistent compare result for diffcursor"); } } + protected boolean isInHashClash() { - return this.hashClash != 0; + return this.hashClash != HashClash.NONE; } - protected void resolveHashClash2() { - if(hashClash == 1) { - this.hashClash = 0; + + protected void resolveHashClashWithSecondEntry() { + switch (this.hashClash) { + case STUCK_CURSOR_1: + this.hashClash = HashClash.NONE; this.cursorRelation = 0; this.key = cursor1.key; this.fromValue = cursor1.value; this.toValue = defaultValue; - } else if(hashClash == 2) { - this.hashClash = 0; + break; + case STUCK_CURSOR_2: + this.hashClash = HashClash.NONE; this.cursorRelation = 0; this.key = cursor2.key; this.fromValue = defaultValue; this.toValue = cursor2.value; - } else throw new IllegalArgumentException("Inconsistent compare result for diffcursor"); + break; + default: + throw new IllegalArgumentException("Inconsistent compare result for diffcursor"); + } } - protected boolean sameValues() { - if(this.fromValue == null) { + if (this.fromValue == null) { return this.toValue == null; } else { return this.fromValue.equals(this.toValue); } } + protected boolean moveOne() { - if(isTerminated()) { + if (isTerminated()) { return false; } - if(this.cursorRelation > 0 || cursor2.isTerminated()) { + if (this.cursorRelation > 0 || cursor2.isTerminated()) { return cursor1.move(); - } else if(this.cursorRelation < 0 || cursor1.isTerminated()) { + } else if (this.cursorRelation < 0 || cursor1.isTerminated()) { return cursor2.move(); } else { boolean moved1 = cursor1.move(); @@ -164,8 +169,9 @@ public class MapDiffCursor implements DiffCursor, Cursor{ return moved1 && moved2; } } + private boolean skipNode() { - if(isTerminated()) { + if (isTerminated()) { throw new IllegalStateException("DiffCursor tries to skip when terminated!"); } boolean update1 = cursor1.skipCurrentNode(); @@ -173,42 +179,43 @@ public class MapDiffCursor implements DiffCursor, Cursor{ updateState(); return update1 && update2; } - + protected boolean moveToConsistentState() { - if(!isTerminated()) { + if (!isTerminated()) { boolean changed; boolean lastResult = true; do { changed = false; - if(MapCursor.sameSubnode(cursor1, cursor2)) { + if (MapCursor.sameSubnode(cursor1, cursor2)) { lastResult = skipNode(); changed = true; } - if(sameValues()) { + if (sameValues()) { lastResult = moveOne(); changed = true; } updateState(); - } while(changed && !isTerminated()); + } while (changed && !isTerminated()); return lastResult; } else { return false; } } - + public boolean move() { - if(!isTerminated()) { - if(isInHashClash()) { - this.resolveHashClash2(); + if (!isTerminated()) { + if (isInHashClash()) { + this.resolveHashClashWithSecondEntry(); return true; } else { - if(moveOne()) { + if (moveOne()) { return moveToConsistentState(); } else { return false; } } - - } else return false; + + } else + return false; } } diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/map/internal/MutableNode.java b/store/src/main/java/org/eclipse/viatra/solver/data/map/internal/MutableNode.java index 9e63b941..b5fee673 100644 --- a/store/src/main/java/org/eclipse/viatra/solver/data/map/internal/MutableNode.java +++ b/store/src/main/java/org/eclipse/viatra/solver/data/map/internal/MutableNode.java @@ -52,19 +52,22 @@ public class MutableNode extends Node { this.cachedHash = node.hashCode(); } - @SuppressWarnings("unchecked") @Override public V getValue(K key, ContinousHashProvider hashProvider, V defaultValue, int hash, int depth) { int selectedHashFragment = hashFragment(hash, shiftDepth(depth)); + @SuppressWarnings("unchecked") K keyCandidate = (K) this.content[2 * selectedHashFragment]; if (keyCandidate != null) { if (keyCandidate.equals(key)) { - return (V) this.content[2 * selectedHashFragment + 1]; + @SuppressWarnings("unchecked") + V value = (V) this.content[2 * selectedHashFragment + 1]; + return value; } else { return defaultValue; } } else { - Node nodeCandidate = (Node) content[2 * selectedHashFragment + 1]; + @SuppressWarnings("unchecked") + var nodeCandidate = (Node) content[2 * selectedHashFragment + 1]; if (nodeCandidate != null) { int newDepth = depth + 1; int newHash = newHash(hashProvider, key, hash, newDepth); @@ -75,11 +78,11 @@ public class MutableNode extends Node { } } - @SuppressWarnings("unchecked") @Override - public Node putValue(K key, V value, OldValueBox oldValue, ContinousHashProvider hashProvider, V defaultValue, int hash, - int depth) { + public Node putValue(K key, V value, OldValueBox oldValue, ContinousHashProvider hashProvider, + V defaultValue, int hash, int depth) { int selectedHashFragment = hashFragment(hash, shiftDepth(depth)); + @SuppressWarnings("unchecked") K keyCandidate = (K) content[2 * selectedHashFragment]; if (keyCandidate != null) { // If has key @@ -105,10 +108,11 @@ public class MutableNode extends Node { } } else { // If it does not have key, check for value - Node nodeCandidate = (Node) content[2 * selectedHashFragment + 1]; + @SuppressWarnings("unchecked") + var nodeCandidate = (Node) content[2 * selectedHashFragment + 1]; if (nodeCandidate != null) { // If it has value, it is a subnode -> upate that - Node newNode = nodeCandidate.putValue(key, value, oldValue, hashProvider, defaultValue, + var newNode = nodeCandidate.putValue(key, value, oldValue, hashProvider, defaultValue, newHash(hashProvider, key, hash, depth + 1), depth + 1); return updateWithSubNode(selectedHashFragment, newNode, value.equals(defaultValue)); } else { @@ -125,10 +129,11 @@ public class MutableNode extends Node { } } - @SuppressWarnings("unchecked") - private Node addEntry(K key, V value, OldValueBox oldValue, int selectedHashFragment) { + private Node addEntry(K key, V value, OldValueBox oldValueBox, int selectedHashFragment) { content[2 * selectedHashFragment] = key; - oldValue.setOldValue((V) content[2 * selectedHashFragment + 1]); + @SuppressWarnings("unchecked") + V oldValue = (V) content[2 * selectedHashFragment + 1]; + oldValueBox.setOldValue(oldValue); content[2 * selectedHashFragment + 1] = value; updateHash(); return this; @@ -231,6 +236,8 @@ public class MutableNode extends Node { return this; } + // Pass everything as parameters for performance. + @SuppressWarnings("squid:S107") private MutableNode newNodeWithTwoEntries(ContinousHashProvider hashProvider, K key1, V value1, int oldHash1, K key2, V value2, int oldHash2, int newdepth) { int newHash1 = newHash(hashProvider, key1, oldHash1, newdepth); @@ -377,7 +384,6 @@ public class MutableNode extends Node { } } - @SuppressWarnings({ "unchecked" }) @Override public void checkIntegrity(ContinousHashProvider hashProvider, V defaultValue, int depth) { // check for orphan nodes @@ -390,7 +396,9 @@ public class MutableNode extends Node { // check the place of data for (int i = 0; i < FACTOR; i++) { if (this.content[2 * i] != null) { + @SuppressWarnings("unchecked") K key = (K) this.content[2 * i]; + @SuppressWarnings("unchecked") V value = (V) this.content[2 * i + 1]; if (value == defaultValue) { @@ -408,7 +416,8 @@ public class MutableNode extends Node { // check subnodes for (int i = 0; i < FACTOR; i++) { if (this.content[2 * i + 1] != null && this.content[2 * i] == null) { - Node subNode = (Node) this.content[2 * i + 1]; + @SuppressWarnings("unchecked") + var subNode = (Node) this.content[2 * i + 1]; subNode.checkIntegrity(hashProvider, defaultValue, depth + 1); } } @@ -436,12 +445,10 @@ public class MutableNode extends Node { return true; if (obj == null) return false; - if (obj instanceof MutableNode) { - MutableNode other = (MutableNode) obj; - return Arrays.deepEquals(this.content, other.content); - } else if (obj instanceof ImmutableNode) { - ImmutableNode other = (ImmutableNode) obj; - return ImmutableNode.compareImmutableMutable(other, this); + if (obj instanceof MutableNode mutableObj) { + return Arrays.deepEquals(this.content, mutableObj.content); + } else if (obj instanceof ImmutableNode immutableObj) { + return ImmutableNode.compareImmutableMutable(immutableObj, this); } else { return false; } diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/model/ModelStoreImpl.java b/store/src/main/java/org/eclipse/viatra/solver/data/model/ModelStoreImpl.java index f5e3b141..a97fb27a 100644 --- a/store/src/main/java/org/eclipse/viatra/solver/data/model/ModelStoreImpl.java +++ b/store/src/main/java/org/eclipse/viatra/solver/data/model/ModelStoreImpl.java @@ -1,7 +1,6 @@ package org.eclipse.viatra.solver.data.model; import java.util.HashMap; -import java.util.HashSet; import java.util.LinkedList; import java.util.List; import java.util.Map; @@ -34,8 +33,7 @@ public class ModelStoreImpl implements ModelStore { Map>> symbolRepresentationsPerHashPerArity = new HashMap<>(); for (DataRepresentation dataRepresentation : dataRepresentations) { - if (dataRepresentation instanceof Relation) { - Relation symbolRepresentation = (Relation) dataRepresentation; + if (dataRepresentation instanceof Relation symbolRepresentation) { addOrCreate(symbolRepresentationsPerHashPerArity, new SimilarRelationEquivalenceClass(symbolRepresentation), symbolRepresentation); } else if (dataRepresentation instanceof AuxilaryData) { @@ -58,10 +56,10 @@ public class ModelStoreImpl implements ModelStore { List> symbolGroup) { final ContinousHashProvider hashProvider = symbolGroup.get(0).getHashProvider(); final Object defaultValue = symbolGroup.get(0).getDefaultValue(); - + List> maps = VersionedMapStoreImpl .createSharedVersionedMapStores(symbolGroup.size(), hashProvider, defaultValue); - + for (int i = 0; i < symbolGroup.size(); i++) { result.put(symbolGroup.get(i), maps.get(i)); } @@ -100,24 +98,20 @@ public class ModelStoreImpl implements ModelStore { } return new ModelImpl(this, maps); } - + @Override - @SuppressWarnings("squid:S1751") public synchronized Set getStates() { - // if not empty, return first - for(VersionedMapStore store : stores.values()) { - return new HashSet<>(store.getStates()); + var iterator = stores.values().iterator(); + if (iterator.hasNext()) { + return Set.copyOf(iterator.next().getStates()); } - // if empty - Set result = new HashSet<>(); - result.add(0l); - return result; + return Set.of(0l); } @Override public synchronized ModelDiffCursor getDiffCursor(long from, long to) { - Map,DiffCursor> diffcursors = new HashMap<>(); - for(Entry, VersionedMapStore> entry : stores.entrySet()) { + Map, DiffCursor> diffcursors = new HashMap<>(); + for (Entry, VersionedMapStore> entry : stores.entrySet()) { DataRepresentation representation = entry.getKey(); DiffCursor diffCursor = entry.getValue().getDiffCursor(from, to); diffcursors.put(representation, diffCursor); diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/model/representation/TruthValue.java b/store/src/main/java/org/eclipse/viatra/solver/data/model/representation/TruthValue.java index aeccde9e..049c7eac 100644 --- a/store/src/main/java/org/eclipse/viatra/solver/data/model/representation/TruthValue.java +++ b/store/src/main/java/org/eclipse/viatra/solver/data/model/representation/TruthValue.java @@ -1,42 +1,49 @@ package org.eclipse.viatra.solver.data.model.representation; -public class TruthValue { - public static final TruthValue True = new TruthValue("true"); - public static final TruthValue False = new TruthValue("false"); - public static final TruthValue Unknown = new TruthValue("unknown"); - public static final TruthValue Error = new TruthValue("error"); - +public enum TruthValue { + TRUE("true"), + + FALSE("false"), + + UNKNOWN("unknown"), + + ERROR("error"); + private final String name; - protected TruthValue(String name) { + + private TruthValue(String name) { this.name = name; } - + public String getName() { return name; } - + public static TruthValue toTruthValue(boolean value) { - if(value) return True; - else return False; + return value ? TRUE : FALSE; } + public boolean isConsistent() { - return this != Error; + return this != ERROR; } + public boolean isComplete() { - return this != Unknown; + return this != UNKNOWN; } + public boolean must() { - return this == True || this == Error; + return this == TRUE || this == ERROR; } + public boolean may() { - return this == True || this == Unknown; + return this == TRUE || this == UNKNOWN; } - + public TruthValue not() { - if(this == True) { - return False; - } else if(this == False) { - return True; + if (this == TRUE) { + return FALSE; + } else if (this == FALSE) { + return TRUE; } else { return this; } diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/query/building/PredicateAtom.java b/store/src/main/java/org/eclipse/viatra/solver/data/query/building/PredicateAtom.java index 3e5ef88e..439298d0 100644 --- a/store/src/main/java/org/eclipse/viatra/solver/data/query/building/PredicateAtom.java +++ b/store/src/main/java/org/eclipse/viatra/solver/data/query/building/PredicateAtom.java @@ -4,52 +4,61 @@ import java.util.List; import java.util.Map; import java.util.Set; -public class PredicateAtom implements DNFAtom{ +public class PredicateAtom implements DNFAtom { private DNFPredicate referred; private List substitution; private boolean positive; private boolean transitive; - + public PredicateAtom(boolean positive, boolean transitive, DNFPredicate referred, List substitution) { this.positive = positive; this.referred = referred; this.substitution = substitution; this.transitive = transitive; } + public DNFPredicate getReferred() { return referred; } + public void setReferred(DNFPredicate referred) { this.referred = referred; } + public List getSubstitution() { return substitution; } + public void setSubstitution(List substitution) { this.substitution = substitution; } + public boolean isPositive() { return positive; } + public void setPositive(boolean positive) { this.positive = positive; } + public boolean isTransitive() { return transitive; } + public void setTransitive(boolean transitive) { this.transitive = transitive; } + @Override public void unifyVariables(Map variables) { - for(int i = 0; i variables) { DNFAtom.addToCollection(variables, substitution); diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/query/building/RelationAtom.java b/store/src/main/java/org/eclipse/viatra/solver/data/query/building/RelationAtom.java index f7152bba..adf2f8c7 100644 --- a/store/src/main/java/org/eclipse/viatra/solver/data/query/building/RelationAtom.java +++ b/store/src/main/java/org/eclipse/viatra/solver/data/query/building/RelationAtom.java @@ -7,37 +7,41 @@ import java.util.Set; import org.eclipse.viatra.solver.data.query.view.FilteredRelationView; import org.eclipse.viatra.solver.data.query.view.RelationView; -public class RelationAtom implements DNFAtom{ +public class RelationAtom implements DNFAtom { RelationView view; List substitution; - + public RelationAtom(RelationView view, List substitution) { this.view = view; this.substitution = substitution; } + public RelationView getView() { return view; } + public void setView(FilteredRelationView view) { this.view = view; } + public List getSubstitution() { return substitution; } + public void setSubstitution(List substitution) { this.substitution = substitution; } - + @Override public void unifyVariables(Map variables) { - for(int i = 0; i variables) { DNFAtom.addToCollection(variables, substitution); diff --git a/store/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationalRuntimeContext.java b/store/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationalRuntimeContext.java index 7d1682b2..fa6a59ab 100644 --- a/store/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationalRuntimeContext.java +++ b/store/src/main/java/org/eclipse/viatra/solver/data/query/internal/RelationalRuntimeContext.java @@ -37,7 +37,6 @@ public class RelationalRuntimeContext implements IQueryRuntimeContext { return metaContext; } - // /** * TODO: check {@link NavigationHelperImpl#coalesceTraversals(Callable)} */ @@ -57,8 +56,7 @@ public class RelationalRuntimeContext implements IQueryRuntimeContext { @Override public boolean isIndexed(IInputKey key, IndexingService service) { - if(key instanceof RelationView) { - RelationView relationalKey = (RelationView) key; + if(key instanceof RelationView relationalKey) { return this.relationUpdateListener.containsRelationalView(relationalKey); } else { return false; -- cgit v1.2.3-54-g00ecf