Hi,

I am trying to write a query that does a table projection, however, there is 
two nested ifthenelse statements. I find that the As clause does not work 
correctly when I mark the iterator and I cannot find a way to get a consistent 
projection.

Ideally. Each THEN statement should contribute to the projection, but this does 
not occur.

I think perhaps the problem is in my ELSE conditions, where I would need to 
created fake nodes for the AS Statements to work with null values when 
deserializing. Any ideas?

Below is two sample Gremlin, and .Net c# equivalent and then the REST outputs. 
(NOTE there is a bug with table projectons in gremlin, and column order is 
reversed from data, but I can work around this. 
https://github.com/neo4j/community/issues/114)

This script is try to get nodes and if those nodes have further relationships 
then go get those nodes and mark them with AS statements and if those nodes 
have relationships, then mark those end nodes with as statements, and then put 
them all in a table

So I imagine the table like this if there were 3 levels deep, and one property 
per node to project

Sample desired output if there is 3 nodes at level 0, 1 node at level 2 and 1 
node at level 3.
Column1 Column2 Column3
PropertyA, null, null
PropertyA, PropertyB, null
PropertyA, PropertyB, PropetyC


e.g.

g.v('0').outE[[label:'HOSTS']].inV.filter{ 
it['Key'].equalsIgnoreCase('romikoagency') 
}.inE[[label:'USER_BELONGS_TO']].outV.filter{ 
it['Username'].equalsIgnoreCase('romiko.derbynew') 
}.outE[[label:'USER_LINKED_TO_CENTRE']].inV.as('Centre').inE[[label:'USER_LINKED_TO_CENTRE']].outV.as('UserGivenName').as('UserFamilyName').inE[[label:'CREATED_BY']].outV.filter{
 it['Completed'] == false }.as('ReferralId')

.ifThenElse{it.outE[[label:'REFERRAL_HAS_WHO_SECTION']].inV.hasNext()}
{it.outE[[label:'REFERRAL_HAS_WHO_SECTION']].inV
.as('ReferralDate')
.ifThenElse{it.outE[[label:'HAS_PARTICIPANT']].inV.hasNext()}
{it.outE[[label:'HAS_PARTICIPANT']].inV.inE[[label:'HAS_PARTICIPANT']].outV.inE[[label:'REFERRAL_HAS_WHO_SECTION']].outV.filter
{ it['Completed'] == false }
.as('ParticipantDisplayOrder')
.as('ParticipantName')}
{it}}
{it}
.table(new 
Table()){it['Name']}{it['FamilyName']}{it['GivenName']}{it['UniqueId']}{it['ReferralDate']}{it['Name']}{it['DisplayOrder']}.cap


var incompleteReferrals = graphClient
                .RootNode
                .Out<Agency>(Hosts.TypeKey, a => a.Key == 
userIdentifier.AgencyKey)
                .In<User>(UserBelongsTo.TypeKey, u => u.Username == 
userIdentifier.Username)
                .Out<Centre>(UserLinkedToCentre.TypeKey)
                .As("Centre")
                .In<User>(UserLinkedToCentre.TypeKey)
                .As("UserGivenName")
                .As("UserFamilyName")
                .In<Referral>(CreatedBy.TypeKey, r => r.Completed == false)
                .As("ReferralId")
                .IfThenElse(new 
GremlinIterator().Out<ReferralWhoSection>(ReferralHasWhoSection.TypeKey).GremlinHasNext(),
                        new 
GremlinIterator().Out<ReferralWhoSection>(ReferralHasWhoSection.TypeKey)
                        .As("ReferralDate")
                        .IfThenElse(new 
GremlinIterator().Out<ReferralParticipant>(HasParticipant.TypeKey).GremlinHasNext(),
                            new 
GremlinIterator().Out<ReferralParticipant>(HasParticipant.TypeKey)
                            .In<ReferralWhoSection>(HasParticipant.TypeKey)
                            .In<Referral>(ReferralHasWhoSection.TypeKey, r => 
r.Completed == false)
                            .As("ParticipantDisplayOrder")
                            .As("ParticipantName")
                            ,
                             new GremlinIterator())
                            ,
                         new GremlinIterator())
                .Table<ReferralByGroup, Centre, User, User, Referral, 
ReferralWhoSection, ReferralParticipant, ReferralParticipant>(
                    centre => centre.Name,
                    user => user.FamilyName,
                    user => user.GivenName,
                    referral => referral.UniqueId,
                    who => who.ReferralDate,
                    participant => participant.Name,
                    participant => participant.DisplayOrder
                );


Output:
[ [ {
  "data" : [ [ "Centre2", "Derbynew", "Romiko", 402 ], [ "null", "null", 
"null", "null" ], [ "null", "Romiko", "null", "null" ], [ "Centre2", "null", 
"null", "null" ], [ "null", "null", "null", "null" ], [ "null", "null", 
"Derbynew", "null" ], [ 11, "null", "null", "null" ], [ "Centre2", "Derbynew", 
"Romiko", 371 ], [ "null", "null", "null", "null" ], [ "null", "Bob", "null", 
"null" ], [ "Centre", "null", "null", "null" ], [ "null", "null", "null", 
"null" ], [ "null", "null", "Builder", "null" ], [ 1, "null", "null", "null" ], 
[ "Centre", "Builder", "Bob", 31 ], [ "null", "null", "null", "null" ], [ 
"null", "Bob", "null", "null" ], [ "Centre", "null", "null", "null" ], [ 
"null", "null", "null", "null" ], [ "null", "null", "Builder", "null" ], [ 1, 
"null", "null", "null" ], [ "Centre", "Builder", "Bob", 81 ], [ "null", "null", 
"null", "null" ], [ "null", "Bob", "null", "null" ], [ "Centre", "null", 
"null", "null" ], [ "null", "null", "null", "null" ], [ "null", "null", "Builde
 r", "null" ], [ 1, "null", "null", "null" ], [ "Centre", "Builder", "Bob", 121 
], [ "null", "null", "null", "null" ], [ "null", "Bob", "null", "null" ], [ 
"Centre", "null", "null", "null" ], [ "null", "null", "null", "null" ], [ 
"null", "null", "Builder", "null" ], [ 1, "null", "null", "null" ], [ "Centre", 
"Builder", "Bob", 141 ], [ "null", "null", "null", "null" ], [ "null", "Bob", 
"null", "null" ], [ "Centre", "null", "null", "null" ], [ "null", "null", 
"null", "null" ], [ "null", "null", "Builder", "null" ], [ 1, "null", "null", 
"null" ], [ "Centre", "Builder", "Bob", 171 ], [ "null", "null", "null", "null" 
], [ "null", "Bob", "null", "null" ], [ "Centre", "null", "null", "null" ], [ 
"null", "null", "null", "null" ], [ "null", "null", "Builder", "null" ], [ 1, 
"null", "null", "null" ], [ "Centre", "Builder", "Bob", 182 ], [ "null", 
"null", "null", "null" ], [ "null", "Bob", "null", "null" ], [ "Centre", 
"null", "null", "null" ], [ "null", "null", "null", "null" ], [ "nul
 l", "null", "Builder", "null" ], [ 1, "null", "null", "null" ], [ "Centre", 
"Builder", "Bob", 184 ], [ "null", "null", "null", "null" ], [ "null", "Bob", 
"null", "null" ], [ "Centre", "null", "null", "null" ], [ "null", "null", 
"null", "null" ], [ "null", "null", "Builder", "null" ], [ 1, "null", "null", 
"null" ], [ "Centre", "Derbynew", "Romiko", 402 ], [ "null", "null", "null", 
"null" ], [ "null", "Romiko", "null", "null" ], [ "Centre", "null", "null", 
"null" ], [ "null", "null", "null", "null" ], [ "null", "null", "Derbynew", 
"null" ], [ 1, "null", "null", "null" ], [ "Centre", "Derbynew", "Romiko", 371 
], [ "null", "null", "null", "null" ] ],
  "columns" : [ "Centre", "UserFamilyName", "UserGivenName", "ReferralId" ]
} ] ]



I then tried it with AS statements AFTER the IFTHENELSE expression, like this:
{"script":"g.v('0').outE[[label:'HOSTS']].inV.filter{ 
it['Key'].equalsIgnoreCase('romikoagency') 
}.inE[[label:'USER_BELONGS_TO']].outV.filter{ 
it['Username'].equalsIgnoreCase('romiko.derbynew') 
}.outE[[label:'USER_LINKED_TO_CENTRE']].inV.as('Centre').inE[[label:'USER_LINKED_TO_CENTRE']].outV.as('UserGivenName').as('UserFamilyName').inE[[label:'CREATED_BY']].outV.filter{
 it['Completed'] == false }.as('ReferralId')
.ifThenElse{it.outE[[label:'REFERRAL_HAS_WHO_SECTION']].inV.hasNext()}{it.outE[[label:'REFERRAL_HAS_WHO_SECTION']].inV
.ifThenElse{it.outE[[label:'HAS_PARTICIPANT']].inV.hasNext()}{it.outE[[label:'HAS_PARTICIPANT']].inV.inE[[label:'HAS_PARTICIPANT']].outV.inE[[label:'REFERRAL_HAS_WHO_SECTION']].outV.filter{
 it['Completed'] == false }}
{it}
.as('ParticipantDisplayOrder')
.as('ParticipantName')}
{it}
.as('ReferralDate').table(new 
Table()){it['Name']}{it['FamilyName']}{it['GivenName']}{it['UniqueId']}{it['ReferralDate']}{it['Name']}{it['DisplayOrder']}.cap"}


var incompleteReferrals = graphClient

                .RootNode

                .Out<Agency>(Hosts.TypeKey, a => a.Key == 
userIdentifier.AgencyKey)

                .In<User>(UserBelongsTo.TypeKey, u => u.Username == 
userIdentifier.Username)

                .Out<Centre>(UserLinkedToCentre.TypeKey)

                .As("Centre")

                .In<User>(UserLinkedToCentre.TypeKey)

                .As("UserGivenName")

                .As("UserFamilyName")

                .In<Referral>(CreatedBy.TypeKey, r => r.Completed == false)

                .As("ReferralId")

                .IfThenElse(new 
GremlinIterator().Out<ReferralWhoSection>(ReferralHasWhoSection.TypeKey).GremlinHasNext(),

                        new 
GremlinIterator().Out<ReferralWhoSection>(ReferralHasWhoSection.TypeKey)

                        .IfThenElse(new 
GremlinIterator().Out<ReferralParticipant>(HasParticipant.TypeKey).GremlinHasNext(),

                            new 
GremlinIterator().Out<ReferralParticipant>(HasParticipant.TypeKey)

                            .In<ReferralWhoSection>(HasParticipant.TypeKey)

                            .In<Referral>(ReferralHasWhoSection.TypeKey, r => 
r.Completed == false)

                            ,

                             new GremlinIterator())

                            .As("ParticipantDisplayOrder")

                            .As("ParticipantName")

                            ,

                         new GremlinIterator())

                .As("ReferralDate")

                .Table<ReferralByGroup, Centre, User, User, Referral, 
ReferralWhoSection, ReferralParticipant, ReferralParticipant>(

                    centre => centre.Name,

                    user => user.FamilyName,

                    user => user.GivenName,

                    referral => referral.UniqueId,

                    who => who.ReferralDate,

                    participant => participant.Name,

                    participant => participant.DisplayOrder

                );


Which produces this result
[ [ {
  "data" : [ [ "Centre2", "Derbynew", "Romiko", 402, "null" ], [ "Centre2", 
"null", "null", "null", "null" ], [ 11, "null", "null", "null", "null" ], [ 
"null", "Romiko", "null", "null", "null" ], [ "null", "null", "Derbynew", 
"null", 401 ], [ "null", "null", "null", "null", "null" ], [ "null", "null", 
"null", "null", "null" ], [ "Centre2", "Derbynew", "Romiko", 371, "null" ], [ 
"Centre2", "null", "null", "null", "null" ], [ 1, "null", "null", "null", 
"null" ], [ "null", "Bob", "null", "null", "null" ], [ "null", "null", 
"Builder", "null", 1 ], [ "null", "null", "null", "null", "null" ], [ "null", 
"null", "null", "null", "null" ], [ "Centre", "Builder", "Bob", 31, "null" ], [ 
"Centre", "null", "null", "null", "null" ], [ 1, "null", "null", "null", "null" 
], [ "null", "Bob", "null", "null", "null" ], [ "null", "null", "Builder", 
"null", 81 ], [ "null", "null", "null", "null", "null" ], [ "null", "null", 
"null", "null", "null" ], [ "Centre", "Builder", "Bob", 81, "null" ], [ "Ce
 ntre", "null", "null", "null", "null" ], [ 1, "null", "null", "null", "null" 
], [ "null", "Bob", "null", "null", "null" ], [ "null", "null", "Builder", 
"null", 101 ], [ "null", "null", "null", "null", "null" ], [ "null", "null", 
"null", "null", "null" ], [ "Centre", "Builder", "Bob", 121, "null" ], [ 
"Centre", "null", "null", "null", "null" ], [ 1, "null", "null", "null", "null" 
], [ "null", "Bob", "null", "null", "null" ], [ "null", "null", "Builder", 
"null", 131 ], [ "null", "null", "null", "null", "null" ], [ "null", "null", 
"null", "null", "null" ], [ "Centre", "Builder", "Bob", 141, "null" ], [ 
"Centre", "null", "null", "null", "null" ], [ 1, "null", "null", "null", "null" 
], [ "null", "Bob", "null", "null", "null" ], [ "null", "null", "Builder", 
"null", 161 ], [ "null", "null", "null", "null", "null" ], [ "null", "null", 
"null", "null", "null" ], [ "Centre", "Builder", "Bob", 171, "null" ], [ 
"Centre", "null", "null", "null", "null" ], [ 1, "null", "null", "null", "nul
 l" ], [ "null", "Bob", "null", "null", "null" ], [ "null", "null", "Builder", 
"null", 171 ], [ "null", "null", "null", "null", "null" ], [ "null", "null", 
"null", "null", "null" ], [ "Centre", "Builder", "Bob", 182, "null" ], [ 
"Centre", "null", "null", "null", "null" ], [ 1, "null", "null", "null", "null" 
], [ "null", "Bob", "null", "null", "null" ], [ "null", "null", "Builder", 
"null", 183 ], [ "null", "null", "null", "null", "null" ], [ "null", "null", 
"null", "null", "null" ], [ "Centre", "Builder", "Bob", 184, "null" ], [ 
"Centre", "null", "null", "null", "null" ], [ 1, "null", "null", "null", "null" 
], [ "null", "Bob", "null", "null", "null" ], [ "null", "null", "Builder", 
"null", 331 ], [ "null", "null", "null", "null", "null" ], [ "null", "null", 
"null", "null", "null" ], [ "Centre", "Derbynew", "Romiko", 402, "null" ], [ 
"Centre", "null", "null", "null", "null" ], [ 1, "null", "null", "null", "null" 
], [ "null", "Romiko", "null", "null", "null" ], [ "null", "null", 
 "Derbynew", "null", 401 ], [ "null", "null", "null", "null", "null" ], [ 
"null", "null", "null", "null", "null" ], [ "Centre", "Derbynew", "Romiko", 
371, "null" ], [ "Centre", "null", "null", "null", "null" ] ],
  "columns" : [ "Centre", "UserFamilyName", "UserGivenName", "ReferralId", 
"ReferralDate" ]
} ] ]

Above, the two columns for ParticipantDisplayOrder and ParticipantName are 
missing.

I can achieve the desired result if I do 3 separate database calls, but I need 
to do only one query for performance. So traverse and while traversing mark 
data for the table output.
_______________________________________________
Neo4j mailing list
User@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user

Reply via email to