Skip to content

Commit

Permalink
Don't cache idRef provider, add test case for enum
Browse files Browse the repository at this point in the history
Also makes most NodeQuery queries static instances, and
moves some stuff around in tests.
  • Loading branch information
milesziemer committed Jan 23, 2024
1 parent 505285e commit 0d87e6e
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ public final class NeighborProviderIndex implements KnowledgeIndex {
private volatile NeighborProvider reversed;
private volatile NeighborProvider providerWithTraits;
private volatile NeighborProvider reversedWithTraits;
private volatile NeighborProvider providerWithIdRefs;

public NeighborProviderIndex(Model model) {
provider = NeighborProvider.precomputed(model);
Expand Down Expand Up @@ -77,28 +76,6 @@ public NeighborProvider getProviderWithTraitRelationships() {
return result;
}

/**
* Gets the neighbor provider that includes idRef relationships.
*
* @return Returns the provider.
*/
public NeighborProvider getProviderWithIdRefRelationships() {
NeighborProvider result = providerWithIdRefs;

if (result == null) {
Model model = getOrThrowModel();
synchronized (this) {
result = providerWithIdRefs;
if (result == null) {
providerWithIdRefs = result = NeighborProvider.cached(
NeighborProvider.withIdRefRelationships(model, provider));
}
}
}

return result;
}

/**
* Gets a reversed, bottom up neighbor provider.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,33 @@
* and the results are aggregated.
*/
final class NodeQuery {
private static final Query SELF = Stream::of;
private static final Query ANY_MEMBER = (node) -> {
if (node == null || !node.isObjectNode()) {
return Stream.empty();
}
return node.expectObjectNode().getMembers().values().stream();
};
private static final Query ANY_ELEMENT = (node) -> {
if (node == null || !node.isArrayNode()) {
return Stream.empty();
}
return node.expectArrayNode().getElements().stream();
};
private static final Query ANY_MEMBER_NAME = (node) -> {
if (node == null || !node.isObjectNode()) {
return Stream.empty();
}
return node.expectObjectNode().getMembers().keySet().stream();
};

private final List<Query> queries = new ArrayList<>();

NodeQuery() {
}

NodeQuery self() {
queries.add(Stream::of);
queries.add(SELF);
return this;
}

Expand All @@ -39,32 +59,17 @@ NodeQuery member(String name) {
}

NodeQuery anyMember() {
queries.add(node -> {
if (node == null || !node.isObjectNode()) {
return Stream.empty();
}
return node.expectObjectNode().getMembers().values().stream();
});
queries.add(ANY_MEMBER);
return this;
}

NodeQuery anyElement() {
queries.add(node -> {
if (node == null || !node.isArrayNode()) {
return Stream.empty();
}
return node.expectArrayNode().getElements().stream();
});
queries.add(ANY_ELEMENT);
return this;
}

NodeQuery anyMemberName() {
queries.add(node -> {
if (node == null || !node.isObjectNode()) {
return Stream.empty();
}
return node.expectObjectNode().getMembers().keySet().stream();
});
queries.add(ANY_MEMBER_NAME);
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public UnreferencedShapes(Predicate<Shape> keepFilter) {
* @return Returns the unreferenced shapes.
*/
public Set<Shape> compute(Model model) {
Walker shapeWalker = new Walker(NeighborProviderIndex.of(model).getProviderWithIdRefRelationships());
NeighborProvider baseProvider = NeighborProviderIndex.of(model).getProvider();
NeighborProvider providerWithIdRefRelationships = NeighborProvider.withIdRefRelationships(model, baseProvider);
Walker shapeWalker = new Walker(providerWithIdRefRelationships);

// Find all shapes connected to any service shape.
Set<Shape> connected = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,18 +97,14 @@ public void canGetIdRefRelationshipsThroughTraitDefs() {
List<Relationship> relationships = provider.getNeighbors(shape).stream()
.filter(relationship -> relationship.getRelationshipType().equals(RelationshipType.ID_REF))
.collect(Collectors.toList());

assertThat(relationships, containsInAnyOrder(
equalTo(Relationship.create(shape, RelationshipType.ID_REF, ref))));

Shape shape1 = model.expectShape(ShapeId.from("com.foo#refStruct$other"));
Shape ref1 = model.expectShape(ShapeId.from("com.foo#ReferencedInTraitDef"));
List<Relationship> relationships1 = provider.getNeighbors(shape1).stream()
.filter(relationship -> relationship.getRelationshipType().equals(RelationshipType.ID_REF))
.collect(Collectors.toList());

assertThat(relationships1, containsInAnyOrder(
equalTo(Relationship.create(shape1, RelationshipType.ID_REF, ref1))));
assertThat(relationships, containsInAnyOrder(Relationship.create(shape, RelationshipType.ID_REF, ref)));
assertThat(relationships1, containsInAnyOrder(Relationship.create(shape1, RelationshipType.ID_REF, ref1)));
}

@Test
Expand All @@ -125,17 +121,13 @@ public void canGetIdRefRelationshipsThroughMultipleLevelsOfIdRef() {
List<Relationship> relationships = provider.getNeighbors(shape).stream()
.filter(relationship -> relationship.getRelationshipType().equals(RelationshipType.ID_REF))
.collect(Collectors.toList());

assertThat(relationships, containsInAnyOrder(
Relationship.create(shape, RelationshipType.ID_REF, ref)));

Shape shape1 = model.expectShape(ShapeId.from("com.foo#ConnectedThroughReferenced"));
Shape ref1 = model.expectShape(ShapeId.from("com.foo#AnotherReferenced"));
List<Relationship> relationships1 = provider.getNeighbors(shape1).stream()
.filter(relationship -> relationship.getRelationshipType().equals(RelationshipType.ID_REF))
.collect(Collectors.toList());

assertThat(relationships1, containsInAnyOrder(
Relationship.create(shape1, RelationshipType.ID_REF, ref1)));
assertThat(relationships, containsInAnyOrder(Relationship.create(shape, RelationshipType.ID_REF, ref)));
assertThat(relationships1, containsInAnyOrder(Relationship.create(shape1, RelationshipType.ID_REF, ref1)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ public void checksShapeReferencesThroughIdRef() {
.unwrap();

Set<Shape> shapes = new UnreferencedShapes().compute(m);

assertThat(shapes, empty());
}

Expand All @@ -100,6 +101,7 @@ public void doesNotCheckShapeReferencesThroughIdRefOnUnconnectedShapes() {
.unwrap();

Set<ShapeId> ids = new UnreferencedShapes().compute(m).stream().map(Shape::getId).collect(Collectors.toSet());

assertThat(ids, containsInAnyOrder(
ShapeId.from("com.foo#WithTrait"),
ShapeId.from("com.foo#Referenced"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ operation GetFoo {
twelve: Twelve
thirteen: Thirteen
fourteen: Fourteen
fifteen: Fifteen
}
}

Expand Down Expand Up @@ -218,3 +219,19 @@ structure Fourteen {}

string Ref14

// --
@trait
structure withIdRefOnEnum {
refEnum: RefEnum
}

@idRef(failWhenMissing: true)
enum RefEnum {
REF15 = "com.foo#Ref15"
}

@withIdRefOnEnum(refEnum: "com.foo#Ref15")
structure Fifteen {}

structure Ref15 {}

0 comments on commit 0d87e6e

Please sign in to comment.