Added classes to manages tree types
This commit is contained in:
parent
0983c65505
commit
7129eee9f1
|
@ -0,0 +1,111 @@
|
|||
package org.gcube.informationsystem.utils.knowledge;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import org.gcube.informationsystem.types.reference.Type;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class Node implements Comparable<Node> {
|
||||
|
||||
public static String INDENTATION = "\t";
|
||||
|
||||
private Type type;
|
||||
|
||||
private Tree tree;
|
||||
|
||||
private Node parent;
|
||||
private Set<Node> children;
|
||||
|
||||
public Node(Type type) {
|
||||
this.type = type;
|
||||
this.children = new TreeSet<Node>();
|
||||
}
|
||||
|
||||
public Tree getTree() {
|
||||
return tree;
|
||||
}
|
||||
|
||||
private void setTree(Tree tree) {
|
||||
this.tree = tree;
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
public Node getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public Node setParent(Node parent) {
|
||||
this.parent = parent;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Node addChild(Node child) {
|
||||
children.add(child);
|
||||
child.setParent(this);
|
||||
child.setTree(tree);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Set<Node> getChildrenNodes() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public Set<Type> getChildren() {
|
||||
Set<Type> sortedChildren = new TreeSet<>();
|
||||
for (Node treeNode : children) {
|
||||
sortedChildren.add(treeNode.type);
|
||||
}
|
||||
return sortedChildren;
|
||||
}
|
||||
|
||||
@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(type.getName());
|
||||
for (Node child : children) {
|
||||
stringBuffer.append("\n");
|
||||
stringBuffer.append(child.createTree(level+1));
|
||||
}
|
||||
return stringBuffer;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Node other) {
|
||||
if (this == other) {
|
||||
return 0;
|
||||
}
|
||||
if (other == null) {
|
||||
return -1;
|
||||
}
|
||||
if (getClass() != other.getClass()) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return type.getName().compareTo(other.type.getName());
|
||||
}
|
||||
|
||||
|
||||
public void elaborate(NodeElaborator nodeElaborator) throws Exception {
|
||||
elaborate(nodeElaborator, 0);
|
||||
}
|
||||
|
||||
protected void elaborate(NodeElaborator nodeElaborator, int level) throws Exception {
|
||||
nodeElaborator.elaborate(this, level);
|
||||
for (Node child : children) {
|
||||
child.elaborate(nodeElaborator, level+1);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package org.gcube.informationsystem.utils.knowledge;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public interface NodeElaborator {
|
||||
|
||||
public void elaborate(Node node, int level) throws Exception;
|
||||
|
||||
}
|
|
@ -0,0 +1,79 @@
|
|||
package org.gcube.informationsystem.utils.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);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,73 @@
|
|||
package org.gcube.informationsystem.utils.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;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue