Search in sources :

Example 11 with VertexWrapper

use of org.apache.atlas.catalog.VertexWrapper in project incubator-atlas by apache.

the class GenericRelation method traverse.

@Override
public Collection<RelationSet> traverse(VertexWrapper vWrapper) {
    Collection<RelationSet> relations = new ArrayList<>();
    Vertex v = vWrapper.getVertex();
    String vertexType = v.getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY);
    Map<String, Collection<VertexWrapper>> vertexMap = new HashMap<>();
    for (Edge e : v.getEdges(Direction.OUT)) {
        String edgeLabel = e.getLabel();
        String edgePrefix = String.format("%s%s.", Constants.INTERNAL_PROPERTY_KEY_PREFIX, vertexType);
        if (edgeLabel.startsWith(edgePrefix)) {
            Vertex adjacentVertex = e.getVertex(Direction.IN);
            if (!isDeleted(adjacentVertex)) {
                VertexWrapper relationVertex = new VertexWrapper(adjacentVertex, resourceDefinition);
                String relationName = edgeLabel.substring(edgePrefix.length());
                Collection<VertexWrapper> vertices = vertexMap.get(relationName);
                if (vertices == null) {
                    vertices = new ArrayList<>();
                    vertexMap.put(relationName, vertices);
                }
                vertices.add(relationVertex);
            }
        }
    }
    for (Map.Entry<String, Collection<VertexWrapper>> entry : vertexMap.entrySet()) {
        relations.add(new RelationSet(entry.getKey(), entry.getValue()));
    }
    return relations;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) VertexWrapper(org.apache.atlas.catalog.VertexWrapper) Collection(java.util.Collection) Edge(com.tinkerpop.blueprints.Edge) Map(java.util.Map) HashMap(java.util.HashMap)

Example 12 with VertexWrapper

use of org.apache.atlas.catalog.VertexWrapper in project incubator-atlas by apache.

the class BaseQuery method execute.

@Override
public Collection<Map<String, Object>> execute(Map<String, Object> updateProperties) throws ResourceNotFoundException {
    Collection<Map<String, Object>> resultMaps = new ArrayList<>();
    try {
        for (Vertex vertex : executeQuery()) {
            VertexWrapper vWrapper = wrapVertex(vertex);
            for (Map.Entry<String, Object> property : updateProperties.entrySet()) {
                vWrapper.setProperty(property.getKey(), property.getValue());
                vWrapper.setProperty(Constants.MODIFICATION_TIMESTAMP_PROPERTY_KEY, System.currentTimeMillis());
            }
            resultMaps.add(processPropertyMap(vWrapper));
        }
        getGraph().commit();
    } catch (Throwable e) {
        getGraph().rollback();
        throw e;
    }
    return resultMaps;
}
Also used : AtlasVertex(org.apache.atlas.repository.graphdb.AtlasVertex) Vertex(com.tinkerpop.blueprints.Vertex) VertexWrapper(org.apache.atlas.catalog.VertexWrapper) ArrayList(java.util.ArrayList) Map(java.util.Map)

Example 13 with VertexWrapper

use of org.apache.atlas.catalog.VertexWrapper in project incubator-atlas by apache.

the class EntityTagResourceDefinition method getTermProjection.

private Projection getTermProjection() {
    return new Projection("term", Projection.Cardinality.SINGLE, new TransformFunctionPipe<>(new PipeFunction<VertexWrapper, Collection<ProjectionResult>>() {

        @Override
        public Collection<ProjectionResult> compute(VertexWrapper start) {
            Map<String, Object> map = new TreeMap<>(new ResourceComparator());
            StringBuilder sb = new StringBuilder();
            sb.append("v1/taxonomies/");
            String fullyQualifiedName = start.getVertex().getProperty(Constants.ENTITY_TYPE_PROPERTY_KEY);
            String[] paths = fullyQualifiedName.split("\\.");
            // first path segment is the taxonomy
            sb.append(paths[0]);
            for (int i = 1; i < paths.length; ++i) {
                String path = paths[i];
                if (path != null && !path.isEmpty()) {
                    sb.append("/terms/");
                    sb.append(path);
                }
            }
            map.put("href", sb.toString());
            return Collections.singleton(new ProjectionResult("term", start, Collections.singleton(map)));
        }
    }));
}
Also used : ResourceComparator(org.apache.atlas.catalog.ResourceComparator) Projection(org.apache.atlas.catalog.projection.Projection) ProjectionResult(org.apache.atlas.catalog.projection.ProjectionResult) VertexWrapper(org.apache.atlas.catalog.VertexWrapper) PipeFunction(com.tinkerpop.pipes.PipeFunction)

Example 14 with VertexWrapper

use of org.apache.atlas.catalog.VertexWrapper in project incubator-atlas by apache.

the class TaxonomyResourceDefinition method getTermsProjection.

private Projection getTermsProjection() {
    final String termsProjectionName = "terms";
    return new Projection(termsProjectionName, Projection.Cardinality.SINGLE, new TransformFunctionPipe<>(new PipeFunction<VertexWrapper, Collection<ProjectionResult>>() {

        private String baseHref = "v1/taxonomies/";

        @Override
        public Collection<ProjectionResult> compute(VertexWrapper v) {
            Map<String, Object> map = new HashMap<>();
            map.put("href", baseHref + v.getProperty("name") + "/terms");
            return Collections.singleton(new ProjectionResult(termsProjectionName, v, Collections.singleton(map)));
        }
    }));
}
Also used : ProjectionResult(org.apache.atlas.catalog.projection.ProjectionResult) VertexWrapper(org.apache.atlas.catalog.VertexWrapper) Projection(org.apache.atlas.catalog.projection.Projection) PipeFunction(com.tinkerpop.pipes.PipeFunction)

Example 15 with VertexWrapper

use of org.apache.atlas.catalog.VertexWrapper in project incubator-atlas by apache.

the class AtlasEntityQueryTest method testExecute_Collection.

//todo: add tests for instance query and getInitialPipeline()
@Test
public void testExecute_Collection() throws Exception {
    AtlasGraph graph = createStrictMock(AtlasGraph.class);
    QueryExpression expression = createStrictMock(QueryExpression.class);
    ResourceDefinition resourceDefinition = createStrictMock(ResourceDefinition.class);
    Request request = createStrictMock(Request.class);
    GremlinPipeline initialPipeline = createStrictMock(GremlinPipeline.class);
    Pipe queryPipe = createStrictMock(Pipe.class);
    Pipe expressionPipe = createStrictMock(Pipe.class);
    Pipe notDeletedPipe = createStrictMock(Pipe.class);
    GremlinPipeline rootPipeline = createStrictMock(GremlinPipeline.class);
    GremlinPipeline queryPipeline = createStrictMock(GremlinPipeline.class);
    GremlinPipeline expressionPipeline = createStrictMock(GremlinPipeline.class);
    GremlinPipeline notDeletedPipeline = createStrictMock(GremlinPipeline.class);
    Vertex vertex1 = createStrictMock(Vertex.class);
    VertexWrapper vertex1Wrapper = createStrictMock(VertexWrapper.class);
    List<Vertex> results = new ArrayList<>();
    results.add(vertex1);
    Map<String, Object> vertex1PropertyMap = new HashMap<>();
    vertex1PropertyMap.put("prop1", "prop1.value1");
    vertex1PropertyMap.put("prop2", "prop2.value1");
    Map<String, Object> filteredVertex1PropertyMap = new HashMap<>();
    filteredVertex1PropertyMap.put("prop1", "prop1.value1");
    // mock expectations
    expect(initialPipeline.add(queryPipe)).andReturn(queryPipeline);
    expect(initialPipeline.add(notDeletedPipe)).andReturn(notDeletedPipeline);
    expect(initialPipeline.as("root")).andReturn(rootPipeline);
    expect(expression.asPipe()).andReturn(expressionPipe);
    expect(rootPipeline.add(expressionPipe)).andReturn(expressionPipeline);
    expect(expressionPipeline.back("root")).andReturn(rootPipeline);
    expect(rootPipeline.toList()).andReturn(results);
    graph.commit();
    expect(vertex1Wrapper.getPropertyMap()).andReturn(vertex1PropertyMap);
    expect(resourceDefinition.filterProperties(request, vertex1PropertyMap)).andReturn(filteredVertex1PropertyMap);
    expect(resourceDefinition.resolveHref(filteredVertex1PropertyMap)).andReturn("/foo/bar");
    expect(request.getCardinality()).andReturn(Request.Cardinality.COLLECTION);
    replay(graph, expression, resourceDefinition, request, initialPipeline, queryPipe, expressionPipe, notDeletedPipe, rootPipeline, queryPipeline, expressionPipeline, notDeletedPipeline, vertex1, vertex1Wrapper);
    // end mock expectations
    AtlasEntityQuery query = new TestAtlasEntityQuery(expression, resourceDefinition, request, initialPipeline, queryPipe, notDeletedPipe, graph, vertex1Wrapper);
    // invoke method being tested
    Collection<Map<String, Object>> queryResults = query.execute();
    assertEquals(queryResults.size(), 1);
    Map<String, Object> queryResultMap = queryResults.iterator().next();
    assertEquals(queryResultMap.size(), 2);
    assertEquals(queryResultMap.get("prop1"), "prop1.value1");
    assertEquals(queryResultMap.get("href"), "/foo/bar");
    verify(graph, expression, resourceDefinition, request, initialPipeline, queryPipe, expressionPipe, notDeletedPipe, rootPipeline, queryPipeline, expressionPipeline, notDeletedPipeline, vertex1, vertex1Wrapper);
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) GremlinPipeline(com.tinkerpop.gremlin.java.GremlinPipeline) ResourceDefinition(org.apache.atlas.catalog.definition.ResourceDefinition) Request(org.apache.atlas.catalog.Request) Pipe(com.tinkerpop.pipes.Pipe) VertexWrapper(org.apache.atlas.catalog.VertexWrapper) AtlasGraph(org.apache.atlas.repository.graphdb.AtlasGraph) Test(org.testng.annotations.Test)

Aggregations

VertexWrapper (org.apache.atlas.catalog.VertexWrapper)15 Vertex (com.tinkerpop.blueprints.Vertex)6 Test (org.testng.annotations.Test)6 PipeFunction (com.tinkerpop.pipes.PipeFunction)5 Edge (com.tinkerpop.blueprints.Edge)4 ArrayList (java.util.ArrayList)4 Projection (org.apache.atlas.catalog.projection.Projection)4 ProjectionResult (org.apache.atlas.catalog.projection.ProjectionResult)4 GremlinPipeline (com.tinkerpop.gremlin.java.GremlinPipeline)3 ResourceComparator (org.apache.atlas.catalog.ResourceComparator)3 TermVertexWrapper (org.apache.atlas.catalog.TermVertexWrapper)3 Pipe (com.tinkerpop.pipes.Pipe)2 Map (java.util.Map)2 Request (org.apache.atlas.catalog.Request)2 TermPath (org.apache.atlas.catalog.TermPath)2 ResourceDefinition (org.apache.atlas.catalog.definition.ResourceDefinition)2 AtlasGraph (org.apache.atlas.repository.graphdb.AtlasGraph)2 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 AtlasVertex (org.apache.atlas.repository.graphdb.AtlasVertex)1