
prerna.engine.impl.rdf.InMemorySesameEngine Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright 2015 Defense Health Agency (DHA)
*
* If your use of this software does not include any GPLv2 components:
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ----------------------------------------------------------------------------
* If your use of this software includes any GPLv2 components:
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*******************************************************************************/
package prerna.engine.impl.rdf;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Vector;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openrdf.model.Literal;
import org.openrdf.model.URI;
import org.openrdf.model.Value;
import org.openrdf.model.ValueFactory;
import org.openrdf.query.BooleanQuery;
import org.openrdf.query.GraphQuery;
import org.openrdf.query.GraphQueryResult;
import org.openrdf.query.MalformedQueryException;
import org.openrdf.query.Query;
import org.openrdf.query.QueryEvaluationException;
import org.openrdf.query.QueryLanguage;
import org.openrdf.query.TupleQuery;
import org.openrdf.query.TupleQueryResult;
import org.openrdf.query.Update;
import org.openrdf.query.UpdateExecutionException;
import org.openrdf.repository.RepositoryConnection;
import org.openrdf.repository.RepositoryException;
import org.openrdf.repository.UnknownTransactionStateException;
import org.openrdf.repository.sail.SailRepositoryConnection;
import org.openrdf.rio.RDFHandlerException;
import org.openrdf.rio.rdfxml.RDFXMLWriter;
import org.openrdf.sail.SailConnection;
import org.openrdf.sail.SailException;
import prerna.engine.api.IDatabaseEngine;
import prerna.engine.impl.AbstractDatabaseEngine;
import prerna.util.Constants;
import prerna.util.Utility;
/**
* Holds the database in memory, and uses the Sesame API to facilitate querying of RDF data sources.
*/
public class InMemorySesameEngine extends AbstractDatabaseEngine {
private static final Logger classLogger = LogManager.getLogger(InMemorySesameEngine.class);
private RepositoryConnection rc = null;
private SailConnection sc = null;
private ValueFactory vf = null;
private boolean connected = false;
/**
* Method setRepositoryConnection. Sets the repository connection.
* @param rc RepositoryConnection. The repository connection that this is being set to.
*/
public void setRepositoryConnection(RepositoryConnection rc) {
this.rc = rc;
this.sc = ((SailRepositoryConnection) rc).getSailConnection();
this.vf = rc.getValueFactory();
this.connected = true;
}
/**
* Method getRepositoryConnection. Gets the repository connection.
* @return RepositoryConnection - the connection to the repository.*/
public RepositoryConnection getRepositoryConnection() {
return this.rc;
}
public void open(String smssFilePath) {
// no meaning to this now
}
@Override
public void open(Properties smssProp) {
// no meaning to this now
}
/**
* Closes the data base associated with the engine. This will prevent further changes from being made in the data store and
* safely ends the active transactions and closes the engine.
*/
@Override
public void close() {
if(connected) {
try {
rc.clear();
rc.rollback();
rc.close();
connected = false;
} catch (RepositoryException e) {
classLogger.error(Constants.STACKTRACE, e);
}
}
}
/**
* Runs the passed string query against the engine as a SELECT query. The query passed must be in the structure of a SELECT
* SPARQL query and the result format will depend on the engine type.
* @param query the string version of the SELECT query to be run against the engine
* @return triple query results that can be displayed as a grid */
public Object execQuery(String query) {
try {
Query fullQuery = rc.prepareQuery(QueryLanguage.SPARQL, query);
classLogger.debug("\nSPARQL: " + Utility.cleanLogString(query));
fullQuery.setIncludeInferred(true /* includeInferred */);
if(fullQuery instanceof TupleQuery){
TupleQueryResult sparqlResults = ((TupleQuery) fullQuery).evaluate();
return sparqlResults;
}
else if (fullQuery instanceof GraphQuery){
GraphQueryResult res = ((GraphQuery) fullQuery).evaluate();
return res;
}
else if (fullQuery instanceof BooleanQuery){
Boolean bool = ((BooleanQuery) fullQuery).evaluate();
return bool;
}
} catch (RepositoryException e) {
classLogger.error(Constants.STACKTRACE, e);
} catch (MalformedQueryException e) {
classLogger.error(Constants.STACKTRACE, e);
} catch (QueryEvaluationException e) {
classLogger.error(Constants.STACKTRACE, e);
}
return null;
}
/**
* Runs the passed string query against the engine as an INSERT query. The query passed must be in the structure of an INSERT
* SPARQL query or an INSERT DATA SPARQL query
* and there are no returned results. The query will result in the specified triples getting added to the
* data store.
* @param query the INSERT or INSERT DATA SPARQL query to be run against the engine
*/
public void insertData(String query) {
Update up;
try {
up = rc.prepareUpdate(QueryLanguage.SPARQL, query);
//sc.addStatement(vf.createURI(""),vf.createURI(""),vf.createURI(""));
classLogger.debug("\nSPARQL: " + query);
//tq.setIncludeInferred(true /* includeInferred */);
//tq.evaluate();
rc.setAutoCommit(false);
up.execute();
} catch (RepositoryException e) {
classLogger.error(Constants.STACKTRACE, e);
} catch (UpdateExecutionException e) {
classLogger.error(Constants.STACKTRACE, e);
}catch (MalformedQueryException e)
{
classLogger.error(Constants.STACKTRACE, e);
}
}
@Override
public DATABASE_TYPE getDatabaseType()
{
return IDatabaseEngine.DATABASE_TYPE.SESAME;
}
/**
* Processes a SELECT query just like {@link #execSelectQuery(String)} but gets the results in the exact format that the database stores them.
* This is important for things like param values so that we can take the returned value and fill the main query without needing modification
* @param sparqlQuery the SELECT SPARQL query to be run against the engine
* @return the Vector of Strings representing the full uris of all of the query results */
public Vector
© 2015 - 2025 Weber Informatics LLC | Privacy Policy