Implementing knowledge
This commit is contained in:
parent
a1573b5238
commit
a7d98b821d
|
@ -80,7 +80,7 @@ public enum AccessType {
|
|||
|
||||
private static Logger logger = LoggerFactory.getLogger(AccessType.class);
|
||||
|
||||
|
||||
private static AccessType[] modelTypes;
|
||||
private static Set<String> names;
|
||||
|
||||
static {
|
||||
|
@ -90,6 +90,12 @@ public enum AccessType {
|
|||
String name = accessType.getName();
|
||||
names.add(name);
|
||||
}
|
||||
|
||||
modelTypes = new AccessType[] {
|
||||
AccessType.PROPERTY,
|
||||
AccessType.RESOURCE, AccessType.FACET,
|
||||
AccessType.IS_RELATED_TO, AccessType.CONSISTS_OF
|
||||
};
|
||||
}
|
||||
|
||||
private final Class<? extends Element> clz;
|
||||
|
@ -179,4 +185,9 @@ public enum AccessType {
|
|||
throw new RuntimeException(error);
|
||||
}
|
||||
}
|
||||
|
||||
public static AccessType[] getModelTypes() {
|
||||
return modelTypes;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,40 @@
|
|||
package org.gcube.informationsystem.discovery;
|
||||
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.Set;
|
||||
|
||||
import org.gcube.informationsystem.base.reference.Element;
|
||||
import org.gcube.informationsystem.tree.NodeElementInformation;
|
||||
import org.gcube.informationsystem.types.TypeMapper;
|
||||
|
||||
public class ClassElementInformation implements NodeElementInformation<Class<Element>> {
|
||||
|
||||
@Override
|
||||
public String getIdentifier(Class<Element> clz) {
|
||||
return TypeMapper.getType(clz);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getParentIdentifiers(Class<Element> root, Class<Element> clz) {
|
||||
Set<String> ret = new LinkedHashSet<>();
|
||||
if(clz == root) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
Class<?>[] interfaces = clz.getInterfaces();
|
||||
for(Class<?> i : interfaces) {
|
||||
if(root.isAssignableFrom(i)) {
|
||||
@SuppressWarnings("unchecked")
|
||||
String id = getIdentifier((Class<Element>) i);
|
||||
ret.add(id);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void extraElaboration(Class<Element> t) {
|
||||
// Nothing to do
|
||||
}
|
||||
|
||||
}
|
|
@ -8,8 +8,8 @@ import org.gcube.informationsystem.base.reference.Element;
|
|||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public interface DiscoveredElementAction {
|
||||
public interface DiscoveredElementAction<E extends Element> {
|
||||
|
||||
public <E extends Element> void analizeElement(Class<E> e) throws Exception;
|
||||
public void analizeElement(Class<E> e) throws Exception;
|
||||
|
||||
}
|
||||
|
|
|
@ -6,10 +6,12 @@ package org.gcube.informationsystem.discovery;
|
|||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
||||
import org.gcube.informationsystem.base.reference.Element;
|
||||
import org.gcube.informationsystem.base.reference.properties.PropertyElement;
|
||||
|
@ -26,9 +28,9 @@ public class Discovery<E extends Element> {
|
|||
public static Logger logger = LoggerFactory.getLogger(Discovery.class);
|
||||
|
||||
protected final Class<E> root;
|
||||
protected final List<Package> packages;
|
||||
protected final List<Class<? extends E>> discoveredElement;
|
||||
protected final List<DiscoveredElementAction> discoveredElementActions;
|
||||
protected final Set<Package> packages;
|
||||
protected final Set<Class<E>> discoveredElement;
|
||||
protected final Set<DiscoveredElementAction<Element>> discoveredElementActions;
|
||||
|
||||
protected final boolean includeRootPackage;
|
||||
|
||||
|
@ -40,26 +42,27 @@ public class Discovery<E extends Element> {
|
|||
|
||||
public Discovery(Class<E> root, boolean includeRootPackage) throws Exception {
|
||||
this.root = root;
|
||||
this.packages = new ArrayList<>();
|
||||
this.packages = new HashSet<>();
|
||||
this.includeRootPackage = includeRootPackage;
|
||||
if(includeRootPackage) {
|
||||
addPackage(root.getPackage());
|
||||
}
|
||||
this.discovered = false;
|
||||
this.discoveredElement = new ArrayList<>();
|
||||
this.discoveredElementActions = new ArrayList<>();
|
||||
this.discoveredElement = new TreeSet<>();
|
||||
this.discoveredElementActions = new TreeSet<>();
|
||||
add(root);
|
||||
}
|
||||
|
||||
public List<Class<? extends E>> getDiscoveredElement() {
|
||||
public Set<Class<E>> getDiscoveredElement() {
|
||||
return discoveredElement;
|
||||
}
|
||||
|
||||
public synchronized void addDiscoveredElementActions(DiscoveredElementAction discoveredElementAction) throws Exception {
|
||||
@SuppressWarnings("unchecked")
|
||||
public synchronized void addDiscoveredElementActions(DiscoveredElementAction<Element> discoveredElementAction) throws Exception {
|
||||
discoveredElementActions.add(discoveredElementAction);
|
||||
if(discovered) {
|
||||
for(Class<? extends E> clz : discoveredElement) {
|
||||
discoveredElementAction.analizeElement(clz);
|
||||
for(Class<E> clz : discoveredElement) {
|
||||
discoveredElementAction.analizeElement((Class<Element>) clz);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -71,17 +74,23 @@ public class Discovery<E extends Element> {
|
|||
packages.add(p);
|
||||
}
|
||||
|
||||
protected void add(Class<? extends E> clz) throws Exception {
|
||||
public void addPackages(Collection<Package> packages) {
|
||||
for(Package p : packages) {
|
||||
addPackage(p);
|
||||
}
|
||||
}
|
||||
|
||||
protected void add(Class<E> clz) throws Exception {
|
||||
if(!discoveredElement.contains(clz)) {
|
||||
discoveredElement.add(clz);
|
||||
for(DiscoveredElementAction discoveredElementAction : discoveredElementActions) {
|
||||
for(DiscoveredElementAction<E> discoveredElementAction : discoveredElementActions) {
|
||||
discoveredElementAction.analizeElement(clz);
|
||||
}
|
||||
logger.info("+ Added {}.", clz);
|
||||
}
|
||||
}
|
||||
|
||||
protected void analizeElement(Class<? extends E> clz) throws Exception {
|
||||
protected void analizeElement(Class<E> clz) throws Exception {
|
||||
logger.trace("Analizyng {}", clz);
|
||||
|
||||
if(!clz.isInterface()) {
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
package org.gcube.informationsystem.discovery.knowledge;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.gcube.informationsystem.base.reference.AccessType;
|
||||
import org.gcube.informationsystem.base.reference.Element;
|
||||
import org.gcube.informationsystem.discovery.Discovery;
|
||||
import org.gcube.informationsystem.discovery.RegistrationProvider;
|
||||
import org.gcube.informationsystem.serialization.ElementMappingAction;
|
||||
import org.gcube.informationsystem.tree.AddElementToTreeAction;
|
||||
import org.gcube.informationsystem.tree.Tree;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class ModelExtensionKnowledge {
|
||||
|
||||
protected RegistrationProvider registrationProvider;
|
||||
|
||||
protected Map<AccessType, Tree<Class<Element>>> trees;
|
||||
|
||||
public ModelExtensionKnowledge(RegistrationProvider registrationProvider) {
|
||||
this.registrationProvider = registrationProvider;
|
||||
this.trees = new HashMap<>();
|
||||
}
|
||||
|
||||
public void createKnowledge() throws Exception {
|
||||
|
||||
AccessType[] modelTypes = AccessType.getModelTypes();
|
||||
|
||||
for(AccessType accessType : modelTypes) {
|
||||
Class<Element> clz = accessType.getTypeClass();
|
||||
|
||||
Tree<Class<Element>> tree = new Tree<>(clz, null);
|
||||
trees.put(accessType, tree);
|
||||
AddElementToTreeAction aetta = new AddElementToTreeAction(tree);
|
||||
|
||||
Discovery<? extends Element> discovery = new Discovery<>(clz, false);
|
||||
discovery.addPackages(registrationProvider.getPackagesToRegister());
|
||||
discovery.addDiscoveredElementActions(new ElementMappingAction());
|
||||
discovery.addDiscoveredElementActions(aetta);
|
||||
|
||||
|
||||
discovery.discover();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
package org.gcube.informationsystem.discovery.knowledge;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public interface NodeElaborator<C extends Comparable<C>> {
|
||||
|
||||
public void elaborate(Node<C> node, int level) throws Exception;
|
||||
|
||||
}
|
|
@ -1,73 +0,0 @@
|
|||
package org.gcube.informationsystem.discovery.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;
|
||||
}
|
||||
|
||||
}
|
|
@ -9,10 +9,10 @@ import org.gcube.informationsystem.discovery.DiscoveredElementAction;
|
|||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
class ElementMappingAction implements DiscoveredElementAction {
|
||||
public class ElementMappingAction implements DiscoveredElementAction<Element> {
|
||||
|
||||
@Override
|
||||
public <E extends Element> void analizeElement(Class<E> e) throws Exception {
|
||||
public void analizeElement(Class<Element> e) throws Exception {
|
||||
ElementMapper.registerSubtype(e);
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,25 @@
|
|||
/**
|
||||
*
|
||||
*/
|
||||
package org.gcube.informationsystem.tree;
|
||||
|
||||
import org.gcube.informationsystem.base.reference.Element;
|
||||
import org.gcube.informationsystem.discovery.DiscoveredElementAction;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class AddElementToTreeAction implements DiscoveredElementAction<Element> {
|
||||
|
||||
protected Tree<Class<Element>> tree;
|
||||
|
||||
public AddElementToTreeAction(Tree<Class<Element>> tree) {
|
||||
this.tree = tree;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void analizeElement(Class<Element> e) throws Exception {
|
||||
tree.addNode(e);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
package org.gcube.informationsystem.discovery.knowledge;
|
||||
package org.gcube.informationsystem.tree;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
@ -6,57 +6,57 @@ import java.util.TreeSet;
|
|||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class Node<C extends Comparable<C>> implements Comparable<Node<C>> {
|
||||
public class Node<T> implements Comparable<Node<T>> {
|
||||
|
||||
public static String INDENTATION = "\t";
|
||||
|
||||
private C c;
|
||||
private Tree<C> tree;
|
||||
private T t;
|
||||
private Tree<T> tree;
|
||||
|
||||
private Node<C> parent;
|
||||
private Set<Node<C>> children;
|
||||
private Node<T> parent;
|
||||
private Set<Node<T>> children;
|
||||
|
||||
public Node(C c) {
|
||||
this.c = c;
|
||||
public Node(T t) {
|
||||
this.t = t;
|
||||
this.children = new TreeSet<>();
|
||||
}
|
||||
|
||||
public Tree<C> getTree() {
|
||||
public Tree<T> getTree() {
|
||||
return tree;
|
||||
}
|
||||
|
||||
private void setTree(Tree<C> tree) {
|
||||
private void setTree(Tree<T> tree) {
|
||||
this.tree = tree;
|
||||
}
|
||||
|
||||
public C getNodeElement() {
|
||||
return c;
|
||||
public T getNodeElement() {
|
||||
return t;
|
||||
}
|
||||
|
||||
public Node<C> getParent() {
|
||||
public Node<T> getParent() {
|
||||
return parent;
|
||||
}
|
||||
|
||||
public Node<C> setParent(Node<C> parent) {
|
||||
public Node<T> setParent(Node<T> parent) {
|
||||
this.parent = parent;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Node<C> addChild(Node<C> child) {
|
||||
public Node<T> addChild(Node<T> child) {
|
||||
children.add(child);
|
||||
child.setParent(this);
|
||||
child.setTree(tree);
|
||||
return this;
|
||||
}
|
||||
|
||||
public Set<Node<C>> getChildrenNodes() {
|
||||
public Set<Node<T>> getChildrenNodes() {
|
||||
return children;
|
||||
}
|
||||
|
||||
public Set<C> getChildrenElement() {
|
||||
Set<C> children = new TreeSet<>();
|
||||
for (Node<C> child : this.children) {
|
||||
children.add(child.c);
|
||||
public Set<T> getChildrenElement() {
|
||||
Set<T> children = new TreeSet<>();
|
||||
for (Node<T> child : this.children) {
|
||||
children.add(child.t);
|
||||
}
|
||||
return children;
|
||||
}
|
||||
|
@ -71,8 +71,8 @@ public class Node<C extends Comparable<C>> implements Comparable<Node<C>> {
|
|||
for (int i = 0; i < level; ++i) {
|
||||
stringBuffer.append(Node.INDENTATION);
|
||||
}
|
||||
stringBuffer.append(tree.getNodeElementInformation().getIdentifier(c));
|
||||
for (Node<C> child : children) {
|
||||
stringBuffer.append(tree.getNodeElementInformation().getIdentifier(t));
|
||||
for (Node<T> child : children) {
|
||||
stringBuffer.append("\n");
|
||||
stringBuffer.append(child.createTree(level+1));
|
||||
}
|
||||
|
@ -80,7 +80,7 @@ public class Node<C extends Comparable<C>> implements Comparable<Node<C>> {
|
|||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(Node<C> other) {
|
||||
public int compareTo(Node<T> other) {
|
||||
if (this == other) {
|
||||
return 0;
|
||||
}
|
||||
|
@ -91,19 +91,19 @@ public class Node<C extends Comparable<C>> implements Comparable<Node<C>> {
|
|||
return -1;
|
||||
}
|
||||
|
||||
NodeElementInformation<C> nei = tree.getNodeElementInformation();
|
||||
NodeElementInformation<T> nei = tree.getNodeElementInformation();
|
||||
|
||||
return nei.getIdentifier(c).compareTo(nei.getIdentifier(other.c));
|
||||
return nei.getIdentifier(t).compareTo(nei.getIdentifier(other.t));
|
||||
}
|
||||
|
||||
|
||||
public void elaborate(NodeElaborator<C> nodeElaborator) throws Exception {
|
||||
public void elaborate(NodeElaborator<T> nodeElaborator) throws Exception {
|
||||
elaborate(nodeElaborator, 0);
|
||||
}
|
||||
|
||||
protected void elaborate(NodeElaborator<C> nodeElaborator, int level) throws Exception {
|
||||
protected void elaborate(NodeElaborator<T> nodeElaborator, int level) throws Exception {
|
||||
nodeElaborator.elaborate(this, level);
|
||||
for (Node<C> child : children) {
|
||||
for (Node<T> child : children) {
|
||||
child.elaborate(nodeElaborator, level+1);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
package org.gcube.informationsystem.tree;
|
||||
|
||||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public interface NodeElaborator<T> {
|
||||
|
||||
public void elaborate(Node<T> node, int level) throws Exception;
|
||||
|
||||
}
|
|
@ -1,14 +1,14 @@
|
|||
package org.gcube.informationsystem.discovery.knowledge;
|
||||
package org.gcube.informationsystem.tree;
|
||||
|
||||
import java.util.Set;
|
||||
|
||||
public interface NodeElementInformation<C extends Comparable<C>> {
|
||||
public interface NodeElementInformation<T> {
|
||||
|
||||
public abstract String getIdentifier(C c);
|
||||
public abstract String getIdentifier(T t);
|
||||
|
||||
public abstract Set<String> getParentIdentifiers(C c);
|
||||
public abstract Set<String> getParentIdentifiers(T root, T t);
|
||||
|
||||
public abstract void extraElaboration(C c);
|
||||
public abstract void extraElaboration(T t);
|
||||
|
||||
|
||||
// protected void createUsageKnowledge(Type type) {
|
|
@ -1,4 +1,4 @@
|
|||
package org.gcube.informationsystem.discovery.knowledge;
|
||||
package org.gcube.informationsystem.tree;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
@ -7,19 +7,19 @@ import java.util.Set;
|
|||
/**
|
||||
* @author Luca Frosini (ISTI - CNR)
|
||||
*/
|
||||
public class Tree<C extends Comparable<C>> {
|
||||
public class Tree<T> {
|
||||
|
||||
private boolean allowMultipleInheritance;
|
||||
|
||||
private Node<C> root;
|
||||
private NodeElementInformation<C> nei;
|
||||
private Map<String, Node<C>> locate;
|
||||
private Node<T> root;
|
||||
private NodeElementInformation<T> nei;
|
||||
private Map<String, Node<T>> locate;
|
||||
|
||||
public Tree(C c, NodeElementInformation<C> nei) throws Exception {
|
||||
public Tree(T t, NodeElementInformation<T> nei) throws Exception {
|
||||
this.allowMultipleInheritance = false;
|
||||
this.nei = nei;
|
||||
this.locate = new HashMap<>();
|
||||
this.root = addNode(c);
|
||||
this.root = addNode(t);
|
||||
|
||||
}
|
||||
|
||||
|
@ -27,22 +27,22 @@ public class Tree<C extends Comparable<C>> {
|
|||
this.allowMultipleInheritance = allowMultipleInheritance;
|
||||
}
|
||||
|
||||
public NodeElementInformation<C> getNodeElementInformation() {
|
||||
public NodeElementInformation<T> getNodeElementInformation() {
|
||||
return nei;
|
||||
}
|
||||
|
||||
public Node<C> addNode(C c) {
|
||||
String identifier = nei.getIdentifier(c);
|
||||
public Node<T> addNode(T t) {
|
||||
String identifier = nei.getIdentifier(t);
|
||||
if(locate.containsKey(identifier)) {
|
||||
// Has been already added
|
||||
return locate.get(identifier);
|
||||
}
|
||||
|
||||
Node<C> node = new Node<>(c);
|
||||
Node<T> node = new Node<>(t);
|
||||
|
||||
Set<String> parentIdentifiers = nei.getParentIdentifiers(c);
|
||||
Set<String> parentIdentifiers = nei.getParentIdentifiers(root.getNodeElement(), t);
|
||||
for(String parentIdentifier : parentIdentifiers) {
|
||||
Node<C> parentNode = locate.get(parentIdentifier);
|
||||
Node<T> parentNode = locate.get(parentIdentifier);
|
||||
if(parentNode==null) {
|
||||
throw new RuntimeException("I can find parent for " + identifier + ". Missing parent is " + parentIdentifier);
|
||||
}
|
||||
|
@ -55,12 +55,12 @@ public class Tree<C extends Comparable<C>> {
|
|||
}
|
||||
|
||||
this.locate.put(identifier, node);
|
||||
this.nei.extraElaboration(c);
|
||||
this.nei.extraElaboration(t);
|
||||
|
||||
return node;
|
||||
}
|
||||
|
||||
public Node<C> getRoot() {
|
||||
public Node<T> getRoot() {
|
||||
return root;
|
||||
}
|
||||
|
||||
|
@ -69,7 +69,7 @@ public class Tree<C extends Comparable<C>> {
|
|||
return root.toString();
|
||||
}
|
||||
|
||||
public void elaborate(NodeElaborator<C> nodeElaborator) throws Exception {
|
||||
public void elaborate(NodeElaborator<T> nodeElaborator) throws Exception {
|
||||
root.elaborate(nodeElaborator);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue