Get the class heirarchy of a vertex

107 views
Skip to first unread message

Chris Whalen

unread,
Feb 2, 2015, 11:45:28 AM2/2/15
to orient-...@googlegroups.com
I have been digging through the java api javadocs as well as the documentation, but I cannot figure out how to get the full class ancestry of a vertex.  I think I can write a separate query against the schema to return the ancestry but it would be best to have it returned in the original query so that I do not have to do multiple queries when working with millions of documents.  I was thinking, something along the lines of:

select @rid, @class, @ancestors from V

Is this possible?

Chris Whalen

unread,
Jul 30, 2015, 6:59:00 PM7/30/15
to OrientDB, cwhal...@gmail.com
Has anyone figured out how to return the parent classes of a vertex as a projection?  When I get the vertex, I need to know what it's parent classes are.  So if I get a Square classed vertex, I need to know that it is of a sub class of Rectangle and Shape.

alessand...@gmail.com

unread,
Jul 31, 2015, 6:19:54 AM7/31/15
to OrientDB, cwhal...@gmail.com
Hi Chris,
can you send me your schema ?

Alessandro

Chris Whalen

unread,
Jul 31, 2015, 1:30:10 PM7/31/15
to OrientDB, cwhal...@gmail.com, alessand...@gmail.com
Here is the demo schema that I've been playing with.  This is a pretty bare schema.  The inheritance could go much deeper.  With this schema, if I search for BrandAssets, For each entry returned, I need to know that it is a BrandAsset, Asset, and Entity.
Untitled-1.jpg

hartmut bischoff

unread,
Jul 31, 2015, 2:50:33 PM7/31/15
to OrientDB, cwhal...@gmail.com
Hi,

in my view, thats not the job of a database-request. Instead its a matter of your OOP-Design.

I thought, even Java has basic OOP-Support.
So why do you not assign the retrieved Class to the proper Java-Object?

I have done this for ruby. There its just a matter of two lines of code:
    def self.orientdb_class name
       klass = Class.new( self )
       name =  name.to_s.camelize
       if self.send :const_defined?, name
          self.send :const_get, name  # returns the Const
       else
          self.send :const_set  , name , klass  # creates and returns the Const
         end
   end


Elsewhere you simply define the Class in the usual way. Everytime a Database-object is fetched, I call BaseClass.orientdb_class( vertex_name )  and get the proper Ruby-class. 
The ruby-class knows about its parents and has the proper methods to deal with the fetched content.
Take a look at ActiveOrient

Maybe its not that simple in Java, but as Jruby exists, somehow it must work.

Chris Whalen

unread,
Jul 31, 2015, 4:20:08 PM7/31/15
to OrientDB, cwhal...@gmail.com, topo...@gmail.com
I don't disagree.  Normally, I would not expect a RDB request to return this information but due to orientdb's polymorphism capabilities I think it would be reasonable.  As my schema is not static, I cannot rely on a java object to determine a class' ancestors; new sub classes will be defined during run time.

hartmut bischoff

unread,
Jul 31, 2015, 5:15:00 PM7/31/15
to orient-...@googlegroups.com, cwhal...@gmail.com, topo...@gmail.com


On Friday, July 31, 2015 at 10:20:08 PM UTC+2, Chris Whalen wrote:
I don't disagree.  Normally, I would not expect a RDB request to return this information but due to orientdb's polymorphism capabilities I think it would be reasonable.  As my schema is not static, I cannot rely on a java object to determine a class' ancestors; new sub classes will be defined during run time

For this purpose I maintain a »classes-Object« which is updated whenever a class is created or removed. Then there is a method to get the class-hierarchy of the database, based on the object-root

If you want to go the other way, just define a method 'superClass' for every fetched Object. Then Method.superClass.superClass.(...) reveals the hole tree of the corresponding ruby/java-objects. 


 

nagaraja sosale ramaswamy

unread,
Aug 2, 2015, 11:20:40 AM8/2/15
to OrientDB, cwhal...@gmail.com, topo...@gmail.com
a possible solution is to write a simple server side function which performs the query against the metadata:schema special target and finds the ancestors. then you could use the function in your sql query, like 'select ancestorClasses(@rid) from #15:2'

such a function itself could be developed using this query gets the immediate ancestor for given @rid:

select superClasses from ( select expand(classes) from metadata:schema ) where name in (select @class from #15:2) unwind superClasses

then you could recurse over the superClasses to obtain the complete hierarchy.

This approach can be easily extended to handle the case of multiple inheritance

Chris Whalen

unread,
Aug 4, 2015, 1:32:22 AM8/4/15
to OrientDB, cwhal...@gmail.com, topo...@gmail.com, nagu...@gmail.com
I like this approach. I created a server side function named :GetClassAncestors

var g = orient.getGraphNoTx();

var classLookup = {};

var classSchema = g.command("sql", "select name, superClass from ( select expand(classes) from metadata:schema)");


for(var i=0; i < classSchema.length; i++) {
  
  var schemaClass = classSchema[i];
  var className = schemaClass.getRecord().field('name')
  var parentName = schemaClass.getRecord().field('superClass')
  classLookup[className] = parentName;
  
};

var objectAncestors = [];

var objectAncestor = objectClassName;
while(objectAncestor != null)
{
  objectAncestors[objectAncestors.length] = objectAncestor;
  objectAncestor = classLookup[objectAncestor]
}

return objectAncestors;

You then call it: "select GetClassAncestors(@class) from #15:2"
Reply all
Reply to author
Forward
0 new messages