Improving ModelKnowledge creation
This commit is contained in:
parent
d7d609bbf8
commit
5321570886
|
@ -3,9 +3,9 @@
|
|||
*/
|
||||
package org.gcube.informationsystem.discovery.knowledge;
|
||||
|
||||
import org.gcube.informationsystem.base.reference.AccessType;
|
||||
import org.gcube.informationsystem.base.reference.Element;
|
||||
import org.gcube.informationsystem.discovery.DiscoveredElementAction;
|
||||
import org.gcube.informationsystem.model.reference.entities.Resource;
|
||||
import org.gcube.informationsystem.tree.Tree;
|
||||
import org.gcube.informationsystem.types.TypeMapper;
|
||||
import org.gcube.informationsystem.types.reference.Type;
|
||||
|
@ -18,6 +18,25 @@ public class AddElementToTreeAction implements DiscoveredElementAction<Element>
|
|||
|
||||
protected Tree<Class<Element>> tree;
|
||||
|
||||
protected UsageKnowledge facetKnowledge;
|
||||
protected UsageKnowledge resourceKnowledge;
|
||||
|
||||
public UsageKnowledge getFacetKnowledge() {
|
||||
return facetKnowledge;
|
||||
}
|
||||
|
||||
public void setFacetKnowledge(UsageKnowledge facetKnowledge) {
|
||||
this.facetKnowledge = facetKnowledge;
|
||||
}
|
||||
|
||||
public UsageKnowledge getResourceKnowledge() {
|
||||
return resourceKnowledge;
|
||||
}
|
||||
|
||||
public void setResourceKnowledge(UsageKnowledge resourceKnowledge) {
|
||||
this.resourceKnowledge = resourceKnowledge;
|
||||
}
|
||||
|
||||
public AddElementToTreeAction(Tree<Class<Element>> tree) {
|
||||
this.tree = tree;
|
||||
}
|
||||
|
@ -25,19 +44,20 @@ public class AddElementToTreeAction implements DiscoveredElementAction<Element>
|
|||
@Override
|
||||
public void analizeElement(Class<Element> e) throws Exception {
|
||||
tree.addNode(e);
|
||||
Type type = TypeMapper.createTypeDefinition(e);
|
||||
createUsageKnowledge(type);
|
||||
createUsageKnowledge(e);
|
||||
}
|
||||
|
||||
protected void createUsageKnowledge(Type type) {
|
||||
if (type.getAccessType() != AccessType.RESOURCE) {
|
||||
protected void createUsageKnowledge(Class<Element> e) {
|
||||
if (e.isAssignableFrom(Resource.class)) {
|
||||
return;
|
||||
}
|
||||
ResourceType rt = (ResourceType) type;
|
||||
UsageKnowledge fk = UsageKnowledge.getFacetKnowledge();
|
||||
fk.addAll(rt.getFacets());
|
||||
UsageKnowledge rk = UsageKnowledge.getResourceKnowledge();
|
||||
rk.addAll(rt.getResources());
|
||||
|
||||
if(facetKnowledge!=null && resourceKnowledge!=null) {
|
||||
Type type = TypeMapper.createTypeDefinition(e);
|
||||
ResourceType rt = (ResourceType) type;
|
||||
facetKnowledge.addAll(rt.getFacets());
|
||||
resourceKnowledge.addAll(rt.getResources());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,10 @@
|
|||
package org.gcube.informationsystem.discovery.knowledge;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.ServiceLoader;
|
||||
import java.util.Set;
|
||||
|
@ -22,19 +25,49 @@ public class ModelKnowledge {
|
|||
|
||||
private static final Logger logger = LoggerFactory.getLogger(ModelKnowledge.class);
|
||||
|
||||
private static ModelKnowledge modelKnowledge;
|
||||
|
||||
public static ModelKnowledge getGlobalModelKnowledgeInstance() {
|
||||
if(modelKnowledge==null) {
|
||||
modelKnowledge = new ModelKnowledge();
|
||||
ServiceLoader<? extends RegistrationProvider> registrationProviders = ServiceLoader
|
||||
.load(RegistrationProvider.class);
|
||||
for(RegistrationProvider rp : registrationProviders) {
|
||||
modelKnowledge.addRegistrationProvider(rp);
|
||||
}
|
||||
}
|
||||
return modelKnowledge;
|
||||
}
|
||||
|
||||
protected boolean created;
|
||||
|
||||
public static ModelKnowledge getInstance() {
|
||||
return new ModelKnowledge();
|
||||
}
|
||||
protected Map<AccessType,UsageKnowledge> usageKnowledges;
|
||||
|
||||
protected Map<AccessType, Tree<Class<Element>>> trees;
|
||||
protected Map<AccessType, Discovery<? extends Element>> discoveries;
|
||||
protected List<RegistrationProvider> registrationProviders;
|
||||
|
||||
protected ModelKnowledge() {
|
||||
public ModelKnowledge() {
|
||||
this.trees = new HashMap<>();
|
||||
this.discoveries = new HashMap<>();
|
||||
this.created = false;
|
||||
this.usageKnowledges = new HashMap<>();
|
||||
this.registrationProviders = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addRegistrationProvider(RegistrationProvider rp) {
|
||||
registrationProviders.add(rp);
|
||||
}
|
||||
|
||||
public void addRegistrationProviders(Collection<RegistrationProvider> rps) {
|
||||
registrationProviders.addAll(rps);
|
||||
}
|
||||
|
||||
public UsageKnowledge getUsageKnowledge(AccessType accessType) {
|
||||
if(!created) {
|
||||
throw new RuntimeException("You must invoke createKnowledge() first");
|
||||
}
|
||||
return usageKnowledges.get(accessType);
|
||||
}
|
||||
|
||||
public synchronized void createKnowledge(boolean force) throws Exception {
|
||||
|
@ -53,15 +86,14 @@ public class ModelKnowledge {
|
|||
return;
|
||||
}
|
||||
|
||||
AccessType[] modelTypes = AccessType.getModelTypes();
|
||||
|
||||
Set<Package> allPackages = new HashSet<>();
|
||||
ServiceLoader<? extends RegistrationProvider> registrationProviders = ServiceLoader
|
||||
.load(RegistrationProvider.class);
|
||||
for(RegistrationProvider rp : registrationProviders) {
|
||||
allPackages.addAll(rp.getPackagesToRegister());
|
||||
}
|
||||
|
||||
|
||||
AccessType[] modelTypes = AccessType.getModelTypes();
|
||||
|
||||
ClassInformation classInformation = new ClassInformation();
|
||||
|
||||
for(AccessType accessType : modelTypes) {
|
||||
|
@ -70,6 +102,15 @@ public class ModelKnowledge {
|
|||
Tree<Class<Element>> tree = new Tree<>(clz, classInformation);
|
||||
trees.put(accessType, tree);
|
||||
AddElementToTreeAction aetta = new AddElementToTreeAction(tree);
|
||||
if(accessType == AccessType.RESOURCE) {
|
||||
UsageKnowledge facetKnowledge = new UsageKnowledge();
|
||||
this.usageKnowledges.put(AccessType.FACET, facetKnowledge);
|
||||
aetta.setFacetKnowledge(facetKnowledge);
|
||||
|
||||
UsageKnowledge resourceKnowledge = new UsageKnowledge();
|
||||
this.usageKnowledges.put(AccessType.RESOURCE, resourceKnowledge);
|
||||
aetta.setResourceKnowledge(resourceKnowledge);
|
||||
}
|
||||
|
||||
Discovery<? extends Element> discovery = new Discovery<>(clz);
|
||||
discoveries.put(accessType, discovery);
|
||||
|
|
|
@ -13,27 +13,9 @@ import org.gcube.informationsystem.types.reference.properties.LinkedEntity;
|
|||
*/
|
||||
public class UsageKnowledge {
|
||||
|
||||
private static UsageKnowledge facetKnowledge;
|
||||
private static UsageKnowledge resourceKnowledge;
|
||||
|
||||
public static UsageKnowledge getFacetKnowledge() {
|
||||
if(UsageKnowledge.facetKnowledge==null) {
|
||||
UsageKnowledge.facetKnowledge = new UsageKnowledge();
|
||||
}
|
||||
return UsageKnowledge.facetKnowledge;
|
||||
}
|
||||
|
||||
public static UsageKnowledge getResourceKnowledge() {
|
||||
if(UsageKnowledge.resourceKnowledge==null) {
|
||||
UsageKnowledge.resourceKnowledge = new UsageKnowledge();
|
||||
}
|
||||
return UsageKnowledge.resourceKnowledge;
|
||||
}
|
||||
|
||||
|
||||
protected Map<String, Set<LinkedEntity>> map;
|
||||
|
||||
private UsageKnowledge(){
|
||||
public UsageKnowledge(){
|
||||
this.map = new LinkedHashMap<>();
|
||||
}
|
||||
|
||||
|
|
|
@ -107,7 +107,7 @@ public abstract class ElementMapper {
|
|||
}
|
||||
|
||||
try {
|
||||
ModelKnowledge.getInstance().createKnowledge();
|
||||
ModelKnowledge.getGlobalModelKnowledgeInstance().createKnowledge();
|
||||
}catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
|
|
|
@ -25,7 +25,7 @@ public class DiscoveryTest {
|
|||
|
||||
@Test
|
||||
public void testDiscovery() throws Exception {
|
||||
ModelKnowledge modelKnowledge = ModelKnowledge.getInstance();
|
||||
ModelKnowledge modelKnowledge = ModelKnowledge.getGlobalModelKnowledgeInstance();
|
||||
modelKnowledge.createKnowledge();
|
||||
|
||||
AccessType[] accessTypes = AccessType.getModelTypes();
|
||||
|
|
Loading…
Reference in New Issue