Skip to content

Commit

Permalink
migrate to version v2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Quim committed Apr 22, 2019
1 parent 55c7c22 commit 2b859d8
Show file tree
Hide file tree
Showing 13 changed files with 421 additions and 138 deletions.
5 changes: 4 additions & 1 deletion src/main/java/com/example/mahout/Classifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,14 @@ private void deleteTmpFiles() {
System.out.println("Files deleted correctly\n");
}

public ArrayList<Pair<String, Pair<String, Double>>> classify(String companyName, List<Requirement> requirements, String property) throws IOException, SQLException, JSONException {
public ArrayList<Pair<String, Pair<String, Double>>> classify(String companyName, List<Requirement> requirements, String property) throws Exception {

/* Load the companyModel from the database */
CompanyModel companyModel = companyModelDAO.findOne(companyName, property);

if (companyModel == null)
throw new Exception("Model not found");

createTmpFiles(companyModel);

Configuration configuration = new Configuration();
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/com/example/mahout/DAO/CompanyModelDAO.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ public interface CompanyModelDAO {

boolean delete(String name, String property) throws SQLException;

boolean deleteByCompany(String name) throws SQLException;

boolean deleteAll() throws SQLException;

boolean deleteAllMulti(String company, String property) throws SQLException;

List<CompanyModel> findAllMulti(String enterpriseName, String property) throws SQLException, IOException;

List<CompanyModel> findAll() throws SQLException, IOException;

boolean exists(String name, String property) throws SQLException;
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/com/example/mahout/DAO/CompanyModelDAOMySQL.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,60 @@ public boolean delete(String companyName, String property) throws SQLException {
return result != 0;
}

@Override
public boolean deleteByCompany(String companyName) throws SQLException {
PreparedStatement ps;
ps = c.prepareStatement("DELETE FROM file_model_documents WHERE COMPANY_NAME = ?");
ps.setString(1, companyName);
int result = ps.executeUpdate();
ps.close();
return result != 0;
}

@Override
public boolean deleteAll() throws SQLException {
PreparedStatement ps;
ps = c.prepareStatement("DELETE FROM file_model_documents");
int result = ps.executeUpdate();
ps.close();
return result != 0;
}

@Override
public boolean deleteAllMulti(String companyName, String property) throws SQLException {
PreparedStatement ps;
ps = c.prepareStatement("DELETE FROM file_model_documents WHERE COMPANY_NAME = ? AND PROPERTY LIKE ? || '%'");
ps.setString(1, companyName);
ps.setString(2, property);
int result = ps.executeUpdate();
ps.close();
return result != 0;
}

@Override
public List<CompanyModel> findAllMulti(String enterpriseName, String property) throws SQLException, IOException {
PreparedStatement ps;
ps = c.prepareStatement("SELECT * FROM file_model_documents WHERE COMPANY_NAME = ? AND PROPERTY LIKE ? || '%'");
ps.setString(1, enterpriseName);
ps.setString(2, property);
ps.execute();
ResultSet rs = ps.getResultSet();

List<CompanyModel> fileModels = new ArrayList<>();
while (rs.next()) {
fileModels.add(new CompanyModel(
rs.getString("COMPANY_NAME"),
rs.getString("PROPERTY"),
rs.getBytes("MODEL"),
rs.getBytes("LABELINDEX"),
rs.getBytes("DICTIONARY"),
rs.getBytes("FREQUENCIES")));
}
ps.close();
rs.close();

return fileModels; }

@Override
public List<CompanyModel> findAll() throws SQLException, IOException {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import com.example.mahout.service.DataService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
Expand All @@ -29,18 +31,46 @@ public String test() throws Exception {
return "OK";
}

@PostMapping("/train")
@ApiOperation(value = "Train a model",
@PostMapping("model")
@ApiOperation(value = "Create a model",
notes = "Given a list of requirements, and a specific company and property, a new model is generated and stored in "
+ "the database")
+ "the database. Each model is identified by a company and a property. Therefore, there is only one model per" +
" property and company.")
@ApiResponses(value = {@ApiResponse(code = 200, message = "OK", response = String.class)})
public String train(@ApiParam(value = "Request with the requirements to train", required = true) @RequestBody RequirementList request,
@ApiParam(value = "Property of the classifier", required = true) @RequestParam("property") String property,
@ApiParam(value = "Company to which the model belong", required = true) @RequestParam("company") String enterpriseName) throws Exception {
public String train(@ApiParam(value = "Request with the requirements to train the model", required = true) @RequestBody RequirementList request,
@ApiParam(value = "Property of the classifier (requirement_type)", required = true) @RequestParam("property") String property,
@ApiParam(value = "Proprietary company of the model", required = true) @RequestParam("company") String enterpriseName) throws Exception {

classificationService.train(request, property, enterpriseName);

return "Train succesfull";
return "Train successful";
}

@RequestMapping(value = "model", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation(value = "Update a model",
notes = "Given a list of requirements, updates the model of the classifier for the given company")
@ApiResponses(value = {@ApiResponse(code = 200, message = "OK", response = String.class)})
public ResponseEntity update(@ApiParam(value = "Request with the requirements to train and update the model", required = true) @RequestBody RequirementList request,
@ApiParam(value = "Property of the classifier (requirement_type)", required = true) @RequestParam("property") String property,
@ApiParam(value = "Proprietary company of the model", required = true) @RequestParam("company") String enterpriseName) throws Exception {

String msg = classificationService.update(request, property, enterpriseName);
return new ResponseEntity<>(msg, msg.equals("Update succsesfull") ? HttpStatus.OK : HttpStatus.NOT_FOUND);

}

@RequestMapping(value = "model", method = RequestMethod.DELETE)
@ApiOperation(value = "Delete a model",
notes = "Given a **company** and a **property**, deletes the associated stored model. Additionally this method allows" +
" some variations:\n" +
"- If *property* = \"ALL\", removes all models of the given company.\n" +
"- If *company* = \"ALL\", removes all models of all companies (used as safe drop database).")
@ApiResponses(value = {@ApiResponse(code = 200, message = "Files deleted correctly", response = String.class),
@ApiResponse(code = 404, message = "Model(s) not found", response = String.class)})
public ResponseEntity deleteModel(@RequestBody CompanyPropertyKey request) throws Exception {

String msg = classificationService.delete(request);
return new ResponseEntity<>(msg, msg.equals("Model(s) not found") ? HttpStatus.NOT_FOUND : HttpStatus.OK);
}

@RequestMapping(value = "classify", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
Expand All @@ -49,8 +79,8 @@ public String train(@ApiParam(value = "Request with the requirements to train",
" and a recommended label is returned for each requirement (with a level of confidence)")
@ApiResponses( value = {@ApiResponse(code = 200, message = "OK", response = RecommendationList.class)})
public RecommendationList classify(@ApiParam(value = "Request with the requirements to classify", required = true)@RequestBody ClassifyRequirementList request,
@ApiParam(value = "Property of the classifier", required = true) @RequestParam("property") String property,
@ApiParam(value = "Company to which the model belong", required = true) @RequestParam("company") String enterpriseName) throws Exception {
@ApiParam(value = "Property of the classifier (requirement_type)", required = true) @RequestParam("property") String property,
@ApiParam(value = "Proprietary company of the model", required = true) @RequestParam("company") String enterpriseName) throws Exception {

return classificationService.classify(new RequirementList(request), property, enterpriseName);

Expand All @@ -73,32 +103,9 @@ public Stats trainAndTest(@ApiParam(value = "Request with the requirements to te

}

@RequestMapping(value = "update", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
@ApiOperation(value = "Update a model",
notes = "Given a list of requirements, updates the model of the classifier for the given company")
@ApiResponses(value = {@ApiResponse(code = 200, message = "OK", response = String.class)})
public String update(@ApiParam(value = "Request with the requirements to train and update the model", required = true) @RequestBody RequirementList request,
@ApiParam(value = "Property of the classifier", required = true) @RequestParam("property") String property,
@ApiParam(value = "Company to which the model belong", required = true) @RequestParam("company") String enterpriseName) throws Exception {

return classificationService.update(request, property, enterpriseName);

}

//TODO refactor to DELETE
@RequestMapping(value = "deleteModel", method = RequestMethod.POST)
@ApiOperation(value = "Deleted an existing model",
notes = "Given a company and a property, deletes the associated stored model.")
@ApiResponses(value = {@ApiResponse(code = 200, message = "OK", response = String.class)})
public String deleteModel(@RequestBody CompanyPropertyKey request) throws Exception {

return classificationService.delete(request);

}

@RequestMapping(value = "parse", method = RequestMethod.POST)
/*@RequestMapping(value = "parse", method = RequestMethod.POST)
public RequirementList parseSiemensToOpenReq(@RequestBody SiemensRequirementList siemensRequirementList) {
return dataService.parseSiemensToOpenReq(siemensRequirementList);
}
}*/

}
Loading

0 comments on commit 2b859d8

Please sign in to comment.