Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
clickout committed Jul 26, 2023
1 parent 1aff4d3 commit 8ac2c46
Show file tree
Hide file tree
Showing 7 changed files with 291 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import ai.stapi.graph.configuration.GraphRepositoryConfiguration;
import ai.stapi.graph.inMemoryGraph.InMemoryGraphRepository;
import ai.stapi.graphoperations.graphLoader.GraphLoader;
import ai.stapi.graphoperations.graphLoader.graphLoaderOGMFactory.GraphLoaderOgmFactory;
import ai.stapi.graphoperations.graphLoader.graphLoaderOgm.GraphLoaderOgmFactory;
import ai.stapi.graphoperations.graphLoader.inmemory.InMemoryAscendingSortResolver;
import ai.stapi.graphoperations.graphLoader.inmemory.InMemoryDescendingSortResolver;
import ai.stapi.graphoperations.graphLoader.inmemory.InMemoryGenericSearchOptionResolver;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,46 @@

import ai.stapi.graph.traversableGraphElements.TraversableGraphElement;
import ai.stapi.graphoperations.graphLanguage.graphDescription.specific.query.GraphElementQueryDescription;
import ai.stapi.graphoperations.objectGraphLanguage.ObjectGraphMapping;
import ai.stapi.graphoperations.objectGraphLanguage.ObjectObjectGraphMapping;
import ai.stapi.identity.UniqueIdentifier;

import java.util.List;

public interface GraphLoader {

<T> GraphLoaderFindAsObjectOutput<T> find(
GraphElementQueryDescription graphElementQueryDescription,
Class<T> objectClass,
GraphLoaderReturnType... returnTypes
);

<T> GraphLoaderGetAsObjectOutput<T> get(
UniqueIdentifier elementId,
GraphElementQueryDescription graphElementQueryDescription,
Class<T> objectClass,
GraphLoaderReturnType... returnTypes
);

List<TraversableGraphElement> findAsTraversable(
GraphElementQueryDescription graphDescription
);

TraversableGraphElement getAsTraversable(
UniqueIdentifier elementId,
GraphElementQueryDescription graphDescription
);

<T> GraphLoaderFindAsObjectOutput<T> find(
GraphElementQueryDescription graphElementQueryDescription,
ObjectObjectGraphMapping objectObjectGraphMapping,
Class<T> objectClass,
GraphLoaderReturnType... returnTypes
);

<T> GraphLoaderGetAsObjectOutput<T> get(
UniqueIdentifier elementId,
GraphElementQueryDescription graphElementQueryDescription,
ObjectObjectGraphMapping objectObjectGraphMapping,
Class<T> objectClass,
GraphLoaderReturnType... returnTypes
);

List<TraversableGraphElement> findAsTraversable(GraphElementQueryDescription graphDescription);

TraversableGraphElement getAsTraversable(UniqueIdentifier elementId,
GraphElementQueryDescription graphDescription);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package ai.stapi.graphoperations.graphLoader.graphLoaderOgm;

import ai.stapi.graphoperations.graphLanguage.graphDescription.GraphDescription;
import ai.stapi.graphoperations.graphLanguage.graphDescription.specific.positive.*;
import ai.stapi.graphoperations.graphLoader.graphLoaderOgm.exception.GraphLoaderOGMIsInvalid;
import ai.stapi.graphoperations.objectGraphLanguage.*;

public class GraphLoaderOGMValidator {

private GraphLoaderOGMValidator() {
}

public static void validate(ObjectGraphMapping objectGraphMapping) {
if (objectGraphMapping instanceof LeafObjectGraphMapping leaf) {
GraphLoaderOGMValidator.validateLeaf(leaf);
return;
}
if (objectGraphMapping instanceof ListObjectGraphMapping list) {
GraphLoaderOGMValidator.validateList(list);
return;
}
if (objectGraphMapping instanceof ObjectObjectGraphMapping object) {
GraphLoaderOGMValidator.validateObject(object);
return;
}
if (objectGraphMapping instanceof ReferenceObjectGraphMapping reference) {
GraphLoaderOGMValidator.validateReference(reference);
return;
}
if (objectGraphMapping instanceof InterfaceObjectGraphMapping inter) {
GraphLoaderOGMValidator.validateInterface(inter);
return;
}
throw GraphLoaderOGMIsInvalid.becauseUnexpectedOGMEncountered(objectGraphMapping);
}

private static void validateLeaf(LeafObjectGraphMapping leaf) {
var graphDescription = leaf.getGraphDescription();
if (graphDescription instanceof UuidIdentityDescription uuid) {
GraphLoaderOGMValidator.validateUuid(uuid);
return;
}
if (graphDescription instanceof AbstractAttributeDescription attribute) {
GraphLoaderOGMValidator.validateLeafAttribute(attribute);
return;
}
throw GraphLoaderOGMIsInvalid.becauseLeafOGMHasUnexpectedGraphDescription(leaf);
}

private static void validateList(ListObjectGraphMapping list) {
var graphDescription = list.getGraphDescription();
var childOGM = list.getChildObjectGraphMapping();
if (graphDescription instanceof AbstractAttributeDescription attribute) {
GraphLoaderOGMValidator.validateListAttribute(attribute);
if (childOGM instanceof LeafObjectGraphMapping leaf) {
var childDescription = leaf.getGraphDescription();
if (childDescription instanceof NullGraphDescription) {
return;
}
if (childDescription instanceof AbstractAttributeValueDescription) {
return;
}
throw GraphLoaderOGMIsInvalid.becauseListChildLeafOgmHadUnexpectedGraphDescription(childDescription);
}
throw GraphLoaderOGMIsInvalid.becauseListOGMContainedAttributeDescriptionButNotChildLeafOGM(childOGM);
}
if (graphDescription instanceof NullGraphDescription) {
if (
childOGM instanceof ReferenceObjectGraphMapping
|| childOGM instanceof InterfaceObjectGraphMapping
|| childOGM instanceof ObjectObjectGraphMapping
) {
GraphLoaderOGMValidator.validate(childOGM);
return;
}
}
throw GraphLoaderOGMIsInvalid.becauseListOGMHadUnexpectedGraphDescription(graphDescription);
}

private static void validateObject(ObjectObjectGraphMapping object) {

}

private static void validateReference(ReferenceObjectGraphMapping reference) {

}

private static void validateInterface(InterfaceObjectGraphMapping inter) {

}

private static void validateUuid(UuidIdentityDescription uuidIdentityDescription) {

}

private static void validateLeafAttribute(AbstractAttributeDescription attribute) {

}

private static void validateListAttribute(AbstractAttributeDescription attribute) {

}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ai.stapi.graphoperations.graphLoader.graphLoaderOGMFactory;
package ai.stapi.graphoperations.graphLoader.graphLoaderOgm;

import ai.stapi.graphoperations.graphLanguage.graphDescription.GraphDescription;
import ai.stapi.graphoperations.graphLanguage.graphDescription.graphDescriptionBuilder.GraphDescriptionBuilder;
Expand All @@ -21,8 +21,7 @@ public class GraphLoaderOgmFactory {

public ObjectGraphMapping create(GraphDescription graphDescription) {
var ogmBuilder = new ObjectGraphMappingBuilder();
ogmBuilder.setGraphDescription(
new GraphDescriptionBuilder().copyWithNewChildren(graphDescription));
ogmBuilder.setGraphDescription(new GraphDescriptionBuilder().copyWithNewChildren(graphDescription));
graphDescription.getChildGraphDescriptions().forEach(childDescription ->
this.resolveChildGraphDescription(childDescription, ogmBuilder)
);
Expand All @@ -35,8 +34,7 @@ private void resolveChildGraphDescription(
) {
if (graphDescription instanceof AbstractNodeDescription) {
var nodeParam = (NodeDescriptionParameters) graphDescription.getParameters();
var childOgmBuilder = ogmBuilder.addField(nodeParam.getNodeType())
.addObjectAsObjectFieldMapping();
var childOgmBuilder = ogmBuilder.addField(nodeParam.getNodeType()).addObjectAsObjectFieldMapping();
if (graphDescription instanceof NodeQueryGraphDescription nodeQueryGraphDescription) {
childOgmBuilder.setGraphDescription(
new NodeQueryGraphDescription(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package ai.stapi.graphoperations.graphLoader.graphLoaderOgm.exception;

import ai.stapi.graphoperations.graphLanguage.graphDescription.GraphDescription;
import ai.stapi.graphoperations.objectGraphLanguage.LeafObjectGraphMapping;
import ai.stapi.graphoperations.objectGraphLanguage.ObjectGraphMapping;
import ai.stapi.utils.Stringifier;

public class GraphLoaderOGMIsInvalid extends RuntimeException {

private GraphLoaderOGMIsInvalid(String becauseMessage) {
super("OGM to be used in Graph Loader is invalid, because " + becauseMessage);
}

public static GraphLoaderOGMIsInvalid becauseLeafOGMHasUnexpectedGraphDescription(
LeafObjectGraphMapping leafObjectGraphMapping
) {
return new GraphLoaderOGMIsInvalid(
String.format(
"leaf OGM has unexpected graph description. " +
"It should contain either UUID or Attribute description.%nLeaf OGM: '%s'",
Stringifier.convertToString(leafObjectGraphMapping)
)
);
}

public static GraphLoaderOGMIsInvalid becauseUnexpectedOGMEncountered(
ObjectGraphMapping invalidOGM
) {
return new GraphLoaderOGMIsInvalid(
String.format(
"unexpected OGM encountered. There should only be Leaf, List, Object, Reference, Interface OGMs." +
"%nInvalid OGM: '%s'",
Stringifier.convertToString(invalidOGM)
)
);
}

public static GraphLoaderOGMIsInvalid becauseListOGMContainedAttributeDescriptionButNotChildLeafOGM(
ObjectGraphMapping invalidChildOGM
) {
return new GraphLoaderOGMIsInvalid(
String.format(
"list OGM contained attribute description, but child leaf OGM wasnt leaf." +
"%nInvalid child OGM: '%s'",
Stringifier.convertToString(invalidChildOGM)
)
);
}

public static GraphLoaderOGMIsInvalid becauseListChildLeafOgmHadUnexpectedGraphDescription(
GraphDescription childDescription
) {
return new GraphLoaderOGMIsInvalid(
String.format(
"leaf OGM inside list OGM had unexpected graph description. It should either be null or attribute value."
+ "%nInvalid description: '%s'",
Stringifier.convertToString(childDescription)
)
);
}

public static GraphLoaderOGMIsInvalid becauseListOGMHadUnexpectedGraphDescription(GraphDescription graphDescription) {
return new GraphLoaderOGMIsInvalid(
String.format(
"list OGM had unexpected graph description. It should either be null or attribute description."
+ "%nInvalid description: '%s'",
Stringifier.convertToString(graphDescription)
)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import ai.stapi.graphoperations.graphLoader.GraphLoaderReturnType;
import ai.stapi.graphoperations.graphLoader.exceptions.GraphLoaderException;
import ai.stapi.graphoperations.graphLoader.search.SearchQueryParameters;
import ai.stapi.graphoperations.objectGraphLanguage.ObjectObjectGraphMapping;
import ai.stapi.identity.UniqueIdentifier;
import ai.stapi.schema.structureSchema.exception.FieldsNotFoundException;
import ai.stapi.schema.structureSchemaProvider.StructureSchemaFinder;
Expand Down Expand Up @@ -115,6 +116,25 @@ public TraversableGraphElement getAsTraversable(
return graph.getGraph().loadGraphElement(elementId, elementType);
}

@Override
public <T> GraphLoaderFindAsObjectOutput<T> find(
ObjectObjectGraphMapping objectObjectGraphMapping,
Class<T> objectClass,
GraphLoaderReturnType... returnTypes
) {
return null;
}

@Override
public <T> GraphLoaderGetAsObjectOutput<T> get(
UniqueIdentifier elementId,
ObjectObjectGraphMapping objectObjectGraphMapping,
Class<T> objectClass,
GraphLoaderReturnType... returnTypes
) {
return null;
}

@Override
public <T> GraphLoaderFindAsObjectOutput<T> find(
GraphElementQueryDescription graphElementQueryDescription,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package ai.stapi.graphoperations.graphLoader.graphLoaderOgm;

import ai.stapi.graphoperations.fixtures.AttributeTypes;
import ai.stapi.graphoperations.fixtures.ComplexJoinedGraphFixturesProvider;
import ai.stapi.graphoperations.graphLanguage.graphDescription.specific.positive.EdgeDescriptionParameters;
import ai.stapi.graphoperations.graphLanguage.graphDescription.specific.positive.NodeDescription;
import ai.stapi.graphoperations.graphLanguage.graphDescription.specific.positive.NodeDescriptionParameters;
import ai.stapi.graphoperations.graphLanguage.graphDescription.specific.positive.OutgoingEdgeDescription;
import ai.stapi.graphoperations.graphLanguage.graphDescription.specific.query.AttributeQueryDescription;
import ai.stapi.graphoperations.graphLanguage.graphDescription.specific.query.NodeQueryGraphDescription;
import ai.stapi.graphoperations.graphLanguage.graphDescription.specific.query.OutgoingEdgeQueryDescription;
import ai.stapi.graphoperations.graphLoader.search.SearchQueryParameters;
import ai.stapi.graphoperations.graphLoader.search.sortOption.DescendingSortOption;
import ai.stapi.test.integration.IntegrationTestCase;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

class GraphLoaderOgmFactoryTest extends IntegrationTestCase {

@Autowired
private GraphLoaderOgmFactory graphLoaderOgmFactory;

@Test
void itCanMapComplexGraphDescription() {
var sort = new DescendingSortOption(
new OutgoingEdgeDescription(
new EdgeDescriptionParameters(ComplexJoinedGraphFixturesProvider.EDGE_TYPE_1),
new NodeDescription(
new NodeDescriptionParameters(ComplexJoinedGraphFixturesProvider.NODE_TYPE_2),
new OutgoingEdgeDescription(
new EdgeDescriptionParameters(ComplexJoinedGraphFixturesProvider.EDGE_TYPE_2),
new NodeDescription(
new NodeDescriptionParameters(ComplexJoinedGraphFixturesProvider.NODE_TYPE_3),
new AttributeQueryDescription(AttributeTypes.EXAMPLE_QUANTITY)
)
)
)
)
);
var searchQueryParameters = SearchQueryParameters.builder().addSortOption(sort).build();
var graphDescription = new NodeQueryGraphDescription(
new NodeDescriptionParameters(ComplexJoinedGraphFixturesProvider.NODE_TYPE_1),
searchQueryParameters,
new AttributeQueryDescription(AttributeTypes.EXAMPLE_NAME),
new OutgoingEdgeQueryDescription(
new EdgeDescriptionParameters(ComplexJoinedGraphFixturesProvider.EDGE_TYPE_1),
SearchQueryParameters.from(),
new NodeQueryGraphDescription(
new NodeDescriptionParameters(ComplexJoinedGraphFixturesProvider.NODE_TYPE_2),
SearchQueryParameters.from(),
new OutgoingEdgeQueryDescription(
new EdgeDescriptionParameters(ComplexJoinedGraphFixturesProvider.EDGE_TYPE_2),
new SearchQueryParameters(),
new NodeQueryGraphDescription(
new NodeDescriptionParameters(ComplexJoinedGraphFixturesProvider.NODE_TYPE_3),
new AttributeQueryDescription(AttributeTypes.EXAMPLE_NAME),
new AttributeQueryDescription(AttributeTypes.EXAMPLE_QUANTITY),
new AttributeQueryDescription(AttributeTypes.EXAMPLE_STRING_ATTRIBUTE)
)
)
)
)
);
var actual = this.graphLoaderOgmFactory.create(graphDescription);
this.thenObjectApproved(actual);
}
}

0 comments on commit 8ac2c46

Please sign in to comment.