diff --git a/CHANGELOG.md b/CHANGELOG.md index fbab38a..2d69f82 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm # Changelog for Information System Model +## [v7.1.0-SNAPSHOT] + +- Improved support for model knowledge [#25922] + ## [v7.0.0] - Reorganized E/R format [#24992] diff --git a/pom.xml b/pom.xml index a234795..b4f9fae 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ org.gcube.information-system information-system-model - 7.0.0 + 7.1.0-SNAPSHOT Information System Model Information System Model is the reference model of the gCube Information System jar diff --git a/src/main/java/org/gcube/informationsystem/discovery/knowledge/ClassInformation.java b/src/main/java/org/gcube/informationsystem/discovery/knowledge/ClassInformation.java index 04b32f3..22fb4c9 100644 --- a/src/main/java/org/gcube/informationsystem/discovery/knowledge/ClassInformation.java +++ b/src/main/java/org/gcube/informationsystem/discovery/knowledge/ClassInformation.java @@ -3,6 +3,7 @@ package org.gcube.informationsystem.discovery.knowledge; import java.util.LinkedHashSet; import java.util.Set; +import org.gcube.informationsystem.base.reference.AccessType; import org.gcube.informationsystem.base.reference.Element; import org.gcube.informationsystem.tree.NodeInformation; import org.gcube.informationsystem.types.TypeMapper; @@ -35,4 +36,14 @@ public class ClassInformation implements NodeInformation> { return ret; } + @Override + public AccessType getAccessType(Class clz) { + return AccessType.getAccessType(clz); + } + + @Override + public Class getRoot(AccessType accessType) { + return accessType.getTypeClass(); + } + } diff --git a/src/main/java/org/gcube/informationsystem/knowledge/ModelKnowledge.java b/src/main/java/org/gcube/informationsystem/knowledge/ModelKnowledge.java new file mode 100644 index 0000000..1f75afb --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/knowledge/ModelKnowledge.java @@ -0,0 +1,147 @@ +package org.gcube.informationsystem.knowledge; + +import java.util.Collection; +import java.util.HashMap; +import java.util.Map; +import java.util.Set; + +import org.gcube.informationsystem.base.reference.AccessType; +import org.gcube.informationsystem.tree.NodeInformation; +import org.gcube.informationsystem.tree.Tree; +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> { + + private static final Logger logger = LoggerFactory.getLogger(ModelKnowledge.class); + + protected NI nodeInformation; + protected Map> trees; + protected Map> usageKnowledges; + + public ModelKnowledge() { + 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 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 + */ + usageKnowledge = new UsageKnowledge>(accessType); + }else { + usageKnowledge = new UsageKnowledge(accessType); + } + usageKnowledges.put(accessType, usageKnowledge); + } + + } + + protected void add(LinkedEntity linkedEntity, UsageKnowledge relationUsage, UsageKnowledge targetEntityUsage) { + if (linkedEntity != null) { + @SuppressWarnings("unchecked") + UsageKnowledge resourceUsage = (UsageKnowledge) 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); + } + } + + protected void addAll(Collection linkedEntities, UsageKnowledge relationUsage, UsageKnowledge targetEntityUsage) { + if(linkedEntities!=null) { + for(LinkedEntity le : linkedEntities) { + add(le, relationUsage, targetEntityUsage); + } + } + } + + protected void addPropertyUsage(Set properties, UsageKnowledge propertyUsage) { + // for() + } + + public void addType(T t) { + AccessType accessType = nodeInformation.getAccessType(t); + + @SuppressWarnings("unchecked") + UsageKnowledge propertyUsage = (UsageKnowledge) usageKnowledges.get(AccessType.PROPERTY); + + @SuppressWarnings("unchecked") + UsageKnowledge resourceUsage = (UsageKnowledge) usageKnowledges.get(AccessType.RESOURCE); + @SuppressWarnings("unchecked") + UsageKnowledge facetUsage = (UsageKnowledge) usageKnowledges.get(AccessType.FACET); + @SuppressWarnings("unchecked") + UsageKnowledge isRelatedToUsage = (UsageKnowledge) usageKnowledges.get(AccessType.IS_RELATED_TO); + @SuppressWarnings("unchecked") + UsageKnowledge consistsOfUsage = (UsageKnowledge) usageKnowledges.get(AccessType.CONSISTS_OF); + + switch (accessType) { + case PROPERTY: + // Nothing to do + break; + + case RESOURCE: + ResourceType resourceType = (ResourceType) t; + addAll(resourceType.getFacets(), consistsOfUsage, facetUsage); + addAll(resourceType.getResources(), isRelatedToUsage, resourceUsage); + break; + + case FACET: + FacetType facetType = (FacetType) t; + addPropertyUsage(facetType.getProperties(), propertyUsage); + break; + + case IS_RELATED_TO: + IsRelatedToType isRelatedToType = (IsRelatedToType) t; + + break; + + case CONSISTS_OF: + ConsistsOfType consistsOfType = (ConsistsOfType) t; + + break; + + default: + break; + } + + if(accessType == AccessType.RESOURCE) { + + + } + } + + public Tree getTree(AccessType accessType) { + return trees.get(accessType); + } + + public UsageKnowledge getUsageKnowledge(AccessType accessType) { + return usageKnowledges.get(accessType); + } +} diff --git a/src/main/java/org/gcube/informationsystem/knowledge/TypeInformation.java b/src/main/java/org/gcube/informationsystem/knowledge/TypeInformation.java new file mode 100644 index 0000000..ce46a9d --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/knowledge/TypeInformation.java @@ -0,0 +1,40 @@ +package org.gcube.informationsystem.knowledge; + +import java.util.LinkedHashSet; +import java.util.Set; + +import org.gcube.informationsystem.base.reference.AccessType; +import org.gcube.informationsystem.tree.NodeInformation; +import org.gcube.informationsystem.types.TypeMapper; +import org.gcube.informationsystem.types.reference.Type; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class TypeInformation implements NodeInformation { + + @Override + public String getIdentifier(Type type) { + return type.getName(); + } + + @Override + public Set getParentIdentifiers(Type root, Type type) { + if(type.getName().compareTo(root.getName())==0) { + return new LinkedHashSet<>(); + } + + return type.getExtendedTypes(); + } + + @Override + public AccessType getAccessType(Type type) { + return type.getAccessType(); + } + + @Override + public Type getRoot(AccessType accessType) { + return TypeMapper.createTypeDefinition(accessType.getTypeClass()); + } + +} diff --git a/src/main/java/org/gcube/informationsystem/knowledge/UsageKnowledge.java b/src/main/java/org/gcube/informationsystem/knowledge/UsageKnowledge.java new file mode 100644 index 0000000..f497a54 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/knowledge/UsageKnowledge.java @@ -0,0 +1,35 @@ +package org.gcube.informationsystem.knowledge; + +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; +import java.util.TreeSet; + +import org.gcube.informationsystem.base.reference.AccessType; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class UsageKnowledge { + + protected Map> map; + + public UsageKnowledge(AccessType accessType){ + this.map = new LinkedHashMap<>(); + } + + public void add(String typeName, U u) { + Set list = map.get(typeName); + if(list==null) { + list = new TreeSet<>(); + map.put(typeName, list); + } + list.add(u); + } + + public Set getUsage(String typeName){ + Set list = map.get(typeName); + return list; + } + +} diff --git a/src/main/java/org/gcube/informationsystem/tree/NodeInformation.java b/src/main/java/org/gcube/informationsystem/tree/NodeInformation.java index 846d5e9..610e2de 100644 --- a/src/main/java/org/gcube/informationsystem/tree/NodeInformation.java +++ b/src/main/java/org/gcube/informationsystem/tree/NodeInformation.java @@ -2,10 +2,16 @@ package org.gcube.informationsystem.tree; import java.util.Set; +import org.gcube.informationsystem.base.reference.AccessType; + public interface NodeInformation { - public abstract String getIdentifier(T t); + public String getIdentifier(T t); - public abstract Set getParentIdentifiers(T root, T t); + public Set getParentIdentifiers(T root, T t); + + public AccessType getAccessType(T t); + + public T getRoot(AccessType accessType); } diff --git a/src/main/java/org/gcube/informationsystem/tree/Tree.java b/src/main/java/org/gcube/informationsystem/tree/Tree.java index fd3ce02..941a964 100644 --- a/src/main/java/org/gcube/informationsystem/tree/Tree.java +++ b/src/main/java/org/gcube/informationsystem/tree/Tree.java @@ -15,7 +15,7 @@ public class Tree { private NodeInformation ni; private Map> locate; - public Tree(T t, NodeInformation ni) throws Exception { + public Tree(T t, NodeInformation ni) { this.allowMultipleInheritance = true; this.ni = ni; this.locate = new HashMap<>();