79 lines
2.0 KiB
Java
79 lines
2.0 KiB
Java
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;
|
|
|
|
/**
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
*/
|
|
public class TypesKnowledge {
|
|
|
|
private static TypesKnowledge instance;
|
|
|
|
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);
|
|
}
|
|
}
|