I have this android example code, However when I run this I got file cannot be opened IOException.
Wat to do?
package co.chintan.sparkseetest;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.sparsity.sparksee.gdb.AttributeKind;
import com.sparsity.sparksee.gdb.Condition;
import com.sparsity.sparksee.gdb.DataType;
import com.sparsity.sparksee.gdb.Database;
import com.sparsity.sparksee.gdb.EdgesDirection;
import com.sparsity.sparksee.gdb.Graph;
import com.sparsity.sparksee.gdb.Objects;
import com.sparsity.sparksee.gdb.ObjectsIterator;
import com.sparsity.sparksee.gdb.Session;
import com.sparsity.sparksee.gdb.Sparksee;
import com.sparsity.sparksee.gdb.SparkseeConfig;
import com.sparsity.sparksee.gdb.Value;
public class MainActivity extends Activity {
// Widgets
private TextView tvSortida;
private Button btnQuery1;
private Button btnQuery2;
// Database directory
private String gdbDir;
//
// Sparksee
//
private final String SparkseeLicense = "YOUR LICENSE";
private Sparksee sparksee;
private Database gdb;
private Session sess;
//
// Sparksee schema
//
// Movies
private int movieNodeType;
private int movieIdAttr;
private int movieTitleAttr;
private int movieYearAttr;
// People
private int peopleNodeType;
private int peopleIdAttr;
private int peopleNameAttr;
// Cast
private int castEdgeType;
private int castCharacterAttr;
// Directs
private int directsEdgeType;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Widgets
tvSortida = (TextView) findViewById(R.id.textViewSortida);
btnQuery1 = (Button) findViewById(R.id.buttonQuery1);
btnQuery2 = (Button) findViewById(R.id.buttonQuery2);
// Database directory
gdbDir = getDir("HelloSparksee_data", MODE_PRIVATE).getAbsolutePath();
Log.d("Directory",gdbDir);
// Sparksee
sparksee = null;
gdb = null;
sess = null;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
// Open the graph database
// Try to open the database
if (openGDB())
setQueryButtonsStatus(true); // Enable the query buttons
else
setQueryButtonsStatus(false); // Disable the query buttons
}
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
// Close the graph database
closeGDB();
}
private void setQueryButtonsStatus(boolean status)
{
btnQuery1.setEnabled(status);
btnQuery2.setEnabled(status);
}
public void onCreateGDB(View view)
{
// Close the GDB if it's open
closeGDB();
if (createGDB())
{
tvSortida.setText("Base de dades creada");
setQueryButtonsStatus(true);
}
else
setQueryButtonsStatus(false);
}
public void onQuery1(View view)
{
query1GDB();
}
public void onQuery2(View view)
{
query2GDB();
}
private boolean openGDB()
{
// Prepare the SparkseeConfig
// After the open we will only run readonly queries,
// so we could open the database as read only and
// set the recovery to false.
// TODO
SparkseeConfig cfg = new SparkseeConfig();
cfg.setLicense(SparkseeLicense);
cfg.setLogFile(gdbDir+"/HelloSparksee.log");
//cfg.setLogLevel(LogLevel.Off);
cfg.setCacheMaxSize(15);
// Open the graph database
// TODO
sparksee = new Sparksee(cfg);
try {
gdb = sparksee.open(gdbDir+"/HelloSparksee.gdb", true);
sess = gdb.newSession();
// Get the schema
// TODO
Graph graph = sess.getGraph();
movieNodeType = graph.findType("MOVIE");
movieIdAttr = graph.findAttribute(movieNodeType, "ID");
movieTitleAttr = graph.findAttribute(movieNodeType, "TITLE");
movieYearAttr = graph.findAttribute(movieNodeType, "YEAR");
peopleNodeType = graph.findType("PEOPLE");
peopleIdAttr = graph.findAttribute(peopleNodeType, "ID");
peopleNameAttr = graph.findAttribute(peopleNodeType, "NAME");
castEdgeType = graph.findType("CAST");
castCharacterAttr = graph.findAttribute(castEdgeType, "CHARACTER");
directsEdgeType = graph.findType("DIRECTS");
} catch (Exception e) {
tvSortida.setText(e.getMessage());
return false;
}
tvSortida.setText("Base de dades oberta");
return true;
}
private boolean createGDB()
{
// Prepare the SparkseeConfig
SparkseeConfig cfg = new SparkseeConfig();
cfg.setLicense(SparkseeLicense);
cfg.setLogFile(gdbDir+"/HelloSparksee.log");
//cfg.setLogLevel(LogLevel.Off);
cfg.setCacheMaxSize(15);
cfg.setRecoveryEnabled(true);
//cfg.setRecoveryCheckpointTime(0);
// Create the graph database
sparksee = new Sparksee(cfg);
try {
gdb = sparksee.create(gdbDir+"/HelloSparksee.gdb", "HelloSparksee");
// A session is required to access the database and
// only one thread can use it at the same time.
// Keeping the session open is NOT required.
// We can create a new session an CLOSE it whenever we need.
// But it may be more efficient to just keep it open while the
// database is open.
sess = gdb.newSession();
Graph graph = sess.getGraph();
// Start a transaction
// With recovery enabled using transactions is recommended
sess.begin();
//
// Build the schema
//
// Add the MOVIE node type with it's attributes:
// ID: Long Unique
// TITLE: String Indexed
// YEAR: Integer Indexed
movieNodeType = graph.newNodeType("MOVIE");
movieIdAttr = graph.newAttribute(movieNodeType, "ID", DataType.Long, AttributeKind.Unique);
movieTitleAttr = graph.newAttribute(movieNodeType, "TITLE", DataType.String, AttributeKind.Indexed);
//TODO: movieYearAttr = ...
movieYearAttr = graph.newAttribute(movieNodeType, "YEAR", DataType.Integer, AttributeKind.Indexed);
// Add the PEOPLE node type with it's attributes:
// ID: Long Unique
// Name: String Indexed
// TODO
peopleNodeType = graph.newNodeType("PEOPLE");
peopleIdAttr = graph.newAttribute(peopleNodeType, "ID", DataType.Long, AttributeKind.Unique);
peopleNameAttr = graph.newAttribute(peopleNodeType, "NAME", DataType.String, AttributeKind.Indexed);
// Add an undirected edge type (CAST) with a basic String attribute (CHARACTER)
// TODO
castEdgeType = graph.newEdgeType("CAST", false, true);
castCharacterAttr = graph.newAttribute(castEdgeType, "CHARACTER", DataType.String, AttributeKind.Basic);
// Add an edge type (DIRECTS) restricted to go from PEOPLE to MOVIE
// TODO
directsEdgeType = graph.newRestrictedEdgeType("DIRECTS", peopleNodeType, movieNodeType, false);
//
// Add data
//
// At least one Value instance is required to set the attributes
Value value = new Value();
//
// Add some MOVIE nodes
//
// 1 / Lost in Translation / 2003
long mLostInTranslation = graph.newNode(movieNodeType);
graph.setAttribute(mLostInTranslation, movieIdAttr, value.setLong(1));
graph.setAttribute(mLostInTranslation, movieTitleAttr, value.setString("Lost in Translation"));
//graph.setAttribute(mLostInTranslation, movieYearAttr, value.setInteger(2003));
graph.setAttribute(mLostInTranslation, movieYearAttr, value.setInteger(2003));
// 2 / Vicky Cristina Barcelona / 2008
// TODO
long mVickyCB = graph.newNode(movieNodeType);
graph.setAttribute(mVickyCB, movieIdAttr, value.setLong(2));
graph.setAttribute(mVickyCB, movieTitleAttr, value.setString("Vicky Cristina Barcelona"));
graph.setAttribute(mVickyCB, movieYearAttr, value.setInteger(2008));
// 3 / Manhattan / 1979
// TODO
long mManhattan = graph.newNode(movieNodeType);
graph.setAttribute(mManhattan, movieIdAttr, value.setLong(3));
graph.setAttribute(mManhattan, movieTitleAttr, value.setString("Manhattan"));
graph.setAttribute(mManhattan, movieYearAttr, value.setInteger(1979));
// Add some PEOPLE nodes
// 1 / Scarlett Johansson
// TODO
long pScarlett = graph.newNode(peopleNodeType);
graph.setAttribute(pScarlett, peopleIdAttr, value.setLong(1));
graph.setAttribute(pScarlett, peopleNameAttr, value.setString("Scarlett Johansson"));
// 2 / Bill Murray
// TODO
long pBill = graph.newNode(peopleNodeType);
graph.setAttribute(pBill, peopleIdAttr, value.setLong(2));
graph.setAttribute(pBill, peopleNameAttr, value.setString("Bill Murray"));
// 3 / Sofia Coppola
// TODO
long pSofia = graph.newNode(peopleNodeType);
graph.setAttribute(pSofia, peopleIdAttr, value.setLong(3));
graph.setAttribute(pSofia, peopleNameAttr, value.setString("Sofia Coppola"));
// 4 / Woody Allen
// TODO
long pWoody = graph.newNode(peopleNodeType);
graph.setAttribute(pWoody, peopleIdAttr, value.setLong(4));
graph.setAttribute(pWoody, peopleNameAttr, value.setString("Woody Allen"));
// 5 / Penélope Cruz
// TODO
long pPenelope = graph.newNode(peopleNodeType);
graph.setAttribute(pPenelope, peopleIdAttr, value.setLong(5));
graph.setAttribute(pPenelope, peopleNameAttr, value.setString("Penélope Cruz"));
// 6 / Diane Keaton
// TODO
long pDiane = graph.newNode(peopleNodeType);
graph.setAttribute(pDiane, peopleIdAttr, value.setLong(6));
graph.setAttribute(pDiane, peopleNameAttr, value.setString("Diane Keaton"));
//
// Add some CAST edges and it's attributes
//
// Lost in Translation -> Scarlett Johansson / Charlotte
// TODO
long anEdge;
anEdge = graph.newEdge(castEdgeType, mLostInTranslation, pScarlett);
graph.setAttribute(anEdge, castCharacterAttr, value.setString("Charlotte"));
// Lost in Translation -> Bill Murray / Bob Harris
// TODO
anEdge = graph.newEdge(castEdgeType, mLostInTranslation, pBill);
graph.setAttribute(anEdge, castCharacterAttr, value.setString("Bob Harris"));
// Vicky Cristina Barcelona -> Scarlett Johansson / Cristina
// TODO
anEdge = graph.newEdge(castEdgeType, mVickyCB, pScarlett);
graph.setAttribute(anEdge, castCharacterAttr, value.setString("Cristina"));
// Vicky Cristina Barcelona -> Penélope Cruz / Maria Elena
// TODO
anEdge = graph.newEdge(castEdgeType, mVickyCB, pPenelope);
graph.setAttribute(anEdge, castCharacterAttr, value.setString("Maria Elena"));
// Manhattan -> Diane Keaton / Mary
// TODO
anEdge = graph.newEdge(castEdgeType, mManhattan, pDiane);
graph.setAttribute(anEdge, castCharacterAttr, value.setString("Mary"));
// Manhattan -> Woody Allen / Isaac
// TODO
anEdge = graph.newEdge(castEdgeType, mManhattan, pWoody);
graph.setAttribute(anEdge, castCharacterAttr, value.setString("Isaac"));
//
// Add some DIRECTS edges
//
// Sofia Coppola -> Lost in Translation
// TODO
anEdge = graph.newEdge(directsEdgeType, pSofia, mLostInTranslation);
// Woody Allen -> Vicky Cristina Barcelona
// TODO
anEdge = graph.newEdge(directsEdgeType, pWoody, mVickyCB);
// Woody Allen -> Manhattan
// TODO
anEdge = graph.newEdge(directsEdgeType, pWoody, mManhattan);
// End the transaction
sess.commit();
} catch (Exception e) {
tvSortida.setText(e.getMessage());
if (sess != null)
sess.commit();
closeGDB();
return false;
}
return true;
}
private void query1GDB()
{
try {
// Get the Graph
Graph graph = sess.getGraph();
// Start a transaction
// With recovery enabled using transactions is recommended
sess.begin();
// Query the graph database
Value value = new Value();
// QUERY: Who acted both in movies of Woody Allen and Sofia Coppola?
String sortida = "Who acted both in movies of Woody Allen and Sofia Coppola?\n";
// Who acted in woody allen's movies?
// TODO
// Get the movies directed by Woody Allen
long pWoody = graph.findObject(peopleIdAttr, value.setLong(4));
Objects directedByWoody = graph.neighbors(pWoody, directsEdgeType, EdgesDirection.Outgoing);
// Get the cast of the movies directed by Woody Allen
Objects castDirectedByWoody = graph.neighbors(directedByWoody, castEdgeType, EdgesDirection.Any);
// We don't need the directedByWoody collection anymore, so we should close it
directedByWoody.close();
// Who acted in Sofia Coppola's movies?
// TODO
// Get the movies directed by Sofia Coppola
long pSofia = graph.findObject(peopleIdAttr, value.setLong(3));
Objects directedBySofia = graph.neighbors(pSofia, directsEdgeType, EdgesDirection.Outgoing);
// Get the cast of the movies directed by Sofia Coppola
Objects castDirectedBySofia = graph.neighbors(directedBySofia, castEdgeType, EdgesDirection.Any);
// We don't need the directedBySofia collection anymore, so we should close it
directedBySofia.close();
// Who acted in both? Intersect the results.
// TODO
Objects castFromBoth = Objects.combineIntersection(castDirectedByWoody, castDirectedBySofia);
// We don't need the other collections anymore
castDirectedByWoody.close();
castDirectedBySofia.close();
// Add the name of the people found to the result string
// TODO
ObjectsIterator it = castFromBoth.iterator();
while (it.hasNext())
{
long peopleOid = it.next();
graph.getAttribute(peopleOid, peopleNameAttr, value);
sortida += value.getString() +"\n";
}
// The ObjectsIterator must be closed
it.close();
// The Objects must be closed
castFromBoth.close();
// End the transaction
sess.commit();
//
// Show the result
//
tvSortida.setText(sortida);
} catch (Exception e) {
tvSortida.setText(e.getMessage());
}
}
private void query2GDB()
{
try {
// Get the Graph
Graph graph = sess.getGraph();
// Start a transaction
sess.begin();
// Query the graph database
Value value = new Value();
// QUERY: Which movies were published after 2000?
String sortida = "Movies published after year 2000:\n";
// Get the movies with a year attribute value >= 2000
// TODO
Objects objs = graph.select(movieYearAttr, Condition.GreaterEqual, value.setInteger(2000));
// Add the title of each movie found to the result string
// TODO
ObjectsIterator it = objs.iterator();
while (it.hasNext())
{
long movie = it.next();
// Get the movie title
graph.getAttribute(movie, movieTitleAttr, value);
sortida += value.getString() + "\n";
}
it.close();
objs.close();
// End the transaction
sess.commit();
//
// Show the result
//
tvSortida.setText(sortida);
} catch (Exception e) {
tvSortida.setText(e.getMessage());
}
}
private void closeGDB()
{
if ( sess != null )
{
sess.close();
sess = null;
}
if( gdb != null)
{
gdb.close();
gdb = null;
}
if (sparksee != null)
{
sparksee.close();
sparksee = null;
}
}
}
...