diff --git a/src/main/java/org/gcube/informationsystem/base/reference/AccessType.java b/src/main/java/org/gcube/informationsystem/base/reference/AccessType.java index ab69e7b..56db223 100644 --- a/src/main/java/org/gcube/informationsystem/base/reference/AccessType.java +++ b/src/main/java/org/gcube/informationsystem/base/reference/AccessType.java @@ -80,7 +80,7 @@ public enum AccessType { private static Logger logger = LoggerFactory.getLogger(AccessType.class); - + private static AccessType[] modelTypes; private static Set names; static { @@ -90,6 +90,12 @@ public enum AccessType { String name = accessType.getName(); names.add(name); } + + modelTypes = new AccessType[] { + AccessType.PROPERTY, + AccessType.RESOURCE, AccessType.FACET, + AccessType.IS_RELATED_TO, AccessType.CONSISTS_OF + }; } private final Class clz; @@ -179,4 +185,9 @@ public enum AccessType { throw new RuntimeException(error); } } + + public static AccessType[] getModelTypes() { + return modelTypes; + } + } diff --git a/src/main/java/org/gcube/informationsystem/discovery/ClassElementInformation.java b/src/main/java/org/gcube/informationsystem/discovery/ClassElementInformation.java new file mode 100644 index 0000000..36bd29f --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/discovery/ClassElementInformation.java @@ -0,0 +1,40 @@ +package org.gcube.informationsystem.discovery; + +import java.util.LinkedHashSet; +import java.util.Set; + +import org.gcube.informationsystem.base.reference.Element; +import org.gcube.informationsystem.tree.NodeElementInformation; +import org.gcube.informationsystem.types.TypeMapper; + +public class ClassElementInformation implements NodeElementInformation> { + + @Override + public String getIdentifier(Class clz) { + return TypeMapper.getType(clz); + } + + @Override + public Set getParentIdentifiers(Class root, Class clz) { + Set ret = new LinkedHashSet<>(); + if(clz == root) { + return ret; + } + + Class[] interfaces = clz.getInterfaces(); + for(Class i : interfaces) { + if(root.isAssignableFrom(i)) { + @SuppressWarnings("unchecked") + String id = getIdentifier((Class) i); + ret.add(id); + } + } + return ret; + } + + @Override + public void extraElaboration(Class t) { + // Nothing to do + } + +} diff --git a/src/main/java/org/gcube/informationsystem/discovery/DiscoveredElementAction.java b/src/main/java/org/gcube/informationsystem/discovery/DiscoveredElementAction.java index dfdd42e..3c27b6f 100644 --- a/src/main/java/org/gcube/informationsystem/discovery/DiscoveredElementAction.java +++ b/src/main/java/org/gcube/informationsystem/discovery/DiscoveredElementAction.java @@ -8,8 +8,8 @@ import org.gcube.informationsystem.base.reference.Element; /** * @author Luca Frosini (ISTI - CNR) */ -public interface DiscoveredElementAction { +public interface DiscoveredElementAction { - public void analizeElement(Class e) throws Exception; + public void analizeElement(Class e) throws Exception; } diff --git a/src/main/java/org/gcube/informationsystem/discovery/Discovery.java b/src/main/java/org/gcube/informationsystem/discovery/Discovery.java index e928657..8c6c3c9 100644 --- a/src/main/java/org/gcube/informationsystem/discovery/Discovery.java +++ b/src/main/java/org/gcube/informationsystem/discovery/Discovery.java @@ -6,10 +6,12 @@ package org.gcube.informationsystem.discovery; import java.lang.reflect.Method; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; -import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.TreeSet; import org.gcube.informationsystem.base.reference.Element; import org.gcube.informationsystem.base.reference.properties.PropertyElement; @@ -26,9 +28,9 @@ public class Discovery { public static Logger logger = LoggerFactory.getLogger(Discovery.class); protected final Class root; - protected final List packages; - protected final List> discoveredElement; - protected final List discoveredElementActions; + protected final Set packages; + protected final Set> discoveredElement; + protected final Set> discoveredElementActions; protected final boolean includeRootPackage; @@ -40,26 +42,27 @@ public class Discovery { public Discovery(Class root, boolean includeRootPackage) throws Exception { this.root = root; - this.packages = new ArrayList<>(); + this.packages = new HashSet<>(); this.includeRootPackage = includeRootPackage; if(includeRootPackage) { addPackage(root.getPackage()); } this.discovered = false; - this.discoveredElement = new ArrayList<>(); - this.discoveredElementActions = new ArrayList<>(); + this.discoveredElement = new TreeSet<>(); + this.discoveredElementActions = new TreeSet<>(); add(root); } - public List> getDiscoveredElement() { + public Set> getDiscoveredElement() { return discoveredElement; } - public synchronized void addDiscoveredElementActions(DiscoveredElementAction discoveredElementAction) throws Exception { + @SuppressWarnings("unchecked") + public synchronized void addDiscoveredElementActions(DiscoveredElementAction discoveredElementAction) throws Exception { discoveredElementActions.add(discoveredElementAction); if(discovered) { - for(Class clz : discoveredElement) { - discoveredElementAction.analizeElement(clz); + for(Class clz : discoveredElement) { + discoveredElementAction.analizeElement((Class) clz); } } } @@ -71,17 +74,23 @@ public class Discovery { packages.add(p); } - protected void add(Class clz) throws Exception { + public void addPackages(Collection packages) { + for(Package p : packages) { + addPackage(p); + } + } + + protected void add(Class clz) throws Exception { if(!discoveredElement.contains(clz)) { discoveredElement.add(clz); - for(DiscoveredElementAction discoveredElementAction : discoveredElementActions) { + for(DiscoveredElementAction discoveredElementAction : discoveredElementActions) { discoveredElementAction.analizeElement(clz); } logger.info("+ Added {}.", clz); } } - protected void analizeElement(Class clz) throws Exception { + protected void analizeElement(Class clz) throws Exception { logger.trace("Analizyng {}", clz); if(!clz.isInterface()) { diff --git a/src/main/java/org/gcube/informationsystem/discovery/knowledge/ModelExtensionKnowledge.java b/src/main/java/org/gcube/informationsystem/discovery/knowledge/ModelExtensionKnowledge.java new file mode 100644 index 0000000..a01bf03 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/discovery/knowledge/ModelExtensionKnowledge.java @@ -0,0 +1,50 @@ +package org.gcube.informationsystem.discovery.knowledge; + +import java.util.HashMap; +import java.util.Map; + +import org.gcube.informationsystem.base.reference.AccessType; +import org.gcube.informationsystem.base.reference.Element; +import org.gcube.informationsystem.discovery.Discovery; +import org.gcube.informationsystem.discovery.RegistrationProvider; +import org.gcube.informationsystem.serialization.ElementMappingAction; +import org.gcube.informationsystem.tree.AddElementToTreeAction; +import org.gcube.informationsystem.tree.Tree; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class ModelExtensionKnowledge { + + protected RegistrationProvider registrationProvider; + + protected Map>> trees; + + public ModelExtensionKnowledge(RegistrationProvider registrationProvider) { + this.registrationProvider = registrationProvider; + this.trees = new HashMap<>(); + } + + public void createKnowledge() throws Exception { + + AccessType[] modelTypes = AccessType.getModelTypes(); + + for(AccessType accessType : modelTypes) { + Class clz = accessType.getTypeClass(); + + Tree> tree = new Tree<>(clz, null); + trees.put(accessType, tree); + AddElementToTreeAction aetta = new AddElementToTreeAction(tree); + + Discovery discovery = new Discovery<>(clz, false); + discovery.addPackages(registrationProvider.getPackagesToRegister()); + discovery.addDiscoveredElementActions(new ElementMappingAction()); + discovery.addDiscoveredElementActions(aetta); + + + discovery.discover(); + } + + } + +} diff --git a/src/main/java/org/gcube/informationsystem/discovery/knowledge/NodeElaborator.java b/src/main/java/org/gcube/informationsystem/discovery/knowledge/NodeElaborator.java deleted file mode 100644 index bab77d7..0000000 --- a/src/main/java/org/gcube/informationsystem/discovery/knowledge/NodeElaborator.java +++ /dev/null @@ -1,10 +0,0 @@ -package org.gcube.informationsystem.discovery.knowledge; - -/** - * @author Luca Frosini (ISTI - CNR) - */ -public interface NodeElaborator> { - - public void elaborate(Node node, int level) throws Exception; - -} diff --git a/src/main/java/org/gcube/informationsystem/discovery/knowledge/UsageKnowledge.java b/src/main/java/org/gcube/informationsystem/discovery/knowledge/UsageKnowledge.java deleted file mode 100644 index 511c1d3..0000000 --- a/src/main/java/org/gcube/informationsystem/discovery/knowledge/UsageKnowledge.java +++ /dev/null @@ -1,73 +0,0 @@ -package org.gcube.informationsystem.discovery.knowledge; - -import java.util.Collection; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Set; -import java.util.TreeSet; - -import org.gcube.informationsystem.types.reference.properties.LinkedEntity; - -/** - * @author Luca Frosini (ISTI - CNR) - */ -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> map; - - private UsageKnowledge(){ - this.map = new LinkedHashMap<>(); - } - - protected void add(String typeName, LinkedEntity linkedEntity) { - Set list = map.get(typeName); - if(list==null) { - list = new TreeSet<>(); - map.put(typeName, list); - } - list.add(linkedEntity); - } - - public void add(LinkedEntity linkedEntity) { - if(linkedEntity!=null) { - String source = linkedEntity.getSource(); - add(source, linkedEntity); - String relation = linkedEntity.getRelation(); - add(relation, linkedEntity); - String target = linkedEntity.getTarget(); - add(target, linkedEntity); - } - } - - public void addAll(Collection linkedEntities) { - if(linkedEntities!=null) { - for(LinkedEntity le : linkedEntities) { - add(le); - } - } - } - - public Set getUsage(String typeName){ - Set list = map.get(typeName); - return list; - } - -} diff --git a/src/main/java/org/gcube/informationsystem/serialization/ElementMappingAction.java b/src/main/java/org/gcube/informationsystem/serialization/ElementMappingAction.java index 0d58d8d..9dc24dd 100644 --- a/src/main/java/org/gcube/informationsystem/serialization/ElementMappingAction.java +++ b/src/main/java/org/gcube/informationsystem/serialization/ElementMappingAction.java @@ -9,10 +9,10 @@ import org.gcube.informationsystem.discovery.DiscoveredElementAction; /** * @author Luca Frosini (ISTI - CNR) */ -class ElementMappingAction implements DiscoveredElementAction { +public class ElementMappingAction implements DiscoveredElementAction { @Override - public void analizeElement(Class e) throws Exception { + public void analizeElement(Class e) throws Exception { ElementMapper.registerSubtype(e); } diff --git a/src/main/java/org/gcube/informationsystem/tree/AddElementToTreeAction.java b/src/main/java/org/gcube/informationsystem/tree/AddElementToTreeAction.java new file mode 100644 index 0000000..9b6b613 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/tree/AddElementToTreeAction.java @@ -0,0 +1,25 @@ +/** + * + */ +package org.gcube.informationsystem.tree; + +import org.gcube.informationsystem.base.reference.Element; +import org.gcube.informationsystem.discovery.DiscoveredElementAction; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public class AddElementToTreeAction implements DiscoveredElementAction { + + protected Tree> tree; + + public AddElementToTreeAction(Tree> tree) { + this.tree = tree; + } + + @Override + public void analizeElement(Class e) throws Exception { + tree.addNode(e); + } + +} diff --git a/src/main/java/org/gcube/informationsystem/discovery/knowledge/Node.java b/src/main/java/org/gcube/informationsystem/tree/Node.java similarity index 53% rename from src/main/java/org/gcube/informationsystem/discovery/knowledge/Node.java rename to src/main/java/org/gcube/informationsystem/tree/Node.java index 57868f7..e2c52b4 100644 --- a/src/main/java/org/gcube/informationsystem/discovery/knowledge/Node.java +++ b/src/main/java/org/gcube/informationsystem/tree/Node.java @@ -1,4 +1,4 @@ -package org.gcube.informationsystem.discovery.knowledge; +package org.gcube.informationsystem.tree; import java.util.Set; import java.util.TreeSet; @@ -6,57 +6,57 @@ import java.util.TreeSet; /** * @author Luca Frosini (ISTI - CNR) */ -public class Node> implements Comparable> { +public class Node implements Comparable> { public static String INDENTATION = "\t"; - private C c; - private Tree tree; + private T t; + private Tree tree; - private Node parent; - private Set> children; + private Node parent; + private Set> children; - public Node(C c) { - this.c = c; + public Node(T t) { + this.t = t; this.children = new TreeSet<>(); } - public Tree getTree() { + public Tree getTree() { return tree; } - private void setTree(Tree tree) { + private void setTree(Tree tree) { this.tree = tree; } - public C getNodeElement() { - return c; + public T getNodeElement() { + return t; } - public Node getParent() { + public Node getParent() { return parent; } - public Node setParent(Node parent) { + public Node setParent(Node parent) { this.parent = parent; return this; } - public Node addChild(Node child) { + public Node addChild(Node child) { children.add(child); child.setParent(this); child.setTree(tree); return this; } - public Set> getChildrenNodes() { + public Set> getChildrenNodes() { return children; } - public Set getChildrenElement() { - Set children = new TreeSet<>(); - for (Node child : this.children) { - children.add(child.c); + public Set getChildrenElement() { + Set children = new TreeSet<>(); + for (Node child : this.children) { + children.add(child.t); } return children; } @@ -71,8 +71,8 @@ public class Node> implements Comparable> { for (int i = 0; i < level; ++i) { stringBuffer.append(Node.INDENTATION); } - stringBuffer.append(tree.getNodeElementInformation().getIdentifier(c)); - for (Node child : children) { + stringBuffer.append(tree.getNodeElementInformation().getIdentifier(t)); + for (Node child : children) { stringBuffer.append("\n"); stringBuffer.append(child.createTree(level+1)); } @@ -80,7 +80,7 @@ public class Node> implements Comparable> { } @Override - public int compareTo(Node other) { + public int compareTo(Node other) { if (this == other) { return 0; } @@ -91,19 +91,19 @@ public class Node> implements Comparable> { return -1; } - NodeElementInformation nei = tree.getNodeElementInformation(); + NodeElementInformation nei = tree.getNodeElementInformation(); - return nei.getIdentifier(c).compareTo(nei.getIdentifier(other.c)); + return nei.getIdentifier(t).compareTo(nei.getIdentifier(other.t)); } - public void elaborate(NodeElaborator nodeElaborator) throws Exception { + public void elaborate(NodeElaborator nodeElaborator) throws Exception { elaborate(nodeElaborator, 0); } - protected void elaborate(NodeElaborator nodeElaborator, int level) throws Exception { + protected void elaborate(NodeElaborator nodeElaborator, int level) throws Exception { nodeElaborator.elaborate(this, level); - for (Node child : children) { + for (Node child : children) { child.elaborate(nodeElaborator, level+1); } } diff --git a/src/main/java/org/gcube/informationsystem/tree/NodeElaborator.java b/src/main/java/org/gcube/informationsystem/tree/NodeElaborator.java new file mode 100644 index 0000000..46c4988 --- /dev/null +++ b/src/main/java/org/gcube/informationsystem/tree/NodeElaborator.java @@ -0,0 +1,10 @@ +package org.gcube.informationsystem.tree; + +/** + * @author Luca Frosini (ISTI - CNR) + */ +public interface NodeElaborator { + + public void elaborate(Node node, int level) throws Exception; + +} diff --git a/src/main/java/org/gcube/informationsystem/discovery/knowledge/NodeElementInformation.java b/src/main/java/org/gcube/informationsystem/tree/NodeElementInformation.java similarity index 59% rename from src/main/java/org/gcube/informationsystem/discovery/knowledge/NodeElementInformation.java rename to src/main/java/org/gcube/informationsystem/tree/NodeElementInformation.java index 2d0563f..b50457b 100644 --- a/src/main/java/org/gcube/informationsystem/discovery/knowledge/NodeElementInformation.java +++ b/src/main/java/org/gcube/informationsystem/tree/NodeElementInformation.java @@ -1,14 +1,14 @@ -package org.gcube.informationsystem.discovery.knowledge; +package org.gcube.informationsystem.tree; import java.util.Set; -public interface NodeElementInformation> { +public interface NodeElementInformation { - public abstract String getIdentifier(C c); + public abstract String getIdentifier(T t); - public abstract Set getParentIdentifiers(C c); + public abstract Set getParentIdentifiers(T root, T t); - public abstract void extraElaboration(C c); + public abstract void extraElaboration(T t); // protected void createUsageKnowledge(Type type) { diff --git a/src/main/java/org/gcube/informationsystem/discovery/knowledge/Tree.java b/src/main/java/org/gcube/informationsystem/tree/Tree.java similarity index 59% rename from src/main/java/org/gcube/informationsystem/discovery/knowledge/Tree.java rename to src/main/java/org/gcube/informationsystem/tree/Tree.java index ebd0f69..d0a20ee 100644 --- a/src/main/java/org/gcube/informationsystem/discovery/knowledge/Tree.java +++ b/src/main/java/org/gcube/informationsystem/tree/Tree.java @@ -1,4 +1,4 @@ -package org.gcube.informationsystem.discovery.knowledge; +package org.gcube.informationsystem.tree; import java.util.HashMap; import java.util.Map; @@ -7,19 +7,19 @@ import java.util.Set; /** * @author Luca Frosini (ISTI - CNR) */ -public class Tree> { +public class Tree { private boolean allowMultipleInheritance; - private Node root; - private NodeElementInformation nei; - private Map> locate; + private Node root; + private NodeElementInformation nei; + private Map> locate; - public Tree(C c, NodeElementInformation nei) throws Exception { + public Tree(T t, NodeElementInformation nei) throws Exception { this.allowMultipleInheritance = false; this.nei = nei; this.locate = new HashMap<>(); - this.root = addNode(c); + this.root = addNode(t); } @@ -27,22 +27,22 @@ public class Tree> { this.allowMultipleInheritance = allowMultipleInheritance; } - public NodeElementInformation getNodeElementInformation() { + public NodeElementInformation getNodeElementInformation() { return nei; } - public Node addNode(C c) { - String identifier = nei.getIdentifier(c); + public Node addNode(T t) { + String identifier = nei.getIdentifier(t); if(locate.containsKey(identifier)) { // Has been already added return locate.get(identifier); } - Node node = new Node<>(c); + Node node = new Node<>(t); - Set parentIdentifiers = nei.getParentIdentifiers(c); + Set parentIdentifiers = nei.getParentIdentifiers(root.getNodeElement(), t); for(String parentIdentifier : parentIdentifiers) { - Node parentNode = locate.get(parentIdentifier); + Node parentNode = locate.get(parentIdentifier); if(parentNode==null) { throw new RuntimeException("I can find parent for " + identifier + ". Missing parent is " + parentIdentifier); } @@ -55,12 +55,12 @@ public class Tree> { } this.locate.put(identifier, node); - this.nei.extraElaboration(c); + this.nei.extraElaboration(t); return node; } - public Node getRoot() { + public Node getRoot() { return root; } @@ -69,7 +69,7 @@ public class Tree> { return root.toString(); } - public void elaborate(NodeElaborator nodeElaborator) throws Exception { + public void elaborate(NodeElaborator nodeElaborator) throws Exception { root.elaborate(nodeElaborator); } } \ No newline at end of file