Refactoring
This commit is contained in:
parent
484690555a
commit
982cce370c
|
@ -1,10 +1,11 @@
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
package org.gcube.informationsystem.tree;
|
package org.gcube.informationsystem.discovery.knowledge;
|
||||||
|
|
||||||
import org.gcube.informationsystem.base.reference.Element;
|
import org.gcube.informationsystem.base.reference.Element;
|
||||||
import org.gcube.informationsystem.discovery.DiscoveredElementAction;
|
import org.gcube.informationsystem.discovery.DiscoveredElementAction;
|
||||||
|
import org.gcube.informationsystem.tree.Tree;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Luca Frosini (ISTI - CNR)
|
* @author Luca Frosini (ISTI - CNR)
|
|
@ -1,4 +1,4 @@
|
||||||
package org.gcube.informationsystem.discovery;
|
package org.gcube.informationsystem.discovery.knowledge;
|
||||||
|
|
||||||
import java.util.LinkedHashSet;
|
import java.util.LinkedHashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
|
@ -8,12 +8,13 @@ import java.util.Set;
|
||||||
|
|
||||||
import org.gcube.informationsystem.base.reference.AccessType;
|
import org.gcube.informationsystem.base.reference.AccessType;
|
||||||
import org.gcube.informationsystem.base.reference.Element;
|
import org.gcube.informationsystem.base.reference.Element;
|
||||||
import org.gcube.informationsystem.discovery.ClassInformation;
|
|
||||||
import org.gcube.informationsystem.discovery.Discovery;
|
import org.gcube.informationsystem.discovery.Discovery;
|
||||||
import org.gcube.informationsystem.discovery.RegistrationProvider;
|
import org.gcube.informationsystem.discovery.RegistrationProvider;
|
||||||
import org.gcube.informationsystem.serialization.ElementMappingAction;
|
import org.gcube.informationsystem.serialization.ElementMappingAction;
|
||||||
import org.gcube.informationsystem.tree.AddElementToTreeAction;
|
|
||||||
import org.gcube.informationsystem.tree.Tree;
|
import org.gcube.informationsystem.tree.Tree;
|
||||||
|
import org.gcube.informationsystem.types.TypeMapper;
|
||||||
|
import org.gcube.informationsystem.types.knowledge.AddTypeToTreeAction;
|
||||||
|
import org.gcube.informationsystem.types.knowledge.TypeInformation;
|
||||||
import org.gcube.informationsystem.types.reference.Type;
|
import org.gcube.informationsystem.types.reference.Type;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -26,11 +27,12 @@ public class ModelKnowledge {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Map<AccessType, Tree<Class<Element>>> trees;
|
protected Map<AccessType, Tree<Class<Element>>> trees;
|
||||||
protected Map<AccessType, Tree<Type>> typesTree;
|
protected Map<AccessType, Tree<Type>> typeTrees;
|
||||||
protected Map<AccessType, Discovery<? extends Element>> discoveries;
|
protected Map<AccessType, Discovery<? extends Element>> discoveries;
|
||||||
|
|
||||||
protected ModelKnowledge() {
|
protected ModelKnowledge() {
|
||||||
this.trees = new HashMap<>();
|
this.trees = new HashMap<>();
|
||||||
|
this.typeTrees = new HashMap<>();
|
||||||
this.discoveries = new HashMap<>();
|
this.discoveries = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,15 +48,19 @@ public class ModelKnowledge {
|
||||||
}
|
}
|
||||||
|
|
||||||
ClassInformation classInformation = new ClassInformation();
|
ClassInformation classInformation = new ClassInformation();
|
||||||
|
TypeInformation typeInformation = new TypeInformation();
|
||||||
|
|
||||||
for(AccessType accessType : modelTypes) {
|
for(AccessType accessType : modelTypes) {
|
||||||
Class<Element> clz = accessType.getTypeClass();
|
|
||||||
|
|
||||||
|
Class<Element> clz = accessType.getTypeClass();
|
||||||
Tree<Class<Element>> tree = new Tree<>(clz, classInformation);
|
Tree<Class<Element>> tree = new Tree<>(clz, classInformation);
|
||||||
trees.put(accessType, tree);
|
trees.put(accessType, tree);
|
||||||
AddElementToTreeAction aetta = new AddElementToTreeAction(tree);
|
AddElementToTreeAction aetta = new AddElementToTreeAction(tree);
|
||||||
|
|
||||||
|
Type type = TypeMapper.createTypeDefinition(clz);
|
||||||
|
Tree<Type> typeTree = new Tree<>(type, typeInformation);
|
||||||
|
typeTrees.put(accessType, typeTree);
|
||||||
|
AddTypeToTreeAction attta = new AddTypeToTreeAction(typeTree);
|
||||||
|
|
||||||
|
|
||||||
Discovery<? extends Element> discovery = new Discovery<>(clz);
|
Discovery<? extends Element> discovery = new Discovery<>(clz);
|
||||||
|
@ -62,6 +68,7 @@ public class ModelKnowledge {
|
||||||
discovery.addPackages(allPackages);
|
discovery.addPackages(allPackages);
|
||||||
discovery.addDiscoveredElementActions(new ElementMappingAction());
|
discovery.addDiscoveredElementActions(new ElementMappingAction());
|
||||||
discovery.addDiscoveredElementActions(aetta);
|
discovery.addDiscoveredElementActions(aetta);
|
||||||
|
discovery.addDiscoveredElementActions(attta);
|
||||||
|
|
||||||
discovery.discover();
|
discovery.discover();
|
||||||
|
|
||||||
|
@ -69,8 +76,12 @@ public class ModelKnowledge {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Tree<Class<Element>> getTree(AccessType accessType) {
|
public Tree<Class<Element>> getClassesTree(AccessType accessType) {
|
||||||
return trees.get(accessType);
|
return trees.get(accessType);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Tree<Type> getTypesTree(AccessType accessType) {
|
||||||
|
return typeTrees.get(accessType);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,7 @@ import org.gcube.com.fasterxml.jackson.databind.node.TextNode;
|
||||||
import org.gcube.informationsystem.base.reference.AccessType;
|
import org.gcube.informationsystem.base.reference.AccessType;
|
||||||
import org.gcube.informationsystem.base.reference.Element;
|
import org.gcube.informationsystem.base.reference.Element;
|
||||||
import org.gcube.informationsystem.discovery.Discovery;
|
import org.gcube.informationsystem.discovery.Discovery;
|
||||||
|
import org.gcube.informationsystem.discovery.knowledge.ModelKnowledge;
|
||||||
import org.gcube.informationsystem.types.TypeMapper;
|
import org.gcube.informationsystem.types.TypeMapper;
|
||||||
import org.gcube.informationsystem.types.reference.Type;
|
import org.gcube.informationsystem.types.reference.Type;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
|
@ -105,6 +106,11 @@ public abstract class ElementMapper {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
ModelKnowledge.getInstance().createKnowledge();
|
||||||
|
}catch (Exception e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,94 +0,0 @@
|
||||||
package org.gcube.informationsystem.tobereplaced;
|
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.ServiceLoader;
|
|
||||||
|
|
||||||
import org.gcube.informationsystem.base.reference.AccessType;
|
|
||||||
import org.gcube.informationsystem.base.reference.Element;
|
|
||||||
import org.gcube.informationsystem.base.reference.entities.EntityElement;
|
|
||||||
import org.gcube.informationsystem.base.reference.properties.PropertyElement;
|
|
||||||
import org.gcube.informationsystem.base.reference.relations.RelationElement;
|
|
||||||
import org.gcube.informationsystem.discovery.DiscoveredElementAction;
|
|
||||||
import org.gcube.informationsystem.discovery.Discovery;
|
|
||||||
import org.gcube.informationsystem.discovery.RegistrationProvider;
|
|
||||||
import org.gcube.informationsystem.types.reference.Type;
|
|
||||||
import org.slf4j.Logger;
|
|
||||||
import org.slf4j.LoggerFactory;
|
|
||||||
|
|
||||||
public class DiscoveryUtility {
|
|
||||||
|
|
||||||
public static Logger logger = LoggerFactory.getLogger(DiscoveryUtility.class);
|
|
||||||
|
|
||||||
public static List<Package> discoverPackages(){
|
|
||||||
List<Package> packages = new ArrayList<Package>();
|
|
||||||
|
|
||||||
Class<Type> tdClz = Type.class;
|
|
||||||
packages.add(tdClz.getPackage());
|
|
||||||
|
|
||||||
AccessType[] accessTypes = AccessType.values();
|
|
||||||
for(AccessType accessType : accessTypes) {
|
|
||||||
Class<Element> clz = accessType.getTypeClass();
|
|
||||||
packages.add(clz.getPackage());
|
|
||||||
}
|
|
||||||
|
|
||||||
ServiceLoader<? extends RegistrationProvider> regsitrationProviders = ServiceLoader
|
|
||||||
.load(RegistrationProvider.class);
|
|
||||||
for(RegistrationProvider registrationProvider : regsitrationProviders) {
|
|
||||||
packages.addAll(registrationProvider.getPackagesToRegister());
|
|
||||||
}
|
|
||||||
|
|
||||||
return packages;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void discover(DiscoveredElementAction schemaAction) throws Exception {
|
|
||||||
List<Package> packages = DiscoveryUtility.discoverPackages();
|
|
||||||
DiscoveryUtility.manageISM(schemaAction , packages);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void manageISM(DiscoveredElementAction dea, Package... packages) throws Exception {
|
|
||||||
Discovery<PropertyElement> propertyDiscovery = new Discovery<>(PropertyElement.class);
|
|
||||||
if(Objects.nonNull(packages)) {
|
|
||||||
Arrays.stream(packages).forEach(p -> propertyDiscovery.addPackage(p));
|
|
||||||
}
|
|
||||||
propertyDiscovery.discover();
|
|
||||||
for(Class<? extends PropertyElement> property : propertyDiscovery.getDiscoveredElement()) {
|
|
||||||
logger.trace("Going to manage : {}", property);
|
|
||||||
dea.analizeElement(property);
|
|
||||||
}
|
|
||||||
|
|
||||||
Discovery<EntityElement> entityDiscovery = new Discovery<>(EntityElement.class);
|
|
||||||
if(Objects.nonNull(packages)) {
|
|
||||||
Arrays.stream(packages).forEach(p -> entityDiscovery.addPackage(p));
|
|
||||||
}
|
|
||||||
entityDiscovery.discover();
|
|
||||||
|
|
||||||
for(Class<? extends EntityElement> entity : entityDiscovery.getDiscoveredElement()) {
|
|
||||||
logger.trace("Going to manage : {}", entity);
|
|
||||||
dea.analizeElement(entity);
|
|
||||||
}
|
|
||||||
|
|
||||||
@SuppressWarnings("rawtypes")
|
|
||||||
Discovery<RelationElement> relationDiscovery = new Discovery<RelationElement>(RelationElement.class);
|
|
||||||
if(Objects.nonNull(packages))
|
|
||||||
Arrays.stream(packages).forEach(p -> relationDiscovery.addPackage(p));
|
|
||||||
relationDiscovery.discover();
|
|
||||||
|
|
||||||
for(@SuppressWarnings("rawtypes")
|
|
||||||
Class<? extends RelationElement> relation : relationDiscovery.getDiscoveredElement()) {
|
|
||||||
logger.trace("Going to manage : {}", relation);
|
|
||||||
dea.analizeElement(relation);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static void manageISM(DiscoveredElementAction schemaAction, List<Package> packages) throws Exception {
|
|
||||||
if(Objects.nonNull(packages) && packages.size() > 0) {
|
|
||||||
manageISM(schemaAction, packages.stream().toArray(Package[]::new));
|
|
||||||
} else {
|
|
||||||
manageISM(schemaAction);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -0,0 +1,27 @@
|
||||||
|
package org.gcube.informationsystem.types.knowledge;
|
||||||
|
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import org.gcube.informationsystem.tree.NodeInformation;
|
||||||
|
import org.gcube.informationsystem.types.reference.Type;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author Luca Frosini (ISTI - CNR)
|
||||||
|
*/
|
||||||
|
public class TypeInformation implements NodeInformation<Type> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getIdentifier(Type type) {
|
||||||
|
return type.getName();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<String> getParentIdentifiers(Type root, Type type) {
|
||||||
|
if(getIdentifier(type).compareTo(getIdentifier(root))==0) {
|
||||||
|
return new HashSet<>();
|
||||||
|
}
|
||||||
|
return type.getSuperClasses();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -0,0 +1 @@
|
||||||
|
org.gcube.informationsystem.discovery.ISModelRegistrationProvider
|
|
@ -1,19 +1,11 @@
|
||||||
package org.gcube.informationsystem.utils.discovery;
|
package org.gcube.informationsystem.utils.discovery;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.ServiceLoader;
|
|
||||||
|
|
||||||
import org.gcube.informationsystem.base.reference.AccessType;
|
import org.gcube.informationsystem.base.reference.AccessType;
|
||||||
import org.gcube.informationsystem.base.reference.Element;
|
import org.gcube.informationsystem.base.reference.Element;
|
||||||
import org.gcube.informationsystem.base.reference.entities.EntityElement;
|
import org.gcube.informationsystem.discovery.knowledge.ModelKnowledge;
|
||||||
import org.gcube.informationsystem.base.reference.properties.PropertyElement;
|
import org.gcube.informationsystem.tree.Tree;
|
||||||
import org.gcube.informationsystem.base.reference.relations.RelationElement;
|
|
||||||
import org.gcube.informationsystem.discovery.Discovery;
|
|
||||||
import org.gcube.informationsystem.discovery.RegistrationProvider;
|
|
||||||
import org.gcube.informationsystem.tobereplaced.DiscoveryUtility;
|
|
||||||
import org.gcube.informationsystem.discovery.DiscoveredElementAction;
|
|
||||||
import org.gcube.informationsystem.types.TypeMapper;
|
import org.gcube.informationsystem.types.TypeMapper;
|
||||||
import org.gcube.informationsystem.types.reference.Type;
|
import org.gcube.informationsystem.types.reference.Type;
|
||||||
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition;
|
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition;
|
||||||
|
@ -32,12 +24,22 @@ public class ISMDiscoveryTest {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ISMDiscoveryTest.class);
|
private static final Logger logger = LoggerFactory.getLogger(ISMDiscoveryTest.class);
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testISMDIscovery() {
|
public void testDiscovery() throws Exception {
|
||||||
Discovery<PropertyElement> propertyDiscovery = new Discovery<>(PropertyElement.class);
|
ModelKnowledge modelKnowledge = ModelKnowledge.getInstance();
|
||||||
propertyDiscovery.analizeElement(PropertyType.class);
|
modelKnowledge.createKnowledge();
|
||||||
|
|
||||||
|
AccessType[] accessTypes = AccessType.getModelTypes();
|
||||||
|
for(AccessType accessType : accessTypes) {
|
||||||
|
|
||||||
|
Tree<Class<Element>> classesTree = modelKnowledge.getClassesTree(accessType);
|
||||||
|
logger.info("Classes tree for {} is\n{}", accessType.getName(), classesTree.toString());
|
||||||
|
|
||||||
|
Tree<Type> typesTree = modelKnowledge.getTypesTree(accessType);
|
||||||
|
logger.info("Types tree for {} is\n{}", accessType.getName(), typesTree.toString());
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private void checkType(Type type, Type expected) {
|
private void checkType(Type type, Type expected) {
|
||||||
Assert.assertTrue(type.getName().compareTo(expected.getName())==0);
|
Assert.assertTrue(type.getName().compareTo(expected.getName())==0);
|
||||||
Assert.assertTrue(type.getVersion().compareTo(expected.getVersion())==0);
|
Assert.assertTrue(type.getVersion().compareTo(expected.getVersion())==0);
|
||||||
|
@ -64,7 +66,6 @@ public class ISMDiscoveryTest {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testcheckType() throws Exception {
|
public void testcheckType() throws Exception {
|
||||||
Class<? extends Element> clz = PropertyType.class;
|
Class<? extends Element> clz = PropertyType.class;
|
||||||
|
@ -78,65 +79,6 @@ public class ISMDiscoveryTest {
|
||||||
checkType(type, expected);
|
checkType(type, expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
|
||||||
public void test() throws Exception {
|
|
||||||
|
|
||||||
List<Package> packages = new ArrayList<Package>();
|
|
||||||
|
|
||||||
Class<Type> tdClz = Type.class;
|
|
||||||
packages.add(tdClz.getPackage());
|
|
||||||
|
|
||||||
AccessType[] accessTypes = AccessType.values();
|
|
||||||
for(AccessType accessType : accessTypes) {
|
|
||||||
Class<Element> clz = accessType.getTypeClass();
|
|
||||||
packages.add(clz.getPackage());
|
|
||||||
}
|
|
||||||
|
|
||||||
ServiceLoader<? extends RegistrationProvider> regsitrationProviders = ServiceLoader
|
|
||||||
.load(RegistrationProvider.class);
|
|
||||||
for(RegistrationProvider registrationProvider : regsitrationProviders) {
|
|
||||||
packages.addAll(registrationProvider.getPackagesToRegister());
|
|
||||||
}
|
|
||||||
|
|
||||||
DiscoveredElementAction schemaAction = new DiscoveredElementAction() {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <R extends RelationElement<? extends EntityElement, ? extends EntityElement>> void manageRelationClass(
|
|
||||||
Class<R> r) throws Exception {
|
|
||||||
Type type = TypeMapper.createTypeDefinition(r);
|
|
||||||
Assert.assertTrue(type.getName().compareTo(r.getSimpleName())==0);
|
|
||||||
String typeDefinitionJsonString = TypeMapper.serializeTypeDefinition(type);
|
|
||||||
logger.debug("{} {}", r.getSimpleName(), typeDefinitionJsonString);
|
|
||||||
Type expected = TypeMapper.deserializeTypeDefinition(typeDefinitionJsonString);
|
|
||||||
checkType(type, expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <P extends PropertyElement> void managePropertyClass(Class<P> p) throws Exception {
|
|
||||||
Type type = TypeMapper.createTypeDefinition(p);
|
|
||||||
Assert.assertTrue(type.getName().compareTo(p.getSimpleName())==0);
|
|
||||||
String typeDefinitionJsonString = TypeMapper.serializeTypeDefinition(type);
|
|
||||||
logger.debug("{} {}", p.getSimpleName(), typeDefinitionJsonString);
|
|
||||||
Type expected = TypeMapper.deserializeTypeDefinition(typeDefinitionJsonString);
|
|
||||||
checkType(type, expected);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public <E extends EntityElement> void manageEntityClass(Class<E> e) throws Exception {
|
|
||||||
Type type = TypeMapper.createTypeDefinition(e);
|
|
||||||
Assert.assertTrue(type.getName().compareTo(e.getSimpleName())==0);
|
|
||||||
String typeDefinitionJsonString = TypeMapper.serializeTypeDefinition(type);
|
|
||||||
logger.debug("{} {}", e.getSimpleName(), typeDefinitionJsonString);
|
|
||||||
Type expected = TypeMapper.deserializeTypeDefinition(typeDefinitionJsonString);
|
|
||||||
checkType(type, expected);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
DiscoveryUtility.manageISM(schemaAction , packages);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testTypeDefinition() throws Exception {
|
public void testTypeDefinition() throws Exception {
|
||||||
Class<? extends Element> clz = PropertyType.class;
|
Class<? extends Element> clz = PropertyType.class;
|
||||||
|
|
Loading…
Reference in New Issue