简体   繁体   中英

Gremlin: Get all vertices hasLabel 'model' and their children with all properties

I have a graph which should have the following json structure

{
    "models": [
    {
        "name": "Model1",
        "id": 19,
        "diagram": [
        {
            "name": "Diagram1",
            "id": "34"
        },
        {
            "name": "Diagram2",
            "id": "36"
        }
        ]
    },
    {
        "name": "Model2",
        "id": 14,
        "diagram": [
        {
            "name": "Diagram2",
            "id": "32"
        }
        ]
    }
    ]
}

Here is my sample graph:

g.addV('org').property('name', 'Org_seppmed').as('org1').addV('org').property('name', 'Org_siemens').as('org2').addV('user').property('name', 'U_Sebastian').as('user1').addV('user').property('name', 'U_Jaroslaw').as('user2').addV('user').property('name', 'U_Jürgen').as('user3').addV('user').property('name', 'U_Carsten').as('user4').addV('user').property('name', 'U_DevUser').as('user5').addE('administratedBy').from('org1').to('user1').addE('hasEmployee').from('org1').to('user2').addE('hasEmployee').from('org1').to('user3').addE('hasEmployee').from('org2').to('user4').addE('hasEmployee').from('org1').to('user5').addE('hasGuest').from('org1').to('user4').addV('project').property('name', 'P_Bremsen').as('projectmbt1').addV('project').property('name', 'P_Blinker').as('projectmbt2').addE('owns').from('user1').to('projectmbt1').addE('owns').from('user2').to('projectmbt2').addE('writes').from('user2').to('projectmbt1').addE('writes').from('user3').to('projectmbt1').addE('reads').from('user4').to('projectmbt1').addE('writes').from('user1').to('projectmbt2').addE('writes').from('user3').to('projectmbt2').addV('model').property('name', 'M_Bremse').as('modelmbt1').addV('model').property('name', 'M_Blinker').as('modelmbt2').addV('model').property('name', 'M_Bremse2').as('modelmbt3').addE('has').from('projectmbt1').to('modelmbt1').addE('has').from('projectmbt1').to('modelmbt3').addE('has').from('projectmbt2').to('modelmbt2').addV('diagram').property('name', 'D_D1').as('diagramd1').addV('diagram').property('name', 'D_D2').as('diagramd2').addV('diagram').property('name', 'D_D3').as('diagramd3').addV('diagram').property('name', 'D_D1').as('diagramd4').addV('testsuite').property('name', 'T_TestD1').as('testsuited1').addE('has').from('modelmbt1').to('diagramd1').addE('has').from('modelmbt1').to('diagramd2').addE('has').from('modelmbt2').to('diagramd3').addE('has').from('modelmbt3').to('diagramd4').addE('has').from('modelmbt1').to('testsuited1').addV('initial').as('initMBT1D1').addV('precondition').as('preMBT1D1').addV('teststep').property('name', 'N_Blinking left').as('node1MBT1D1').addV('teststep').property('name', 'N_Blinking right').as('node2MBT1D1').addV('teststep').property('name', 'N_Hazard flashers').as('node3MBT1D1').addV('teststep').property('name', 'N_Emergency flashers').as('node4MBT1D1').addV('teststep').property('name', 'N_Blinking off').as('node5MBT1D1').addV('node').as('connodeMBT1D1').addV('postcondition').as('postMBT1D1').addV('final').as('finalMBT1D1').addE('has').from('diagramd1').to('initMBT1D1').addE('has').from('diagramd1').to('preMBT1D1').addE('has').from('diagramd1').to('node1MBT1D1').addE('has').from('diagramd1').to('node2MBT1D1').addE('has').from('diagramd1').to('node3MBT1D1').addE('has').from('diagramd1').to('node4MBT1D1').addE('has').from('diagramd1').to('node5MBT1D1').addE('has').from('diagramd1').to('connodeMBT1D1').addE('has').from('diagramd1').to('postMBT1D1').addE('has').from('diagramd1').to('finalMBT1D1').addE('pointsTo').from('initMBT1D1').to('preMBT1D1').addE('pointsTo').from('preMBT1D1').to('node1MBT1D1').addE('pointsTo').from('preMBT1D1').to('node2MBT1D1').addE('pointsTo').from('preMBT1D1').to('node3MBT1D1').addE('pointsTo').from('preMBT1D1').to('node4MBT1D1').addE('pointsTo').from('preMBT1D1').to('node5MBT1D1').addE('pointsTo').from('node1MBT1D1').to('postMBT1D1').addE('pointsTo').from('postMBT1D1').to('node1MBT1D1').addE('pointsTo').from('node2MBT1D1').to('postMBT1D1').addE('pointsTo').from('node3MBT1D1').to('postMBT1D1').addE('pointsTo').from('node4MBT1D1').to('postMBT1D1').addE('pointsTo').from('node5MBT1D1').to('postMBT1D1').addE('pointsTo').from('postMBT1D1').to('connodeMBT1D1').addE('linksTo').from('connodeMBT1D1').to('diagramd2').addE('pointsTo').from('connodeMBT1D1').to('finalMBT1D1').addV('initial').as('initMBT1D2').addV('teststep').property('name', 'N_Trigger left').as('node1MBT1D2').addV('teststep').property('name', 'N_Trigger right').as('node2MBT1D2').addV('verificationpoint').property('name', 'N_Triggercheck').as('node3MBT1D2').addV('final').as('finalMBT1D2').addE('has').from('diagramd2').to('initMBT1D2').addE('has').from('diagramd2').to('node1MBT1D2').addE('has').from('diagramd2').to('node2MBT1D2').addE('has').from('diagramd2').to('node3MBT1D2').addE('has').from('diagramd2').to('finalMBT1D2').addE('pointsTo').from('initMBT1D2').to('node1MBT1D2').addE('pointsTo').from('initMBT1D2').to('node2MBT1D2').addE('pointsTo').from('node1MBT1D2').to('node3MBT1D2').addE('pointsTo').from('node2MBT1D2').to('node3MBT1D2').addE('pointsTo').from('node3MBT1D2').to('finalMBT1D2')

Now I am trying to find a query to get above structure:

g.V().hasLabel("model").out().hasLabel("diagram").path().by(elementMap())

The result is like:

==>path[{id=12384, label=model, name=Model2}, {id=28904, label=diagram, name=Diagram2}]
==>path[{id=37056, label=model, name=Model1}, {id=16448, label=diagram, name=Diagram1}]
==>path[{id=37056, label=model, name=Model1}, {id=24808, label=diagram, name=Diagram1}]

My expectation was to get a Map<Model, List<Diagram>> . But i get for each diagram of a model a complete new entry, instead of adding all related diagrams to a model.

How can I solve my problem?

If you want to generate a Map you will need to group using the model as the key. Something like shown below. Obviously, you will not really want to use the label but something that identifies the class of the model, if you have such a property, such as name .

g.V().hasLabel("model").
      group().
        by('name').
        by(out().hasLabel("diagram").path().by(elementMap()).fold())

EDITED after discussion in comments:

If you do not want the whole path in the result but just the unique diagram nodes and their properties, the query can be amended to as shown below. This version also adds all of the properties from the label node as the group (map) key.

g.V().hasLabel("model").
      group(). 
        by(elementMap()).
        by(out().hasLabel("diagram").elementMap().fold())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM