Ported lib to new is-model utilities
This commit is contained in:
parent
183d662403
commit
195a3af39b
10
pom.xml
10
pom.xml
|
@ -75,11 +75,11 @@
|
|||
<artifactId>logback-classic</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.gcube.resource-management</groupId>
|
||||
<artifactId>gcube-model</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<!-- <dependency> -->
|
||||
<!-- <groupId>org.gcube.resource-management</groupId> -->
|
||||
<!-- <artifactId>gcube-model</artifactId> -->
|
||||
<!-- <scope>test</scope> -->
|
||||
<!-- </dependency> -->
|
||||
</dependencies>
|
||||
|
||||
</project>
|
|
@ -1,86 +0,0 @@
|
|||
package org.gcube.informationsystem.utils.documentation.generator;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
import org.gcube.informationsystem.base.reference.AccessType;
|
||||
import org.gcube.informationsystem.base.reference.Element;
|
||||
import org.gcube.informationsystem.types.TypeMapper;
|
||||
import org.gcube.informationsystem.types.reference.Type;
|
||||
import org.gcube.informationsystem.utils.discovery.ElementSpecilizationDiscovery;
|
||||
import org.gcube.informationsystem.utils.discovery.RegistrationProvider;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class ClassesDiscoveryGenerator extends Generator {
|
||||
|
||||
protected TreeGenerator treeGenerator;
|
||||
|
||||
public ClassesDiscoveryGenerator() {
|
||||
|
||||
}
|
||||
|
||||
public List<Package> getPackages(){
|
||||
|
||||
List<Package> packages = new ArrayList<Package>();
|
||||
|
||||
AccessType[] accessTypes = new AccessType[] {
|
||||
AccessType.PROPERTY,
|
||||
AccessType.ENTITY,
|
||||
AccessType.RELATION
|
||||
};
|
||||
|
||||
for(AccessType accessType : accessTypes) {
|
||||
Class<Element> clz = accessType.getTypeClass();
|
||||
packages.add(clz.getPackage());
|
||||
}
|
||||
|
||||
return packages;
|
||||
}
|
||||
|
||||
public List<Package> getExtensionsPackages(){
|
||||
List<Package> packages = new ArrayList<Package>();
|
||||
ServiceLoader<? extends RegistrationProvider> regsitrationProviders = ServiceLoader
|
||||
.load(RegistrationProvider.class);
|
||||
for(RegistrationProvider registrationProvider : regsitrationProviders) {
|
||||
packages.addAll(registrationProvider.getPackagesToRegister());
|
||||
}
|
||||
return packages;
|
||||
}
|
||||
|
||||
public void generate() throws Exception {
|
||||
List<Package> packages = getPackages();
|
||||
packages.addAll(getExtensionsPackages());
|
||||
generate(packages);
|
||||
}
|
||||
|
||||
protected <E extends Element> void discover(AccessType at, List<Package> packages) throws Exception {
|
||||
ElementSpecilizationDiscovery<E> discovery = new ElementSpecilizationDiscovery<>(at.getTypeClass(), false);
|
||||
if(Objects.nonNull(packages)) {
|
||||
for(Package p : packages) {
|
||||
discovery.addPackage(p);
|
||||
}
|
||||
}
|
||||
discovery.discover();
|
||||
|
||||
for(Class<? extends E> clz : discovery.getDiscovered()) {
|
||||
Type type = TypeMapper.createTypeDefinition(clz);
|
||||
treeGenerator.addType(type);
|
||||
}
|
||||
}
|
||||
|
||||
public void generate(List<Package> packages) throws Exception {
|
||||
treeGenerator = new TreeGenerator();
|
||||
AccessType[] types = new AccessType[] {
|
||||
AccessType.PROPERTY,
|
||||
AccessType.RESOURCE, AccessType.FACET,
|
||||
AccessType.CONSISTS_OF, AccessType.IS_RELATED_TO};
|
||||
for(AccessType at : types) {
|
||||
discover(at, packages);
|
||||
}
|
||||
treeGenerator.generate();
|
||||
}
|
||||
}
|
|
@ -25,14 +25,8 @@ public class Generator {
|
|||
|
||||
private static final Logger logger = LoggerFactory.getLogger(Generator.class);
|
||||
|
||||
protected AccessType[] accessTypes;
|
||||
|
||||
public Generator() {
|
||||
accessTypes = new AccessType[] {
|
||||
AccessType.PROPERTY,
|
||||
AccessType.ENTITY, AccessType.RESOURCE, AccessType.FACET,
|
||||
AccessType.RELATION, AccessType.CONSISTS_OF, AccessType.IS_RELATED_TO
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
public Class<? extends DocumentationGenerator> getDocumentationGeneratorClass(AccessType accessType)
|
||||
|
@ -132,6 +126,14 @@ public class Generator {
|
|||
return file;
|
||||
}
|
||||
|
||||
protected void writeSectionOnly(Type type, File file, Integer level) throws Exception {
|
||||
DocumentationGenerator dg = getDocumentationGenerator(type);
|
||||
if(level!=null) {
|
||||
dg.setLevel(level);
|
||||
}
|
||||
StringBuffer sb = dg.generateSection();
|
||||
Files.write(file.toPath(), sb.toString().getBytes(), StandardOpenOption.APPEND);
|
||||
}
|
||||
|
||||
protected void writeTypeToFile(Type type, File file, Integer level) throws Exception {
|
||||
DocumentationGenerator dg = getDocumentationGenerator(type);
|
||||
|
|
|
@ -1,24 +1,19 @@
|
|||
package org.gcube.informationsystem.utils.documentation.generator;
|
||||
|
||||
import java.io.File;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.StandardOpenOption;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TreeMap;
|
||||
import java.util.Collection;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
import org.gcube.informationsystem.base.reference.AccessType;
|
||||
import org.gcube.informationsystem.model.reference.properties.Encrypted;
|
||||
import org.gcube.informationsystem.model.reference.properties.Header;
|
||||
import org.gcube.informationsystem.model.reference.properties.PropagationConstraint;
|
||||
import org.gcube.informationsystem.model.reference.properties.Property;
|
||||
import org.gcube.informationsystem.base.reference.Element;
|
||||
import org.gcube.informationsystem.discovery.ISModelRegistrationProvider;
|
||||
import org.gcube.informationsystem.discovery.RegistrationProvider;
|
||||
import org.gcube.informationsystem.discovery.knowledge.ModelKnowledge;
|
||||
import org.gcube.informationsystem.tree.Node;
|
||||
import org.gcube.informationsystem.tree.NodeElaborator;
|
||||
import org.gcube.informationsystem.tree.Tree;
|
||||
import org.gcube.informationsystem.types.TypeMapper;
|
||||
import org.gcube.informationsystem.types.reference.Type;
|
||||
import org.gcube.informationsystem.utils.documentation.model.DocumentationGenerator;
|
||||
import org.gcube.informationsystem.utils.knowledge.Node;
|
||||
import org.gcube.informationsystem.utils.knowledge.NodeElaborator;
|
||||
import org.gcube.informationsystem.utils.knowledge.Tree;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
|
@ -34,62 +29,36 @@ public class TreeGenerator extends Generator {
|
|||
public static final String ENTITIES_FILENAME = "entities.rst";
|
||||
public static final String RELATIONS_FILENAME = "relations.rst";
|
||||
|
||||
protected static final List<Class<? extends Property>> isModelProperties;
|
||||
|
||||
static {
|
||||
isModelProperties = new ArrayList<>();
|
||||
isModelProperties.add(Header.class);
|
||||
isModelProperties.add(Encrypted.class);
|
||||
isModelProperties.add(PropagationConstraint.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) {
|
||||
// Head has been already added
|
||||
return;
|
||||
}
|
||||
Tree tree = types.get(accessType);
|
||||
tree.addNode(type);
|
||||
}
|
||||
|
||||
public void elaborateTree(final AccessType at, Tree tree, final File file) throws Exception {
|
||||
public void elaborateTree(final AccessType at, Tree<Class<Element>> tree, File file, RegistrationProvider rp) throws Exception {
|
||||
logger.info("Going to elaborate the following type tree\n{}", tree.toString());
|
||||
|
||||
NodeElaborator documentationNE = new NodeElaborator() {
|
||||
|
||||
protected void writeSectionOnly(Type type, int level) throws Exception {
|
||||
DocumentationGenerator dg = getDocumentationGenerator(type);
|
||||
dg.setLevel(level);
|
||||
StringBuffer sb = dg.generateSection();
|
||||
Files.write(file.toPath(), sb.toString().getBytes(), StandardOpenOption.APPEND);
|
||||
}
|
||||
Collection<Package> packages = rp.getPackagesToRegister();
|
||||
|
||||
NodeElaborator<Class<Element>> documentationNE = new NodeElaborator<Class<Element>>() {
|
||||
|
||||
@Override
|
||||
public void elaborate(Node node, int level) throws Exception {
|
||||
Type type = node.getType();
|
||||
public void elaborate(Node<Class<Element>> node, int level) throws Exception {
|
||||
Class<Element> clz = node.getNodeElement();
|
||||
if(level!=0 && !packages.contains(clz.getPackage())) {
|
||||
logger.debug("Discarding {} because it does not belong to {}", clz.getSimpleName(), rp.getModelName());
|
||||
return;
|
||||
}
|
||||
Type type = TypeMapper.createTypeDefinition(clz);
|
||||
if(level==0) {
|
||||
/*
|
||||
* Root node has been already documented in IS_MODEL_FILENAME
|
||||
* Going to skip it
|
||||
*/
|
||||
writeSectionOnly(type, level);
|
||||
writeSectionOnly(type, file, level);
|
||||
} else {
|
||||
writeTypeToFile(type, file, level);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
tree.elaborate(documentationNE);
|
||||
|
@ -97,36 +66,49 @@ public class TreeGenerator extends Generator {
|
|||
|
||||
public void generate() throws Exception {
|
||||
|
||||
ModelKnowledge modelKnowledge = ModelKnowledge.getInstance();
|
||||
modelKnowledge.createKnowledge();
|
||||
|
||||
File is = getFile(IS_MODEL_FILENAME, true);
|
||||
File file = null;
|
||||
|
||||
for(AccessType at : accessTypes) {
|
||||
Type type = TypeMapper.createTypeDefinition(at.getTypeClass());
|
||||
writeTypeToFile(type, is);
|
||||
for(AccessType at : AccessType.getModelTypes()) {
|
||||
|
||||
|
||||
switch (at) {
|
||||
case PROPERTY:
|
||||
file = getFile(PROPERTIES_FILENAME, true);
|
||||
for(Class<? extends Property> clz : isModelProperties) {
|
||||
Type t = TypeMapper.createTypeDefinition(clz);
|
||||
writeTypeToFile(t, is, 1);
|
||||
}
|
||||
break;
|
||||
|
||||
case ENTITY:
|
||||
case RESOURCE:
|
||||
file = getFile(ENTITIES_FILENAME, true);
|
||||
continue;
|
||||
Type type = TypeMapper.createTypeDefinition(AccessType.ENTITY.getTypeClass());
|
||||
writeTypeToFile(type, is);
|
||||
break;
|
||||
|
||||
case RELATION:
|
||||
case IS_RELATED_TO:
|
||||
file = getFile(RELATIONS_FILENAME, true);
|
||||
continue;
|
||||
type = TypeMapper.createTypeDefinition(AccessType.RELATION.getTypeClass());
|
||||
writeTypeToFile(type, is);
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
Tree tree = types.get(at);
|
||||
elaborateTree(at, tree, file);
|
||||
Type type = TypeMapper.createTypeDefinition(at.getTypeClass());
|
||||
writeTypeToFile(type, is);
|
||||
|
||||
Tree<Class<Element>> tree = modelKnowledge.getClassesTree(at);
|
||||
|
||||
ServiceLoader<? extends RegistrationProvider> registrationProviders = ServiceLoader
|
||||
.load(RegistrationProvider.class);
|
||||
for(RegistrationProvider rp : registrationProviders) {
|
||||
if(!(rp instanceof ISModelRegistrationProvider)) {
|
||||
elaborateTree(at, tree, file, rp);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,11 +3,11 @@ package org.gcube.informationsystem.utils.documentation.model.entities;
|
|||
import java.util.Set;
|
||||
|
||||
import org.gcube.informationsystem.base.reference.AccessType;
|
||||
import org.gcube.informationsystem.discovery.knowledge.UsageKnowledge;
|
||||
import org.gcube.informationsystem.types.reference.Type;
|
||||
import org.gcube.informationsystem.types.reference.properties.LinkedEntity;
|
||||
import org.gcube.informationsystem.types.reference.properties.PropertyDefinition;
|
||||
import org.gcube.informationsystem.utils.documentation.rst.table.Table;
|
||||
import org.gcube.informationsystem.utils.knowledge.UsageKnowledge;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
|
|
|
@ -3,11 +3,11 @@ package org.gcube.informationsystem.utils.documentation.model.entities;
|
|||
import java.util.Set;
|
||||
|
||||
import org.gcube.informationsystem.base.reference.AccessType;
|
||||
import org.gcube.informationsystem.discovery.knowledge.UsageKnowledge;
|
||||
import org.gcube.informationsystem.types.reference.Type;
|
||||
import org.gcube.informationsystem.types.reference.entities.ResourceType;
|
||||
import org.gcube.informationsystem.types.reference.properties.LinkedEntity;
|
||||
import org.gcube.informationsystem.utils.documentation.rst.table.Table;
|
||||
import org.gcube.informationsystem.utils.knowledge.UsageKnowledge;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
|
|
|
@ -3,10 +3,10 @@ package org.gcube.informationsystem.utils.documentation.model.relations;
|
|||
import java.util.Set;
|
||||
|
||||
import org.gcube.informationsystem.base.reference.AccessType;
|
||||
import org.gcube.informationsystem.discovery.knowledge.UsageKnowledge;
|
||||
import org.gcube.informationsystem.types.reference.Type;
|
||||
import org.gcube.informationsystem.types.reference.properties.LinkedEntity;
|
||||
import org.gcube.informationsystem.utils.documentation.rst.table.Table;
|
||||
import org.gcube.informationsystem.utils.knowledge.UsageKnowledge;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
|
|
|
@ -3,10 +3,10 @@ package org.gcube.informationsystem.utils.documentation.model.relations;
|
|||
import java.util.Set;
|
||||
|
||||
import org.gcube.informationsystem.base.reference.AccessType;
|
||||
import org.gcube.informationsystem.discovery.knowledge.UsageKnowledge;
|
||||
import org.gcube.informationsystem.types.reference.Type;
|
||||
import org.gcube.informationsystem.types.reference.properties.LinkedEntity;
|
||||
import org.gcube.informationsystem.utils.documentation.rst.table.Table;
|
||||
import org.gcube.informationsystem.utils.knowledge.UsageKnowledge;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
package org.gcube.informationsystem.utils.documentation;
|
||||
|
||||
import org.gcube.informationsystem.utils.documentation.generator.ClassesDiscoveryGenerator;
|
||||
import org.gcube.informationsystem.utils.documentation.generator.TreeGenerator;
|
||||
import org.gcube.informationsystem.utils.documentation.model.DocumentationGenerator;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -12,8 +12,8 @@ public class GenerateTest {
|
|||
@Test
|
||||
public void testDocGeneration() throws Exception {
|
||||
DocumentationGenerator.setDefaultOffsetLevel(2);
|
||||
ClassesDiscoveryGenerator documentationGenerator = new ClassesDiscoveryGenerator();
|
||||
documentationGenerator.generate();
|
||||
TreeGenerator treeGenerator = new TreeGenerator();
|
||||
treeGenerator.generate();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue