sparqlGraphLibrary/src/main/java/com/ge/research/semtk/load/utility/ImportSpecHandler.java
private String lookupUri(String nodeID, ArrayList<String> record) throws Exception {
		
		// get total possible number available, if it isn't known
		if (! this.lookupResultCount.containsKey(nodeID)) {
			this.lookupResultCount.put(nodeID, this.lookupCount(nodeID, 2));
		}
		
		// create a new nodegroup copy:  
		ArrayList<String> builtStrings = new ArrayList<String>();
		
		// Build the mapping results into builtStrings
		for (ImportMapping mapping : this.lookupMappings.get(nodeID)) {
			String builtStr = mapping.buildString(record);
			
			// check for empties
			if (builtStr == null || builtStr.isEmpty()) {
				return UriCache.EMPTY_LOOKUP;
			}
						
			builtStrings.add(builtStr);
		}
				
		// return quickly if answer is already cached
		String cachedUri = this.uriCache.getUri(this.lookupNodegroupMD5.get(nodeID), builtStrings);
		
		// if already cached
		if (cachedUri != null) {
		
			// if "createIfMissing" and has a mapping and is a generated (not looked up) URI
			if (this.lookupMode.containsKey(nodeID) && 
					this.lookupMode.get(nodeID).equals(ImportSpec.LOOKUP_MODE_CREATE) && 
					this.getImportMapping(nodeID, ImportMapping.NO_PROPERTY) != null &&
					this.uriCache.isGenerated(cachedUri)) {
				
				// make sure that two different records aren't creating different values for same URI
				ImportMapping map = this.getImportMapping(nodeID, ImportMapping.NO_PROPERTY);
				String newUri = map.buildString(record);
				
				if (! newUri.equals(cachedUri)) {
					if (newUri.equals(UriCache.NOT_FOUND))    { newUri = "<GUID>"; }
					if (cachedUri.equals(UriCache.NOT_FOUND)) { cachedUri = "<GUID>"; }
					throw new Exception("Can't create a URI with two different values: " + cachedUri + " and " + newUri);
				}
			}
			
			// survived the check: return the cached value
			return cachedUri;
			
		} else {
			NodeGroup lookupNodegroup = null; // only build when needed  (queries and errors)
			
			// Run the query
			// make this thread-safe
			Table tab = null;
			if (this.lookupResultCount.get(nodeID) == 0) {
				// don't run a query if we know it's going to fail.
				tab = new Table(new String[] {"empty_row"}, new String[] {"string"});
				
			} else {
				lookupNodegroup = this.getLookupNodegroup(nodeID);
				SparqlEndpointInterface safeEndpoint = this.nonThreadSafeEndpoint.copy();
				String query = this.getLookupQuery(lookupNodegroup, nodeID, builtStrings);
				
				TableResultSet res = (TableResultSet) safeEndpoint.executeQueryAndBuildResultSet(query, SparqlResultTypes.TABLE);
				res.throwExceptionIfUnsuccessful();
				tab = res.getTable();
				
			}
			
			// Check and return results
			if (tab.getNumRows() > 1) {
				// multiple found: error
				String lookup = lookupNodegroup.getReturnedSparqlIDs().toString();
				throw new Exception("URI lookup on " + lookup + " found multiple URI's matching: " + String.join(",", builtStrings));
				
			} else if (tab.getNumRows() == 0) {
				// zero found
				if (this.getLookupMode(nodeID).equals(ImportSpec.LOOKUP_MODE_NO_CREATE)) {
					// this is the only spot where we didn't run query but need lookupNodegroup for error msg
					if (lookupNodegroup == null) {
						lookupNodegroup = this.getLookupNodegroup(nodeID);
					}
					String lookup = lookupNodegroup.getReturnedSparqlIDs().toString();
					throw new Exception("URI lookup on " + lookup + " failed on: " + String.join(",", builtStrings) );
					
				} else {
					// set URI to NOT_FOUND
					ImportMapping m = this.getImportMapping(nodeID, ImportMapping.NO_PROPERTY);
					this.uriCache.setUriNotFound(this.lookupNodegroupMD5.get(nodeID), builtStrings, (m == null) ? null : m.buildString(record));
					return UriCache.NOT_FOUND;
				}
				
			} else {
				// 1 found:  cache and return
				if (this.getLookupMode(nodeID).equals(ImportSpec.LOOKUP_MODE_ERR_IF_EXISTS)) {
					String lookup = lookupNodegroup.getReturnedSparqlIDs().toString();
					throw new Exception("URI lookup on 'error if exists' " + lookup + " already exists for: " + String.join(",", builtStrings) );
					
				} else {
					// found 1.  Normal success.
					String uri = tab.getCell(0,0);
					this.uriCache.putUri(this.lookupNodegroupMD5.get(nodeID), builtStrings, uri);
					
					// if we found a URI in via lookup in triplestore, cache some more
					this.preFetchUriCache(nodeID);
					
					return uri;
				}
			}
		}
	}
/**
	 * Look up a URI.  Checking URICache first, and saving results to URICache.
	 * If URI is not found and MODE_CREATE, then it returns UriCache.NOT_FOUND
	 *    and caller is responsible for generating not found guids.
	 * @param ImportMappings
	 * @return  uri or URICache.NOT_FOUND or URICache.EMPTY_LOOKUP
	 * @throws Exception - error, or not found and NO_CREATE, or found multiple
	 */
Look up a URI.  Checking URICache first, and saving results to URICache.
If URI is not found and MODE_CREATE, then it returns UriCache.NOT_FOUND
and caller is responsible for generating not found guids.
@param ImportMappings
@return  uri or URICache.NOT_FOUND or URICache.EMPTY_LOOKUP
@throws Exception - error, or not found and NO_CREATE, or found multiple
Look up a URI . Checking URICache first and saving results to URICache . If URI is not found and MODE_CREATE then it returns UriCache . NOT_FOUND and caller is responsible for generating not found guids . @param ImportMappings @return uri or URICache . NOT_FOUND or URICache . EMPTY_LOOKUP @throws Exception - error or not found and NO_CREATE or found multiple
private String lookupUri(String nodeID, ArrayList<String> record) throws Exception {
		if (! this.lookupResultCount.containsKey(nodeID)) {
			this.lookupResultCount.put(nodeID, this.lookupCount(nodeID, 2));
		}
		ArrayList<String> builtStrings = new ArrayList<String>();
		for (ImportMapping mapping : this.lookupMappings.get(nodeID)) {
			String builtStr = mapping.buildString(record);
			if (builtStr == null || builtStr.isEmpty()) {
				return UriCache.EMPTY_LOOKUP;
			}
			builtStrings.add(builtStr);
		}
		String cachedUri = this.uriCache.getUri(this.lookupNodegroupMD5.get(nodeID), builtStrings);
		if (cachedUri != null) {
			if (this.lookupMode.containsKey(nodeID) && 
					this.lookupMode.get(nodeID).equals(ImportSpec.LOOKUP_MODE_CREATE) && 
					this.getImportMapping(nodeID, ImportMapping.NO_PROPERTY) != null &&
					this.uriCache.isGenerated(cachedUri)) {
				ImportMapping map = this.getImportMapping(nodeID, ImportMapping.NO_PROPERTY);
				String newUri = map.buildString(record);
				if (! newUri.equals(cachedUri)) {
					if (newUri.equals(UriCache.NOT_FOUND))    { newUri = "<GUID>"; }
					if (cachedUri.equals(UriCache.NOT_FOUND)) { cachedUri = "<GUID>"; }
					throw new Exception("Can't create a URI with two different values: " + cachedUri + " and " + newUri);
				}
			}
			return cachedUri;
		} else {
			NodeGroup lookupNodegroup = null; 
			Table tab = null;
			if (this.lookupResultCount.get(nodeID) == 0) {
				tab = new Table(new String[] {"empty_row"}, new String[] {"string"});
			} else {
				lookupNodegroup = this.getLookupNodegroup(nodeID);
				SparqlEndpointInterface safeEndpoint = this.nonThreadSafeEndpoint.copy();
				String query = this.getLookupQuery(lookupNodegroup, nodeID, builtStrings);
				TableResultSet res = (TableResultSet) safeEndpoint.executeQueryAndBuildResultSet(query, SparqlResultTypes.TABLE);
				res.throwExceptionIfUnsuccessful();
				tab = res.getTable();
			}
			if (tab.getNumRows() > 1) {
				String lookup = lookupNodegroup.getReturnedSparqlIDs().toString();
				throw new Exception("URI lookup on " + lookup + " found multiple URI's matching: " + String.join(",", builtStrings));
			} else if (tab.getNumRows() == 0) {
				if (this.getLookupMode(nodeID).equals(ImportSpec.LOOKUP_MODE_NO_CREATE)) {
					if (lookupNodegroup == null) {
						lookupNodegroup = this.getLookupNodegroup(nodeID);
					}
					String lookup = lookupNodegroup.getReturnedSparqlIDs().toString();
					throw new Exception("URI lookup on " + lookup + " failed on: " + String.join(",", builtStrings) );
				} else {
					ImportMapping m = this.getImportMapping(nodeID, ImportMapping.NO_PROPERTY);
					this.uriCache.setUriNotFound(this.lookupNodegroupMD5.get(nodeID), builtStrings, (m == null) ? null : m.buildString(record));
					return UriCache.NOT_FOUND;
				}
			} else {
				if (this.getLookupMode(nodeID).equals(ImportSpec.LOOKUP_MODE_ERR_IF_EXISTS)) {
					String lookup = lookupNodegroup.getReturnedSparqlIDs().toString();
					throw new Exception("URI lookup on 'error if exists' " + lookup + " already exists for: " + String.join(",", builtStrings) );
				} else {
					String uri = tab.getCell(0,0);
					this.uriCache.putUri(this.lookupNodegroupMD5.get(nodeID), builtStrings, uri);
					this.preFetchUriCache(nodeID);
					return uri;
				}
			}
		}
	}
private String lookupUri ( String nodeID , ArrayList < String > record ) throws Exception { if ( ! this . lookupResultCount . containsKey ( nodeID ) ) { this . lookupResultCount . put ( nodeID , this . lookupCount ( nodeID , 2 ) ) ; } ArrayList < String > builtStrings = new ArrayList < String > ( ) ; for ( ImportMapping mapping : this . lookupMappings . get ( nodeID ) ) { String builtStr = mapping . buildString ( record ) ; if ( builtStr == null || builtStr . isEmpty ( ) ) { return UriCache . EMPTY_LOOKUP ; } builtStrings . add ( builtStr ) ; } String cachedUri = this . uriCache . getUri ( this . lookupNodegroupMD5 . get ( nodeID ) , builtStrings ) ; if ( cachedUri != null ) { if ( this . lookupMode . containsKey ( nodeID ) && this . lookupMode . get ( nodeID ) . equals ( ImportSpec . LOOKUP_MODE_CREATE ) && this . getImportMapping ( nodeID , ImportMapping . NO_PROPERTY ) != null && this . uriCache . isGenerated ( cachedUri ) ) { ImportMapping map = this . getImportMapping ( nodeID , ImportMapping . NO_PROPERTY ) ; String newUri = map . buildString ( record ) ; if ( ! newUri . equals ( cachedUri ) ) { if ( newUri . equals ( UriCache . NOT_FOUND ) ) { newUri = "<GUID>" ; } if ( cachedUri . equals ( UriCache . NOT_FOUND ) ) { cachedUri = "<GUID>" ; } throw new Exception ( "Can't create a URI with two different values: " + cachedUri + " and " + newUri ) ; } } return cachedUri ; } else { NodeGroup lookupNodegroup = null ; Table tab = null ; if ( this . lookupResultCount . get ( nodeID ) == 0 ) { tab = new Table ( new String [ ] { "empty_row" } , new String [ ] { "string" } ) ; } else { lookupNodegroup = this . getLookupNodegroup ( nodeID ) ; SparqlEndpointInterface safeEndpoint = this . nonThreadSafeEndpoint . copy ( ) ; String query = this . getLookupQuery ( lookupNodegroup , nodeID , builtStrings ) ; TableResultSet res = ( TableResultSet ) safeEndpoint . executeQueryAndBuildResultSet ( query , SparqlResultTypes . TABLE ) ; res . throwExceptionIfUnsuccessful ( ) ; tab = res . getTable ( ) ; } if ( tab . getNumRows ( ) > 1 ) { String lookup = lookupNodegroup . getReturnedSparqlIDs ( ) . toString ( ) ; throw new Exception ( "URI lookup on " + lookup + " found multiple URI's matching: " + String . join ( "," , builtStrings ) ) ; } else if ( tab . getNumRows ( ) == 0 ) { if ( this . getLookupMode ( nodeID ) . equals ( ImportSpec . LOOKUP_MODE_NO_CREATE ) ) { if ( lookupNodegroup == null ) { lookupNodegroup = this . getLookupNodegroup ( nodeID ) ; } String lookup = lookupNodegroup . getReturnedSparqlIDs ( ) . toString ( ) ; throw new Exception ( "URI lookup on " + lookup + " failed on: " + String . join ( "," , builtStrings ) ) ; } else { ImportMapping m = this . getImportMapping ( nodeID , ImportMapping . NO_PROPERTY ) ; this . uriCache . setUriNotFound ( this . lookupNodegroupMD5 . get ( nodeID ) , builtStrings , ( m == null ) ? null : m . buildString ( record ) ) ; return UriCache . NOT_FOUND ; } } else { if ( this . getLookupMode ( nodeID ) . equals ( ImportSpec . LOOKUP_MODE_ERR_IF_EXISTS ) ) { String lookup = lookupNodegroup . getReturnedSparqlIDs ( ) . toString ( ) ; throw new Exception ( "URI lookup on 'error if exists' " + lookup + " already exists for: " + String . join ( "," , builtStrings ) ) ; } else { String uri = tab . getCell ( 0 , 0 ) ; this . uriCache . putUri ( this . lookupNodegroupMD5 . get ( nodeID ) , builtStrings , uri ) ; this . preFetchUriCache ( nodeID ) ; return uri ; } } } }
Look up a URI.
Look up a URI .
// get total possible number available, if it isn't known // create a new nodegroup copy:   // Build the mapping results into builtStrings // check for empties // return quickly if answer is already cached // if already cached // if "createIfMissing" and has a mapping and is a generated (not looked up) URI // make sure that two different records aren't creating different values for same URI // survived the check: return the cached value // only build when needed  (queries and errors) // Run the query // make this thread-safe // don't run a query if we know it's going to fail. // Check and return results // multiple found: error // zero found // this is the only spot where we didn't run query but need lookupNodegroup for error msg // set URI to NOT_FOUND // 1 found:  cache and return // found 1.  Normal success. // if we found a URI in via lookup in triplestore, cache some more