How to bind a class to a Vertex and also have a property bound to an Edge property using Tinkerpop Frames?

71 views Asked by At

I want to have a Java class to bind to this relationship:

Vertex - Relationship - Vertex (a:Clause)-[r:HasClause]-(b:Clause)

The problem is that the edge of class "HasClause" should have a property called "alias" on the same class - I don't know how I should annotate the class to do that automatically:

@JsonDeserialize(as = Clause.class)
public interface IClause extends VertexFrame {

    @Property("nodeClass")
    public String getNodeClass();

    @Property("nodeClass")
    public void setNodeClass(String str);

    /* that would be a property on the Vertex not on the Edge 
    @Property("alias")
    public void setAlias(String id);

    @Property("alias")
    public String getAlias();
    */

    @Adjacency(label = "HasClause", direction = Direction.OUT)
    public Iterable<IClause> getClauses();

    @Adjacency(label = "HasClause", direction = Direction.OUT)
    public void setClauses(Iterable<IClause> clauses);
}

Thanks

1

There are 1 answers

2
Nick Grealy On

I don't know if there's a way you can do this using the @Adjacency annotation (I can't see any way).

One way you could do this, is by using a @JavaHandlerClass. This basically allows you to customise the implementation of your Frame's methods. In the following example, we'll join two Vertex's, and add a custom property 'alias' to the Edge.

Just to make things easier, I'll use the same classes from your other question - Why simple set and then get on Dynamic Proxy does not persist? (using TinkerPop Frames JavaHandler)

IVert

@JavaHandlerClass(Vert.class)
public interface IVert extends VertexFrame {

    @JavaHandler
    public void setTestVar(IVert vert);

}

Vert

abstract class Vert implements JavaHandlerContext<Vertex>, IVert {

    public void setTestVar(IVert testVar){
        Edge edge = asVertex().addEdge('foobar', testVar.asVertex())
        edge.setProperty('alias', 'chickens')
    }

}

Main method (Groovy)

IVert vert = framedGraph.addVertex('myuniqueid', IVert)
IVert vert2 = framedGraph.addVertex('myuniqueid2', IVert)
vert.setTestVar(vert2)
Edge e = g.getVertex('myuniqueid').getEdges(Direction.BOTH, 'foobar').iterator().next()
assert e.getProperty('alias') == 'chickens'