MIDAS Civil with Java
This document is a MIDAS Civil API operating example using Java.
Β
1. Setting Java Development Environment
1.1 Download file
Download an installing file from the Java official website.
Β
1.2 Install Java
Installing Java.
Β
These editors offer a convenient developing environment for users.
Recommended Code Editors for Java
(This manual is written using Eclipse.)
Β
1.3 Install additional module
JSON Module helps your work powerfully in Java.
Setting the JSON Module in the Eclipse as below.
β Download JSON-Java through the under link.
GitHub - stleary/JSON-java: A reference implementation of a JSON package in Java.
β‘ Run the Eclipse, select the file path, and click the Launch button.
β’ Create a new Java Project.
β£ In Package Explorer -> Buildpath/Configure Build Path
β€ Open the previously downloaded JSON-Jave file as below.
β₯ Create Example Class for generating examples.
β¦ You can find the JSON-Java is registered as below.
Β
2. Operating MIDAS/Civil API Using Java
Letβs look over with simple examples.
2.1 Operating MIDAS/Civil API
Civil can execute to communicate with API as below.
Java Code
public class Example {
public static void main(String[] args) throws InterruptedException {
String mCivil = "C:\\CivilBuild-API\\CVLw.exe /API";
Runtime rt = Runtime.getRuntime();
Process p;
try {
p = rt.exec(mCivil);
p.getErrorStream().close();
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
}
}
}
MIDAS/Civil execute file path needs to be edited depending on the user.
2.2 Open MIDAS/Civil API Document
Open the existed file after running MIDAS/Civil.
Java Code
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
public class Example {
public static void main(String[] args) throws InterruptedException {
String mCivil = "C:\\CivilBuild-API\\CVLw.exe /API";
Runtime rt = Runtime.getRuntime();
Process p;
try {
p = rt.exec(mCivil);
p.getErrorStream().close();
Thread.sleep(10000);
String rm = "POST";
String dcOpen = "/doc/open";
JSONObject jOpen = new JSONObject();
jOpen.put("Argument", "C:\\FSM 1Cells Box.mcb");
restAPI(rm,dcOpen,jOpen);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void restAPI(String rm, String ct, JSONObject jobj) {
try {
URL url = new URL("http://127.0.0.1:10024"+ct);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(rm);
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoInput(true);
conn.setDoOutput(true);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
bw.write(jobj.toString());
bw.flush();
bw.close();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while((line=br.readLine()) !=null) {
sb.append(line);
}
System.out.print(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
MIDAS/Civil execute file path needs to be edited depending on the user.
You can find the URL address in the APIServer.exe, which operates MIDAS/Civil through API.
2.3 MIDAS/Civil API- Run the Analysis and Close program
Now, run the analysis with opened MIDAS/Civil file and close the Civil.
Β
Java Code
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;
public class Example {
public static void main(String[] args) throws InterruptedException {
String mCivil = "C:\\CivilBuild-API\\CVLw.exe /API";
Runtime rt = Runtime.getRuntime();
Process p;
try {
p = rt.exec(mCivil);
p.getErrorStream().close();
Thread.sleep(10000);
String post = "POST";
String dcOpen = "/doc/open";
JSONObject jOpen = new JSONObject();
jOpen.put("Argument", "C:\\FSM 1Cells Box.mcb");
restAPI(post,dcOpen,jOpen);
String dcAnal = "/doc/anal";
JSONObject jDump = new JSONObject();
restAPI(post,dcAnal,jDump);
String dcExit = "/doc/exit";
restAPI(post,dcExit,jDump);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void restAPI(String rm, String ct, JSONObject jobj) {
try {
URL url = new URL("http://127.0.0.1:10024"+ct);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod(rm);
conn.setRequestProperty("Content-Type", "application/json");
conn.setDoInput(true);
conn.setDoOutput(true);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
bw.write(jobj.toString());
bw.flush();
bw.close();
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
while((line=br.readLine()) !=null) {
sb.append(line);
}
System.out.print(sb.toString());
} catch (Exception e) {
e.printStackTrace();
}
}
}
Β
3. Command for operating
3.1 DOC Command
DOC command controls documents such as create/open/close files.
Β
Open document: β/doc/openβ
Create a new document: β/doc/newβ
Close document: β/doc/closeβ
Run analysis: β/doc/analβ
Save document: β/doc/saveβ
Save as: β/doc/saveasβ
Import JSON data: β/doc/importβ
Export JSON data: β/doc/exportβ
Close program: βdoc/exitβ
Β
Java Code(Example)
Β
3.2 DB Command
DB Command requests the data name. Each Data name is embodied by GET/POST/PUT/DELETE methods.
GET DB Method: inquiry opened file data.
Β
Example1: This example code calls all Nodes information from the opened file and prints specific Node information.
Β
Example2: The following code calls only specific Node information from the opened file.
Β
Post DB Method: Add data to the opened file. If the key value exists, then it will get an error.
Β
The following example adds a specific Node to the opened file and prints that Node information.
Β
Put DB Method: Change data to the opened file. If you request the key value that does not exist, it will get an error.
The following example changes the coordinates of a specific node.
Β
Delete DB Method: Delete the opened file data. If you request the key value that does not exist, it will get an error.
Β
Example1: Delete the specific Node, and print that deleted Node information.
Β
Example2: Delete all Nodes.
Β
3.3 POST Command
Post Command is post-process operators after analysis.
Β
Call result table: β/post/tableβ
Example1: This example calls the analyzed results from the opened document and saves them in JSON format.
Β
Example2: Call the analyzed results from the opened file, and print the previous results.
Β
Β
3.4 VIEW Command
View Command takes images in Post-process.
Β
Diagram call: β/view/captureβ
This example shows how to call the Diagram and save it where you want to.
Β