Improved solution

This commit is contained in:
luca.frosini 2023-10-27 18:11:58 +02:00
parent 27854806df
commit 8b7aa5660a
6 changed files with 135 additions and 6 deletions

View File

@ -192,10 +192,18 @@ public enum AccessType {
}
}
/**
* @return an array of AccessTypes containing only
* Model Types (i.e. Property, Resource, Facet, IsRelatedTo, ConsistsOf)
*/
public static AccessType[] getModelTypes() {
return modelTypes;
}
/**
* @return an array of AccessTypes containing only
* Entity/Relation Model Types (i.e. Resource, Facet, IsRelatedTo, ConsistsOf)
*/
public static AccessType[] getERTypes() {
return erTypes;
}

View File

@ -18,6 +18,7 @@ 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.Node;
import org.gcube.informationsystem.tree.Tree;
import org.gcube.informationsystem.types.PropertyTypeName;
import org.gcube.informationsystem.types.TypeMapper;
@ -44,6 +45,8 @@ public class ModelKnowledge<T, TI extends TypeInformation<T>> {
protected UsageKnowledge<Entry<String,PropertyDefinition>> propertyUsage;
protected Map<AccessType, UsageKnowledge<LinkedEntity>> erTypesUsage;
protected Map<String, AccessType> locate;
public ModelKnowledge(TI typeInformation) {
this.typeInformation = typeInformation;
@ -157,9 +160,17 @@ public class ModelKnowledge<T, TI extends TypeInformation<T>> {
AccessType accessType = typeInformation.getAccessType(t);
String typeName = typeInformation.getIdentifier(t);
if(locate.get(typeName)!=null) {
logger.trace("The type {} was already added to the ModelKnowledge", typeName);
return;
}
locate.put(typeName, accessType);
Tree<T> tree = trees.get(accessType);
tree.addNode(t);
UsageKnowledge<LinkedEntity> resourceUsage = erTypesUsage.get(AccessType.RESOURCE);
UsageKnowledge<LinkedEntity> facetUsage = erTypesUsage.get(AccessType.FACET);
UsageKnowledge<LinkedEntity> isRelatedToUsage = erTypesUsage.get(AccessType.IS_RELATED_TO);
@ -227,6 +238,8 @@ public class ModelKnowledge<T, TI extends TypeInformation<T>> {
break;
}
}
public Tree<T> getTree(AccessType accessType) {
@ -273,7 +286,31 @@ public class ModelKnowledge<T, TI extends TypeInformation<T>> {
return erTypesUsage.get(AccessType.CONSISTS_OF);
}
/**
* Return the type if it is contained in the Knowledge
* @param typeName the type we are looking for
* @return the Type
* @throws RuntimeException
*/
public T getTypeByName(String typeName) throws RuntimeException {
return getNodeByName(typeName).getNodeElement();
}
/**
* Return the node fro the type if it is contained in the Knowledge
* @param typeName the type we are looking for
* @return the Type
* @throws RuntimeException
*/
public Node<T> getNodeByName(String typeName) throws RuntimeException {
AccessType accessType = locate.get(typeName);
if(accessType==null) {
throw new RuntimeException("The type " + typeName + " is not contained in the Knowledge");
}
Tree<T> tree = trees.get(accessType);
return tree.getNodeByIdentifier(typeName);
}
}

View File

@ -0,0 +1,21 @@
package org.gcube.informationsystem.model.knowledge;
import java.util.Collection;
import org.gcube.informationsystem.base.reference.AccessType;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public interface TypesDiscoverer<T> {
/**
* This function discover all available types of a certain
* AccessType. Allowed AccessTypes are only IS Model Types
* see {@link AccessType#getModelTypes()}
* @param accessType
* @return
*/
public Collection<T> discover(AccessType accessType);
}

View File

@ -117,4 +117,8 @@ public class Tree<T> {
public void elaborate(NodeElaborator<T> nodeElaborator) throws Exception {
rootNode.elaborate(nodeElaborator);
}
public Node<T> getNodeByIdentifier(String identifier){
return locate.get(identifier);
}
}

View File

@ -1,6 +1,10 @@
package org.gcube.informationsystem.types.knowledge;
import java.util.Collection;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.model.knowledge.ModelKnowledge;
import org.gcube.informationsystem.model.knowledge.TypesDiscoverer;
import org.gcube.informationsystem.types.reference.Type;
/**
@ -8,12 +12,67 @@ import org.gcube.informationsystem.types.reference.Type;
*/
public class TypesKnowledge {
protected static ModelKnowledge<Type, TypeInformation> modelKnowledge;
private static TypesKnowledge instance;
public synchronized static ModelKnowledge<Type, TypeInformation> getModelKnowledge() {
if(modelKnowledge==null) {
modelKnowledge = new ModelKnowledge<>(new TypeInformation());
public synchronized static TypesKnowledge getInstance() {
if(instance==null) {
instance = new TypesKnowledge();
}
return instance;
}
protected boolean initialized;
protected ModelKnowledge<Type, TypeInformation> modelKnowledge;
protected TypesDiscoverer<Type> typesDiscoverer;
public TypesKnowledge() {
initialized = false;
modelKnowledge = new ModelKnowledge<>(new TypeInformation());
}
public TypesDiscoverer<Type> getTypesDiscoverer() {
return typesDiscoverer;
}
public void setTypesDiscoverer(TypesDiscoverer<Type> typesDiscoverer) {
this.typesDiscoverer = typesDiscoverer;
}
public ModelKnowledge<Type, TypeInformation> getModelKnowledge() {
if(!initialized) {
discover();
}
return modelKnowledge;
}
protected synchronized void init(boolean forceReinitialization) {
if(typesDiscoverer!=null && (initialized==false || forceReinitialization)) {
initialized = false;
modelKnowledge = new ModelKnowledge<>(new TypeInformation());
AccessType[] modelTypes = AccessType.getModelTypes();
for(AccessType modelType : modelTypes) {
Collection<Type> types = typesDiscoverer.discover(modelType);
modelKnowledge.addAllType(types);
}
initialized = true;
}
}
/**
* This method do nothing if TypesDiscoverer
* was not set.
* Otherwise initialized the ModelKnowledge
* if it was not already initialized.
* To enforce rediscovery use renew method.
*/
public void discover() {
init(false);
}
/**
* Force reinitialization of
*/
public void renew() {
init(true);
}
}

View File

@ -44,7 +44,7 @@ public class ModelKnowledgeTest{
@Test
public void test() throws Exception {
ModelKnowledge<Type, TypeInformation> modelKnowledge = TypesKnowledge.getModelKnowledge();
ModelKnowledge<Type, TypeInformation> modelKnowledge = TypesKnowledge.getInstance().getModelKnowledge();
File typesDirectory = getTypesDirectory();