2023-10-24 15:18:48 +02:00
|
|
|
package org.gcube.informationsystem.knowledge;
|
|
|
|
|
2023-10-25 18:01:11 +02:00
|
|
|
import java.util.AbstractMap.SimpleEntry;
|
2023-10-24 15:18:48 +02:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.HashMap;
|
2023-10-25 18:01:11 +02:00
|
|
|
import java.util.HashSet;
|
2023-10-24 15:18:48 +02:00
|
|
|
import java.util.Map;
|
2023-10-25 18:01:11 +02:00
|
|
|
import java.util.Map.Entry;
|
2023-10-24 15:18:48 +02:00
|
|
|
import java.util.Set;
|
|
|
|
|
|
|
|
import org.gcube.informationsystem.base.reference.AccessType;
|
2023-10-25 18:01:11 +02:00
|
|
|
import org.gcube.informationsystem.base.reference.entities.EntityElement;
|
|
|
|
import org.gcube.informationsystem.model.reference.entities.Facet;
|
|
|
|
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;
|
2023-10-24 15:18:48 +02:00
|
|
|
import org.gcube.informationsystem.tree.NodeInformation;
|
|
|
|
import org.gcube.informationsystem.tree.Tree;
|
2023-10-25 18:01:11 +02:00
|
|
|
import org.gcube.informationsystem.types.PropertyTypeName;
|
|
|
|
import org.gcube.informationsystem.types.TypeMapper;
|
|
|
|
import org.gcube.informationsystem.types.impl.properties.PropertyDefinitionImpl;
|
|
|
|
import org.gcube.informationsystem.types.reference.Type;
|
2023-10-24 15:18:48 +02:00
|
|
|
import org.gcube.informationsystem.types.reference.entities.FacetType;
|
|
|
|
import org.gcube.informationsystem.types.reference.entities.ResourceType;
|
|
|
|
import org.gcube.informationsystem.types.reference.properties.LinkedEntity;
|
|
|
|
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition;
|
|
|
|
import org.gcube.informationsystem.types.reference.relations.ConsistsOfType;
|
|
|
|
import org.gcube.informationsystem.types.reference.relations.IsRelatedToType;
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
|
|
*/
|
|
|
|
public class ModelKnowledge<T, NI extends NodeInformation<T>> {
|
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(ModelKnowledge.class);
|
|
|
|
|
|
|
|
protected NI nodeInformation;
|
|
|
|
protected Map<AccessType, Tree<T>> trees;
|
|
|
|
protected Map<AccessType, UsageKnowledge<?>> usageKnowledges;
|
|
|
|
|
2023-10-25 18:01:11 +02:00
|
|
|
public ModelKnowledge(NI nodeInformation) {
|
|
|
|
this.nodeInformation = nodeInformation;
|
2023-10-24 15:18:48 +02:00
|
|
|
reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void reset() {
|
|
|
|
|
|
|
|
this.trees = new HashMap<>();
|
|
|
|
this.usageKnowledges = new HashMap<>();
|
|
|
|
|
|
|
|
AccessType[] modelTypes = AccessType.getModelTypes();
|
|
|
|
for(AccessType accessType : modelTypes) {
|
|
|
|
T t = nodeInformation.getRoot(accessType);
|
|
|
|
|
|
|
|
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
|
|
|
|
* property type
|
|
|
|
*/
|
2023-10-25 18:01:11 +02:00
|
|
|
usageKnowledge = new UsageKnowledge<Map.Entry<T,PropertyDefinition>>(accessType);
|
2023-10-24 15:18:48 +02:00
|
|
|
}else {
|
|
|
|
usageKnowledge = new UsageKnowledge<LinkedEntity>(accessType);
|
|
|
|
}
|
|
|
|
usageKnowledges.put(accessType, usageKnowledge);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-10-25 18:01:11 +02:00
|
|
|
protected void addUsage(LinkedEntity linkedEntity, UsageKnowledge<LinkedEntity> relationUsage, UsageKnowledge<LinkedEntity> targetEntityUsage) {
|
2023-10-24 15:18:48 +02:00
|
|
|
if (linkedEntity != null) {
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
UsageKnowledge<LinkedEntity> resourceUsage = (UsageKnowledge<LinkedEntity>) usageKnowledges.get(AccessType.RESOURCE);
|
|
|
|
String source = linkedEntity.getSource();
|
|
|
|
resourceUsage.add(source, linkedEntity);
|
|
|
|
String relation = linkedEntity.getRelation();
|
|
|
|
relationUsage.add(relation, linkedEntity);
|
|
|
|
String target = linkedEntity.getTarget();
|
|
|
|
targetEntityUsage.add(target, linkedEntity);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 18:01:11 +02:00
|
|
|
protected void addAllUsage(Collection<LinkedEntity> linkedEntities, UsageKnowledge<LinkedEntity> relationUsage, UsageKnowledge<LinkedEntity> targetEntityUsage) {
|
2023-10-24 15:18:48 +02:00
|
|
|
if(linkedEntities!=null) {
|
|
|
|
for(LinkedEntity le : linkedEntities) {
|
2023-10-25 18:01:11 +02:00
|
|
|
addUsage(le, relationUsage, targetEntityUsage);
|
2023-10-24 15:18:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-10-25 18:01:11 +02:00
|
|
|
protected void addPropertyUsage(T t, Set<PropertyDefinition> properties, UsageKnowledge<Map.Entry<T,PropertyDefinition>> propertyUsage) {
|
|
|
|
if(properties==null || properties.size()==0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for(PropertyDefinition propertyDefinition : properties) {
|
|
|
|
PropertyTypeName propertyTypeName = ((PropertyDefinitionImpl) propertyDefinition).getPropertyTypeName();
|
|
|
|
if(propertyTypeName.isGenericType()) {
|
|
|
|
SimpleEntry<T,PropertyDefinition> entry = new SimpleEntry<>(t, propertyDefinition);
|
|
|
|
propertyUsage.add(propertyTypeName.getGenericClassName(), entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void addMetadataUsage(T t, UsageKnowledge<Entry<T, PropertyDefinition>> propertyUsage) {
|
|
|
|
Type type = TypeMapper.createTypeDefinition(EntityElement.class);
|
|
|
|
addPropertyUsage(t, type.getProperties(), propertyUsage);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected void addPropagationConstraintUsage(T t, UsageKnowledge<Entry<T, PropertyDefinition>> propertyUsage) {
|
|
|
|
Type type = TypeMapper.createTypeDefinition(Relation.class);
|
|
|
|
addPropertyUsage(t, type.getProperties(), propertyUsage);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void addAllType(Collection<T> types) {
|
|
|
|
Set<T> toRetry = new HashSet<>();
|
|
|
|
|
|
|
|
for(T t : types) {
|
|
|
|
logger.trace("Going to add {}", nodeInformation.getIdentifier(t));
|
|
|
|
try {
|
|
|
|
addType(t);
|
|
|
|
}catch (RuntimeException e) {
|
|
|
|
toRetry.add(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(toRetry.size()>0) {
|
|
|
|
addAllType(toRetry);
|
|
|
|
}
|
2023-10-24 15:18:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public void addType(T t) {
|
|
|
|
AccessType accessType = nodeInformation.getAccessType(t);
|
2023-10-25 18:01:11 +02:00
|
|
|
String typeName = nodeInformation.getIdentifier(t);
|
|
|
|
|
|
|
|
Tree<T> tree = trees.get(accessType);
|
|
|
|
tree.addNode(t);
|
2023-10-24 15:18:48 +02:00
|
|
|
|
|
|
|
@SuppressWarnings("unchecked")
|
2023-10-25 18:01:11 +02:00
|
|
|
UsageKnowledge<Map.Entry<T,PropertyDefinition>> propertyUsage = (UsageKnowledge<Map.Entry<T,PropertyDefinition>>) usageKnowledges.get(AccessType.PROPERTY);
|
2023-10-24 15:18:48 +02:00
|
|
|
|
|
|
|
@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);
|
|
|
|
|
|
|
|
switch (accessType) {
|
|
|
|
case PROPERTY:
|
|
|
|
// Nothing to do
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RESOURCE:
|
|
|
|
ResourceType resourceType = (ResourceType) t;
|
2023-10-25 18:01:11 +02:00
|
|
|
addAllUsage(resourceType.getFacets(), consistsOfUsage, facetUsage);
|
|
|
|
addAllUsage(resourceType.getResources(), isRelatedToUsage, resourceUsage);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Metadata is defined in parent type i.e. EntityElement.
|
|
|
|
* We want to have a reference to all Base type
|
|
|
|
*/
|
|
|
|
if(typeName.compareTo(Resource.NAME)==0) {
|
|
|
|
addMetadataUsage(t, propertyUsage);
|
|
|
|
}
|
2023-10-24 15:18:48 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case FACET:
|
|
|
|
FacetType facetType = (FacetType) t;
|
2023-10-25 18:01:11 +02:00
|
|
|
if(typeName.compareTo(Facet.NAME)==0) {
|
|
|
|
addMetadataUsage(t, propertyUsage);
|
|
|
|
}
|
|
|
|
addPropertyUsage(t, facetType.getProperties(), propertyUsage);
|
|
|
|
|
2023-10-24 15:18:48 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case IS_RELATED_TO:
|
|
|
|
IsRelatedToType isRelatedToType = (IsRelatedToType) t;
|
2023-10-25 18:01:11 +02:00
|
|
|
if(typeName.compareTo(IsRelatedTo.NAME)==0) {
|
|
|
|
addMetadataUsage(t, propertyUsage);
|
|
|
|
addPropagationConstraintUsage(t, propertyUsage);
|
|
|
|
}
|
|
|
|
addPropertyUsage(t, isRelatedToType.getProperties(), propertyUsage);
|
2023-10-24 15:18:48 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
case CONSISTS_OF:
|
|
|
|
ConsistsOfType consistsOfType = (ConsistsOfType) t;
|
2023-10-25 18:01:11 +02:00
|
|
|
if(typeName.compareTo(ConsistsOf.NAME)==0) {
|
|
|
|
addMetadataUsage(t, propertyUsage);
|
|
|
|
addPropagationConstraintUsage(t, propertyUsage);
|
|
|
|
}
|
|
|
|
addPropertyUsage(t, consistsOfType.getProperties(), propertyUsage);
|
2023-10-24 15:18:48 +02:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public Tree<T> getTree(AccessType accessType) {
|
|
|
|
return trees.get(accessType);
|
|
|
|
}
|
|
|
|
|
|
|
|
public UsageKnowledge<?> getUsageKnowledge(AccessType accessType) {
|
|
|
|
return usageKnowledges.get(accessType);
|
|
|
|
}
|
|
|
|
}
|