2
votes

I have built a a class graph of legacy code base with Neo4j. I'm trying to query for all the methods of parent class that are overridden in a child class.

My nodes and relationships look something like this:

Classes are defined as such:

(c:Class {name: "ClassName"})-[:IS_CHILD_OF]->(p:Class {name: "ParentClassName")

Methods are defined as such:

(c:Class)-[:HAS]->(m:Method {name: "methodName"})

I don't have much experience with Neo4j, but this is the best I came up with so far for the query:

MATCH (child:Class)-[:HAS]->(m) MATCH (parent:Class)-[:HAS]->(mp) WHERE m.name = mp.name AND child-[:IS_CHILD_OF]->parent RETURN child, parent;

It seems to be doing something, but this query has been running for some time now, and i'm not even sure if that will be the result I'm looking for. Any pointers?

2

2 Answers

1
votes

You should check out http://jqassistant.org which does all of this out of the box and it built on neo4j allowing you queries and rules.

This might be more efficient:

create index on :Method(name);

match (m:Method)
with m, m.name
match (m2:Method {name:name})<-[:HAS]-(child:Class)<-[:IS_CHILD_OF*]-(parent:Class)-[:HAS]->(m)
using index m2:Method(name)
RETURN m.name, child.name, parent.name;
1
votes

So, I found a solution that seems to be giving me what I want.

MATCH (cf:Function)<-[:HAS]-(cc:Class)-[:IS_CHILD_OF*]->(pc:Class)-[:HAS]->(pf:Function)
WHERE cf.name = pf.name 
AND cf.scope <> pf.scope
RETURN cc, cf, pc, pf;