Improved solution
This commit is contained in:
parent
e403b2459c
commit
d1d9ae4a4a
|
@ -81,6 +81,7 @@ public enum AccessType {
|
|||
private static Logger logger = LoggerFactory.getLogger(AccessType.class);
|
||||
|
||||
private static AccessType[] modelTypes;
|
||||
private static AccessType[] erTypes;
|
||||
private static Set<String> names;
|
||||
|
||||
static {
|
||||
|
@ -96,6 +97,11 @@ public enum AccessType {
|
|||
AccessType.RESOURCE, AccessType.FACET,
|
||||
AccessType.IS_RELATED_TO, AccessType.CONSISTS_OF
|
||||
};
|
||||
|
||||
erTypes = new AccessType[] {
|
||||
AccessType.RESOURCE, AccessType.FACET,
|
||||
AccessType.IS_RELATED_TO, AccessType.CONSISTS_OF
|
||||
};
|
||||
}
|
||||
|
||||
private final Class<? extends Element> clz;
|
||||
|
@ -190,4 +196,8 @@ public enum AccessType {
|
|||
return modelTypes;
|
||||
}
|
||||
|
||||
public static AccessType[] getERTypes() {
|
||||
return erTypes;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,9 +1,11 @@
|
|||
package org.gcube.informationsystem.knowledge;
|
||||
|
||||
import java.util.AbstractMap.SimpleEntry;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.Set;
|
||||
|
@ -39,7 +41,9 @@ public class ModelKnowledge<T, NI extends NodeInformation<T>> {
|
|||
|
||||
protected NI nodeInformation;
|
||||
protected Map<AccessType, Tree<T>> trees;
|
||||
protected Map<AccessType, UsageKnowledge<?>> usageKnowledges;
|
||||
|
||||
protected UsageKnowledge<Entry<String,PropertyDefinition>> propertyUsage;
|
||||
protected Map<AccessType, UsageKnowledge<LinkedEntity>> erTypesUsage;
|
||||
|
||||
public ModelKnowledge(NI nodeInformation) {
|
||||
this.nodeInformation = nodeInformation;
|
||||
|
@ -49,7 +53,7 @@ public class ModelKnowledge<T, NI extends NodeInformation<T>> {
|
|||
private void reset() {
|
||||
|
||||
this.trees = new HashMap<>();
|
||||
this.usageKnowledges = new HashMap<>();
|
||||
this.erTypesUsage = new HashMap<>();
|
||||
|
||||
AccessType[] modelTypes = AccessType.getModelTypes();
|
||||
for(AccessType accessType : modelTypes) {
|
||||
|
@ -58,27 +62,26 @@ public class ModelKnowledge<T, NI extends NodeInformation<T>> {
|
|||
Tree<T> tree = new Tree<>(t, nodeInformation);
|
||||
trees.put(accessType, tree);
|
||||
|
||||
UsageKnowledge<?> usageKnowledge;
|
||||
if(accessType == AccessType.PROPERTY) {
|
||||
/*
|
||||
* In this case we get the type which uses such a property
|
||||
* and the property definition.
|
||||
* In this way we have a complete overview of the usege of the
|
||||
* In this way we have a complete overview of the usage of the
|
||||
* property type
|
||||
*/
|
||||
usageKnowledge = new UsageKnowledge<Map.Entry<T,PropertyDefinition>>(accessType);
|
||||
propertyUsage = new UsageKnowledge<>(accessType);
|
||||
}else {
|
||||
usageKnowledge = new UsageKnowledge<LinkedEntity>(accessType);
|
||||
UsageKnowledge<LinkedEntity> usageKnowledge = new UsageKnowledge<LinkedEntity>(accessType);
|
||||
erTypesUsage.put(accessType, usageKnowledge);
|
||||
}
|
||||
usageKnowledges.put(accessType, usageKnowledge);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
protected void addUsage(LinkedEntity linkedEntity, UsageKnowledge<LinkedEntity> relationUsage, UsageKnowledge<LinkedEntity> targetEntityUsage) {
|
||||
if (linkedEntity != null) {
|
||||
@SuppressWarnings("unchecked")
|
||||
UsageKnowledge<LinkedEntity> resourceUsage = (UsageKnowledge<LinkedEntity>) usageKnowledges.get(AccessType.RESOURCE);
|
||||
UsageKnowledge<LinkedEntity> resourceUsage = erTypesUsage.get(AccessType.RESOURCE);
|
||||
String source = linkedEntity.getSource();
|
||||
resourceUsage.add(source, linkedEntity);
|
||||
String relation = linkedEntity.getRelation();
|
||||
|
@ -96,27 +99,28 @@ public class ModelKnowledge<T, NI extends NodeInformation<T>> {
|
|||
}
|
||||
}
|
||||
|
||||
protected void addPropertyUsage(T t, Set<PropertyDefinition> properties, UsageKnowledge<Map.Entry<T,PropertyDefinition>> propertyUsage) {
|
||||
protected void addPropertyUsage(T t, Set<PropertyDefinition> properties) {
|
||||
if(properties==null || properties.size()==0) {
|
||||
return;
|
||||
}
|
||||
String typeName = nodeInformation.getIdentifier(t);
|
||||
for(PropertyDefinition propertyDefinition : properties) {
|
||||
PropertyTypeName propertyTypeName = ((PropertyDefinitionImpl) propertyDefinition).getPropertyTypeName();
|
||||
if(propertyTypeName.isGenericType()) {
|
||||
SimpleEntry<T,PropertyDefinition> entry = new SimpleEntry<>(t, propertyDefinition);
|
||||
SimpleEntry<String,PropertyDefinition> entry = new SimpleEntry<>(typeName, propertyDefinition);
|
||||
propertyUsage.add(propertyTypeName.getGenericClassName(), entry);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected void addMetadataUsage(T t, UsageKnowledge<Entry<T, PropertyDefinition>> propertyUsage) {
|
||||
protected void addMetadataUsage(T t) {
|
||||
Type type = TypeMapper.createTypeDefinition(EntityElement.class);
|
||||
addPropertyUsage(t, type.getProperties(), propertyUsage);
|
||||
addPropertyUsage(t, type.getProperties());
|
||||
}
|
||||
|
||||
protected void addPropagationConstraintUsage(T t, UsageKnowledge<Entry<T, PropertyDefinition>> propertyUsage) {
|
||||
protected void addPropagationConstraintUsage(T t) {
|
||||
Type type = TypeMapper.createTypeDefinition(Relation.class);
|
||||
addPropertyUsage(t, type.getProperties(), propertyUsage);
|
||||
addPropertyUsage(t, type.getProperties());
|
||||
}
|
||||
|
||||
public void addAllType(Collection<T> types) {
|
||||
|
@ -143,17 +147,10 @@ public class ModelKnowledge<T, NI extends NodeInformation<T>> {
|
|||
Tree<T> tree = trees.get(accessType);
|
||||
tree.addNode(t);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
UsageKnowledge<Map.Entry<T,PropertyDefinition>> propertyUsage = (UsageKnowledge<Map.Entry<T,PropertyDefinition>>) usageKnowledges.get(AccessType.PROPERTY);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
UsageKnowledge<LinkedEntity> resourceUsage = (UsageKnowledge<LinkedEntity>) usageKnowledges.get(AccessType.RESOURCE);
|
||||
@SuppressWarnings("unchecked")
|
||||
UsageKnowledge<LinkedEntity> facetUsage = (UsageKnowledge<LinkedEntity>) usageKnowledges.get(AccessType.FACET);
|
||||
@SuppressWarnings("unchecked")
|
||||
UsageKnowledge<LinkedEntity> isRelatedToUsage = (UsageKnowledge<LinkedEntity>) usageKnowledges.get(AccessType.IS_RELATED_TO);
|
||||
@SuppressWarnings("unchecked")
|
||||
UsageKnowledge<LinkedEntity> consistsOfUsage = (UsageKnowledge<LinkedEntity>) usageKnowledges.get(AccessType.CONSISTS_OF);
|
||||
UsageKnowledge<LinkedEntity> resourceUsage = erTypesUsage.get(AccessType.RESOURCE);
|
||||
UsageKnowledge<LinkedEntity> facetUsage = erTypesUsage.get(AccessType.FACET);
|
||||
UsageKnowledge<LinkedEntity> isRelatedToUsage = erTypesUsage.get(AccessType.IS_RELATED_TO);
|
||||
UsageKnowledge<LinkedEntity> consistsOfUsage = erTypesUsage.get(AccessType.CONSISTS_OF);
|
||||
|
||||
switch (accessType) {
|
||||
case PROPERTY:
|
||||
|
@ -170,35 +167,35 @@ public class ModelKnowledge<T, NI extends NodeInformation<T>> {
|
|||
* We want to have a reference to all Base type
|
||||
*/
|
||||
if(typeName.compareTo(Resource.NAME)==0) {
|
||||
addMetadataUsage(t, propertyUsage);
|
||||
addMetadataUsage(t);
|
||||
}
|
||||
break;
|
||||
|
||||
case FACET:
|
||||
FacetType facetType = (FacetType) t;
|
||||
if(typeName.compareTo(Facet.NAME)==0) {
|
||||
addMetadataUsage(t, propertyUsage);
|
||||
addMetadataUsage(t);
|
||||
}
|
||||
addPropertyUsage(t, facetType.getProperties(), propertyUsage);
|
||||
addPropertyUsage(t, facetType.getProperties());
|
||||
|
||||
break;
|
||||
|
||||
case IS_RELATED_TO:
|
||||
IsRelatedToType isRelatedToType = (IsRelatedToType) t;
|
||||
if(typeName.compareTo(IsRelatedTo.NAME)==0) {
|
||||
addMetadataUsage(t, propertyUsage);
|
||||
addPropagationConstraintUsage(t, propertyUsage);
|
||||
addMetadataUsage(t);
|
||||
addPropagationConstraintUsage(t);
|
||||
}
|
||||
addPropertyUsage(t, isRelatedToType.getProperties(), propertyUsage);
|
||||
addPropertyUsage(t, isRelatedToType.getProperties());
|
||||
break;
|
||||
|
||||
case CONSISTS_OF:
|
||||
ConsistsOfType consistsOfType = (ConsistsOfType) t;
|
||||
if(typeName.compareTo(ConsistsOf.NAME)==0) {
|
||||
addMetadataUsage(t, propertyUsage);
|
||||
addPropagationConstraintUsage(t, propertyUsage);
|
||||
addMetadataUsage(t);
|
||||
addPropagationConstraintUsage(t);
|
||||
}
|
||||
addPropertyUsage(t, consistsOfType.getProperties(), propertyUsage);
|
||||
addPropertyUsage(t, consistsOfType.getProperties());
|
||||
break;
|
||||
|
||||
default:
|
||||
|
@ -211,7 +208,47 @@ public class ModelKnowledge<T, NI extends NodeInformation<T>> {
|
|||
return trees.get(accessType);
|
||||
}
|
||||
|
||||
public UsageKnowledge<?> getUsageKnowledge(AccessType accessType) {
|
||||
return usageKnowledges.get(accessType);
|
||||
public UsageKnowledge<?> getModelTypesUsage(AccessType accessType) {
|
||||
List<AccessType> modelTypes = Arrays.asList(AccessType.getModelTypes());
|
||||
if(!modelTypes.contains(accessType)) {
|
||||
throw new RuntimeException("Only ModelTypes are allowed, i.e. " + modelTypes.toString());
|
||||
}
|
||||
if(accessType == AccessType.PROPERTY) {
|
||||
return propertyUsage;
|
||||
}else {
|
||||
return erTypesUsage.get(accessType);
|
||||
}
|
||||
}
|
||||
|
||||
public UsageKnowledge<LinkedEntity> getERTypesUsage(AccessType accessType) {
|
||||
List<AccessType> erTypes = Arrays.asList(AccessType.getERTypes());
|
||||
if(!erTypes.contains(accessType)) {
|
||||
throw new RuntimeException("Only ERTypes are allowed, i.e. " + erTypes.toString());
|
||||
}
|
||||
return erTypesUsage.get(accessType);
|
||||
}
|
||||
|
||||
public UsageKnowledge<Entry<String,PropertyDefinition>> getPropertyUsage(){
|
||||
return propertyUsage;
|
||||
}
|
||||
|
||||
public UsageKnowledge<LinkedEntity> getResourceUsage() {
|
||||
return erTypesUsage.get(AccessType.RESOURCE);
|
||||
}
|
||||
|
||||
public UsageKnowledge<LinkedEntity> getFacetUsage() {
|
||||
return erTypesUsage.get(AccessType.FACET);
|
||||
}
|
||||
|
||||
public UsageKnowledge<LinkedEntity> getIsRelatedToUsage() {
|
||||
return erTypesUsage.get(AccessType.IS_RELATED_TO);
|
||||
}
|
||||
|
||||
public UsageKnowledge<LinkedEntity> getConsistsOfUsage() {
|
||||
return erTypesUsage.get(AccessType.CONSISTS_OF);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -6,10 +6,9 @@ import java.net.URL;
|
|||
import java.nio.charset.Charset;
|
||||
import java.nio.file.Files;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
import org.gcube.com.fasterxml.jackson.databind.JsonNode;
|
||||
import org.gcube.com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import org.gcube.informationsystem.base.reference.AccessType;
|
||||
import org.gcube.informationsystem.queries.templates.QueryTemplateTest;
|
||||
import org.gcube.informationsystem.tree.Node;
|
||||
|
@ -17,6 +16,8 @@ import org.gcube.informationsystem.tree.NodeElaborator;
|
|||
import org.gcube.informationsystem.tree.Tree;
|
||||
import org.gcube.informationsystem.types.TypeMapper;
|
||||
import org.gcube.informationsystem.types.reference.Type;
|
||||
import org.gcube.informationsystem.types.reference.properties.LinkedEntity;
|
||||
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition;
|
||||
import org.junit.Test;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
@ -77,7 +78,7 @@ public class ModelKnowledgeTest{
|
|||
|
||||
for(AccessType modelType : modelTypes) {
|
||||
Tree<Type> tree = modelKnowledge.getTree(modelType);
|
||||
UsageKnowledge<?> usageKnowledge = modelKnowledge.getUsageKnowledge(modelType);
|
||||
UsageKnowledge<?> usageKnowledge = modelKnowledge.getModelTypesUsage(modelType);
|
||||
|
||||
tree.elaborate(new NodeElaborator<Type>() {
|
||||
|
||||
|
@ -88,11 +89,19 @@ public class ModelKnowledgeTest{
|
|||
stringBuffer.append(Node.INDENTATION);
|
||||
}
|
||||
|
||||
String typeName = node.getNodeElement().getName();
|
||||
List<?> usage = usageKnowledge.getUsage(typeName);
|
||||
Type type = node.getNodeElement();
|
||||
String typeName = type.getName();
|
||||
|
||||
logger.info("{}- {}{} {}", stringBuffer.toString(), typeName, level==0 ? " (ROOT) " : "", usage!=null ? "known usage from definitions " + usage : "No known usage from definitions");
|
||||
|
||||
if(type.getAccessType()!=AccessType.PROPERTY) {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<LinkedEntity> usage = (List<LinkedEntity>) usageKnowledge.getUsage(typeName);
|
||||
logger.info("{}- {}{} {}", stringBuffer.toString(), typeName, level==0 ? " (ROOT) " : "", usage!=null ? "known usage from definitions " + usage : "No known usage from definitions");
|
||||
}else {
|
||||
@SuppressWarnings("unchecked")
|
||||
List<Map.Entry<String,PropertyDefinition>> usage = (List<Entry<String, PropertyDefinition>>) usageKnowledge.getUsage(typeName);
|
||||
logger.info("{}- {}{} {}", stringBuffer.toString(), typeName, level==0 ? " (ROOT) " : "", usage!=null ? "known usage from definitions " + usage : "No known usage from definitions");
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue