/** * Run a single paragraph. * * @param paragraphId ID of paragraph */ public void run(String paragraphId) { Paragraph p = getParagraph(paragraphId); if (p.isBlankParagraph()) { logger.info("skip to run blank paragraph. {}", p.getId()); return; } p.setListener(jobListenerFactory.getParagraphJobListener(this)); String requiredReplName = p.getRequiredReplName(); Interpreter intp = factory.getInterpreter(p.getUser(), getId(), requiredReplName); if (intp == null) { String intpExceptionMsg = p.getJobName() + "'s Interpreter " + requiredReplName + " not found"; InterpreterException intpException = new InterpreterException(intpExceptionMsg); InterpreterResult intpResult = new InterpreterResult(InterpreterResult.Code.ERROR, intpException.getMessage()); p.setReturn(intpResult, intpException); p.setStatus(Job.Status.ERROR); throw intpException; } if (p.getConfig().get("enabled") == null || (Boolean) p.getConfig().get("enabled")) { p.setAuthenticationInfo(p.getAuthenticationInfo()); intp.getScheduler().submit(p); } }
/** Clear all paragraph output of note */ public void clearAllParagraphOutput() { synchronized (paragraphs) { for (Paragraph p : paragraphs) { p.setReturn(null, null); } } }
/** * Clone paragraph and add it to note. * * @param srcParagraph source paragraph */ void addCloneParagraph(Paragraph srcParagraph) { // Keep paragraph original ID final Paragraph newParagraph = new Paragraph(srcParagraph.getId(), this, this, factory); Map<String, Object> config = new HashMap<>(srcParagraph.getConfig()); Map<String, Object> param = new HashMap<>(srcParagraph.settings.getParams()); Map<String, Input> form = new HashMap<>(srcParagraph.settings.getForms()); newParagraph.setConfig(config); newParagraph.settings.setParams(param); newParagraph.settings.setForms(form); newParagraph.setText(srcParagraph.getText()); newParagraph.setTitle(srcParagraph.getTitle()); try { Gson gson = new Gson(); String resultJson = gson.toJson(srcParagraph.getReturn()); InterpreterResult result = gson.fromJson(resultJson, InterpreterResult.class); newParagraph.setReturn(result, null); } catch (Exception e) { // 'result' part of Note consists of exception, instead of actual interpreter results logger.warn( "Paragraph " + srcParagraph.getId() + " has a result with exception. " + e.getMessage()); } synchronized (paragraphs) { paragraphs.add(newParagraph); } if (noteEventListener != null) { noteEventListener.onParagraphCreate(newParagraph); } }
/** * Clear paragraph output by id. * * @param paragraphId ID of paragraph * @return Paragraph */ public Paragraph clearParagraphOutput(String paragraphId) { synchronized (paragraphs) { for (Paragraph p : paragraphs) { if (p.getId().equals(paragraphId)) { p.setReturn(null, null); return p; } } } return null; }