Exemple #1
0
  public Rules getRules(int ruleId) {

    PreparedStatement ps = null;
    ResultSet rs = null;
    Rules rules = new Rules();

    try {
      ps = con.prepareStatement(SELECT_RULE_QRY);
      ps.setInt(1, ruleId);
      rs = ps.executeQuery();

      String sourceID = "";
      String ruleString = "";
      String sourceFields = "";
      String ruleName = "";
      int userId = 0;
      if (rs.next()) {
        ruleId = rs.getInt("RuleID");
        ruleName = rs.getString(RULE_NAME);
        sourceID = rs.getString(SOURCE_ID);
        ruleString = rs.getString(RULE_QUERIES);
        sourceFields = rs.getString(SOURCE_FIELDS);
        userId = rs.getInt("user_id");
        LOGGER.debug(ruleString);
      }
      rules.setRuleID(ruleId);
      rules.setRuleName(ruleName);
      rules.setSource(sourceID);
      rules.setUserId(userId);
      rules.setExtractFields(sourceFields);
      //            objectMapper.convertValue(ruleString, Rule.class);
      List<Rule> ruless = new ArrayList<Rule>();

      JsonObject rulesObj = (new JsonParser()).parse(ruleString).getAsJsonObject();

      JsonArray rulesJson = rulesObj.get("rules").getAsJsonArray();
      for (int i = 0; i < rulesJson.size(); i++) {
        JsonObject tempObj = rulesJson.get(i).getAsJsonObject();
        Rule rule = new Rule();
        rule.setRuleOperator(tempObj.get("ruleOperator").getAsString());
        rule.setDataField(tempObj.get("dataField").getAsString());
        rule.setRuleParameters(tempObj.get("ruleParameters").getAsString());
        ruless.add(rule);
      }

      rules.setRules(ruless);
      // rulesObj.get("")
      // {"source":"ds59","rules":[{"ruleOperator":"coordinates","dataField":"loc","ruleParameters":"-120.2,40.1,-118.2,32.4"},{"ruleOperator":">","dataField":"value","ruleParameters":"100"}],"extractFields":"loc,value,timestamp"}
      LOGGER.debug(rules.toString());
    } catch (Exception e) {
      e.printStackTrace();
    } finally {

    }
    return rules;
  }
Exemple #2
0
  public int registerDefaultRule(DataSource ds) {
    int key = 0;

    Rules rules = new Rules();
    rules.setRuleName(DEFAULT);
    rules.setSource("ds" + ds.getSrcID());
    rules.setUserId(Integer.parseInt(ds.getUserId()));
    rules.setExtractFields(DEFAULT_EXTRACT_FIELD);

    // Sample Rule Insert
    // {"source":"ds42","extractFields":"loc,theme,value,timestamp,","rules":[{"dataField":"loc","ruleOperator":"coordinates","ruleParameters":"33,33,33,33"}]}

    try {
      PreparedStatement ps =
          con.prepareStatement(INSERT_DEFAULT_RULE_QRY, Statement.RETURN_GENERATED_KEYS);
      ps.setString(1, ds.getSrcID() + "_" + rules.getRuleName());
      String ruleQuery =
          "{\"rules\":[{\"ruleOperator\":\"coordinates\",\"dataField\":\"loc\",\"ruleParameters\":\""
              + ds.getInitParam().getSwLat()
              + ","
              + ds.getInitParam().getSwLong()
              + ","
              + ds.getInitParam().getNeLat()
              + ","
              + ds.getInitParam().getNeLong()
              + "\"}]}";

      ps.setString(2, ruleQuery);
      ps.setString(3, rules.getSource());
      ps.setString(4, rules.getExtractFields());
      ps.setInt(5, rules.getUserId());
      ps.executeUpdate();
      ResultSet rs = ps.getGeneratedKeys();
      if (rs.next()) {
        // Retrieve the auto generated key(s).
        key = rs.getInt(1);
      }

    } catch (Exception ex) {
      ex.printStackTrace();
    }

    return key;
  }
  public void run() {
    build();

    isRunning = true;

    // Start the Data Process
    for (int i = 0; i < dataProcesses.size(); i++) {
      new Thread(dataProcesses.get(i)).start();
    }

    // Start the C++ Operation
    String[] compile = null;
    String run = "";
    if (OS == 0) {
      String[] tcompile = {
        "sh", contextPath + "proc/Debug/compile.sh", queryID, "EmageOperators_" + queryID
      };
      compile = tcompile;
      run = contextPath + "proc/Debug/EmageOperators_" + queryID;
    } else if (OS == 1) {
      String[] tcompile = {
        "\"" + contextPath + "proc\\Debug\\compile.bat\"", queryID, "EmageOperators_" + queryID
      }; // changed due to new proj structure
      compile = tcompile;
      run =
          "\""
              + contextPath
              + "proc\\Debug\\EmageOperators_"
              + queryID
              + ".exe\""; // changed due to new proj structure
    }

    try {
      if (compile != null && run != "") {
        // Compile the query
        proc = Runtime.getRuntime().exec(compile);
        InputStream inputStream = proc.getInputStream();
        InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

        String line;
        while ((line = bufferedReader.readLine()) != null) {
          log.info(line);
        }
        proc.destroy();
        proc.waitFor();
        log.info("Compiled!");

        // Run the query
        proc = Runtime.getRuntime().exec(run);
        inputStream = proc.getInputStream();
        inputStreamReader = new InputStreamReader(inputStream);
        bufferedReader = new BufferedReader(inputStreamReader);
        while (isRunning && (line = bufferedReader.readLine()) != null) {
          log.info(line);
        }
        log.info("Run!");
      }

    } catch (IOException e) {
      log.error(e.getMessage());
    } catch (InterruptedException e) {
      log.error(e.getMessage());
    } catch (Exception e) {
      log.error(e.getMessage());
      stop();
    }
  }
Exemple #4
0
  public List<Rules> getAllRules(User user) {

    PreparedStatement ps = null;
    ResultSet rs = null;
    List<Rules> rulesList = new ArrayList<>();

    try {
      ps = con.prepareStatement(SELECT_ALL_RULE_QRY);
      ps.setInt(1, user.getId());
      rs = ps.executeQuery();
      while (rs.next()) {
        Rules rules = new Rules();
        int ruleID;
        String ruleName;
        String sourceID;
        String ruleString;
        String sourceFields;
        int userId;

        ruleID = rs.getInt(RULE_ID);
        ruleName = rs.getString(RULE_NAME);
        if (ruleName == null) {
          ruleName = "";
        }
        sourceID = rs.getString(SOURCE_ID);
        ruleString = rs.getString(RULE_QUERIES);
        if (ruleString == null) {
          ruleString = "";
        }
        sourceFields = rs.getString(SOURCE_FIELDS);
        if (sourceFields == null) {
          sourceFields = "";
        }
        userId = rs.getInt(USER_ID);

        rules.setRuleID(ruleID);
        rules.setRuleName(ruleName);
        rules.setUserId(userId);
        rules.setSource(sourceID);
        rules.setExtractFields(sourceFields);
        List<Rule> ruless = new ArrayList<Rule>();
        if (!ruleString.equals("")) {
          JsonObject rulesObj = (new JsonParser()).parse(ruleString).getAsJsonObject();
          JsonArray rulesJson = rulesObj.get("rules").getAsJsonArray();
          for (int i = 0; i < rulesJson.size(); i++) {
            Rule rule = new Rule();
            JsonObject tempObj = rulesJson.get(i).getAsJsonObject();
            rule.setRuleOperator(tempObj.get("ruleOperator").getAsString());
            rule.setDataField(tempObj.get("dataField").getAsString());
            rule.setRuleParameters(tempObj.get("ruleParameters").getAsString());
            ruless.add(rule);
          }
        }

        rules.setRules(ruless);
        rulesList.add(rules);
      }
      for (int i = 0; i < rulesList.size(); i++) {
        LOGGER.debug(rulesList.get(i).toString());
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {

    }
    return rulesList;
  }