Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improved grammar to add support for multiple transitions #57

Merged
merged 17 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
5953658
changed database calls to use the changed names in recent SEP3 demo f…
wanhoff Sep 24, 2023
d08e822
added test strings with expected result for sep3 and bml in propertie…
wanhoff Apr 23, 2023
7439d8e
added recognition for several other "Datenfeld" entries.
wanhoff May 20, 2023
6aaa552
added Datenfeld as parameter for commanline and in database function …
wanhoff May 24, 2023
8cb07ac
changed test due to add percent character to grammar
wanhoff May 24, 2023
eb0014d
disabled database dependend integration tests
wanhoff May 24, 2023
fa34744
changed database calls to use the changed names in recent SEP3 demo f…
wanhoff Sep 24, 2023
5af28b5
Merge branch 'newTestFileAndDBProperties' of github.com:lat-lon/sep3-…
wanhoff Sep 24, 2023
70369e4
Merge pull request #51 from lat-lon/main
wanhoff Sep 24, 2023
f6521b8
Merge branch 'newTestFileAndDBProperties' of github.com:lat-lon/sep3-…
wanhoff Sep 24, 2023
030621a
moved properties files and fixed database connection for in database use
wanhoff Sep 24, 2023
3cec44c
added missing statement when the connection was changed with new cred…
wanhoff Sep 27, 2023
fe2114c
added support for mutliple uebergaenge
wanhoff Oct 3, 2023
cda62e7
added properties fraglich and sicher for bestandteil
wanhoff Oct 3, 2023
5ef670a
Merge branch 'main' into feature/multipleUebergaenge
wanhoff Oct 23, 2023
b48e011
Merge branch 'feature/multipleUebergaenge' of github.com:lat-lon/sep3…
wanhoff Oct 23, 2023
9a9fe79
removed double entry for df variable
wanhoff Oct 23, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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