Generalising Tree

This commit is contained in:
Luca Frosini 2023-02-03 17:53:42 +01:00
parent 019dabf8a8
commit a1573b5238
13 changed files with 342 additions and 31 deletions

View File

@ -10,6 +10,6 @@ import org.gcube.informationsystem.base.reference.Element;
*/
public interface DiscoveredElementAction {
public <E extends Element> void manageElementType(Class<E> e) throws Exception;
public <E extends Element> void analizeElement(Class<E> e) throws Exception;
}

View File

@ -27,28 +27,43 @@ public class Discovery<E extends Element> {
protected final Class<E> root;
protected final List<Package> packages;
protected final List<Class<? extends E>> discovered;
protected final List<Class<? extends E>> discoveredElement;
protected final List<DiscoveredElementAction> discoveredElementActions;
protected final boolean includeRootPackage;
public List<Class<? extends E>> getDiscovered() {
return discovered;
}
protected boolean discovered;
public Discovery(Class<E> root) {
public Discovery(Class<E> root) throws Exception {
this(root, true);
}
public Discovery(Class<E> root, boolean includeRootPackage) {
public Discovery(Class<E> root, boolean includeRootPackage) throws Exception {
this.root = root;
this.packages = new ArrayList<>();
this.includeRootPackage = includeRootPackage;
if(includeRootPackage) {
addPackage(root.getPackage());
}
this.discovered = new ArrayList<>();
this.discovered = false;
this.discoveredElement = new ArrayList<>();
this.discoveredElementActions = new ArrayList<>();
add(root);
}
public List<Class<? extends E>> getDiscoveredElement() {
return discoveredElement;
}
public synchronized void addDiscoveredElementActions(DiscoveredElementAction discoveredElementAction) throws Exception {
discoveredElementActions.add(discoveredElementAction);
if(discovered) {
for(Class<? extends E> clz : discoveredElement) {
discoveredElementAction.analizeElement(clz);
}
}
}
public void addPackage(Package p) {
if(!includeRootPackage && p==root.getPackage()) {
return;
@ -56,18 +71,21 @@ public class Discovery<E extends Element> {
packages.add(p);
}
protected void add(Class<? extends E> clz) {
if(!discovered.contains(clz)) {
discovered.add(clz);
protected void add(Class<? extends E> clz) throws Exception {
if(!discoveredElement.contains(clz)) {
discoveredElement.add(clz);
for(DiscoveredElementAction discoveredElementAction : discoveredElementActions) {
discoveredElementAction.analizeElement(clz);
}
logger.info("+ Added {}.", clz);
}
}
protected void analizeElement(Class<? extends E> clz) {
protected void analizeElement(Class<? extends E> clz) throws Exception {
logger.trace("Analizyng {}", clz);
if(!clz.isInterface()) {
logger.trace("- Discarding {} that is not an interface", clz);
logger.trace("- Discarding {} because is not an interface", clz);
return;
}
@ -76,7 +94,7 @@ public class Discovery<E extends Element> {
return;
}
if(discovered.contains(clz)) {
if(discoveredElement.contains(clz)) {
logger.trace("- Discarding {} because was already managed", clz);
return;
}
@ -110,8 +128,8 @@ public class Discovery<E extends Element> {
} else if(root.isAssignableFrom(m.getReturnType())) {
@SuppressWarnings("unchecked")
Class<E> type = (Class<E>) m.getReturnType();
analizeElement(type);
Class<E> eClz = (Class<E>) m.getReturnType();
analizeElement(eClz);
}
}
@ -120,16 +138,17 @@ public class Discovery<E extends Element> {
add(clz);
}
public void discover() throws Exception {
public synchronized void discover() throws Exception {
this.discovered = false;
for(Package p : packages) {
List<Class<?>> classes = ReflectionUtility.getClassesForPackage(p);
for(Class<?> clz : classes) {
@SuppressWarnings("unchecked")
Class<E> ism = (Class<E>) clz;
analizeElement(ism);
Class<E> eClz = (Class<E>) clz;
analizeElement(eClz);
}
}
this.discovered = true;
}
}

View File

@ -0,0 +1,110 @@
package org.gcube.informationsystem.discovery.knowledge;
import java.util.Set;
import java.util.TreeSet;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class Node<C extends Comparable<C>> implements Comparable<Node<C>> {
public static String INDENTATION = "\t";
private C c;
private Tree<C> tree;
private Node<C> parent;
private Set<Node<C>> children;
public Node(C c) {
this.c = c;
this.children = new TreeSet<>();
}
public Tree<C> getTree() {
return tree;
}
private void setTree(Tree<C> tree) {
this.tree = tree;
}
public C getNodeElement() {
return c;
}
public Node<C> getParent() {
return parent;
}
public Node<C> setParent(Node<C> parent) {
this.parent = parent;
return this;
}
public Node<C> addChild(Node<C> child) {
children.add(child);
child.setParent(this);
child.setTree(tree);
return this;
}
public Set<Node<C>> getChildrenNodes() {
return children;
}
public Set<C> getChildrenElement() {
Set<C> children = new TreeSet<>();
for (Node<C> child : this.children) {
children.add(child.c);
}
return children;
}
@Override
public String toString() {
return createTree(0).toString();
}
private StringBuffer createTree(int level) {
StringBuffer stringBuffer = new StringBuffer();
for (int i = 0; i < level; ++i) {
stringBuffer.append(Node.INDENTATION);
}
stringBuffer.append(tree.getNodeElementInformation().getIdentifier(c));
for (Node<C> child : children) {
stringBuffer.append("\n");
stringBuffer.append(child.createTree(level+1));
}
return stringBuffer;
}
@Override
public int compareTo(Node<C> other) {
if (this == other) {
return 0;
}
if (other == null) {
return -1;
}
if (getClass() != other.getClass()) {
return -1;
}
NodeElementInformation<C> nei = tree.getNodeElementInformation();
return nei.getIdentifier(c).compareTo(nei.getIdentifier(other.c));
}
public void elaborate(NodeElaborator<C> nodeElaborator) throws Exception {
elaborate(nodeElaborator, 0);
}
protected void elaborate(NodeElaborator<C> nodeElaborator, int level) throws Exception {
nodeElaborator.elaborate(this, level);
for (Node<C> child : children) {
child.elaborate(nodeElaborator, level+1);
}
}
}

View File

@ -0,0 +1,10 @@
package org.gcube.informationsystem.discovery.knowledge;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public interface NodeElaborator<C extends Comparable<C>> {
public void elaborate(Node<C> node, int level) throws Exception;
}

View File

@ -0,0 +1,24 @@
package org.gcube.informationsystem.discovery.knowledge;
import java.util.Set;
public interface NodeElementInformation<C extends Comparable<C>> {
public abstract String getIdentifier(C c);
public abstract Set<String> getParentIdentifiers(C c);
public abstract void extraElaboration(C c);
// protected void createUsageKnowledge(Type type) {
// if(type.getAccessType() != AccessType.RESOURCE) {
// return;
// }
// ResourceType rt = (ResourceType) type;
// UsageKnowledge fk = UsageKnowledge.getFacetKnowledge();
// fk.addAll(rt.getFacets());
// UsageKnowledge rk = UsageKnowledge.getResourceKnowledge();
// rk.addAll(rt.getResources());
// }
}

View File

@ -0,0 +1,75 @@
package org.gcube.informationsystem.discovery.knowledge;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
/**
* @author Luca Frosini (ISTI - CNR)
*/
public class Tree<C extends Comparable<C>> {
private boolean allowMultipleInheritance;
private Node<C> root;
private NodeElementInformation<C> nei;
private Map<String, Node<C>> locate;
public Tree(C c, NodeElementInformation<C> nei) throws Exception {
this.allowMultipleInheritance = false;
this.nei = nei;
this.locate = new HashMap<>();
this.root = addNode(c);
}
public void setAllowMultipleInheritance(boolean allowMultipleInheritance) {
this.allowMultipleInheritance = allowMultipleInheritance;
}
public NodeElementInformation<C> getNodeElementInformation() {
return nei;
}
public Node<C> addNode(C c) {
String identifier = nei.getIdentifier(c);
if(locate.containsKey(identifier)) {
// Has been already added
return locate.get(identifier);
}
Node<C> node = new Node<>(c);
Set<String> parentIdentifiers = nei.getParentIdentifiers(c);
for(String parentIdentifier : parentIdentifiers) {
Node<C> parentNode = locate.get(parentIdentifier);
if(parentNode==null) {
throw new RuntimeException("I can find parent for " + identifier + ". Missing parent is " + parentIdentifier);
}
parentNode.addChild(node);
if(!allowMultipleInheritance) {
break;
}
}
this.locate.put(identifier, node);
this.nei.extraElaboration(c);
return node;
}
public Node<C> getRoot() {
return root;
}
@Override
public String toString() {
return root.toString();
}
public void elaborate(NodeElaborator<C> nodeElaborator) throws Exception {
root.elaborate(nodeElaborator);
}
}

View File

@ -0,0 +1,73 @@
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<String, Set<LinkedEntity>> map;
private UsageKnowledge(){
this.map = new LinkedHashMap<>();
}
protected void add(String typeName, LinkedEntity linkedEntity) {
Set<LinkedEntity> 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<LinkedEntity> linkedEntities) {
if(linkedEntities!=null) {
for(LinkedEntity le : linkedEntities) {
add(le);
}
}
}
public Set<LinkedEntity> getUsage(String typeName){
Set<LinkedEntity> list = map.get(typeName);
return list;
}
}

View File

@ -12,7 +12,7 @@ import org.gcube.informationsystem.discovery.DiscoveredElementAction;
class ElementMappingAction implements DiscoveredElementAction {
@Override
public <E extends Element> void manageElementType(Class<E> e) throws Exception {
public <E extends Element> void analizeElement(Class<E> e) throws Exception {
ElementMapper.registerSubtype(e);
}

View File

@ -54,9 +54,9 @@ public class DiscoveryUtility {
Arrays.stream(packages).forEach(p -> propertyDiscovery.addPackage(p));
}
propertyDiscovery.discover();
for(Class<? extends PropertyElement> property : propertyDiscovery.getDiscovered()) {
for(Class<? extends PropertyElement> property : propertyDiscovery.getDiscoveredElement()) {
logger.trace("Going to manage : {}", property);
dea.manageElementType(property);
dea.analizeElement(property);
}
Discovery<EntityElement> entityDiscovery = new Discovery<>(EntityElement.class);
@ -65,9 +65,9 @@ public class DiscoveryUtility {
}
entityDiscovery.discover();
for(Class<? extends EntityElement> entity : entityDiscovery.getDiscovered()) {
for(Class<? extends EntityElement> entity : entityDiscovery.getDiscoveredElement()) {
logger.trace("Going to manage : {}", entity);
dea.manageElementType(entity);
dea.analizeElement(entity);
}
@SuppressWarnings("rawtypes")
@ -77,9 +77,9 @@ public class DiscoveryUtility {
relationDiscovery.discover();
for(@SuppressWarnings("rawtypes")
Class<? extends RelationElement> relation : relationDiscovery.getDiscovered()) {
Class<? extends RelationElement> relation : relationDiscovery.getDiscoveredElement()) {
logger.trace("Going to manage : {}", relation);
dea.manageElementType(relation);
dea.analizeElement(relation);
}
}

View File

@ -1,4 +1,4 @@
package org.gcube.informationsystem.knowledge;
package org.gcube.informationsystem.types.knowledge;
import java.util.Set;
import java.util.TreeSet;

View File

@ -1,4 +1,4 @@
package org.gcube.informationsystem.knowledge;
package org.gcube.informationsystem.types.knowledge;
/**
* @author Luca Frosini (ISTI - CNR)

View File

@ -1,4 +1,4 @@
package org.gcube.informationsystem.knowledge;
package org.gcube.informationsystem.types.knowledge;
import java.util.HashMap;
import java.util.Set;

View File

@ -1,4 +1,4 @@
package org.gcube.informationsystem.knowledge;
package org.gcube.informationsystem.types.knowledge;
import java.util.Collection;
import java.util.LinkedHashMap;