79 lines
1.9 KiB
Java
79 lines
1.9 KiB
Java
package org.gcube.informationsystem.utils.documentation.knowledge;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.Set;
|
|
|
|
import org.gcube.informationsystem.base.reference.AccessType;
|
|
import org.gcube.informationsystem.types.reference.Type;
|
|
import org.gcube.informationsystem.types.reference.entities.ResourceType;
|
|
|
|
/**
|
|
* @author Luca Frosini (ISTI - CNR)
|
|
*/
|
|
public class Tree {
|
|
|
|
private Node root;
|
|
|
|
private boolean allowMultipleInheritance;
|
|
private HashMap<String, Node> locate = new HashMap<String, Node>();
|
|
|
|
|
|
public Tree(Type head) {
|
|
this.root = new Node(head);
|
|
this.locate.put(head.getName(), root);
|
|
this.allowMultipleInheritance = false;
|
|
}
|
|
|
|
public void setAllowMultipleInheritance(boolean allowMultipleInheritance) {
|
|
this.allowMultipleInheritance = allowMultipleInheritance;
|
|
}
|
|
|
|
public 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());
|
|
}
|
|
|
|
public void addNode(Type type) {
|
|
createUsageKnowledge(type);
|
|
|
|
if(root.getType().getName().compareTo(type.getName())==0) {
|
|
// Root has been already added
|
|
return;
|
|
}
|
|
|
|
Set<String> superClasses = type.getSuperClasses();
|
|
|
|
String name = type.getName();
|
|
for(String superClass : superClasses) {
|
|
Node parent = locate.get(superClass);
|
|
if(parent==null) {
|
|
throw new RuntimeException("I can find parent Node for " + name + ". Missing parent is " + superClass);
|
|
}
|
|
Node node = new Node(type);
|
|
parent.addChild(node);
|
|
locate.put(name, node);
|
|
if(!allowMultipleInheritance) {
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
|
|
public Node getRoot() {
|
|
return root;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return root.toString();
|
|
}
|
|
|
|
public void elaborate(NodeElaborator nodeElaborator) throws Exception {
|
|
root.elaborate(nodeElaborator);
|
|
}
|
|
} |