Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

G2-1654 Trie Validation #383

Merged
merged 1 commit into from
Oct 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 87 additions & 1 deletion ax-adt/src/main/java/com/g2forge/alexandria/adt/trie/Node.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,73 @@
package com.g2forge.alexandria.adt.trie;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.g2forge.alexandria.java.validate.CompositeValidation;
import com.g2forge.alexandria.java.validate.IValidatable;
import com.g2forge.alexandria.java.validate.IValidation;
import com.g2forge.alexandria.path.path.IPath;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

@Getter
@AllArgsConstructor
@ToString
@EqualsAndHashCode
public class Node<KT, V> {
public class Node<KT, V> implements IValidatable {
@Data
@Builder(toBuilder = true)
@RequiredArgsConstructor
public static class ChildLabelValidation<KT> implements IValidation {
protected final KT key;

protected final IPath<KT> label;

@Override
public boolean isValid() {
return getLabel() != null && getKey().equals(getLabel().getFirst());
}
}

@Data
@Builder(toBuilder = true)
@RequiredArgsConstructor
public static class EmptyValueValidation<V> implements IValidation {
protected final boolean isTerminal;

protected final V value;

@Override
public boolean isValid() {
return isTerminal() || getValue() == null;
}
}

@Data
@Builder(toBuilder = true)
@RequiredArgsConstructor
public static class NonCircularValidation<KT> implements IValidation {
protected final Map<? extends Node<KT, ?>, ?> ancestors;

protected final Node<KT, ?> node;

@Override
public boolean isValid() {
return !getAncestors().containsKey(getNode());
}
}

protected final IPath<KT> label;

protected final Map<KT, Node<KT, V>> children;
Expand All @@ -30,4 +83,37 @@ public Node(IPath<KT> label) {
public Node(IPath<KT> label, V value) {
this(label, new HashMap<>(), true, value);
}

@Override
public IValidation validate() {
final Object value = new Object();
final IdentityHashMap<Node<KT, V>, Object> ancestors = new IdentityHashMap<>();
final CompositeValidation.CompositeValidationBuilder retVal = CompositeValidation.builder();

final LinkedList<Node<KT, V>> queue = new LinkedList<Node<KT, V>>();
queue.add(this);
while (!queue.isEmpty()) {
final Node<KT, V> current = queue.removeFirst();
// See if we're done with all the descendants of the current node
if (ancestors.remove(current) != null) continue;
ancestors.put(current, value);

retVal.validation(new EmptyValueValidation<>(isTerminal(), getValue()));

final List<Node<KT, V>> children = new ArrayList<>();
for (Map.Entry<KT, Node<KT, V>> entry : current.getChildren().entrySet()) {
retVal.validation(new ChildLabelValidation<>(entry.getKey(), entry.getValue().getLabel()));
final NonCircularValidation<KT> nonCircularValidation = new NonCircularValidation<>(new IdentityHashMap<>(ancestors), entry.getValue());
retVal.validation(nonCircularValidation);
if (nonCircularValidation.isValid()) children.add(entry.getValue());
}

// Queue the children, but queue this node first to mark the end of the children
Collections.reverse(children);
queue.addFirst(current);
children.stream().forEach(queue::addFirst);
}

return retVal.build();
}
}
33 changes: 33 additions & 0 deletions ax-adt/src/test/java/com/g2forge/alexandria/adt/trie/TestNode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.g2forge.alexandria.adt.trie;

import java.util.HashMap;

import org.junit.Test;

import com.g2forge.alexandria.java.validate.IValidation;
import com.g2forge.alexandria.path.path.Path;
import com.g2forge.alexandria.test.HAssert;

public class TestNode {
@Test
public void terminalValue() {
final IValidation validation = new Node<String, Object>(null, new HashMap<>(), false, new Object()).validate();
HAssert.assertFalse(validation.isValid());
}

@Test
public void childLabel() {
final Node<String, Object> root = new Node<String, Object>(null);
root.getChildren().put("a", new Node<>(new Path<>("b")));
final IValidation validation = root.validate();
HAssert.assertFalse(validation.isValid());
}

@Test
public void circular() {
final Node<String, Object> root = new Node<String, Object>(null);
root.getChildren().put("a", root);
final IValidation validation = root.validate();
HAssert.assertFalse(validation.isValid());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@
import com.g2forge.alexandria.test.HAssert;

public class TestTrie3Node {
protected static final ITrie<Character, String> trie = new Trie<>(new NodeBuilder("t", null).children(c -> {
protected static final Trie<Character, String> trie = new Trie<>(new NodeBuilder("t", null).children(c -> {
c.child("est", "test");
c.child("oast", "toast");
}).build());

@Test
public void validate() {
HAssert.assertTrue(trie.root.validate().isValid());
}

@Test
public void roast() {
final IOptional<String> result = trie.get(NodeBuilder.toLabel("roast"));
Expand Down Expand Up @@ -48,7 +53,7 @@ public void toasting() {
final IOptional<String> result = trie.get(NodeBuilder.toLabel("toasting"));
HAssert.assertFalse(result.isNotEmpty());
}

@Test
public void toasti() {
final IOptional<String> result = trie.get(NodeBuilder.toLabel("toasti"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import com.g2forge.alexandria.test.HAssert;

public class TestTrie5Node {
protected static final ITrie<Character, String> trie = new Trie<>(new NodeBuilder("t", null).children(c0 -> {
protected static final Trie<Character, String> trie = new Trie<>(new NodeBuilder("t", null).children(c0 -> {
c0.child("est", "test");
c0.child("oast", "toast").children(c1 -> {
c1.child("er", "toaster");
Expand Down Expand Up @@ -65,4 +65,9 @@ public void trip() {
final IOptional<String> result = trie.get(NodeBuilder.toLabel("trip"));
HAssert.assertFalse(result.isNotEmpty());
}

@Test
public void validate() {
HAssert.assertTrue(trie.root.validate().isValid());
}
}
Loading