information-system-model-do.../src/main/java/org/gcube/informationsystem/utils/documentation/knowledge/Tree.java

79 lines
1.9 KiB
Java
Raw Normal View History

2023-01-22 19:44:32 +01:00
package org.gcube.informationsystem.utils.documentation.knowledge;
import java.util.HashMap;
2023-01-23 16:21:01 +01:00
import java.util.Set;
2023-01-24 19:30:40 +01:00
import org.gcube.informationsystem.base.reference.AccessType;
2023-01-23 16:21:01 +01:00
import org.gcube.informationsystem.types.reference.Type;
2023-01-24 19:30:40 +01:00
import org.gcube.informationsystem.types.reference.entities.ResourceType;
2023-01-22 19:44:32 +01:00
/**
2023-01-23 16:21:01 +01:00
* @author Luca Frosini (ISTI - CNR)
2023-01-22 19:44:32 +01:00
*/
2023-01-23 16:21:01 +01:00
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;
}
2023-01-24 19:30:40 +01:00
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) {
if(root.getType().getName().compareTo(type.getName())==0) {
2023-01-24 16:54:00 +01:00
// Root has been already added
return;
}
2023-01-24 19:30:40 +01:00
createUsageKnowledge(type);
Set<String> superClasses = type.getSuperClasses();
String name = type.getName();
2023-01-23 16:21:01 +01:00
for(String superClass : superClasses) {
Node parent = locate.get(superClass);
if(parent==null) {
2023-01-24 19:30:40 +01:00
throw new RuntimeException("I can find parent Node for " + name + ". Missing parent is " + superClass);
2023-01-23 16:21:01 +01:00
}
2023-01-24 19:30:40 +01:00
Node node = new Node(type);
2023-01-23 16:21:01 +01:00
parent.addChild(node);
2023-01-24 19:30:40 +01:00
locate.put(name, node);
2023-01-23 16:21:01 +01:00
if(!allowMultipleInheritance) {
return;
2023-01-22 19:44:32 +01:00
}
}
}
2023-01-23 16:21:01 +01:00
public Node getRoot() {
return root;
2023-01-22 19:44:32 +01:00
}
@Override
public String toString() {
2023-01-23 16:21:01 +01:00
return root.toString();
2023-01-22 19:44:32 +01:00
}
2023-01-24 16:54:00 +01:00
public void elaborate(NodeElaborator nodeElaborator) throws Exception {
root.elaborate(nodeElaborator);
}
2023-01-23 16:21:01 +01:00
}