Given the following graph:
- vertices of type "Companies" holding a "name" property
- edges of type "Shareholder" holding a "holding" property .
Definition:
- Beneficiary : Company with direct or indirect holding in the company
Graph example :
-CompanyB holds 0.5 of companyC - CompanyA holds 0.5 companyB . Both companies are considered companyC's beneficiaries (each hold 0.5 directly and 0.25 indirectly respectively
Creating example:
// create an empty graph
graph = TinkerFactory.createModern()
g = graph.traversal()
g.V().drop()
// create data
companyA = g.addV('Company').property('name', 'A').next()
companyB= g.addV('Company').property('name', 'B').next()
companyC = g.addV('Company').property('name', 'C').next()
g.addE("Shareholder").from(companyB)
.to(companyC).property("holding",0.5)
g.addE("Shareholder").from(companyA)
.to(companyB).property("holding", 0.5)
Desired Solution:
- I want to calculate CompanyC's beneficiaries and their holdings in the Company.
- I'm only interesting in two levels deep (e.g I don't care companyA's shareholders )
- I do want to preserve the holding hierarchy .
Solution Example :
[{
name:"B",
holding:0.5,
shareholders:[
{
name:"A",
holding:0.25
}
]}
]
}]
My Attempt :
// js
let companyCBeneficiaries =await g.V(companyC.id)
.inE()
.project(
"holding",
"name",
"shareholders"
)
.by(__.identity().values("holding"))
.by(__.coalesce(__.outV().values("name") , __.constant("")))
.by(
__.coalesce(
__.outV().inE().project(
"name",
"holding"
)
.by(__.coalesce(__.outV().values("name") , __.constant("")))
.by(__.identity().values("holding"))
,
__.constant([])
)
)
.toList()
// console
g.V(companyC).inE().project("holding","name","shareholders")
.by(__.identity().values("holding"))
.by(__.coalesce(__.outV().values("name") ,__.constant("")))
.by(__.coalesce(__.outV().inE().project("name","holding")
.by(__.coalesce(__.outV().values("name") ,
__.constant("")))
.by(__.identity().values("holding"))
,__.constant([]))).toList()
Which result in CompanyA.holding = 0.5 instead of 0.25 (currently Im iterating over companyCBeneficiaries fixing the holding property )