Search in sources :

Example 1 with Relationship

use of com.buschmais.jqassistant.core.report.api.graph.model.Relationship in project jqa-core-framework by buschmais.

the class SubGraphFactory method convert.

private <I extends Identifiable> I convert(Object value) throws ReportException {
    if (value == null) {
        return null;
    } else if (value instanceof Map) {
        Map<String, Object> virtualObject = (Map) value;
        Object role = virtualObject.get(ROLE);
        if (role != null) {
            Map<String, Object> properties = (Map<String, Object>) virtualObject.get(PROPERTIES);
            switch(role.toString().toLowerCase()) {
                case NODE:
                    Node node = new Node();
                    node.setId(nodeId--);
                    Collection<String> labels = (Collection<String>) virtualObject.get(LABELS);
                    node.getLabels().addAll(labels);
                    node.getProperties().putAll(properties);
                    node.setLabel((String) virtualObject.get(LABEL));
                    return (I) node;
                case RELATIONSHIP:
                    Relationship relationship = new Relationship();
                    relationship.setId(relationshipId--);
                    Node startNode = convert(virtualObject.get(START_NODE));
                    Node endNode = convert(virtualObject.get(END_NODE));
                    String type = (String) virtualObject.get(TYPE);
                    relationship.setType(type);
                    relationship.setStartNode(startNode);
                    relationship.setEndNode(endNode);
                    relationship.getProperties().putAll(properties);
                    relationship.setLabel((String) virtualObject.get(LABEL));
                    if (startNode == null || endNode == null || type == null) {
                        throw new ReportException("The virtual relationship does not contain either start node, end node or type: " + relationship);
                    }
                    return (I) relationship;
                case GRAPH:
                    SubGraph subgraph = new SubGraph();
                    subgraph.setId(subgraphId--);
                    subgraph.setParent((Node) convert(subgraph, virtualObject.get(PARENT)));
                    addSubGraphChildren(subgraph, virtualObject, NODES, subgraph.getNodes());
                    addSubGraphChildren(subgraph, virtualObject, RELATIONSHIPS, subgraph.getRelationships());
                    return (I) subgraph;
            }
        }
    } else if (value instanceof CompositeObject) {
        CompositeObject compositeObject = (CompositeObject) value;
        I identifiable = convert(compositeObject.getDelegate());
        identifiable.setLabel(ReportHelper.getLabel(value));
        return identifiable;
    } else if (value instanceof Neo4jNode) {
        Neo4jNode<Neo4jLabel, ?, ?, ?> neo4jNode = (Neo4jNode<Neo4jLabel, ?, ?, ?>) value;
        Node node = new Node();
        node.setId(neo4jNode.getId());
        for (Neo4jLabel label : neo4jNode.getLabels()) {
            node.getLabels().add(label.getName());
        }
        node.getProperties().putAll(neo4jNode.getProperties());
        return (I) node;
    } else if (value instanceof Neo4jRelationship) {
        Neo4jRelationship<?, ?, ?, ?, ?> neo4jRelationship = (Neo4jRelationship) value;
        Relationship relationship = new Relationship();
        relationship.setId(neo4jRelationship.getId());
        relationship.setType(neo4jRelationship.getType().getName());
        relationship.setStartNode((Node) convert(neo4jRelationship.getStartNode()));
        relationship.setEndNode((Node) convert(neo4jRelationship.getEndNode()));
        relationship.getProperties().putAll(neo4jRelationship.getProperties());
        return (I) relationship;
    }
    throw new ReportException("Element type not supported: " + value);
}
Also used : Neo4jLabel(com.buschmais.xo.neo4j.api.model.Neo4jLabel) Node(com.buschmais.jqassistant.core.report.api.graph.model.Node) Neo4jNode(com.buschmais.xo.neo4j.api.model.Neo4jNode) Neo4jRelationship(com.buschmais.xo.neo4j.api.model.Neo4jRelationship) Neo4jNode(com.buschmais.xo.neo4j.api.model.Neo4jNode) CompositeObject(com.buschmais.xo.api.CompositeObject) Neo4jRelationship(com.buschmais.xo.neo4j.api.model.Neo4jRelationship) Relationship(com.buschmais.jqassistant.core.report.api.graph.model.Relationship) Collection(java.util.Collection) ReportException(com.buschmais.jqassistant.core.report.api.ReportException) CompositeObject(com.buschmais.xo.api.CompositeObject) Map(java.util.Map) SubGraph(com.buschmais.jqassistant.core.report.api.graph.model.SubGraph)

Example 2 with Relationship

use of com.buschmais.jqassistant.core.report.api.graph.model.Relationship in project jqa-core-framework by buschmais.

the class SubGraphFactoryTest method subGraph.

@Test
public void subGraph() throws ReportException {
    Map<String, Object> virtualGraph = // 
    MapBuilder.<String, Object>builder().entry("role", // 
    "graph").entry("label", // 
    "Virtual Graph").entry("parent", // 
    getNeo4jNode(0l)).entry("nodes", // 
    singletonList(getNeo4jNode(1l))).entry("relationships", // 
    singletonList(getNeo4jRelationship(1l, "TEST"))).build();
    MapBuilder<String, Object> rowBuilder = MapBuilder.builder();
    Map<String, Object> row = rowBuilder.entry("graph", virtualGraph).build();
    Result<ExecutableRule> result = Result.builder().rows(singletonList(row)).build();
    SubGraph graph = factory.createSubGraph(result);
    assertThat(graph.getParent()).isNull();
    assertThat(graph.getNodes()).isEmpty();
    assertThat(graph.getRelationships()).isEmpty();
    Map<Long, SubGraph> subGraphs = graph.getSubGraphs();
    assertThat(subGraphs).hasSize(1);
    SubGraph subGraph = subGraphs.get(-2l);
    assertThat(subGraph.getId()).isEqualTo(-2l);
    assertThat(subGraph.getLabel()).isEqualTo("Virtual Graph");
    Node parent = subGraph.getParent();
    assertThat(parent).isNotNull();
    assertThat(parent.getId()).isEqualTo(0l);
    Map<Long, Node> nodes = subGraph.getNodes();
    assertThat(nodes).hasSize(1);
    Node node1 = nodes.get(1l);
    assertThat(node1.getId()).isEqualTo(1l);
    Map<Long, Relationship> relationships = subGraph.getRelationships();
    assertThat(relationships).hasSize(1);
    Relationship relationship1 = relationships.get(1l);
    assertThat(relationship1.getId()).isEqualTo(1l);
}
Also used : ExecutableRule(com.buschmais.jqassistant.core.rule.api.model.ExecutableRule) Node(com.buschmais.jqassistant.core.report.api.graph.model.Node) Neo4jNode(com.buschmais.xo.neo4j.api.model.Neo4jNode) Neo4jRelationship(com.buschmais.xo.neo4j.api.model.Neo4jRelationship) Relationship(com.buschmais.jqassistant.core.report.api.graph.model.Relationship) CompositeObject(com.buschmais.xo.api.CompositeObject) SubGraph(com.buschmais.jqassistant.core.report.api.graph.model.SubGraph) Test(org.junit.jupiter.api.Test)

Example 3 with Relationship

use of com.buschmais.jqassistant.core.report.api.graph.model.Relationship in project jqa-core-framework by buschmais.

the class SubGraphFactoryTest method nodeAndRelationship.

@Test
public void nodeAndRelationship() throws ReportException {
    MapBuilder<String, Object> builder = MapBuilder.builder();
    Map<String, Object> nodeProperties = MapBuilder.<String, Object>builder().entry("nodeKey", "value").build();
    builder.entry("node", getNeo4jNode(1l, nodeProperties, "Test1", "Test2"));
    Map<String, Object> relationshipProperties = MapBuilder.<String, Object>builder().entry("relationshipKey", "value").build();
    builder.entry("relation", getNeo4jRelationship(1l, relationshipProperties, "TEST"));
    Result<ExecutableRule> result = Result.builder().rows(singletonList(builder.build())).build();
    SubGraph graph = factory.createSubGraph(result);
    assertThat(graph.getId()).isEqualTo(-1l);
    assertThat(graph.getParent()).isNull();
    assertThat(graph.getSubGraphs()).isEmpty();
    Map<Long, Node> nodes = graph.getNodes();
    assertThat(nodes).hasSize(1);
    Node node = nodes.get(1l);
    assertThat(node.getId()).isEqualTo(1l);
    assertThat(node.getLabels()).containsExactly("Test1", "Test2");
    assertThat(node.getProperties()).isEqualTo(nodeProperties);
    Map<Long, Relationship> relationships = graph.getRelationships();
    assertThat(relationships).hasSize(1);
    Relationship relationship = relationships.get(1l);
    assertThat(relationship.getId()).isEqualTo(1l);
    assertThat(relationship.getType()).isEqualTo("TEST");
    assertThat(relationship.getProperties()).isEqualTo(relationshipProperties);
}
Also used : ExecutableRule(com.buschmais.jqassistant.core.rule.api.model.ExecutableRule) Node(com.buschmais.jqassistant.core.report.api.graph.model.Node) Neo4jNode(com.buschmais.xo.neo4j.api.model.Neo4jNode) Neo4jRelationship(com.buschmais.xo.neo4j.api.model.Neo4jRelationship) Relationship(com.buschmais.jqassistant.core.report.api.graph.model.Relationship) CompositeObject(com.buschmais.xo.api.CompositeObject) SubGraph(com.buschmais.jqassistant.core.report.api.graph.model.SubGraph) Test(org.junit.jupiter.api.Test)

Example 4 with Relationship

use of com.buschmais.jqassistant.core.report.api.graph.model.Relationship in project jqa-core-framework by buschmais.

the class SubGraphFactoryTest method collectionOfNodesAndCollectionOfRelationships.

@Test
public void collectionOfNodesAndCollectionOfRelationships() throws ReportException {
    MapBuilder<String, Object> builder = MapBuilder.builder();
    builder.entry("nodes", asList(asList(getNeo4jNode(1l), getNeo4jNode(2l))));
    builder.entry("relations", asList(asList(getNeo4jRelationship(1l, "TEST"), getNeo4jRelationship(2l, "TEST"))));
    Result<ExecutableRule> result = Result.builder().rows(singletonList(builder.build())).build();
    SubGraph graph = factory.createSubGraph(result);
    assertThat(graph.getId()).isEqualTo(-1l);
    assertThat(graph.getParent()).isNull();
    assertThat(graph.getSubGraphs()).isEmpty();
    Map<Long, Node> nodes = graph.getNodes();
    assertThat(nodes).hasSize(2);
    Node node1 = nodes.get(1l);
    assertThat(node1.getId()).isEqualTo(1l);
    Node node2 = nodes.get(2l);
    assertThat(node2.getId()).isEqualTo(2l);
    Map<Long, Relationship> relationships = graph.getRelationships();
    assertThat(relationships).hasSize(2);
    Relationship relationship1 = relationships.get(1l);
    assertThat(relationship1.getId()).isEqualTo(1l);
    Relationship relationship2 = relationships.get(2l);
    assertThat(relationship2.getId()).isEqualTo(2l);
}
Also used : ExecutableRule(com.buschmais.jqassistant.core.rule.api.model.ExecutableRule) Node(com.buschmais.jqassistant.core.report.api.graph.model.Node) Neo4jNode(com.buschmais.xo.neo4j.api.model.Neo4jNode) Neo4jRelationship(com.buschmais.xo.neo4j.api.model.Neo4jRelationship) Relationship(com.buschmais.jqassistant.core.report.api.graph.model.Relationship) CompositeObject(com.buschmais.xo.api.CompositeObject) SubGraph(com.buschmais.jqassistant.core.report.api.graph.model.SubGraph) Test(org.junit.jupiter.api.Test)

Example 5 with Relationship

use of com.buschmais.jqassistant.core.report.api.graph.model.Relationship in project jqa-core-framework by buschmais.

the class SubGraphFactoryTest method virtualRelationship.

@Test
public void virtualRelationship() throws ReportException {
    Map<Object, Object> properties = MapBuilder.builder().entry("key", "value").build();
    Map<String, Object> virtualNode = // 
    MapBuilder.<String, Object>builder().entry("role", // 
    "relationship").entry("label", // 
    "Virtual Relationship").entry("type", // 
    "TEST").entry("properties", // 
    properties).entry("startNode", // 
    getNeo4jNode(1l)).entry("endNode", // 
    getNeo4jNode(2l)).build();
    MapBuilder<String, Object> builder = MapBuilder.builder();
    builder.entry("relationships", singletonList(virtualNode));
    Result<ExecutableRule> result = Result.builder().rows(singletonList(builder.build())).build();
    SubGraph graph = factory.createSubGraph(result);
    Map<Long, Relationship> relationships = graph.getRelationships();
    assertThat(relationships.size()).isEqualTo(1);
    Relationship relationship = relationships.get(-1l);
    assertThat(relationship).isNotNull();
    assertThat(relationship.getId()).isEqualTo(-1l);
    assertThat(relationship.getLabel()).isEqualTo("Virtual Relationship");
    assertThat(relationship.getType()).isEqualTo("TEST");
    assertThat(relationship.getProperties()).isEqualTo(properties);
    Node startNode = relationship.getStartNode();
    assertThat(startNode).isNotNull();
    assertThat(startNode.getId()).isEqualTo(1l);
    Node endNode = relationship.getEndNode();
    assertThat(endNode).isNotNull();
    assertThat(endNode.getId()).isEqualTo(2l);
}
Also used : ExecutableRule(com.buschmais.jqassistant.core.rule.api.model.ExecutableRule) Neo4jRelationship(com.buschmais.xo.neo4j.api.model.Neo4jRelationship) Relationship(com.buschmais.jqassistant.core.report.api.graph.model.Relationship) Node(com.buschmais.jqassistant.core.report.api.graph.model.Node) Neo4jNode(com.buschmais.xo.neo4j.api.model.Neo4jNode) CompositeObject(com.buschmais.xo.api.CompositeObject) SubGraph(com.buschmais.jqassistant.core.report.api.graph.model.SubGraph) Test(org.junit.jupiter.api.Test)

Aggregations

Node (com.buschmais.jqassistant.core.report.api.graph.model.Node)6 Relationship (com.buschmais.jqassistant.core.report.api.graph.model.Relationship)6 SubGraph (com.buschmais.jqassistant.core.report.api.graph.model.SubGraph)6 CompositeObject (com.buschmais.xo.api.CompositeObject)6 Neo4jNode (com.buschmais.xo.neo4j.api.model.Neo4jNode)6 Neo4jRelationship (com.buschmais.xo.neo4j.api.model.Neo4jRelationship)6 ExecutableRule (com.buschmais.jqassistant.core.rule.api.model.ExecutableRule)4 Test (org.junit.jupiter.api.Test)4 ReportException (com.buschmais.jqassistant.core.report.api.ReportException)2 Neo4jLabel (com.buschmais.xo.neo4j.api.model.Neo4jLabel)2 Collection (java.util.Collection)2 Map (java.util.Map)2 Identifiable (com.buschmais.jqassistant.core.report.api.graph.model.Identifiable)1