Skip to content

Commit

Permalink
Working on R invocation
Browse files Browse the repository at this point in the history
  • Loading branch information
mbruno authored and mbruno committed Jun 24, 2019
1 parent ed17214 commit 4622e5b
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 87 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
<version>20180130</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/it/istat/is2/app/service/LogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import it.istat.is2.app.dao.LogDao;
import it.istat.is2.app.domain.Log;
import it.istat.is2.app.util.IS2Const;
import java.util.ArrayList;

@Service
@Transactional
Expand All @@ -54,6 +55,17 @@ public List<Log> findByIdSessione(Long idSessione) {
return (List<Log>) this.logDao.findByIdSessioneOrderByIdDesc(idSessione);
}

public List<Log> findByIdSessione() {
SessionBean sessionBean = (SessionBean) httpSession.getAttribute(IS2Const.SESSION_BEAN);
List<Log> logs;
if (sessionBean != null) {
logs = this.logDao.findByIdSessioneOrderByIdDesc(sessionBean.getId());
} else{
logs = new ArrayList<>();
}
return logs;
}

public long deleteByIdSessione(Long idSessione) {
return this.logDao.deleteByIdSessione(idSessione);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,13 @@ public String mostraroleset(HttpSession session, Model model, @PathVariable("id"
public String caricafile(HttpSession session, Model model, @PathVariable("idfile") Integer idfile) {

notificationService.removeAllMessages();
List<Log> logs = logService.findByIdSessione();
SxRuleset ruleset = ruleService.findRuleSet(idfile);
List<SxRule> rules = ruleService.findRules(ruleset);

model.addAttribute("ruleset", ruleset);
model.addAttribute("rules", rules);
model.addAttribute("logs", logs);

return "ruleset/preview";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@
*/
package it.istat.is2.rule.controller.rest;

import it.istat.is2.app.domain.Log;
import it.istat.is2.app.service.LogService;
import it.istat.is2.rule.service.RuleService;
import it.istat.is2.workflow.domain.SxRule;
import it.istat.is2.workflow.domain.SxRuleset;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
Expand All @@ -49,11 +51,11 @@ public List<SxRule> ruleslist(Model model) {
return rules;
}

@GetMapping("/runvalidate/{idRuleset}")
@GetMapping("/rules/runvalidate/{idRuleset}")
public void runValidate(@PathVariable("idRuleset") Integer idRuleset) {

ruleService.runValidate(idRuleset);

SxRuleset ruleSet = ruleService.findRuleSet(idRuleset);
ruleService.runValidate(ruleSet);
}

}
82 changes: 82 additions & 0 deletions src/main/java/it/istat/is2/rule/engine/EngineValidate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package it.istat.is2.rule.engine;

import it.istat.is2.app.service.LogService;
import it.istat.is2.workflow.domain.SxRule;
import java.util.List;
import org.apache.log4j.Logger;
import org.rosuda.REngine.Rserve.RConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class EngineValidate {

public static final String INPUT = "input";
public static final String OUTPUT = "output";
public static final String SRC_VALIDATE = "validate.R";
@Value("${serverR.host}")
private String serverRHost;
@Value("${serverR.port}")
private Integer serverRPort;
@Value("${path.script.R}")
private String pathR;
private RConnection connection;
private String[] input;

@Autowired
private LogService logService;

public void init() throws Exception {

logService.save("Connecting to R server...");

// Create a connection to Rserve instance running on default port 6311
if (serverRPort == null || serverRPort == 0) {
serverRPort = 6311;
}

if (serverRHost == null || serverRHost.equals("") || serverRHost.equals("localhost")) {
connection = new RConnection();
} else {
connection = new RConnection(serverRHost, serverRPort);
}

logService.save("Successfully connected!");

connection.eval("setwd('" + pathR + "')");
connection.eval("source('" + SRC_VALIDATE + "')");
logService.save("Validate R script loaded");
}

public void validateRules(List<SxRule> rules) throws Exception {

init();

input = new String[rules.size()];
for(int i = 0; i < rules.size(); i++){
input[i] = rules.get(i).getRule().toUpperCase();
}
connection.assign(INPUT, input);
connection.eval(INPUT + " <- data.frame(rule=" + INPUT + ")");
connection.eval("print(" + INPUT + ")");
String[] out = connection.eval("validate(" + INPUT + ")").asStrings();

logService.save("Script output " + out[0]);

destroy();
}

public void destroy() {
if (connection != null || !connection.isConnected()) {
connection.close();
}
logService.save("Connection to R server closed!");
}

}
72 changes: 0 additions & 72 deletions src/main/java/it/istat/is2/rule/service/EngineValidate.java

This file was deleted.

17 changes: 8 additions & 9 deletions src/main/java/it/istat/is2/rule/service/RuleService.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package it.istat.is2.rule.service;

import it.istat.is2.app.util.FileHandler;
import it.istat.is2.rule.engine.EngineValidate;
import it.istat.is2.workflow.dao.SxRuleDao;
import it.istat.is2.workflow.dao.SxRuleTypeDao;
import it.istat.is2.workflow.dao.SxRulesetDao;
Expand All @@ -50,7 +51,6 @@
@Service
public class RuleService {

// private EngineValidate engine;
@Autowired
private WorkSessionService sessioneLavoroService;
@Autowired
Expand All @@ -59,6 +59,8 @@ public class RuleService {
private SxRuleTypeDao sxRuleTypeDao;
@Autowired
private SxRulesetDao sxRulesetDao;
@Autowired
private EngineValidate engine;

public List<SxRule> findAll() {
return (List<SxRule>) this.sxRuleDao.findAll();
Expand All @@ -68,18 +70,15 @@ public List<SxRuleType> findAllRuleType() {
return (List<SxRuleType>) sxRuleTypeDao.findAll();
}

public void runValidate(Integer idRuleset) {
// List<SxRule> rules = sxRuleDao.findBySxRuleset(idRuleset);
public void runValidate(SxRuleset ruleset) {
List<SxRule> rules = sxRuleDao.findBySxRuleset(ruleset);

try {
// engine.init();
// engine.loadRules(null);
// engine.processOutput();

engine.validateRules(rules);
} catch (Exception e) {
Logger.getRootLogger().debug(e.getMessage());
Logger.getRootLogger().error(e.getMessage());
} finally {
// engine.destroy();
engine.destroy();
}

}
Expand Down
15 changes: 15 additions & 0 deletions src/main/resources/public/js/rulesetpreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,19 @@ function editRule(idRule) {

function deleteRule(idRule) {
alert("Delete rule \'" + idRule + "\'");
}

function runValidate(idRuleset){
$('#loading').modal('show');
$.ajax({
url: _ctx + "/rules/runvalidate/" + idRuleset,
type: "GET",
success: function (data) {
$('#loading').modal('hide');
},
error: function (jqXHR, textStatus, errorThrown) {
alert('Error loading data');
}
});

}
2 changes: 1 addition & 1 deletion src/main/resources/templates/ruleset/preview.html
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
</li>

<div class="btn-group pull-right" role="group">
<a class="bar-right" title="Run validate" th:href="@{'/sessione/mostradataset/'+${session.sessioneBean.id}}">
<a class="bar-right" title="Run validate" href="javascript:void(0)" th:onclick="'javascript:runValidate(' + ${ruleset.id} + ')'">
<i class="fa fa-caret-square-o-right"></i>&nbsp;<span th:text="#{ruleset.package.validate}"></span>
</a>
<a class="" title="Chiudi sessione" th:href="@{'/rule/viewRuleset/'+${session.sessioneBean.id}}">
Expand Down

0 comments on commit 4622e5b

Please sign in to comment.