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.

Java Downloads | Oracle

Oracle/Java Official Website

 

1.2 Install Java

Installing Java.

Java Installation window

 

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.