⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rdfsource.java

📁 这是外国一个开源推理机
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
		query.append(" AND localname ");		if (_rdbms.emptyStringIsNull() && lname.length() == 0) {			query.append("IS NULL");		}		else {			query.append("= '");			query.append(_rdbms.escapeString(lname));			query.append("'");		}		try {			Connection connection = _rdbms.getConnection();			java.sql.Statement statement = connection.createStatement();			ResultSet resultSet = statement.executeQuery(query.toString());			if (resultSet.next()) {				// URI was found				result = resultSet.getInt(1);			}			resultSet.close();			statement.close();			connection.close();		}		catch (SQLException e) {			throw new SailInternalException(e);		}		if (idURI != null) {			// Store the fetched ID in the URI object for use in any subsequent requests			idURI.setInternalId(result);		}		return result;	}	/**	 * Gets the ID of the supplied BNode.	 * 	 * @return the ID of the supplied BNode, or 0 if the BNode was not found.	 */	protected int _getBNodeId(BNode bNode) {		IdBNode idBNode = null;		if (_isOwnValue(bNode)) {			// ID is possibly stored in the BNode object			idBNode = (IdBNode)bNode;			int id = idBNode.getInternalId();			if (id != 0) {				return id;			}		}		// Retrieve the ID from the database		int result = 0;		String nodeId = bNode.getID();		StringBuffer query = new StringBuffer(150);		query.append("SELECT id FROM " + RESOURCES_TABLE);		query.append(" WHERE namespace = 0 AND localname = '");		query.append(_rdbms.escapeString(nodeId));		query.append("'");		try {			Connection connection = _rdbms.getConnection();			java.sql.Statement statement = connection.createStatement();			ResultSet resultSet = statement.executeQuery(query.toString());			if (resultSet.next()) {				// bnode was found				result = resultSet.getInt(1);			}			resultSet.close();			statement.close();			connection.close();		}		catch (SQLException e) {			throw new SailInternalException(e);		}		if (idBNode != null) {			// Store the fetched ID in the BNode object for use in any subsequent requests			idBNode.setInternalId(result);		}		return result;	}	/**	 * Gets the ID of the supplied literal.	 * 	 * @return the ID of the supplied literal, or 0 if the literal was not found.	 */	protected int _getLiteralId(Literal literal) {		IdLiteral idLiteral = null;		if (_isOwnValue(literal)) {			// ID is possibly stored in the Literal object			idLiteral = (IdLiteral)literal;			int id = idLiteral.getInternalId();			if (id != 0) {				return id;			}		}		// Build query to retrieve the ID from the database		// Note: the retrieved labels are compared to the label of the specified		// literal because CLOB/BLOB-comparisons are not supported by Oracle and		// SQL Server (and possibly others). The label's hash code is used to		// make a first selection of possible matching literals.		int result = 0;		int datatypeId = 0;		String label = literal.getLabel();		String language = literal.getLanguage();		URI datatype = literal.getDatatype();		if (datatype != null) {			datatypeId = _getURIId(datatype);			if (datatypeId == 0) {				// datatype not found so literal does not exist				return 0;			}		}		StringBuffer query = new StringBuffer(200);		query.append("SELECT id, label FROM ");		query.append(LITERALS_TABLE);		query.append(" WHERE ");		// labelHash		query.append("labelHash = ");		query.append(_getLabelHash(label));		// datatype		query.append(" AND datatype = " + datatypeId);		// language		query.append(" AND language");		if (language == null) {			query.append(" IS NULL");		}		else {			query.append(" = '");			query.append(_rdbms.escapeString(language));			query.append("'");		}		try {			Connection connection = _rdbms.getConnection();			java.sql.Statement statement = connection.createStatement();			ResultSet resultSet = statement.executeQuery(query.toString());			while (resultSet.next()) {				// a possibly matching literal was found, check its label				String resultLabel = resultSet.getString(2);				if (resultLabel == null) {					resultLabel = "";				}				if (resultLabel.equals(label)) {					// Label matches					result = resultSet.getInt(1);					break;				}			}			resultSet.close();			statement.close();			connection.close();		}		catch (SQLException e) {			throw new SailInternalException(e);		}		if (idLiteral != null) {			// Store the fetched ID in the Literal object for use in any subsequent requests			idLiteral.setInternalId(result);		}		return result;	}	protected boolean _isOwnValue(Value value) {		return value instanceof IdValue &&			((IdValue)value).getRdfSource() == this;	}	/**	 * Gets the ID of the supplied namespace.	 * 	 * @return the ID of the supplied namespace, or 0 if the namespace was not	 *         found.	 */	protected int _getNamespaceId(String namespace) {		RdbmsNamespace n = (RdbmsNamespace)_namespaceTable.get(namespace);		if (n != null) {			return n.getId();		}		return 0;	}	/**	 * Calculates a hash code for the supplied label.	 **/	protected long _getLabelHash(String label) {		// Calculate a 128 bit (16 byte) MD5 hash and use		// the last 8 bytes of this hash for the result.		try {			byte[] labelData = label.getBytes("UTF-8");			byte[] md5hash = null;			synchronized(_md5Digest) {				_md5Digest.reset();				md5hash = _md5Digest.digest(labelData);			}			return (new BigInteger(md5hash)).longValue();		}		catch (UnsupportedEncodingException e) {			// Should never happen as UTF-8 should be available on every Java platform			throw new SailInternalException(					"UTF-8 encoding not available on this platorm", e);		}	}	/**	 * Chops a list of IDs (integers) contained in the supplied ResultSet	 * into one or more chunks. The chunks are returned as string of the	 * form "(n1,n2,...)", including parentheses.	 *	 * @param ResultSet A one-column ResultSet containing integers.	 * @param maxStringLength The maximum length (in characters) of a chunk.	 *	 * @return an array of strings. Each string in the array is a chunk of	 * the list of integers, surrounded by brackets and seperated by	 * commas.	 */	protected String[] _chunkIdSet(ResultSet rs, int maxStringLength)		throws SQLException	{		ArrayList chunkList = new ArrayList();		StringBuffer chunk = new StringBuffer(maxStringLength);		chunk.append("(");		int count = 0;		while (rs.next()) {			String nextId = String.valueOf(rs.getInt(1));			if (chunk.length() + nextId.length() + 1 > maxStringLength) {				// Start with a new chunk				chunk.append(")");				chunkList.add(chunk.toString());				chunk.setLength(0);				chunk.append("(");				count = 0;			}			if (count > 0) {				chunk.append(",");			}			chunk.append(rs.getInt(1));			count++;		}		if (count > 0) {			chunk.append(")");			chunkList.add(chunk.toString());		}		String[] result = new String[chunkList.size()];		for (int i = 0; i < chunkList.size(); i++) {			result[i] = (String)chunkList.get(i);		}		return result;	}	protected void _updateExportedNamespaces() {		ThreadLog.trace("Updating exported namespaces information.");		// First 'reset' all namespaces that are not user-defined to		// non-exported. This eliminates exporting of namespaces belonging		// to removed predicates.		try {			_rdbms.executeUpdate("UPDATE " + NAMESPACES_TABLE + " SET export = "					+ _rdbms.FALSE + " WHERE userDefined = " + _rdbms.FALSE);			// Query for all namespaces that need to be exported: those that			// are the in predicates of statements.			Connection con = _rdbms.getConnection();			java.sql.Statement st = con.createStatement();			ResultSet rs = st.executeQuery("SELECT DISTINCT r.namespace "					+ "FROM " + RESOURCES_TABLE + " r, " + TRIPLES_TABLE + " t "					+ "WHERE t.pred = r.id ");			String[] idChunks = _chunkIdSet(rs, 3500);			rs.close();			st.close();			con.setAutoCommit(false);			st = con.createStatement();			for (int i = 0; i < idChunks.length; i++) {				st.executeUpdate("UPDATE " + NAMESPACES_TABLE + " SET export = "						+ _rdbms.TRUE + " WHERE id IN " + idChunks[i]);			}			con.commit();			st.close();			con.close();			_setExportStatusUpToDate(true);			// re-initialize the namespaces to update the cache.			_initNamespaceCache();		}		catch (SQLException e) {			throw new SailInternalException(e);		}	}/*-----------------------------------------------------------------------+| Methods for retrieving Resource and Literal objects with a specific ID |+-----------------------------------------------------------------------*/	/**	 * Gets the Value for the supplied ID.	 * 	 * @return An IdValue object.	 */	public IdValue getValue(int id) {		if (id > 0) {			return getResource(id);		}		else if (id < 0) {			return getLiteral(id);		}		else { // id == 0			return null;		}	}	/**	 * Gets the Resource for the supplied ID.	 * 	 * @return Either an IdURI or an IdBNode object	 */	public IdResource getResource(int id) {		try {			IdResource result = null;			Connection con = _rdbms.getConnection();			java.sql.Statement st = con.createStatement();			ResultSet rs = st.executeQuery(					"SELECT r.namespace, r.localname " +					"FROM " + RESOURCES_TABLE + " r " +					"WHERE r.id = " + id);			if (rs.next()) {				int nsId = rs.getInt(1);				String localName = rs.getString(2);				if (localName == null) {					localName = "";				}				if (nsId == 0) {					// bnode					result = new IdBNode(this, localName, id);				}				else {					// URI					result = new IdURI(this, _namespaceNames[nsId], localName, id);				}			}			rs.close();			st.close();			con.close();			return result;		}		catch (SQLException e) {			throw new SailInternalException(e);		}	}	/**	 * Gets the Literal for the supplied ID.	 * 	 * @return An IdLiteral object.	 */	public IdLiteral getLiteral(int id) {		try {			IdLiteral result = null;			Connection con = _rdbms.getConnection();			java.sql.Statement st = con.createStatement();			ResultSet rs = st.executeQuery(					"SELECT r.id, r.namespace, r.localname, l.language, l.label " +					"FROM " + LITERALS_TABLE + " l, " + RESOURCES_TABLE + " r " +					"WHERE l.datatype = r.id AND l.id = " + id);			if (rs.next()) {				int dtId = rs.getInt(1); // datatype id				String label = rs.getString(5);				if (label == null) {					label = "";				}				if (dtId != 0) {					// Datatyped literal					int dtNsId = rs.getInt(2); // datatype namespace id					String dtLname = rs.getString(3); // datatype localname					if (dtLname == null) {						dtLname = "";					}					URI datatype = new IdURI(this, _namespaceNames[dtNsId], dtLname, dtId);					result = new IdLiteral(this, label, datatype, id);				}				else {					String lang = rs.getString(4);					if (lang != null) {						// Literal with language attribute						result = new IdLiteral(this, label, lang, id);					}					else {						// Plain literal						result = new IdLiteral(this, label, id);					}				}			}			rs.close();			st.close();			con.close();			return result;		}		catch (SQLException e) {			throw new SailInternalException(e);		}	}	// implements ValueFactory.createURI(String)	public URI createURI(String uri) {		return new IdURI(this, uri, 0);	}	public URI createURI(String namespace, String localName) {		return new IdURI(this, namespace, localName, 0);	}	// implements ValueFactory.createBNode()	public BNode createBNode() {		if (_nextBNodeID == Integer.MAX_VALUE) {			// Start with a new bnode prefix			_updateBNodePrefix();		}		return createBNode(_bnodePrefix + _nextBNodeID++);	}	// implements ValueFactory.createBNode(String)	public BNode createBNode(String nodeId) {		return new IdBNode(this, nodeId, 0);	}	// implements ValueFactory.createLiteral(String)	public Literal createLiteral(String value) {		return new IdLiteral(this, value, 0);	}	// implements ValueFactory.createLiteral(String, String)	public Literal createLiteral(String value, String language) {		return new IdLiteral(this, value, language, 0);	}	// implements ValueFactory.createLiteral(String, URI)	public Literal createLiteral(String value, URI datatype) {		return new IdLiteral(this, value, datatype, 0);	}	// implements ValueFactory.createStatement(Resource, URI, Value)	public Statement createStatement(			Resource subject, URI predicate, Value object)	{		return new StatementImpl(subject, predicate, object);	}}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -