Improved knowledge and tree solution

This commit is contained in:
luca.frosini 2023-10-27 14:50:16 +02:00
parent 6db5256e70
commit b05f0c51f3
5 changed files with 81 additions and 28 deletions

View File

@ -64,7 +64,7 @@ public class ModelKnowledgeValidator implements DiscoveredElementAction<Element>
}
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);
return;
}

View File

@ -57,9 +57,13 @@ public class ModelKnowledge<T, TI extends TypeInformation<T>> {
AccessType[] modelTypes = AccessType.getModelTypes();
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);
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) {
addAllType(toRetry);
}

View File

@ -11,18 +11,53 @@ public class Tree<T> {
private boolean allowMultipleInheritance;
private Node<T> root;
private Node<T> rootNode;
private NodeInformation<T> ni;
private Map<String, Node<T>> locate;
public Tree(T t, NodeInformation<T> ni) {
public Tree() {
this.allowMultipleInheritance = true;
this.ni = ni;
this.locate = new HashMap<>();
this.root = new Node<>(t);
this.root.setTree(this);
String identifier = ni.getIdentifier(t);
this.locate.put(identifier, root);
}
public Tree(NodeInformation<T> ni) {
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) {
@ -43,17 +78,25 @@ public class Tree<T> {
Node<T> node = new Node<>(t);
node.setTree(this);
Set<String> parentIdentifiers = ni.getParentIdentifiers(root.getNodeElement(), t);
for(String parentIdentifier : parentIdentifiers) {
Node<T> parentNode = locate.get(parentIdentifier);
if(parentNode==null) {
throw new RuntimeException("I can find parent for " + identifier + ". Missing parent is " + parentIdentifier);
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.");
}
parentNode.addChild(node);
if(!allowMultipleInheritance) {
break;
} else {
for(String parentIdentifier : parentIdentifiers) {
Node<T> parentNode = locate.get(parentIdentifier);
if(parentNode==null) {
throw new RuntimeException("I can find parent for " + identifier + ". Missing parent is " + parentIdentifier);
}
parentNode.addChild(node);
if(!allowMultipleInheritance) {
break;
}
}
}
@ -62,16 +105,16 @@ public class Tree<T> {
return node;
}
public Node<T> getRoot() {
return root;
public Node<T> getRootNode() {
return rootNode;
}
@Override
public String toString() {
return root.toString();
return rootNode.toString();
}
public void elaborate(NodeElaborator<T> nodeElaborator) throws Exception {
root.elaborate(nodeElaborator);
rootNode.elaborate(nodeElaborator);
}
}

View File

@ -19,10 +19,12 @@ public class TypeInformation implements org.gcube.informationsystem.model.knowle
@Override
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 type.getExtendedTypes();
}

View File

@ -12,7 +12,6 @@ import java.util.Map.Entry;
import org.gcube.informationsystem.base.reference.AccessType;
import org.gcube.informationsystem.model.knowledge.ModelKnowledge;
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.NodeElaborator;
import org.gcube.informationsystem.tree.Tree;
@ -32,7 +31,7 @@ public class ModelKnowledgeTest{
private static final Logger logger = LoggerFactory.getLogger(ModelKnowledgeTest.class);
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 resourcesDirectory = logbackFile.getParentFile();
return new File(resourcesDirectory, "types");
@ -55,6 +54,7 @@ public class ModelKnowledgeTest{
String json = readFile(file);
List<Type> types = TypeMapper.deserializeTypeDefinitions(json);
modelKnowledge.addAllType(types);
logger.trace("-------------------------------------------------------");
}
for(AccessType modelType : modelTypes) {