Skip to content

Commit

Permalink
Merge pull request #57 from lat-lon/feature/multipleUebergaenge
Browse files Browse the repository at this point in the history
Improved grammar to add support for multiple transitions
  • Loading branch information
tfr42 authored Oct 23, 2023
2 parents 4ef4bff + 9a9fe79 commit f704fc8
Show file tree
Hide file tree
Showing 18 changed files with 258 additions and 81 deletions.
7 changes: 7 additions & 0 deletions db.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
URL=jdbc:postgresql://localhost/petroparser
USER=petroparser
PASSWORD=petroparser
WOERTERBUCH=woerterbuch.woerterbuch
SCHLUESSELTYPEN=woerterbuch.schluesseltypen
SCHLUESSELMAPPING=bml.bml_schluesselmapping
DATEFIELD=PETRO
12 changes: 8 additions & 4 deletions src/main/antlr4/org/sep3tools/gen/PetroGrammar.g4
Original file line number Diff line number Diff line change
Expand Up @@ -10,28 +10,32 @@ bestandteile:
;

uebergang_bes:
b1=bestandteil '-' b2=bestandteil
bestandteil ('-' bestandteil)+
| '(' uebergang_bes ')' ( '(' attribute ')' )?
;

bestandteil:
TEIL ( '(' attribute ')' )?
| '(' TEIL ( '(' attribute ')' )? ')';
TEIL ( '(' attribute ')' )? # bestandteil_simple
| '(' bestandteil ')' # bestandteil_klammer
| bestandteil FRAGLICH # bestandteil_fraglich
| bestandteil SICHER # bestandteil_sicher
;

attribute:
attribut # att
| uebergang_att # Uebergang_a
| attr=attribute '(' unter=attribute ')' # unter_Attribute
| attribute ',' attribute # Aufzaehlung_a
;

uebergang_att: attribut '-' attribut;

attribut:
TEIL # attr
| attribut FRAGLICH # attr_fraglich
| attribut SICHER # attr_sicher
| TIEFE # attr_tiefe
;
;

TIEFE: ([0-9]|'.')+;
TEIL: ANY+;
Expand Down
43 changes: 34 additions & 9 deletions src/main/java/org/sep3tools/BmlVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,11 @@ public String visitSchichtbeschreibung(PetroGrammarParser.SchichtbeschreibungCon
return visitChildren(ctx);
}

/**
* process single soil, remove quantifier, if present
* @param ctx the parse tree
* @return translated string for soil parse tree
*/
@Override
public String visitBestandteil(PetroGrammarParser.BestandteilContext ctx) {
public String visitBestandteil_klammer(PetroGrammarParser.Bestandteil_klammerContext ctx) {
return visitBestandteil_simple((PetroGrammarParser.Bestandteil_simpleContext) ctx.bestandteil());
}

public String visitBestandteil_simple(PetroGrammarParser.Bestandteil_simpleContext ctx) {
String boden = getBodenTerm(ctx.TEIL().getText());
String attrib;
if (isNull(ctx.attribute())) {
Expand All @@ -73,6 +71,26 @@ else if (attr.startsWith(" (")) {
return boden + "," + attrib;
}

/**
* Visit a parse tree produced by the {@code bestandteil_sicher} labeled alternative
* in {@link PetroGrammarParser}.
* @param ctx the parse tree
* @return the visitor result
*/
public String visitBestandteil_sicher(PetroGrammarParser.Bestandteil_sicherContext ctx) {
return visitChildren(ctx);
}

/**
* Visit a parse tree produced by the {@code bestandteil_fraglich} labeled alternative
* in {@link PetroGrammarParser}.
* @param ctx the parse tree
* @return the visitor result
*/
public String visitBestandteil_fraglich(PetroGrammarParser.Bestandteil_fraglichContext ctx) {
return visitChildren(ctx);
}

private String getBodenTerm(String boden) {
String bodenTerm = getBmlResultSet(boden);
if (!bodenTerm.isEmpty())
Expand Down Expand Up @@ -122,14 +140,21 @@ public String visitAttr(PetroGrammarParser.AttrContext ctx) {
*/
@Override
public String visitUebergang_bes(PetroGrammarParser.Uebergang_besContext ctx) {
String teile;
String teile = "";
String attrib;

if (ctx.getText().startsWith(" (")) {
teile = visit(ctx.uebergang_bes());
}
else {
teile = visit(ctx.b1) + ", " + visit(ctx.b2);
for (PetroGrammarParser.BestandteilContext teil : ctx.bestandteil()) {
if (teile.isEmpty()) {
teile = visit(teil);
}
else {
teile = teile + ", " + visit(teil);
}
}
}
if (isNull(ctx.attribute())) {
attrib = "";
Expand Down
110 changes: 86 additions & 24 deletions src/main/java/org/sep3tools/JavaConnector.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package org.sep3tools;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.logging.Logger;

/**
Expand All @@ -12,25 +17,54 @@
*/
public final class JavaConnector {

private static boolean credChanged = true;

private static final Logger LOG = Logger.getLogger(JavaConnector.class.getName());

private static String m_url = "jdbc:default:connection";

private static String user = "";
private static String user;

private static String pass;

private static String pass = "";
private static String wb;

private static String wb = "woerterbuch.\"Woerterbuch\"";
private static String st;

private static String st = "woerterbuch.\"Schluesseltypen\"";
private static String sm;

private static String sm = "bml.bml_schluesselmapping";
private static String df;

private static String df = "PETRO";
private static Connection conn;

private JavaConnector() {
}

public static void setPropertiesFile(String filename) {
credChanged = true;
try {
File file = new File(filename);
FileInputStream fileInput = new FileInputStream(file);
Properties properties = new Properties();
properties.load(fileInput);
fileInput.close();

setUrl(properties.getProperty("URL"));
setUser(properties.getProperty("USER"));
setPass(properties.getProperty("PASSWORD"));
setWb(properties.getProperty("WOERTERBUCH"));
setSt(properties.getProperty("SCHLUESSELTYPEN"));
setSm(properties.getProperty("SCHLUESSELMAPPING"));
setDf(properties.getProperty("DATEFIELD"));
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}

public static void setWb(String wb) {
JavaConnector.wb = wb;
}
Expand All @@ -47,32 +81,43 @@ public static void setDf(String df) {
JavaConnector.df = df;
}

public static void setUser(String user) {
JavaConnector.user = user;
public static void setUser(String newUser) {
credChanged = true;
JavaConnector.user = newUser;
}

public static void setPass(String pass) {
JavaConnector.pass = pass;
public static void setPass(String newPass) {
credChanged = true;
JavaConnector.pass = newPass;
}

public static void setUrl(String url) {
JavaConnector.m_url = url;
public static void setUrl(String newUrl) {
credChanged = true;
JavaConnector.m_url = newUrl;
}

// String query = "SELECT Klartext from woerterbuch.Woerterbuch where Kuerzel=";

private static void setConn(String url, String user, String pass) throws SQLException {
if (conn != null && !conn.isClosed())
conn.close();
JavaConnector.conn = DriverManager.getConnection(url, user, pass);
credChanged = false;
}

/**
* translates a SEP3 code to clear text
* @param sep3Code code for translation
* @return translated SEP3 string
* @throws SQLException if DB error occurs
*/
public static String getS3Name(String sep3Code) throws SQLException {
Connection conn = DriverManager.getConnection(m_url, user, pass);
String query = "select \"Kuerzel\", \"Klartext\" from " + wb + " w join " + st + " s "
+ "on w.\"Typ\" = s.\"Nebentypbez\" where (s.\"Datenfeld\" = '" + df + "' "
+ "OR s.\"Datenfeld\" = 'diverse') AND \"Kuerzel\"= ?";
String query = "select kuerzel, klartext from " + wb + " w join " + st + " s "
+ "on w.typ = s.nebentypbez where (s.datenfeld = '" + df + "' "
+ "OR s.datenfeld = 'diverse') AND kuerzel= ?";

if (credChanged)
setConn(m_url, user, pass);
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, sep3Code);
LOG.fine("Executing statement: " + stmt);
Expand All @@ -83,6 +128,10 @@ public static String getS3Name(String sep3Code) throws SQLException {
result = rs.getString(2);
}
LOG.fine("Returning: " + result);

rs.close();
stmt.close();

return result;
}
}
Expand All @@ -94,9 +143,10 @@ public static String getS3Name(String sep3Code) throws SQLException {
* @throws SQLException if DB error occurs
*/
public static String getS3AsBMmlLitho(String sep3Code) throws SQLException {
Connection conn = DriverManager.getConnection(m_url, user, pass);
String query = "select bml_code from " + sm + " where sep3_codelist = 'S3PETRO' AND sep3_code = ?";

if (credChanged)
setConn(m_url, user, pass);
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, sep3Code);
LOG.fine("Executing statement: " + stmt);
Expand All @@ -107,6 +157,10 @@ public static String getS3AsBMmlLitho(String sep3Code) throws SQLException {
result = rs.getString(1);
}
LOG.fine("Returning: " + result);

rs.close();
stmt.close();

return result;
}
}
Expand All @@ -118,10 +172,10 @@ public static String getS3AsBMmlLitho(String sep3Code) throws SQLException {
* @throws SQLException if DB error occurs
*/
public static String getAllowedAttribs(String sep3Code) throws SQLException {
Connection conn = DriverManager.getConnection(m_url, user, pass);
String query = "select \"Kuerzel\", \"Attribute\" from " + wb + " w join " + st + " s "
+ "on w.\"Typ\" = s.\"Nebentypbez\" "
+ "where (s.\"Datenfeld\" = 'PETRO' OR s.\"Datenfeld\" = 'diverse') AND \"Kuerzel\"= ?";
String query = "select kuerzel, attribute from " + wb + " w join " + st + " s " + "on w.typ = s.nebentypbez "
+ "where (s.datenfeld = 'PETRO' OR s.datenfeld = 'diverse') AND kuerzel= ?";
if (credChanged)
setConn(m_url, user, pass);
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, sep3Code);
LOG.fine("Executing statement: " + stmt);
Expand All @@ -132,6 +186,10 @@ public static String getAllowedAttribs(String sep3Code) throws SQLException {
result = rs.getString(2);
}
LOG.fine("Returning: " + result);

rs.close();
stmt.close();

return result;
}
}
Expand All @@ -150,9 +208,10 @@ public static String getBodenQuant(String sep3Code, String quant) throws SQLExce
allowedAttributes = getAllowedAttribs(sep3Code);
quantBez = getQuantBezFromAttribs(allowedAttributes);

Connection conn = DriverManager.getConnection(m_url, user, pass);
String query = "select w.\"Kuerzel\", w.\"Klartext\", s.\"Nebentypbez\" from " + wb + " w join " + st + " s "
+ "on w.\"Typ\" = s.\"Nebentypbez\" where (s.\"Nebentypbez\" = ? AND w.\"Kuerzel\" = ?);";
String query = "select w.kuerzel, w.klartext, s.nebentypbez from " + wb + " w join " + st + " s "
+ "on w.typ = s.nebentypbez where (s.nebentypbez = ? AND w.kuerzel = ?);";
if (credChanged)
setConn(m_url, user, pass);
PreparedStatement stmt = conn.prepareStatement(query);
stmt.setString(1, quantBez);
stmt.setString(2, quant);
Expand All @@ -165,6 +224,9 @@ public static String getBodenQuant(String sep3Code, String quant) throws SQLExce
}
LOG.fine("Returning: " + result);

rs.close();
stmt.close();

return result;
}
}
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/sep3tools/Launch.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import org.postgresql.pljava.annotation.Function;
import org.sep3tools.gen.*;

import java.sql.SQLException;

/**
* Command line launcher for SEP3 tools
*
Expand Down Expand Up @@ -38,6 +40,10 @@ else if (args.length == 0 || args[0].isEmpty()) {
else if (args.length == 1) {
sep3String = args[0];
}
else if (args.length == 2) {
JavaConnector.setPropertiesFile(args[0]);
sep3String = args[1];
}
else {
System.out.println("Aufruf mit folgenden Parametern:\n"
+ "[JDBC-URL] [DB-User] [DB-Passwort] [Woerterbuch-Tabelle] [Schlüsseltypen-Tabelle] <SEP3-String>\n\n"
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/sep3tools/LaunchBML.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,21 @@
import org.sep3tools.gen.PetroGrammarLexer;
import org.sep3tools.gen.PetroGrammarParser;

import java.sql.SQLException;
import java.util.*;

import static java.util.Objects.isNull;

/**
* Command line launcher for SEP3 to BML tool
* Command line launcher for SEP3 to BML tool.
*
* @author Jeronimo Wanhoff <[email protected]>
*/
public class LaunchBML {

/**
* Launches SEP3 tool with arguments:
* @param args DB URL, DB user, DB password, Schluesselmapping table name SEP3 string
* @param args DB URL, DB user, DB password, Schluesselmapping table name SEP3 string.
* to process
*/
public static void main(String[] args) {
Expand Down Expand Up @@ -120,10 +121,10 @@ public static String S3_AsBmlLitho(String s3String, String sm) {
* @return BML format of SEP3 input
*/
@Function
public static String S3_AsBmlLitho_verbose(String s3String, String sm) {
public static String s3AsBmlLithoVerbose(final String s3String, final String sm) {
JavaConnector.setSm(sm);
String result = S3_AsBmlLitho(s3String);
return result;
}

}
}
Loading

0 comments on commit f704fc8

Please sign in to comment.