TransferableGraphUtils.getObjects2 for fixing not-found handling
authorjsimomaa <jani.simomaa@gmail.com>
Thu, 15 Jun 2017 06:32:53 +0000 (09:32 +0300)
committerjsimomaa <jani.simomaa@gmail.com>
Thu, 15 Jun 2017 07:06:44 +0000 (10:06 +0300)
refs #7306

Change-Id: I1d8dfefb51d579b1cb5ca9e15df82c0a8e347619

bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphUtils.java

index 5b40113aecb1fb581e57ec80d1f1786c63df3caa..b210d8dd713348cb38fa846f6558509442d05276 100644 (file)
@@ -146,22 +146,53 @@ public class TransferableGraphUtils {
                return result;
        }
        
+    /**
+     * This implementation is no longer advised to use because it returns 0 as
+     * NOT_FOUND which is in fact a valid ID for resource in graph
+     */
+       @Deprecated
        public static int getPossibleObject(TransferableGraph1 tg, int subject, Identity predicate) {
                int result = 0;
                for(int i=0;i<tg.statements.length;i+=4) {
                        if(tg.statements[i] == subject && tg.statements[i+1] == predicate.resource) {
-                               if(result != 0) return 0;
+                               if(result != 0 && tg.statements[i+3] != result) return 0;
                                result = tg.statements[i+3];
                        }
                }
                return result;
        }
+
+       public static final int NOT_FOUND = -2;
+
+       public static int getPossibleObject2(TransferableGraph1 tg, int subject, Identity predicate) {
+           int result = NOT_FOUND;
+        for(int i=0;i<tg.statements.length;i+=4) {
+            if(tg.statements[i] == subject && tg.statements[i+1] == predicate.resource) {
+                if(result != NOT_FOUND && tg.statements[i+3] != result)
+                    return NOT_FOUND;
+                result = tg.statements[i+3];
+            }
+        }
+        return result;
+       }
        
+       /**
+        * @return 0 for presenting not found which is BAD
+        * @see getPossibleObject2
+        */
+       @Deprecated
        public static int getPossibleObject(TransferableGraph1 tg, Identity subject, String predicate) {
                Identity p = findExternal(tg, predicate);
                if(p == null) return 0;
                return getPossibleObject(tg, subject.resource, p);
        }
+       
+    public static int getPossibleObject2(TransferableGraph1 tg, Identity subject, String predicate) {
+        Identity p = findExternal(tg, predicate);
+        if (p == null)
+            return NOT_FOUND;
+        return getPossibleObject2(tg, subject.resource, p);
+    }
 
        public static Map<Identity, String> getNames(TransferableGraph1 tg, Collection<Identity> ids) {
                Map<Identity, String> result = new HashMap<Identity, String>();