84 lines
2.9 KiB
Java
84 lines
2.9 KiB
Java
import java.io.BufferedWriter;
|
|
import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
import java.sql.Connection;
|
|
import java.sql.DriverManager;
|
|
import java.sql.ResultSet;
|
|
import java.sql.SQLException;
|
|
import java.sql.Statement;
|
|
|
|
public class SQLiteToProlog {
|
|
|
|
public static void main(String[] args) {
|
|
SQLiteToPrologConverter converter = new SQLiteToPrologConverter("tasks.db", "prolog.pl");
|
|
try {
|
|
converter.convert();
|
|
System.out.println("Data has been written to prolog.pl");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
|
|
class SQLiteToPrologConverter {
|
|
private String dbPath;
|
|
private String prologFilePath;
|
|
|
|
public SQLiteToPrologConverter(String dbPath, String prologFilePath) {
|
|
this.dbPath = dbPath;
|
|
this.prologFilePath = prologFilePath;
|
|
}
|
|
|
|
public void convert() throws ClassNotFoundException, SQLException, IOException {
|
|
Connection conn = null;
|
|
Statement stmt = null;
|
|
ResultSet rs = null;
|
|
|
|
try {
|
|
// Ładowanie klasy sterownika JDBC SQLite
|
|
Class.forName("org.sqlite.JDBC");
|
|
|
|
// Nawiązywanie połączenia z bazą danych SQLite
|
|
conn = DriverManager.getConnection("jdbc:sqlite:" + dbPath);
|
|
stmt = conn.createStatement();
|
|
rs = stmt.executeQuery("SELECT * FROM tasks");
|
|
|
|
// Tworzenie pliku Prolog i zapis danych
|
|
BufferedWriter writer = new BufferedWriter(new FileWriter(prologFilePath));
|
|
writer.write("has_duplicates(List) :- ");
|
|
writer.newLine();
|
|
writer.write("\tmember(X, List),");
|
|
writer.newLine();
|
|
writer.write("\tselect(X, List, Rest), ");
|
|
writer.newLine();
|
|
writer.write("\tmember(X, Rest), !.");
|
|
writer.newLine();
|
|
writer.write("collect_todos(Ys) :-");
|
|
writer.newLine();
|
|
writer.write("\tfindall(Y, todo(_, Y), Ys).");
|
|
writer.newLine();
|
|
writer.write("todos_have_duplicates :-");
|
|
writer.newLine();
|
|
writer.write("\tcollect_todos(Ys),");
|
|
writer.newLine();
|
|
writer.write("\thas_duplicates(Ys).");
|
|
writer.newLine();
|
|
while (rs.next()) {
|
|
int id = rs.getInt("id");
|
|
String task = rs.getString("task");
|
|
|
|
// Formatowanie wiersza jako relacji Prolog
|
|
String prologFact = String.format("todo(%d, '%s').", id, task.replace("'", "\\'"));
|
|
writer.write(prologFact);
|
|
writer.newLine();
|
|
}
|
|
|
|
writer.close();
|
|
} finally {
|
|
if (rs != null) rs.close();
|
|
if (stmt != null) stmt.close();
|
|
if (conn != null) conn.close();
|
|
}
|
|
}
|
|
}
|