/src/main/java/org/ourgrid/common/specification/job/JobSpecification.java

https://github.com/OurGrid/OurGrid · Java · 277 lines · 121 code · 59 blank · 97 comment · 33 complexity · 86d0ed530dda42b2bb4d0dad08c430d9 MD5 · raw file

  1. /*
  2. * Copyright (C) 2008 Universidade Federal de Campina Grande
  3. *
  4. * This file is part of OurGrid.
  5. *
  6. * OurGrid is free software: you can redistribute it and/or modify it under the
  7. * terms of the GNU Lesser General Public License as published by the Free
  8. * Software Foundation, either version 3 of the License, or (at your option)
  9. * any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but WITHOUT
  12. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
  14. * for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. *
  19. */
  20. package org.ourgrid.common.specification.job;
  21. import java.io.Serializable;
  22. import java.util.ArrayList;
  23. import java.util.Arrays;
  24. import java.util.LinkedHashMap;
  25. import java.util.List;
  26. import java.util.Map;
  27. import org.ourgrid.common.specification.exception.JobSpecificationException;
  28. import org.ourgrid.common.util.CommonUtils;
  29. /**
  30. * Entity that encapsulates all the information given by the user about each
  31. * job. To inform, the user uses the Description Files that can be compiled by
  32. * the CommonCompiler.
  33. *
  34. * @see org.ourgrid.common.specification.main.JDLCompiler
  35. * @version 2.0
  36. * @author Ricardo Araujo Santos - ricardo@lsd.ufcg.edu.br
  37. *
  38. */
  39. public class JobSpecification implements Serializable {
  40. /**
  41. * Serial identification of the class. It need to be changed only if the
  42. * class interface is changed.
  43. */
  44. private static final long serialVersionUID = 1L;
  45. private String label;
  46. private String requirements;
  47. /** Describes the user preferences to be checked against the resources' annotations. */
  48. private Map<String,String> annotations;
  49. private List<TaskSpecification> taskSpecs;
  50. /** Job source directory where results files are stored */
  51. private String sourceParentDir;
  52. public JobSpecification() {}
  53. /**
  54. * Constructor.
  55. *
  56. * @param label The label for the job.
  57. * @param requirements The logical expression that defines the job. It will
  58. * be used to choose machines that are able to run its tasks. To
  59. * define it well, check the OurGrid manual.
  60. * @param taskSpecs A list with all the task specifications of this job.
  61. */
  62. public JobSpecification( String label, String requirements, List<TaskSpecification> taskSpecs, Map<String,String> annotations) throws JobSpecificationException {
  63. this.annotations = annotations;
  64. this.label = label;
  65. this.requirements = requirements;
  66. this.taskSpecs = taskSpecs;
  67. validate();
  68. }
  69. /**
  70. * This method validates the attributes of this Job Spec
  71. */
  72. private void validate() throws JobSpecificationException {
  73. if ( this.taskSpecs == null ) {
  74. throw new JobSpecificationException( "A Job Spec could not be initialized with a null list of Task Specs." );
  75. }
  76. if ( this.taskSpecs.size() == 0 ) {
  77. throw new JobSpecificationException(
  78. "A Job Spec could not be initialized with an empty list of Task Specs." );
  79. }
  80. if ( this.taskSpecs.contains( null ) ) {
  81. throw new JobSpecificationException(
  82. "A Job Spec could not contain a null element into the list of Task Specs." );
  83. }
  84. }
  85. /**
  86. * Constructor.
  87. *
  88. * @param label The label for the job.
  89. * @param requirements The logical expression that defines the job. It will
  90. * be used to choose machines that are able to run its tasks. To
  91. * define it well, check the OurGrid manual.
  92. * @param taskSpecs A list with all the task specifications of this job.
  93. */
  94. public JobSpecification( String label, String requirements, List<TaskSpecification> taskSpecs ) throws JobSpecificationException {
  95. this(label, requirements, taskSpecs, new LinkedHashMap<String, String>());
  96. }
  97. /**
  98. * The constructor
  99. *
  100. * @param label The label for the job.
  101. * @throws JobSpecificationException
  102. */
  103. public JobSpecification( String label ) {
  104. this.label = label;
  105. this.requirements = "";
  106. this.taskSpecs = new ArrayList<TaskSpecification>();
  107. this.annotations = CommonUtils.createSerializableMap();
  108. }
  109. /**
  110. * Default JDL compliant constructor.
  111. * @param tasks One or more {@link TaskSpecification} built from a JDL expression.
  112. */
  113. public JobSpecification(TaskSpecification... tasks) {
  114. this("");
  115. assert tasks != null : "Null tasks must not be produced by the JDL compiler";
  116. assert tasks.length != 0 : "Empty tasks must not be produced by the JDL compiler";
  117. assert !Arrays.asList( tasks ).contains( null ) : "Null tasks must not be produced by the JDL compiler";
  118. taskSpecs = new ArrayList<TaskSpecification>(Arrays.asList(tasks));
  119. }
  120. /**
  121. * @return A list with the tasks specification in this job.
  122. */
  123. public List<TaskSpecification> getTaskSpecs() {
  124. return taskSpecs;
  125. }
  126. /**
  127. * Inserts a list of task specifications.
  128. *
  129. * @param taskSpecs The list of tasks that will be contained by this job.
  130. * @throws JobSpecificationException
  131. */
  132. public void setTaskSpecs( List<TaskSpecification> taskSpecs ) throws JobSpecificationException {
  133. this.taskSpecs = taskSpecs;
  134. validate();
  135. }
  136. /**
  137. * @return The logical expression that will be used to choose machines to
  138. * run the tasks in this job.
  139. */
  140. public String getRequirements() {
  141. return this.requirements;
  142. }
  143. /**
  144. * Sets the logical expression for the job.
  145. *
  146. * @param expression The logical expression that defines the job. It will be
  147. * used to choose machines that are able to run its tasks.
  148. */
  149. public void setRequirements( String expression ) {
  150. this.requirements = expression;
  151. }
  152. /**
  153. * @return Gets the set of pair attribute=value defining the annotations for the job's preferences.
  154. */
  155. public Map<String, String> getAnnotations() {
  156. return this.annotations;
  157. }
  158. /**
  159. * Sets the set of pair attribute=value defining the annotations for the job's preferences.
  160. *
  161. * @param annotations Map of annotations
  162. *
  163. */
  164. public void setAnnotations( Map<String,String> annotations) {
  165. this.annotations = annotations;
  166. }
  167. /**
  168. * @return The label of the job.
  169. */
  170. public String getLabel() {
  171. return this.label;
  172. }
  173. public void setSourceDirPath( String sourceParentDir ) {
  174. this.sourceParentDir = sourceParentDir;
  175. }
  176. public String getSourceParentDir() {
  177. return this.sourceParentDir;
  178. }
  179. @Override
  180. public int hashCode() {
  181. final int PRIME = 31;
  182. int result = 1;
  183. result = PRIME * result + ((this.label == null) ? 0 : this.label.hashCode());
  184. result = PRIME * result + ((this.requirements == null) ? 0 : this.requirements.hashCode());
  185. result = PRIME * result + ((this.annotations == null) ? 0 : this.annotations.hashCode());
  186. result = PRIME * result + ((this.taskSpecs == null) ? 0 : this.taskSpecs.hashCode());
  187. result = PRIME * result + ((this.sourceParentDir == null) ? 0 : this.sourceParentDir.hashCode());
  188. return result;
  189. }
  190. @Override
  191. public boolean equals( Object obj ) {
  192. if ( this == obj )
  193. return true;
  194. if ( obj == null )
  195. return false;
  196. if ( getClass() != obj.getClass() )
  197. return false;
  198. final JobSpecification other = (JobSpecification) obj;
  199. if ( !(this.label == null ? other.label == null : this.label.equals( other.label )) )
  200. return false;
  201. if ( !(this.requirements == null ? other.requirements == null : this.requirements.equals( other.requirements )) )
  202. return false;
  203. if ( !(this.annotations == null ? other.annotations == null : this.annotations.equals( other.annotations )) )
  204. return false;
  205. if ( !(this.taskSpecs == null ? other.taskSpecs == null : this.taskSpecs.equals( other.taskSpecs )) )
  206. return false;
  207. if ( !(this.sourceParentDir == null ? other.sourceParentDir == null : this.sourceParentDir
  208. .equals( other.sourceParentDir )) )
  209. return false;
  210. return true;
  211. }
  212. public void setLabel(String label) {
  213. this.label = label;
  214. }
  215. public void setSourceParentDir(String sourceParentDir) {
  216. this.sourceParentDir = sourceParentDir;
  217. }
  218. }