/** * Create an internalTaskFlow job with the given task flow job (user) * * @param userJob the user job that will be used to create the internal job. * @return the created internal job. * @throws JobCreationException an exception if the factory cannot create the given job. */ private static InternalJob createJob(TaskFlowJob userJob) throws JobCreationException { if (userJob.getTasks().size() == 0) { logger.info("Job '" + userJob.getName() + "' must contain tasks !"); throw new JobCreationException("This job must contains tasks !"); } // validate taskflow List<FlowChecker.Block> blocks = new ArrayList<>(); FlowError err = FlowChecker.validate(userJob, blocks); if (err != null) { String e = ""; e += "Invalid taskflow: " + err.getMessage() + "; context: " + err.getTask(); logger.error(e); throw new JobCreationException(e, err); } InternalJob job = new InternalTaskFlowJob(); // keep an initial job content job.setTaskFlowJob(userJob); Map<Task, InternalTask> tasksList = new LinkedHashMap<>(); boolean hasPreciousResult = false; for (Task t : userJob.getTasks()) { tasksList.put(t, createTask(userJob, t)); if (!hasPreciousResult) { hasPreciousResult = t.isPreciousResult(); } } for (Entry<Task, InternalTask> entry : tasksList.entrySet()) { if (entry.getKey().getDependencesList() != null) { for (Task t : entry.getKey().getDependencesList()) { entry.getValue().addDependence(tasksList.get(t)); } } job.addTask(entry.getValue()); } // tag matching blocks in InternalTasks for (InternalTask it : tasksList.values()) { for (FlowChecker.Block block : blocks) { if (it.getName().equals(block.start.element.getName())) { it.setMatchingBlock(block.end.element.getName()); } if (it.getName().equals(block.end.element.getName())) { it.setMatchingBlock(block.start.element.getName()); } } } // create if/else/join weak dependencies for (InternalTask it : tasksList.values()) { // it performs an IF action if (it.getFlowScript() != null && it.getFlowScript().getActionType().equals(FlowActionType.IF.toString())) { String ifBranch = it.getFlowScript().getActionTarget(); String elseBranch = it.getFlowScript().getActionTargetElse(); String join = it.getFlowScript().getActionContinuation(); List<InternalTask> joinedBranches = new ArrayList<>(); // find the ifBranch task for (InternalTask it2 : tasksList.values()) { if (it2.getName().equals(ifBranch)) { it2.setIfBranch(it); String match = it2.getMatchingBlock(); // find its matching block task if (match == null) { // no match: single task joinedBranches.add(it2); } else { for (InternalTask it3 : tasksList.values()) { if (it3.getName().equals(match)) { joinedBranches.add(it3); break; } } } break; } } // find the elseBranch task for (InternalTask it2 : tasksList.values()) { if (it2.getName().equals(elseBranch)) { it2.setIfBranch(it); String match = it2.getMatchingBlock(); // find its matching block task if (match == null) { // no match: single task joinedBranches.add(it2); } else { for (InternalTask it3 : tasksList.values()) { if (it3.getName().equals(match)) { joinedBranches.add(it3); break; } } } break; } } // find the joinBranch task for (InternalTask it2 : tasksList.values()) { if (it2.getName().equals(join)) { it2.setJoinedBranches(joinedBranches); } } } } return job; }
/** Creates the task element, corressponding to <define name="task"> */ private Element createTaskElement(Document doc, Task task) { Element taskE = doc.createElementNS(Schemas.SCHEMA_LATEST.namespace, XMLTags.TASK.getXMLName()); // **** attributes ***** // **** common attributes *** if (task.getOnTaskErrorProperty().isSet()) { setAttribute( taskE, XMLAttributes.COMMON_ON_TASK_ERROR, task.getOnTaskErrorProperty().getValue().toString(), true); } if (task.getMaxNumberOfExecutionProperty().isSet()) { setAttribute( taskE, XMLAttributes.COMMON_MAX_NUMBER_OF_EXECUTION, Integer.toString(task.getMaxNumberOfExecution())); } setAttribute(taskE, XMLAttributes.COMMON_NAME, task.getName(), true); if (task.getRestartTaskOnErrorProperty().isSet()) { setAttribute( taskE, XMLAttributes.COMMON_RESTART_TASK_ON_ERROR, task.getRestartTaskOnError().toString()); } // *** task attributes *** if (task.getWallTime() != 0) { setAttribute(taskE, XMLAttributes.TASK_WALLTIME, formatDate(task.getWallTime())); } if (task.isRunAsMe()) { setAttribute(taskE, XMLAttributes.TASK_RUN_AS_ME, "true"); } if (task.isPreciousResult()) { setAttribute(taskE, XMLAttributes.TASK_PRECIOUS_RESULT, "true"); } if (task.isPreciousLogs()) { setAttribute(taskE, XMLAttributes.TASK_PRECIOUS_LOGS, "true"); } // *** elements **** // <ref name="variables"/> if (task.getVariables() != null && !task.getVariables().isEmpty()) { Element variablesE = createTaskVariablesElement(doc, task.getVariables()); taskE.appendChild(variablesE); } // <ref name="taskDescription"/> if (task.getDescription() != null) { Element descrNode = createElement(doc, XMLTags.COMMON_DESCRIPTION.getXMLName(), task.getDescription()); taskE.appendChild(descrNode); } // <ref name="genericInformation"/> if ((task.getGenericInformation() != null) && (task.getGenericInformation().size() > 0)) { Element genericInfoE = createGenericInformation(doc, task.getGenericInformation()); taskE.appendChild(genericInfoE); } // <ref name="depends"/> List<Task> dependencies = task.getDependencesList(); if ((dependencies != null) && (dependencies.size() > 0)) { Element dependsE = doc.createElementNS( Schemas.SCHEMA_LATEST.namespace, XMLTags.TASK_DEPENDENCES.getXMLName()); for (Task dep : dependencies) { Element dependsTask = doc.createElementNS( Schemas.SCHEMA_LATEST.namespace, XMLTags.TASK_DEPENDENCES_TASK.getXMLName()); setAttribute(dependsTask, XMLAttributes.TASK_DEPENDS_REF, dep.getName(), true); dependsE.appendChild(dependsTask); } taskE.appendChild(dependsE); } // if has dependencies // <ref name="inputFiles"/> List<InputSelector> inputFiles = task.getInputFilesList(); if (inputFiles != null) { Element inputFilesE = doc.createElementNS(Schemas.SCHEMA_LATEST.namespace, XMLTags.DS_INPUT_FILES.getXMLName()); for (InputSelector inputSelector : inputFiles) { FileSelector fs = inputSelector.getInputFiles(); Element filesE = doc.createElementNS(Schemas.SCHEMA_LATEST.namespace, XMLTags.DS_FILES.getXMLName()); // the xml only supports one value for the includes/excludes // pattern if (!fs.getIncludes().isEmpty()) setAttribute(filesE, XMLAttributes.DS_INCLUDES, fs.getIncludes().iterator().next(), true); if (!fs.getExcludes().isEmpty()) setAttribute(filesE, XMLAttributes.DS_EXCLUDES, fs.getExcludes().iterator().next(), true); if (inputSelector.getMode() != null) { setAttribute( filesE, XMLAttributes.DS_ACCESS_MODE, inputSelector.getMode().toString(), true); } inputFilesE.appendChild(filesE); } taskE.appendChild(inputFilesE); } // <ref name="parallel"/> Element parallelEnvE = createParallelEnvironment(doc, task); if (parallelEnvE != null) taskE.appendChild(parallelEnvE); // <ref name="selection"/> List<SelectionScript> selectionScripts = task.getSelectionScripts(); if (selectionScripts != null && selectionScripts.size() > 0) { Element selectionE = doc.createElementNS( Schemas.SCHEMA_LATEST.namespace, XMLTags.SCRIPT_SELECTION.getXMLName()); for (SelectionScript selectionScript : selectionScripts) { Element scriptE = createScriptElement(doc, selectionScript); selectionE.appendChild(scriptE); } taskE.appendChild(selectionE); } // <ref name="forkEnvironment"/> if (task.getForkEnvironment() != null) { Element forkEnvE = createForkEnvironmentElement(doc, task.getForkEnvironment()); taskE.appendChild(forkEnvE); } // <ref name="pre"/> Script preScript = task.getPreScript(); if (preScript != null) { Element preE = doc.createElementNS(Schemas.SCHEMA_LATEST.namespace, XMLTags.SCRIPT_PRE.getXMLName()); Element scriptE = createScriptElement(doc, preScript); preE.appendChild(scriptE); taskE.appendChild(preE); } // <ref name="executable"/> Element executableE = null; if (task instanceof JavaTask) { executableE = createJavaExecutableElement(doc, (JavaTask) task); } else if (task instanceof NativeTask) { executableE = createNativeExecutableElement(doc, (NativeTask) task); } else if (task instanceof ScriptTask) { executableE = createScriptExecutableElement(doc, (ScriptTask) task); } taskE.appendChild(executableE); // <ref name="flow"/> Element controlFlowE = createFlowControlElement(doc, task); if (controlFlowE != null) taskE.appendChild(controlFlowE); // <ref name="post"/> Script postScript = task.getPostScript(); if (postScript != null) { Element postE = doc.createElementNS(Schemas.SCHEMA_LATEST.namespace, XMLTags.SCRIPT_POST.getXMLName()); Element scriptE = createScriptElement(doc, postScript); postE.appendChild(scriptE); taskE.appendChild(postE); } // <ref name="cleaning"/> Script cleanScript = task.getCleaningScript(); if (cleanScript != null) { Element cleanE = doc.createElementNS( Schemas.SCHEMA_LATEST.namespace, XMLTags.SCRIPT_CLEANING.getXMLName()); Element scriptE = createScriptElement(doc, cleanScript); cleanE.appendChild(scriptE); taskE.appendChild(cleanE); } // <ref name="outputFiles"/> List<OutputSelector> outputFiles = task.getOutputFilesList(); if (outputFiles != null) { Element outputFilesE = doc.createElementNS( Schemas.SCHEMA_LATEST.namespace, XMLTags.DS_OUTPUT_FILES.getXMLName()); for (OutputSelector outputSelector : outputFiles) { FileSelector fs = outputSelector.getOutputFiles(); Element filesE = doc.createElementNS(Schemas.SCHEMA_LATEST.namespace, XMLTags.DS_FILES.getXMLName()); // the xml only supports one value for the includes/excludes // pattern if (!fs.getIncludes().isEmpty()) setAttribute(filesE, XMLAttributes.DS_INCLUDES, fs.getIncludes().iterator().next(), true); if (!fs.getExcludes().isEmpty()) setAttribute(filesE, XMLAttributes.DS_EXCLUDES, fs.getExcludes().iterator().next(), true); if (outputSelector.getMode() != null) { setAttribute( filesE, XMLAttributes.DS_ACCESS_MODE, outputSelector.getMode().toString(), true); } outputFilesE.appendChild(filesE); } taskE.appendChild(outputFilesE); } return taskE; }