+ public static Resource possibleObjectForType(ReadGraph graph, Resource type, Resource relation) throws DatabaseException {
+ PropertyInfo pi = graph.syncRequest(new PropertyInfoRequest(relation));
+ return possibleObjectForType(graph, type, relation, pi.isFunctional);
+ }
+
+ public static Resource possibleObjectForType(ReadGraph graph, Resource type, Resource relation, boolean functional) throws DatabaseException {
+ if(functional) {
+ Layer0 L0 = Layer0.getInstance(graph);
+ Resource result = graph.getPossibleObject(type, relation);
+ if(result != null)
+ return result;
+ for(Resource su : graph.getObjects(L0.Inherits, type)) {
+ Resource r = possibleObjectForType(graph, su, relation, functional);
+ if(r != null) {
+ if(result != null)
+ return null;
+ result = r;
+ }
+ }
+ return result;
+ } else {
+ Set<Resource> found = objectsForTypeNonFunctional(graph, type, relation, new HashSet<>());
+ return found.size() == 1 ? found.iterator().next() : null;
+ }
+ }
+
+ private static Set<Resource> objectsForTypeNonFunctional(ReadGraph graph, Resource type, Resource relation, Set<Resource> found) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ found.addAll(graph.getObjects(type, relation));
+ for(Resource su : graph.getObjects(L0.Inherits, type)) {
+ objectsForTypeNonFunctional(graph, su, relation, found);
+ }
+ return found;
+ }
+