/** * Test to see if <code>compactExternalId</code> is a compact HL-ID, and return the relevant * object iff <code>compactExternalId</code> is the canonical id for the object. This is intended * to be used in cases where it's not known whether compactExternalId is in fact a valid compact * HL-ID, as it performs extra checks to make sure that the string is in fact the HL-ID for the * returned object. * * @param compactExternalId * @param access * @return the object uniquely identified by compactExternalId * @see #fromCompactExternalId(java.lang.String, com.cyc.base.CycAccess) * @throws IOException */ public static Object fromPossibleCompactExternalId(String compactExternalId, CycAccess access) throws CycConnectionException { if ((compactExternalId == null) || ("".equals(compactExternalId))) { throw new IllegalArgumentException(compactExternalId + "is not a valid compact external id."); } // @todo support CompactHLIDConverter.converter() once we can simply identify // if a compact external id is a String or Number Object result = compactExternalIdToCycObjectCache.get(compactExternalId); if (result != null) { return result; } try { if (CompactHLIDConverter.converter().isNumberCompactHLId(compactExternalId)) { Number num = (Number) CompactHLIDConverter.converter().fromCompactHLId(compactExternalId); result = CycObjectFactory.makeCycNumber(num); } else { result = access .converse() .converseList( "(multiple-value-list (maybe-find-object-by-compact-hl-external-id-string " + DefaultCycObject.stringApiValue(compactExternalId) + "))"); if (((CycArrayList) result).second() == CycObjectFactory.nil) { return null; } } } catch (IOException ioe) { throw new CycConnectionException(ioe); } compactExternalIdToCycObjectCache.put(compactExternalId, ((CycArrayList) result).first()); return ((CycArrayList) result).first(); }
public static String toCompactExternalId(Object cycObject, CycAccess access) throws CycConnectionException { if ((cycObject == null) || (!DefaultCycObject.isCycLObject(cycObject))) { throw new IllegalArgumentException(cycObject + "is not a valid CycL object."); } try { if (cycObject instanceof Number) { return CompactHLIDConverter.converter().toCompactHLId((Number) cycObject); } if (cycObject instanceof CycNumberImpl) { return CompactHLIDConverter.converter() .toCompactHLId(((CycNumberImpl) cycObject).getNumber()); } if (cycObject instanceof String) { return CompactHLIDConverter.converter().toCompactHLId((String) cycObject); } } catch (IOException ioe) { throw new CycConnectionException(ioe); } Object result = cycObjectToCompactExternalIdCache.get(cycObject); if (result != null) { return (String) result; } result = access .converse() .converseObject( "(compact-hl-external-id-string " + DefaultCycObject.stringApiValue(cycObject) + ")"); if (!(result instanceof String)) { throw new BaseClientRuntimeException("Got invalid result: " + result); } cycObjectToCompactExternalIdCache.put(cycObject, (String) result); return (String) result; }
public static String toCompactExternalId(Number cycObject) throws IOException { return CompactHLIDConverter.converter().toCompactHLId(cycObject); }