最後我決定了Gephi 0.9。問題是這個版本的應用程序沒有與Neo4j服務器的驅動程序一起提供,所以我必須創建它並使該應用程序適應這個新驅動程序。
這裏是改變我沒有,
1)模塊DBDrivers.-
我創建的驅動器,用於Neo4j的服務器如下,
package org.gephi.io.database.drivers;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Jose Alvarez de Lara
*/
public class Neo4jDriver implements SQLDriver {
public Neo4jDriver() {
try {
// load the Neo4j-JDBC driver using the current class loader
Class.forName("org.neo4j.jdbc.Driver");
} catch (ClassNotFoundException ex) {
Logger.getLogger(Neo4jDriver.class.getName()).log(Level.SEVERE, null, ex);
}
}
public Connection getConnection(String connectionUrl, String username, String passwd) throws SQLException {
return DriverManager.getConnection(connectionUrl, null, null);
}
@Override
public String getPrefix() {
return "neo4j";
}
@Override
public String toString() {
return "Neo4j";
}
@Override
public boolean equals(Object obj) {
if (obj instanceof Neo4jDriver) {
return ((Neo4jDriver) obj).getPrefix().equals(getPrefix());
} else {
return false;
}
}
@Override
public int hashCode() {
return getPrefix().hashCode();
}
}
2)模塊ImportPluginUI。 -
我在EdgeListPanel的代碼中做了一些改變。 Java類文件女巫是如下,
在我添加的Neo4j延伸的構造,
public EdgeListPanel() {
databaseManager = new EdgeListDatabaseManager();
initComponents();
driverComboBox.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent ie) {
initDriverType((SQLDriver) ie.getItem());
}
});
browseButton.setVisible(false);
browseButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
String lastPath = NbPreferences.forModule(EdgeListPanel.class).get(LAST_PATH, "");
final JFileChooser chooser = new JFileChooser(lastPath);
chooser.setAcceptAllFileFilterUsed(false);
chooser.setDialogTitle(NbBundle.getMessage(EdgeListPanel.class, "EdgeListPanel.sqliteFileChooser.title"));
DialogFileFilter dialogFileFilter = new DialogFileFilter(NbBundle.getMessage(EdgeListPanel.class, "EdgeListPanel.sqliteFileChooser.filefilter"));
dialogFileFilter.addExtension("sqlite");
dialogFileFilter.addExtension("neo4j"); //add extensión for neo4j
dialogFileFilter.addExtension("db");
chooser.addChoosableFileFilter(dialogFileFilter);
int returnFile = chooser.showSaveDialog(null);
if (returnFile != JFileChooser.APPROVE_OPTION) {
return;
}
File file = chooser.getSelectedFile();
hostTextField.setText(file.getAbsolutePath());
//Save last path
NbPreferences.forModule(EdgeListPanel.class).put(LAST_PATH, file.getParentFile().getAbsolutePath());
}
});
}
在initDriverType()方法我啓用Neo4j的面板,
private void initDriverType(final SQLDriver driver) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if (driver != null && driver.getPrefix().equals("sqlite")) {
hostLabel.setText(NbBundle.getMessage(EdgeListPanel.class, "EdgeListPanel.fileLabel.text"));
portTextField.setEnabled(false);
portLabel.setEnabled(false);
dbLabel.setEnabled(false);
dbTextField.setEnabled(false);
userLabel.setEnabled(false);
userTextField.setEnabled(false);
pwdLabel.setEnabled(false);
pwdTextField.setEnabled(false);
pwdTextField.setText("");
userTextField.setText("");
dbTextField.setText("");
portTextField.setText("");
browseButton.setVisible(true);
} else if (driver != null && driver.getPrefix().equals("neo4j")) {
hostLabel.setText(NbBundle.getMessage(EdgeListPanel.class, "EdgeListPanel.hostLabel.text"));
portTextField.setEnabled(true);
portLabel.setEnabled(true);
dbLabel.setEnabled(false);
dbTextField.setEnabled(false);
userLabel.setEnabled(false);
userTextField.setEnabled(false);
pwdLabel.setEnabled(false);
pwdTextField.setEnabled(false);
pwdTextField.setText("");
userTextField.setText("");
dbTextField.setText("");
browseButton.setVisible(false);
} else {
hostLabel.setText(NbBundle.getMessage(EdgeListPanel.class, "EdgeListPanel.hostLabel.text"));
portTextField.setEnabled(true);
portLabel.setEnabled(true);
dbLabel.setEnabled(true);
dbTextField.setEnabled(true);
userLabel.setEnabled(true);
userTextField.setEnabled(true);
pwdLabel.setEnabled(true);
pwdTextField.setEnabled(true);
browseButton.setVisible(false);
}
group.validateAll();
}
});
}
以下方法是一個更通用的方法,然後是舊的isSqlite(),因爲它允許我傳遞任何我需要的前綴。在這種情況下,它們應該是「源碼」和「Neo4j的」
private static boolean isValidDriverPrefix(EdgeListPanel panel, String prefix) {
if (panel.databaseManager.getEdgeListDatabases().size() > 0) {
if (panel.databaseManager.getEdgeListDatabases().get(0).getSQLDriver().getPrefix().equals(prefix)) {
return true;
}
return false;
} else if (panel.getSelectedSQLDriver().getPrefix().equals(prefix)) {
return true;
}
return false;
}
我用它在以下內部類這一塊,
private static class HostOrFileValidator implements Validator<String> {
private EdgeListPanel panel;
public HostOrFileValidator(EdgeListPanel panel) {
this.panel = panel;
}
@Override
public boolean validate(Problems problems, String compName, String model) {
if (isValidDriverPrefix(panel, "sqlite")) {
return Validators.FILE_MUST_BE_FILE.validate(problems, compName, model);
} else {
return Validators.REQUIRE_NON_EMPTY_STRING.validate(problems, compName, model);
}
}
}
private static class NotEmptyValidator implements Validator<String> {
private EdgeListPanel panel;
public NotEmptyValidator(EdgeListPanel panel) {
this.panel = panel;
}
@Override
public boolean validate(Problems problems, String compName, String model) {
if (isValidDriverPrefix(panel, "sqlite") || isValidDriverPrefix(panel, "neo4j")) {
return true;
} else {
return Validators.REQUIRE_NON_EMPTY_STRING.validate(problems, compName, model);
}
}
}
private static class PortValidator implements Validator<String> {
private EdgeListPanel panel;
public PortValidator(EdgeListPanel panel) {
this.panel = panel;
}
@Override
public boolean validate(Problems problems, String compName, String model) {
if (isValidDriverPrefix(panel, "sqlite")) {
return true;
} else {
return Validators.REQUIRE_NON_EMPTY_STRING.validate(problems, compName, model)
&& Validators.REQUIRE_VALID_INTEGER.validate(problems, compName, model)
&& Validators.numberRange(1, 65535).validate(problems, compName, model);
}
}
}
3)模塊ImportPlugin.-
在這個模塊中,我不得不修改類文件ImporterEdgeList.java,改變方法getNodes()和getEdges(),因爲它們在實現Neo4j圖形數據庫時拋出了NullPointerException。這真的很簡單,
private void getNodes(Connection connection) throws SQLException {
//Factory
ElementDraftFactory factory = container.factory();
//Properties
PropertiesAssociations properties = database.getPropertiesAssociations();
Statement s = connection.createStatement();
ResultSet rs = null;
try {
rs = s.executeQuery(database.getNodeQuery());
} catch (SQLException ex) {
report.logIssue(new Issue("Failed to execute Node query", Issue.Level.SEVERE, ex));
return;
}
findNodeAttributesColumns(rs);
ResultSetMetaData metaData = rs.getMetaData();
int columnsCount = metaData.getColumnCount();
int count = 0;
while (rs.next()) {
String id = null;
for (int i = 0; i < columnsCount; i++) {
String columnName = metaData.getColumnLabel(i + 1);
NodeProperties p = properties.getNodeProperty(columnName);
if (p != null) { //Here p could be null
if (p.equals(NodeProperties.ID)) {
String ide = rs.getString(i + 1);
if (ide != null) {
id = ide;
}
}
}
}
NodeDraft node;
if (id != null) {
node = factory.newNodeDraft(id);
} else {
node = factory.newNodeDraft();
}
for (int i = 0; i < columnsCount; i++) {
String columnName = metaData.getColumnLabel(i + 1);
NodeProperties p = properties.getNodeProperty(columnName);
if (p != null) {
injectNodeProperty(p, rs, i + 1, node);
} else {
//Inject node attributes
ColumnDraft col = container.getNodeColumn(columnName);
injectElementAttribute(rs, i + 1, col, node);
}
}
// injectTimeIntervalProperty(node);
container.addNode(node);
++count;
}
rs.close();
s.close();
}
private void getEdges(Connection connection) throws SQLException {
//Factory
ElementDraftFactory factory = container.factory();
//Properties
PropertiesAssociations properties = database.getPropertiesAssociations();
Statement s = connection.createStatement();
ResultSet rs = null;
try {
rs = s.executeQuery(database.getEdgeQuery());
} catch (SQLException ex) {
report.logIssue(new Issue("Failed to execute Edge query", Issue.Level.SEVERE, ex));
return;
}
findEdgeAttributesColumns(rs);
ResultSetMetaData metaData = rs.getMetaData();
int columnsCount = metaData.getColumnCount();
int count = 0;
while (rs.next()) {
String id = null;
for (int i = 0; i < columnsCount; i++) {
String columnName = metaData.getColumnLabel(i + 1);
EdgeProperties p = properties.getEdgeProperty(columnName);
if (p != null) { //Here p could be null
if (p.equals(EdgeProperties.ID)) {
String ide = rs.getString(i + 1);
if (ide != null) {
id = ide;
}
}
}
}
EdgeDraft edge;
if (id != null) {
edge = factory.newEdgeDraft(id);
} else {
edge = factory.newEdgeDraft();
}
for (int i = 0; i < columnsCount; i++) {
String columnName = metaData.getColumnLabel(i + 1);
EdgeProperties p = properties.getEdgeProperty(columnName);
if (p != null) {
injectEdgeProperty(p, rs, i + 1, edge);
} else {
//Inject edge attributes
ColumnDraft col = container.getEdgeColumn(columnName);
injectElementAttribute(rs, i + 1, col, edge);
}
}
// injectTimeIntervalProperty(edge);
container.addEdge(edge);
++count;
}
rs.close();
s.close();
}
就這樣。
這些變化後的Gephi 0.9針對Neo4j 2.1.8電影數據庫和Neo4j 2.3.0-M01的同一個示例數據庫在不需要驗證時進行測試。
希望它有幫助!
是否進口graphdb工作? –
是的,這是一種可能性,但我應該需要Neo4j的驅動程序。謝謝,讓我試試看。 – oggie0563