Refactoring

This commit is contained in:
Luca Frosini 2023-02-06 16:35:30 +01:00
parent c7985f7aad
commit 879cf9b81f
7 changed files with 115 additions and 100 deletions

View File

@ -64,16 +64,16 @@ public enum AccessType {
ENTITY_ELEMENT(EntityElement.class, EntityElement.NAME, EntityElementImpl.class, null),
ENTITY_TYPE(EntityType.class, EntityType.NAME, EntityTypeImpl.class, null),
CONTEXT(Context.class, Context.NAME, ContextImpl.class, null),
QUERY_TEMPLATE(QueryTemplate.class, QueryTemplate.NAME, QueryTemplateImpl.class, null),
CONTEXT(Context.class, Context.NAME, ContextImpl.class, null),
ENTITY(Entity.class, Entity.NAME, EntityImpl.class, null),
RESOURCE(Resource.class, Resource.NAME, ResourceImpl.class, DummyResource.class),
FACET(Facet.class, Facet.NAME, FacetImpl.class, DummyFacet.class),
RELATION_ELEMENT(RelationElement.class, RelationElement.NAME, RelationElementImpl.class, null),
RELATION_TYPE(RelationType.class, RelationType.NAME, RelationTypeImpl.class, null),
RELATION(Relation.class, Relation.NAME, RelationImpl.class, null),
IS_PARENT_OF(IsParentOf.class, IsParentOf.NAME, IsParentOfImpl.class, null),
RELATION(Relation.class, Relation.NAME, RelationImpl.class, null),
IS_RELATED_TO(IsRelatedTo.class, IsRelatedTo.NAME, IsRelatedToImpl.class, DummyIsRelatedTo.class),
CONSISTS_OF(ConsistsOf.class, ConsistsOf.NAME, ConsistsOfImpl.class, null);

View File

@ -32,21 +32,11 @@ public class Discovery<E extends Element> {
protected final Set<Class<E>> discoveredElement;
protected final Set<DiscoveredElementAction<Element>> discoveredElementActions;
protected final boolean includeRootPackage;
protected boolean discovered;
public Discovery(Class<E> root) throws Exception {
this(root, true);
}
public Discovery(Class<E> root, boolean includeRootPackage) throws Exception {
this.root = root;
this.packages = new HashSet<>();
this.includeRootPackage = includeRootPackage;
if(includeRootPackage) {
addPackage(root.getPackage());
}
this.discovered = false;
this.discoveredElement = new TreeSet<>();
this.discoveredElementActions = new TreeSet<>();
@ -68,9 +58,6 @@ public class Discovery<E extends Element> {
}
public void addPackage(Package p) {
if(!includeRootPackage && p==root.getPackage()) {
return;
}
packages.add(p);
}
@ -79,11 +66,7 @@ public class Discovery<E extends Element> {
addPackage(p);
}
}
public void addRegistrationProvider(RegistrationProvider registrationProvider) {
addPackages(registrationProvider.getPackagesToRegister());
}
@SuppressWarnings("unchecked")
protected void add(Class<E> clz) throws Exception {
if(!discoveredElement.contains(clz)) {

View File

@ -0,0 +1,34 @@
package org.gcube.informationsystem.discovery;
import java.util.Collection;
import java.util.HashSet;
import java.util.Set;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.Element;
public class ISModelRegistrationProvider implements RegistrationProvider {
public static final String IS_MODEL_NAME = "Information System (IS) Model";
private static Set<Package> packages;
static {
packages = new HashSet<>();
for(AccessType accessType : RegistrationProvider.getAccessTypes()) {
Class<Element> clz = accessType.getTypeClass();
packages.add(clz.getPackage());
}
}
@Override
public Collection<Package> getPackagesToRegister() {
return ISModelRegistrationProvider.packages;
}
@Override
public String getModelName() {
return IS_MODEL_NAME;
}
}

View File

@ -1,6 +1,10 @@
package org.gcube.informationsystem.discovery;
import java.util.List;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;
import org.gcube.informationsystem.base.reference.AccessType;
/**
* Any model requires to register the defined types.
@ -22,6 +26,16 @@ public interface RegistrationProvider {
* This method must return the list of packages to be registered
* @return the list of packages to register
*/
public List<Package> getPackagesToRegister();
public Collection<Package> getPackagesToRegister();
public static Set<AccessType> getAccessTypes() {
Set<AccessType> accessTypes = new LinkedHashSet<>();
accessTypes.add(AccessType.PROPERTY);
accessTypes.add(AccessType.RESOURCE);
accessTypes.add(AccessType.FACET);
accessTypes.add(AccessType.IS_RELATED_TO);
accessTypes.add(AccessType.CONSISTS_OF);
return accessTypes;
}
}

View File

@ -1,50 +0,0 @@
package org.gcube.informationsystem.discovery.knowledge;
import java.util.HashMap;
import java.util.Map;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.discovery.Discovery;
import org.gcube.informationsystem.discovery.RegistrationProvider;
import org.gcube.informationsystem.serialization.ElementMappingAction;
import org.gcube.informationsystem.tree.AddElementToTreeAction;
import org.gcube.informationsystem.tree.Tree;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ModelExtensionKnowledge {
protected RegistrationProvider registrationProvider;
protected Map<AccessType, Tree<Class<Element>>> trees;
public ModelExtensionKnowledge(RegistrationProvider registrationProvider) {
this.registrationProvider = registrationProvider;
this.trees = new HashMap<>();
}
public void createKnowledge() throws Exception {
AccessType[] modelTypes = AccessType.getModelTypes();
for(AccessType accessType : modelTypes) {
Class<Element> clz = accessType.getTypeClass();
Tree<Class<Element>> tree = new Tree<>(clz, null);
trees.put(accessType, tree);
AddElementToTreeAction aetta = new AddElementToTreeAction(tree);
Discovery<? extends Element> discovery = new Discovery<>(clz, false);
discovery.addRegistrationProvider(registrationProvider);
discovery.addDiscoveredElementActions(new ElementMappingAction());
discovery.addDiscoveredElementActions(aetta);
discovery.discover();
}
}
}

View File

@ -1,9 +1,15 @@
package org.gcube.informationsystem.discovery.knowledge;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.discovery.Discovery;
import org.gcube.informationsystem.discovery.RegistrationProvider;
import org.gcube.informationsystem.serialization.ElementMappingAction;
import org.gcube.informationsystem.tree.AddElementToTreeAction;
import org.gcube.informationsystem.tree.Tree;
/**
@ -11,8 +17,46 @@ import org.gcube.informationsystem.tree.Tree;
*/
public class ModelKnowledge {
protected RegistrationProvider registrationProvider;
protected Map<AccessType, Tree<Class<Element>>> trees;
protected Map<AccessType, Discovery<? extends Element>> discoveries;
public ModelKnowledge(RegistrationProvider registrationProvider) {
this.registrationProvider = registrationProvider;
this.trees = new HashMap<>();
this.discoveries = new HashMap<>();
}
public void createKnowledge() throws Exception {
Set<AccessType> modelTypes = RegistrationProvider.getAccessTypes();
for(AccessType accessType : modelTypes) {
Class<Element> clz = accessType.getTypeClass();
Tree<Class<Element>> tree = new Tree<>(clz, null);
trees.put(accessType, tree);
AddElementToTreeAction aetta = new AddElementToTreeAction(tree);
Discovery<? extends Element> discovery = new Discovery<>(clz);
discoveries.put(accessType, discovery);
discovery.addPackages(registrationProvider.getPackagesToRegister());
discovery.addDiscoveredElementActions(new ElementMappingAction());
discovery.addDiscoveredElementActions(aetta);
discovery.discover();
}
}
public Map<AccessType, Tree<Class<Element>>> getTrees() {
return trees;
}
public void setTrees(Map<AccessType, Tree<Class<Element>>> trees) {
this.trees = trees;
}
}

View File

@ -9,9 +9,10 @@ import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.ServiceLoader;
import java.util.Set;
import org.gcube.com.fasterxml.jackson.core.JsonGenerationException;
import org.gcube.com.fasterxml.jackson.core.JsonParseException;
@ -28,9 +29,7 @@ import org.gcube.com.fasterxml.jackson.databind.node.ObjectNode;
import org.gcube.com.fasterxml.jackson.databind.node.TextNode;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.discovery.RegistrationProvider;
import org.gcube.informationsystem.tobereplaced.DiscoveryUtility;
import org.gcube.informationsystem.discovery.DiscoveredElementAction;
import org.gcube.informationsystem.discovery.Discovery;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.types.reference.Type;
import org.slf4j.Logger;
@ -73,7 +72,6 @@ public abstract class ElementMapper {
ElementMapper.registerSubtype(tdClz);
packages.add(tdClz.getPackage());
AccessType[] accessTypes = AccessType.values();
for(AccessType accessType : accessTypes) {
@SuppressWarnings("rawtypes")
@ -89,33 +87,25 @@ public abstract class ElementMapper {
}
}
registerPackages(packages);
Set<AccessType> baseTypes = new LinkedHashSet<>();
baseTypes.add(AccessType.PROPERTY_ELEMENT);
baseTypes.add(AccessType.ENTITY_ELEMENT);
baseTypes.add(AccessType.RELATION_ELEMENT);
ServiceLoader<? extends RegistrationProvider> regsitrationProviders = ServiceLoader
.load(RegistrationProvider.class);
for(RegistrationProvider registrationProvider : regsitrationProviders) {
registerPackages(registrationProvider.getPackagesToRegister());
for(AccessType accessType : baseTypes) {
Class<Element> clz = accessType.getTypeClass();
try {
Discovery<? extends Element> discovery = new Discovery<>(clz);
discovery.addPackages(packages);
discovery.addDiscoveredElementActions(new ElementMappingAction());
discovery.discover();
}catch (Exception e) {
throw new RuntimeException(e);
}
}
}
public static void registerPackages(List<Package> packages) {
DiscoveredElementAction<Element> schemaAction = new ElementMappingAction();
try {
DiscoveryUtility.manageISM(schemaAction, packages);
} catch(Exception e) {
logger.error("Error registering types", e);
}
}
public static void registerPackages(Package... packages) {
DiscoveredElementAction schemaAction = new ElementMappingAction();
try {
DiscoveryUtility.manageISM(schemaAction, packages);
} catch(Exception e) {
logger.error("Error registering types", e);
}
}
public static <El extends Element> void registerSubtypes(Class<El> clz, Class<El> implementationClass) {