Refs #5706: Improve IS Entity/Relation scanning on IS-Model library to support multiple inheritance
Task-Url: https://support.d4science.org/issues/5706 git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/private/luca.frosini/resource-registry-database-creator@160633 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
parent
2b51b7e1ea
commit
cf89e845ff
|
@ -1,125 +0,0 @@
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
package org.gcube.informationsystem.model.discovery;
|
|
||||||
|
|
||||||
import java.lang.reflect.Method;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.gcube.informationsystem.impl.utils.discovery.ReflectionUtility;
|
|
||||||
import org.gcube.informationsystem.model.ISManageable;
|
|
||||||
import org.gcube.informationsystem.model.annotations.ISProperty;
|
|
||||||
import org.gcube.informationsystem.model.embedded.Embedded;
|
|
||||||
import org.jgrapht.Graph;
|
|
||||||
import org.jgrapht.graph.DefaultDirectedGraph;
|
|
||||||
import org.jgrapht.graph.DefaultEdge;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @author Luca Frosini (ISTI - CNR)
|
|
||||||
*/
|
|
||||||
public class ISMDiscovery<ISM extends ISManageable> {
|
|
||||||
|
|
||||||
private static Logger logger = LoggerFactory.getLogger(ISMDiscovery.class);
|
|
||||||
|
|
||||||
protected final Graph<Class<ISM>, DefaultEdge> graph;
|
|
||||||
|
|
||||||
public Graph<Class<ISM>, DefaultEdge> getGraph() {
|
|
||||||
return graph;
|
|
||||||
}
|
|
||||||
|
|
||||||
protected final Class<ISM> root;
|
|
||||||
protected final Set<Class<ISM>> visited;
|
|
||||||
|
|
||||||
public ISMDiscovery(Class<ISM> root) {
|
|
||||||
this.root = root;
|
|
||||||
|
|
||||||
this.graph = new DefaultDirectedGraph<>(DefaultEdge.class);
|
|
||||||
|
|
||||||
this.visited = new HashSet<>();
|
|
||||||
|
|
||||||
this.graph.addVertex(root);
|
|
||||||
this.visited.add(root);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void addISM(Class<ISM> clz, Class<ISM> parent) {
|
|
||||||
graph.addEdge(parent, clz);
|
|
||||||
visited.add(clz);
|
|
||||||
logger.debug("Adding {} as children of {}", clz, parent);
|
|
||||||
}
|
|
||||||
|
|
||||||
protected void analizeISM(Class<ISM> clz) {
|
|
||||||
logger.trace(" --- Analizyng {}", clz.getCanonicalName());
|
|
||||||
|
|
||||||
if (visited.contains(clz)) {
|
|
||||||
logger.trace(" --------- discarding {} because was already managed", clz);
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
graph.addVertex(clz);
|
|
||||||
|
|
||||||
Class<?>[] interfaces = clz.getInterfaces();
|
|
||||||
|
|
||||||
for (Class<?> interfaceClass : interfaces) {
|
|
||||||
if (!root.isAssignableFrom(interfaceClass)) {
|
|
||||||
logger.trace(" --------- discarding {} because is not a {}", interfaceClass,
|
|
||||||
root.getClass().getSimpleName());
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Class<ISM> parent = (Class<ISM>) interfaceClass;
|
|
||||||
|
|
||||||
if (!visited.contains(parent)) {
|
|
||||||
analizeISM(parent);
|
|
||||||
}
|
|
||||||
|
|
||||||
addISM(clz, parent);
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
if(root==Embedded.class){
|
|
||||||
|
|
||||||
for (Method m : clz.getDeclaredMethods()){
|
|
||||||
m.setAccessible(true);
|
|
||||||
if(m.isAnnotationPresent(ISProperty.class)){
|
|
||||||
if(root.isAssignableFrom(m.getReturnType())){
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Class<ISM> type = (Class<ISM>) m.getReturnType();
|
|
||||||
analizeISM(type);
|
|
||||||
addISM(clz, type);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
logger.trace("{}", (Object[]) interfaces);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void discover() throws Exception {
|
|
||||||
|
|
||||||
List<Class<?>> classes = ReflectionUtility.getClassesForPackage(root.getPackage());
|
|
||||||
for (Class<?> clz : classes) {
|
|
||||||
logger.trace("Analyzing {}", clz);
|
|
||||||
if (!clz.isInterface()) {
|
|
||||||
logger.trace("Discarding {} that is not an interface", clz);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (root.isAssignableFrom(clz)) {
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
Class<ISM> ism = (Class<ISM>) clz;
|
|
||||||
analizeISM(ism);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,10 +1,9 @@
|
||||||
package org.gcube.informationsystem.model.discovery;
|
package org.gcube.informationsystem.model.discovery;
|
||||||
|
|
||||||
|
import org.gcube.informationsystem.impl.utils.discovery.ISMDiscovery;
|
||||||
import org.gcube.informationsystem.model.embedded.Embedded;
|
import org.gcube.informationsystem.model.embedded.Embedded;
|
||||||
import org.gcube.informationsystem.model.entity.Entity;
|
import org.gcube.informationsystem.model.entity.Entity;
|
||||||
import org.gcube.informationsystem.model.relation.Relation;
|
import org.gcube.informationsystem.model.relation.Relation;
|
||||||
import org.jgrapht.graph.DefaultEdge;
|
|
||||||
import org.jgrapht.traverse.BreadthFirstIterator;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -17,16 +16,18 @@ public class ISMDiscoveryTest {
|
||||||
public void testEmbeddedDiscovery() throws Exception{
|
public void testEmbeddedDiscovery() throws Exception{
|
||||||
ISMDiscovery<Embedded> embeddedDiscovery = new ISMDiscovery<>(Embedded.class);
|
ISMDiscovery<Embedded> embeddedDiscovery = new ISMDiscovery<>(Embedded.class);
|
||||||
embeddedDiscovery.discover();
|
embeddedDiscovery.discover();
|
||||||
|
|
||||||
|
for(Class<Embedded> embedded : embeddedDiscovery.getDiscovered()) {
|
||||||
|
logger.info("Going to create : {}", embedded);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testEntityDiscovery() throws Exception{
|
public void testEntityDiscovery() throws Exception{
|
||||||
ISMDiscovery<Entity> entityDiscovery = new ISMDiscovery<>(Entity.class);
|
ISMDiscovery<Entity> entityDiscovery = new ISMDiscovery<>(Entity.class);
|
||||||
entityDiscovery.discover();
|
entityDiscovery.discover();
|
||||||
BreadthFirstIterator<Class<Entity>, DefaultEdge> breadthFirstIterator = new BreadthFirstIterator<Class<Entity>, DefaultEdge>(entityDiscovery.getGraph());
|
|
||||||
|
|
||||||
while(breadthFirstIterator.hasNext()){
|
for(Class<Entity> entity : entityDiscovery.getDiscovered()) {
|
||||||
Class<Entity> entity = breadthFirstIterator.next();
|
|
||||||
logger.info("Going to create : {}", entity);
|
logger.info("Going to create : {}", entity);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,7 @@
|
||||||
package org.gcube.informationsystem.resourceregistry;
|
package org.gcube.informationsystem.resourceregistry;
|
||||||
|
|
||||||
|
import org.gcube.informationsystem.impl.utils.discovery.ISMDiscovery;
|
||||||
import org.gcube.informationsystem.impl.utils.discovery.SchemaAction;
|
import org.gcube.informationsystem.impl.utils.discovery.SchemaAction;
|
||||||
import org.gcube.informationsystem.model.discovery.ISMDiscovery;
|
|
||||||
import org.gcube.informationsystem.model.embedded.Embedded;
|
|
||||||
import org.gcube.informationsystem.model.entity.Entity;
|
|
||||||
import org.gcube.informationsystem.model.relation.Relation;
|
|
||||||
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
|
import org.gcube.informationsystem.resourceregistry.dbinitialization.DatabaseEnvironment;
|
||||||
import org.gcube.informationsystem.resourceregistry.dbinitialization.SchemaActionImpl;
|
import org.gcube.informationsystem.resourceregistry.dbinitialization.SchemaActionImpl;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
@ -12,47 +9,19 @@ import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
public class DataBaseCreator {
|
public class DataBaseCreator {
|
||||||
|
|
||||||
private static Logger logger = LoggerFactory.getLogger(ISMDiscovery.class);
|
private static Logger logger = LoggerFactory.getLogger(ISMDiscovery.class);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void createDatabase() throws Exception{
|
public void createDatabase() throws Exception {
|
||||||
String db = DatabaseEnvironment.DB_URI;
|
String db = DatabaseEnvironment.DB_URI;
|
||||||
logger.info("Created DB {}", db);
|
logger.info("Created DB {}", db);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
|
||||||
@Test
|
@Test
|
||||||
public void createTypes() throws Exception {
|
public void createTypes() throws Exception {
|
||||||
|
|
||||||
SchemaAction schemaAction = new SchemaActionImpl();
|
SchemaAction schemaAction = new SchemaActionImpl();
|
||||||
|
ISMDiscovery.manageISM(schemaAction);
|
||||||
ISMDiscovery<Embedded> embeddedDiscovery = new ISMDiscovery<>(Embedded.class);
|
|
||||||
embeddedDiscovery.discover();
|
|
||||||
|
|
||||||
for(Class<Embedded> embedded : embeddedDiscovery.getDiscovered()) {
|
|
||||||
logger.info("Going to create : {}", embedded);
|
|
||||||
schemaAction.manageEmbeddedClass(embedded);
|
|
||||||
}
|
|
||||||
|
|
||||||
ISMDiscovery<Entity> entityDiscovery = new ISMDiscovery<>(Entity.class);
|
|
||||||
entityDiscovery.discover();
|
|
||||||
|
|
||||||
for(Class<Entity> entity : entityDiscovery.getDiscovered()) {
|
|
||||||
logger.info("Going to create : {}", entity);
|
|
||||||
schemaAction.manageEntityClass(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
|
||||||
ISMDiscovery<Relation> relationDiscovery = new ISMDiscovery<>(Relation.class);
|
|
||||||
relationDiscovery.discover();
|
|
||||||
|
|
||||||
for(@SuppressWarnings("rawtypes") Class<Relation> relation : relationDiscovery.getDiscovered()) {
|
|
||||||
logger.info("Going to create : {}", relation);
|
|
||||||
schemaAction.manageRelationClass(relation);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue