Improved knowledge and tree solution
This commit is contained in:
parent
6db5256e70
commit
b05f0c51f3
|
@ -64,7 +64,7 @@ public class ModelKnowledgeValidator implements DiscoveredElementAction<Element>
|
||||||
}
|
}
|
||||||
|
|
||||||
String typeName = TypeMapper.getType(e);
|
String typeName = TypeMapper.getType(e);
|
||||||
if(node.getTree().getRoot().getNodeElement() == e) {
|
if(node.getTree().getRootNode().getNodeElement() == e) {
|
||||||
logger.info("{}- Type {} is the root type", stringBuffer.toString(), typeName);
|
logger.info("{}- Type {} is the root type", stringBuffer.toString(), typeName);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,9 +57,13 @@ public class ModelKnowledge<T, TI extends TypeInformation<T>> {
|
||||||
|
|
||||||
AccessType[] modelTypes = AccessType.getModelTypes();
|
AccessType[] modelTypes = AccessType.getModelTypes();
|
||||||
for(AccessType accessType : modelTypes) {
|
for(AccessType accessType : modelTypes) {
|
||||||
T t = typeInformation.getRoot(accessType);
|
|
||||||
|
|
||||||
Tree<T> tree = new Tree<>(t, typeInformation);
|
/*
|
||||||
|
* T t = typeInformation.getRoot(accessType);
|
||||||
|
* Tree<T> tree = new Tree<>(t, typeInformation);
|
||||||
|
*/
|
||||||
|
|
||||||
|
Tree<T> tree = new Tree<>(typeInformation);
|
||||||
trees.put(accessType, tree);
|
trees.put(accessType, tree);
|
||||||
|
|
||||||
if(accessType == AccessType.PROPERTY) {
|
if(accessType == AccessType.PROPERTY) {
|
||||||
|
@ -140,6 +144,10 @@ public class ModelKnowledge<T, TI extends TypeInformation<T>> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(types.size()==toRetry.size()) {
|
||||||
|
throw new RuntimeException("Unable to find parent for " + toRetry.toString());
|
||||||
|
}
|
||||||
|
|
||||||
if(toRetry.size()>0) {
|
if(toRetry.size()>0) {
|
||||||
addAllType(toRetry);
|
addAllType(toRetry);
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,18 +11,53 @@ public class Tree<T> {
|
||||||
|
|
||||||
private boolean allowMultipleInheritance;
|
private boolean allowMultipleInheritance;
|
||||||
|
|
||||||
private Node<T> root;
|
private Node<T> rootNode;
|
||||||
private NodeInformation<T> ni;
|
private NodeInformation<T> ni;
|
||||||
private Map<String, Node<T>> locate;
|
private Map<String, Node<T>> locate;
|
||||||
|
|
||||||
public Tree(T t, NodeInformation<T> ni) {
|
public Tree() {
|
||||||
this.allowMultipleInheritance = true;
|
this.allowMultipleInheritance = true;
|
||||||
this.ni = ni;
|
|
||||||
this.locate = new HashMap<>();
|
this.locate = new HashMap<>();
|
||||||
this.root = new Node<>(t);
|
}
|
||||||
this.root.setTree(this);
|
|
||||||
String identifier = ni.getIdentifier(t);
|
public Tree(NodeInformation<T> ni) {
|
||||||
this.locate.put(identifier, root);
|
this();
|
||||||
|
setNodeInformation(ni);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Tree(T root, NodeInformation<T> ni) {
|
||||||
|
this(ni);
|
||||||
|
setRoot(root);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the NodeInformation if and only if it was not previously set.
|
||||||
|
* Otherwise this function has no effect.
|
||||||
|
* @param ni the NodeInformation to set
|
||||||
|
*/
|
||||||
|
public void setNodeInformation(NodeInformation<T> ni) {
|
||||||
|
if(this.ni==null) {
|
||||||
|
this.ni = ni;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set the root if and only if it was not previously set.
|
||||||
|
* Otherwise this function has no effect.
|
||||||
|
* This function raises a RuntimeException is the
|
||||||
|
* NodeInformation was not previously set.
|
||||||
|
* @param root the root to set
|
||||||
|
*/
|
||||||
|
public void setRoot(T root) throws RuntimeException {
|
||||||
|
if(this.ni==null) {
|
||||||
|
throw new RuntimeException("You must set the NodeInformation instance first");
|
||||||
|
}
|
||||||
|
if(this.rootNode==null) {
|
||||||
|
this.rootNode = new Node<>(root);
|
||||||
|
this.rootNode.setTree(this);
|
||||||
|
String identifier = ni.getIdentifier(root);
|
||||||
|
this.locate.put(identifier, rootNode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAllowMultipleInheritance(boolean allowMultipleInheritance) {
|
public void setAllowMultipleInheritance(boolean allowMultipleInheritance) {
|
||||||
|
@ -43,7 +78,14 @@ public class Tree<T> {
|
||||||
Node<T> node = new Node<>(t);
|
Node<T> node = new Node<>(t);
|
||||||
node.setTree(this);
|
node.setTree(this);
|
||||||
|
|
||||||
Set<String> parentIdentifiers = ni.getParentIdentifiers(root.getNodeElement(), t);
|
Set<String> parentIdentifiers = ni.getParentIdentifiers(rootNode!= null ? rootNode.getNodeElement() : null, t);
|
||||||
|
if(parentIdentifiers==null || parentIdentifiers.size()==0) {
|
||||||
|
if(this.rootNode==null) {
|
||||||
|
this.rootNode = node;
|
||||||
|
}else {
|
||||||
|
throw new RuntimeException("A Tree cannot have two root. " + t.toString() + " has not parent.");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
for(String parentIdentifier : parentIdentifiers) {
|
for(String parentIdentifier : parentIdentifiers) {
|
||||||
Node<T> parentNode = locate.get(parentIdentifier);
|
Node<T> parentNode = locate.get(parentIdentifier);
|
||||||
if(parentNode==null) {
|
if(parentNode==null) {
|
||||||
|
@ -56,22 +98,23 @@ public class Tree<T> {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
this.locate.put(identifier, node);
|
this.locate.put(identifier, node);
|
||||||
|
|
||||||
return node;
|
return node;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Node<T> getRoot() {
|
public Node<T> getRootNode() {
|
||||||
return root;
|
return rootNode;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return root.toString();
|
return rootNode.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void elaborate(NodeElaborator<T> nodeElaborator) throws Exception {
|
public void elaborate(NodeElaborator<T> nodeElaborator) throws Exception {
|
||||||
root.elaborate(nodeElaborator);
|
rootNode.elaborate(nodeElaborator);
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -19,10 +19,12 @@ public class TypeInformation implements org.gcube.informationsystem.model.knowle
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Set<String> getParentIdentifiers(Type root, Type type) {
|
public Set<String> getParentIdentifiers(Type root, Type type) {
|
||||||
if(type.getName().compareTo(root.getName())==0) {
|
if(root!=null && type.getName().compareTo(root.getName())==0) {
|
||||||
|
return new LinkedHashSet<>();
|
||||||
|
}
|
||||||
|
if(root==null && AccessType.getAccessType(type.getName())!=null) {
|
||||||
return new LinkedHashSet<>();
|
return new LinkedHashSet<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
return type.getExtendedTypes();
|
return type.getExtendedTypes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,7 +12,6 @@ import java.util.Map.Entry;
|
||||||
import org.gcube.informationsystem.base.reference.AccessType;
|
import org.gcube.informationsystem.base.reference.AccessType;
|
||||||
import org.gcube.informationsystem.model.knowledge.ModelKnowledge;
|
import org.gcube.informationsystem.model.knowledge.ModelKnowledge;
|
||||||
import org.gcube.informationsystem.model.knowledge.UsageKnowledge;
|
import org.gcube.informationsystem.model.knowledge.UsageKnowledge;
|
||||||
import org.gcube.informationsystem.queries.templates.QueryTemplateTest;
|
|
||||||
import org.gcube.informationsystem.tree.Node;
|
import org.gcube.informationsystem.tree.Node;
|
||||||
import org.gcube.informationsystem.tree.NodeElaborator;
|
import org.gcube.informationsystem.tree.NodeElaborator;
|
||||||
import org.gcube.informationsystem.tree.Tree;
|
import org.gcube.informationsystem.tree.Tree;
|
||||||
|
@ -32,7 +31,7 @@ public class ModelKnowledgeTest{
|
||||||
private static final Logger logger = LoggerFactory.getLogger(ModelKnowledgeTest.class);
|
private static final Logger logger = LoggerFactory.getLogger(ModelKnowledgeTest.class);
|
||||||
|
|
||||||
protected File getTypesDirectory() throws Exception {
|
protected File getTypesDirectory() throws Exception {
|
||||||
URL logbackFileURL = QueryTemplateTest.class.getClassLoader().getResource("logback-test.xml");
|
URL logbackFileURL = ModelKnowledgeTest.class.getClassLoader().getResource("logback-test.xml");
|
||||||
File logbackFile = new File(logbackFileURL.toURI());
|
File logbackFile = new File(logbackFileURL.toURI());
|
||||||
File resourcesDirectory = logbackFile.getParentFile();
|
File resourcesDirectory = logbackFile.getParentFile();
|
||||||
return new File(resourcesDirectory, "types");
|
return new File(resourcesDirectory, "types");
|
||||||
|
@ -55,6 +54,7 @@ public class ModelKnowledgeTest{
|
||||||
String json = readFile(file);
|
String json = readFile(file);
|
||||||
List<Type> types = TypeMapper.deserializeTypeDefinitions(json);
|
List<Type> types = TypeMapper.deserializeTypeDefinitions(json);
|
||||||
modelKnowledge.addAllType(types);
|
modelKnowledge.addAllType(types);
|
||||||
|
logger.trace("-------------------------------------------------------");
|
||||||
}
|
}
|
||||||
|
|
||||||
for(AccessType modelType : modelTypes) {
|
for(AccessType modelType : modelTypes) {
|
||||||
|
|
Loading…
Reference in New Issue