Improved code

This commit is contained in:
luca.frosini 2023-10-26 16:35:03 +02:00
parent d2b715a318
commit 9a32f3c534
6 changed files with 56 additions and 62 deletions

View File

@ -3,7 +3,6 @@ package org.gcube.informationsystem.discovery.knowledge;
import java.util.LinkedHashSet;
import java.util.Set;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.base.reference.Element;
import org.gcube.informationsystem.tree.NodeInformation;
import org.gcube.informationsystem.types.TypeMapper;
@ -36,14 +35,4 @@ public class ClassInformation implements NodeInformation<Class<Element>> {
return ret;
}
@Override
public AccessType getAccessType(Class<Element> clz) {
return AccessType.getAccessType(clz);
}
@Override
public Class<Element> getRoot(AccessType accessType) {
return accessType.getTypeClass();
}
}

View File

@ -18,7 +18,6 @@ import org.gcube.informationsystem.model.reference.entities.Resource;
import org.gcube.informationsystem.model.reference.relations.ConsistsOf;
import org.gcube.informationsystem.model.reference.relations.IsRelatedTo;
import org.gcube.informationsystem.model.reference.relations.Relation;
import org.gcube.informationsystem.tree.NodeInformation;
import org.gcube.informationsystem.tree.Tree;
import org.gcube.informationsystem.types.PropertyTypeName;
import org.gcube.informationsystem.types.TypeMapper;
@ -36,18 +35,18 @@ import org.slf4j.LoggerFactory;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class ModelKnowledge<T, NI extends NodeInformation<T>> {
public class ModelKnowledge<T, TI extends TypeInformation<T>> {
private static final Logger logger = LoggerFactory.getLogger(ModelKnowledge.class);
protected NI nodeInformation;
protected TI typeInformation;
protected Map<AccessType, Tree<T>> trees;
protected UsageKnowledge<Entry<String,PropertyDefinition>> propertyUsage;
protected Map<AccessType, UsageKnowledge<LinkedEntity>> erTypesUsage;
public ModelKnowledge(NI nodeInformation) {
this.nodeInformation = nodeInformation;
public ModelKnowledge(TI typeInformation) {
this.typeInformation = typeInformation;
reset();
}
@ -58,9 +57,9 @@ public class ModelKnowledge<T, NI extends NodeInformation<T>> {
AccessType[] modelTypes = AccessType.getModelTypes();
for(AccessType accessType : modelTypes) {
T t = nodeInformation.getRoot(accessType);
T t = typeInformation.getRoot(accessType);
Tree<T> tree = new Tree<>(t, nodeInformation);
Tree<T> tree = new Tree<>(t, typeInformation);
trees.put(accessType, tree);
if(accessType == AccessType.PROPERTY) {
@ -104,7 +103,7 @@ public class ModelKnowledge<T, NI extends NodeInformation<T>> {
if(properties==null || properties.size()==0) {
return;
}
String typeName = nodeInformation.getIdentifier(t);
String typeName = typeInformation.getIdentifier(t);
for(PropertyDefinition propertyDefinition : properties) {
PropertyTypeName propertyTypeName = ((PropertyDefinitionImpl) propertyDefinition).getPropertyTypeName();
if(propertyTypeName.isGenericType()) {
@ -133,7 +132,7 @@ public class ModelKnowledge<T, NI extends NodeInformation<T>> {
Set<T> toRetry = new HashSet<>();
for(T t : types) {
logger.trace("Going to add {}", nodeInformation.getIdentifier(t));
logger.trace("Going to add {}", typeInformation.getIdentifier(t));
try {
addType(t);
}catch (RuntimeException e) {
@ -147,8 +146,8 @@ public class ModelKnowledge<T, NI extends NodeInformation<T>> {
}
public void addType(T t) {
AccessType accessType = nodeInformation.getAccessType(t);
String typeName = nodeInformation.getIdentifier(t);
AccessType accessType = typeInformation.getAccessType(t);
String typeName = typeInformation.getIdentifier(t);
Tree<T> tree = trees.get(accessType);
tree.addNode(t);

View File

@ -1,40 +1,12 @@
package org.gcube.informationsystem.knowledge;
import java.util.LinkedHashSet;
import java.util.Set;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.tree.NodeInformation;
import org.gcube.informationsystem.types.TypeMapper;
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(type.getName().compareTo(root.getName())==0) {
return new LinkedHashSet<>();
}
return type.getExtendedTypes();
}
@Override
public AccessType getAccessType(Type type) {
return type.getAccessType();
}
@Override
public Type getRoot(AccessType accessType) {
return TypeMapper.createTypeDefinition(accessType.getTypeClass());
}
public interface TypeInformation<T> extends NodeInformation<T> {
public AccessType getAccessType(T t);
public T getRoot(AccessType accessType);
}

View File

@ -2,16 +2,10 @@ package org.gcube.informationsystem.tree;
import java.util.Set;
import org.gcube.informationsystem.base.reference.AccessType;
public interface NodeInformation<T> {
public String getIdentifier(T t);
public Set<String> getParentIdentifiers(T root, T t);
public AccessType getAccessType(T t);
public T getRoot(AccessType accessType);
}

View File

@ -0,0 +1,39 @@
package org.gcube.informationsystem.types.knowledge;
import java.util.LinkedHashSet;
import java.util.Set;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.types.reference.Type;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class TypeInformation implements org.gcube.informationsystem.knowledge.TypeInformation<Type> {
@Override
public String getIdentifier(Type type) {
return type.getName();
}
@Override
public Set<String> getParentIdentifiers(Type root, Type type) {
if(type.getName().compareTo(root.getName())==0) {
return new LinkedHashSet<>();
}
return type.getExtendedTypes();
}
@Override
public AccessType getAccessType(Type type) {
return type.getAccessType();
}
@Override
public Type getRoot(AccessType accessType) {
return TypeMapper.createTypeDefinition(accessType.getTypeClass());
}
}

View File

@ -15,6 +15,7 @@ import org.gcube.informationsystem.tree.Node;
import org.gcube.informationsystem.tree.NodeElaborator;
import org.gcube.informationsystem.tree.Tree;
import org.gcube.informationsystem.types.TypeMapper;
import org.gcube.informationsystem.types.knowledge.TypeInformation;
import org.gcube.informationsystem.types.reference.Type;
import org.gcube.informationsystem.types.reference.properties.LinkedEntity;
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition;