Improved solution
This commit is contained in:
parent
7a61f15c66
commit
a7e597fbd4
|
@ -6,11 +6,6 @@
|
|||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry excluding="**" kind="src" output="target/classes" path="src/main/resources">
|
||||
<attributes>
|
||||
<attribute name="maven.pomderived" value="true"/>
|
||||
</attributes>
|
||||
</classpathentry>
|
||||
<classpathentry kind="src" output="target/test-classes" path="src/test/java">
|
||||
<attributes>
|
||||
<attribute name="optional" value="true"/>
|
||||
|
|
|
@ -18,6 +18,7 @@ import org.gcube.informationsystem.utils.discovery.RegistrationProvider;
|
|||
*/
|
||||
public class ClassesDiscoveryGenerator {
|
||||
|
||||
protected TreeGenerator treeGenerator;
|
||||
protected TypeListGenerator typeListGenerator;
|
||||
|
||||
public ClassesDiscoveryGenerator() {
|
||||
|
@ -62,12 +63,12 @@ public class ClassesDiscoveryGenerator {
|
|||
|
||||
for(Class<? extends E> clz : discovery.getDiscovered()) {
|
||||
Type type = TypeMapper.createTypeDefinition(clz);
|
||||
typeListGenerator.addType(type);
|
||||
treeGenerator.addType(type);
|
||||
}
|
||||
}
|
||||
|
||||
protected void generate(Package[] packages) throws Exception {
|
||||
typeListGenerator = new TypeListGenerator();
|
||||
treeGenerator = new TreeGenerator();
|
||||
AccessType[] types = new AccessType[] {
|
||||
AccessType.PROPERTY,
|
||||
AccessType.RESOURCE, AccessType.FACET,
|
||||
|
@ -75,8 +76,6 @@ public class ClassesDiscoveryGenerator {
|
|||
for(AccessType at : types) {
|
||||
discover(at, packages);
|
||||
}
|
||||
|
||||
typeListGenerator.generate();
|
||||
|
||||
treeGenerator.generate();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,78 @@
|
|||
package org.gcube.informationsystem.utils.documentation.generator;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
|
||||
import org.gcube.informationsystem.base.reference.AccessType;
|
||||
import org.gcube.informationsystem.types.TypeMapper;
|
||||
import org.gcube.informationsystem.types.reference.Type;
|
||||
import org.gcube.informationsystem.utils.documentation.knowledge.Tree;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class TreeGenerator {
|
||||
|
||||
private static final Logger logger = LoggerFactory.getLogger(TreeGenerator.class);
|
||||
|
||||
protected Map<AccessType, Tree> types;
|
||||
|
||||
public TreeGenerator() {
|
||||
types = new TreeMap<>();
|
||||
}
|
||||
|
||||
public void addType(Type type) {
|
||||
AccessType accessType = type.getAccessType();
|
||||
if(types.get(accessType)==null) {
|
||||
Type head = TypeMapper.createTypeDefinition(accessType.getTypeClass());
|
||||
Tree tree = new Tree(head);
|
||||
types.put(accessType, tree);
|
||||
}
|
||||
if(type.getName().compareTo(accessType.getName())==0) {
|
||||
return;
|
||||
}
|
||||
Tree tree = types.get(accessType);
|
||||
tree.addNode(type);
|
||||
}
|
||||
|
||||
|
||||
public void elaborateTree(AccessType at, Tree tree) {
|
||||
logger.info("\n{}", tree.toString());
|
||||
|
||||
// elaborateList(propertyTypes);
|
||||
//
|
||||
// File f = getFile(ENTITIES_FILENAME, true);
|
||||
// DocumentationGenerator edg = new EntityDocumentationGenerator(TypeMapper.createTypeDefinition(Entity.class));
|
||||
// edg.setOffsetLevel(offsetLevel);
|
||||
// StringBuffer sb = edg.generateSection();
|
||||
// Files.write(f.toPath(), sb.toString().getBytes(), StandardOpenOption.APPEND);
|
||||
//
|
||||
// elaborateList(resourceTypes);
|
||||
// elaborateList(facetTypes);
|
||||
//
|
||||
// f = getFile(RELATIONS_FILENAME, true);
|
||||
// DocumentationGenerator rdg = new RelationDocumentationGenerator(TypeMapper.createTypeDefinition(Relation.class));
|
||||
// rdg.setOffsetLevel(offsetLevel);
|
||||
// sb = rdg.generateSection();
|
||||
// Files.write(f.toPath(), sb.toString().getBytes(), StandardOpenOption.APPEND);
|
||||
//
|
||||
// elaborateList(isRelatedToTypes);
|
||||
// elaborateList(consistsOfTypes);
|
||||
}
|
||||
|
||||
public void generate() throws Exception {
|
||||
|
||||
AccessType[] accessTypes = new AccessType[] {
|
||||
AccessType.PROPERTY,
|
||||
AccessType.RESOURCE, AccessType.FACET,
|
||||
AccessType.CONSISTS_OF, AccessType.IS_RELATED_TO};
|
||||
for(AccessType at : accessTypes) {
|
||||
Tree tree = types.get(at);
|
||||
elaborateTree(at, tree);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,99 @@
|
|||
package org.gcube.informationsystem.utils.documentation.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());
|
||||
}
|
||||
}
|
|
@ -1,109 +1,54 @@
|
|||
package org.gcube.informationsystem.utils.documentation.knowledge;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.gcube.informationsystem.types.reference.Type;
|
||||
|
||||
/**
|
||||
* @author ycoppel@google.com (Yohann Coppel)
|
||||
*
|
||||
* @param <T> Object's type in the tree.
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class Tree<T> {
|
||||
public class Tree {
|
||||
|
||||
private T head;
|
||||
private List<Tree<T>> leafs;
|
||||
private Tree<T> parent = null;
|
||||
private Node root;
|
||||
|
||||
private HashMap<T, Tree<T>> locate = new HashMap<T, Tree<T>>();
|
||||
private boolean allowMultipleInheritance;
|
||||
private HashMap<String, Node> locate = new HashMap<String, Node>();
|
||||
|
||||
public Tree(T head) {
|
||||
this.head = head;
|
||||
this.leafs = new ArrayList<Tree<T>>();
|
||||
locate.put(head, this);
|
||||
|
||||
public Tree(Type head) {
|
||||
this.root = new Node(head);
|
||||
this.locate.put(head.getName(), root);
|
||||
this.allowMultipleInheritance = false;
|
||||
}
|
||||
|
||||
public void addLeaf(T parent, T leaf) {
|
||||
if (locate.containsKey(parent)) {
|
||||
locate.get(parent).addLeaf(leaf);
|
||||
} else {
|
||||
addLeaf(parent).addLeaf(leaf);
|
||||
public void setAllowMultipleInheritance(boolean allowMultipleInheritance) {
|
||||
this.allowMultipleInheritance = allowMultipleInheritance;
|
||||
}
|
||||
|
||||
public void addNode(Type t) {
|
||||
Set<String> superClasses = t.getSuperClasses();
|
||||
for(String superClass : superClasses) {
|
||||
Node parent = locate.get(superClass);
|
||||
if(parent==null) {
|
||||
throw new RuntimeException("I can find parent Node for " + t.getName() + ". Missing parent is " + superClass);
|
||||
}
|
||||
Node node = new Node(t);
|
||||
parent.addChild(node);
|
||||
locate.put(t.getName(), node);
|
||||
if(!allowMultipleInheritance) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Tree<T> addLeaf(T leaf) {
|
||||
Tree<T> t = new Tree<T>(leaf);
|
||||
leafs.add(t);
|
||||
t.parent = this;
|
||||
t.locate = this.locate;
|
||||
locate.put(leaf, t);
|
||||
return t;
|
||||
}
|
||||
|
||||
public Tree<T> setAsParent(T parentRoot) {
|
||||
Tree<T> t = new Tree<T>(parentRoot);
|
||||
t.leafs.add(this);
|
||||
this.parent = t;
|
||||
t.locate = this.locate;
|
||||
t.locate.put(head, this);
|
||||
t.locate.put(parentRoot, t);
|
||||
return t;
|
||||
}
|
||||
|
||||
public T getHead() {
|
||||
return head;
|
||||
}
|
||||
|
||||
public Tree<T> getTree(T element) {
|
||||
return locate.get(element);
|
||||
}
|
||||
|
||||
public Tree<T> getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public Collection<T> getSuccessors(T root) {
|
||||
Collection<T> successors = new ArrayList<T>();
|
||||
Tree<T> tree = getTree(root);
|
||||
if (null != tree) {
|
||||
for (Tree<T> leaf : tree.leafs) {
|
||||
successors.add(leaf.head);
|
||||
}
|
||||
}
|
||||
return successors;
|
||||
}
|
||||
|
||||
public Collection<Tree<T>> getSubTrees() {
|
||||
return leafs;
|
||||
}
|
||||
|
||||
public static <T> Collection<T> getSuccessors(T of, Collection<Tree<T>> in) {
|
||||
for (Tree<T> tree : in) {
|
||||
if (tree.locate.containsKey(of)) {
|
||||
return tree.getSuccessors(of);
|
||||
}
|
||||
}
|
||||
return new ArrayList<T>();
|
||||
public Node getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return printTree(0);
|
||||
return root.toString();
|
||||
}
|
||||
|
||||
private static final int indent = 2;
|
||||
|
||||
private String printTree(int increment) {
|
||||
String s = "";
|
||||
String inc = "";
|
||||
for (int i = 0; i < increment; ++i) {
|
||||
inc = inc + " ";
|
||||
}
|
||||
s = inc + head;
|
||||
for (Tree<T> child : leafs) {
|
||||
s += "\n" + child.printTree(increment + indent);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
}
|
|
@ -1,45 +0,0 @@
|
|||
package org.gcube.informationsystem.utils.documentation.knowledge;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import org.gcube.informationsystem.types.reference.Type;
|
||||
|
||||
public class TypesKnowledge<T extends Type> {
|
||||
|
||||
protected Map<String, T> typeByName;
|
||||
protected Map<Integer, Set<T>> typesByLevels;
|
||||
protected Map<String, Integer> levelOfTypes;
|
||||
protected Map<String, Set<String>> children;
|
||||
|
||||
public TypesKnowledge() {
|
||||
this.typeByName = new HashMap<>();
|
||||
this.typesByLevels = new HashMap<>();
|
||||
this.levelOfTypes = new HashMap<>();
|
||||
this.children = new HashMap<>();
|
||||
}
|
||||
|
||||
public void addType(T type) {
|
||||
typeByName.put(type.getName(), type);
|
||||
Set<String> superClasses = type.getSuperClasses();
|
||||
for(String superClass : superClasses) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public Set<T> getTypesByLevel(int level) {
|
||||
return typesByLevels.get(level);
|
||||
}
|
||||
|
||||
public int getTypeLevel(Type type) {
|
||||
return getTypeLevel(type.getName());
|
||||
}
|
||||
|
||||
public int getTypeLevel(String typeName) {
|
||||
return levelOfTypes.get(typeName);
|
||||
}
|
||||
|
||||
|
||||
}
|
Loading…
Reference in New Issue