You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ActionModel.java 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * Copyright (c) 2006-2012 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. package com.dmdirc.actions;
  23. import com.dmdirc.Precondition;
  24. import com.dmdirc.ServerManager;
  25. import com.dmdirc.WritableFrameContainer;
  26. import com.dmdirc.commandparser.CommandManager;
  27. import com.dmdirc.commandparser.parsers.CommandParser;
  28. import com.dmdirc.commandparser.parsers.GlobalCommandParser;
  29. import com.dmdirc.config.IdentityManager;
  30. import com.dmdirc.interfaces.actions.ActionType;
  31. import java.util.ArrayList;
  32. import java.util.Arrays;
  33. import java.util.List;
  34. /**
  35. * Represents the basic model of an action, and its triggering mechanism.
  36. * Saving and loading are handled by the Action class.
  37. */
  38. public class ActionModel {
  39. /** The group this action belongs to. */
  40. protected String group;
  41. /** The name of this action. */
  42. protected String name;
  43. /** The ActionTypes that trigger this action. */
  44. protected ActionType[] triggers;
  45. /** The commands to execute if this action is triggered. */
  46. protected String[] response;
  47. /** The change that should be made to the format string, if any. */
  48. protected String newFormat;
  49. /** The conditions for this action. */
  50. protected List<ActionCondition> conditions = new ArrayList<ActionCondition>();
  51. /** The condition tree used for evaluating conditions. */
  52. protected ConditionTree conditionTree;
  53. /** Whether this action has been modified or not. */
  54. protected boolean modified;
  55. /** Whether this action wishes the event to be stopped. */
  56. protected boolean stop;
  57. /** The concurrency group this action belongs to, if any. */
  58. protected String concurrencyGroup;
  59. /** The status of this action. */
  60. protected ActionStatus status = ActionStatus.ACTIVE;
  61. /** A description of any error that occurs while creating the action. */
  62. protected String error;
  63. /** The type of error that occurred, if any. */
  64. protected ActionErrorType errorType;
  65. /**
  66. * Creates a new instance of ActionModel with the specified properties.
  67. *
  68. * @param group The group the action belongs to
  69. * @param name The name of the action
  70. */
  71. public ActionModel(final String group, final String name) {
  72. this.group = group;
  73. this.name = name;
  74. }
  75. /**
  76. * Creates a new instance of ActionModel with the specified properties.
  77. *
  78. * @param group The group the action belongs to
  79. * @param name The name of the action
  80. * @param triggers The triggers to use
  81. * @param response The response to use
  82. * @param conditions The conditions to use
  83. * @param conditionTree The condition tree to use
  84. * @param newFormat The new formatter to use
  85. */
  86. public ActionModel(final String group, final String name,
  87. final ActionType[] triggers, final String[] response,
  88. final List<ActionCondition> conditions,
  89. final ConditionTree conditionTree, final String newFormat) {
  90. this(group, name);
  91. this.triggers = triggers.clone();
  92. this.response = response.clone();
  93. this.conditions = conditions;
  94. this.conditionTree = conditionTree;
  95. this.newFormat = newFormat;
  96. this.modified = true;
  97. }
  98. /**
  99. * Triggers this action.
  100. *
  101. * @param format The format of the message that's going to be displayed.
  102. * @param arguments The arguments from the action that caused this trigger.
  103. * @return True if the execution of the event should be stopped, or false
  104. * if the event may continue
  105. */
  106. @Precondition({
  107. "This action has at least one trigger",
  108. "This action's primary trigger is non-null"
  109. })
  110. public boolean trigger(final StringBuffer format,
  111. final Object... arguments) {
  112. assert triggers.length > 0;
  113. assert triggers[0] != null;
  114. if (status != ActionStatus.ACTIVE) {
  115. return false;
  116. }
  117. final ActionSubstitutor sub = new ActionSubstitutor(triggers[0]);
  118. if (!test(sub, arguments)) {
  119. return false;
  120. }
  121. WritableFrameContainer container = null;
  122. CommandParser cp;
  123. if (arguments.length > 0
  124. && arguments[0] instanceof WritableFrameContainer) {
  125. container = (WritableFrameContainer) arguments[0];
  126. } else if (ServerManager.getServerManager().numServers() > 0) {
  127. container = ServerManager.getServerManager().getServers().get(0);
  128. }
  129. if (container == null) {
  130. cp = new GlobalCommandParser(IdentityManager.getIdentityManager()
  131. .getGlobalConfiguration(), CommandManager.getCommandManager());
  132. } else {
  133. cp = container.getCommandParser();
  134. }
  135. for (String command : response) {
  136. cp.parseCommand(container, sub.doSubstitution(command, arguments));
  137. }
  138. if (newFormat != null && format != null) {
  139. format.setLength(0);
  140. format.append(newFormat);
  141. }
  142. return stop;
  143. }
  144. /**
  145. * Tests to see if this action should be triggered or not.
  146. *
  147. * @param sub The ActionsSubstitutor to use to substitute args
  148. * @param arguments The arguments for the action event
  149. * @return True if the action should be executed, false otherwise
  150. */
  151. public boolean test(final ActionSubstitutor sub,
  152. final Object ... arguments) {
  153. final boolean[] results = new boolean[conditions.size()];
  154. int i = 0;
  155. for (ActionCondition condition : conditions) {
  156. results[i++] = condition.test(sub, arguments);
  157. }
  158. return getRealConditionTree().evaluate(results);
  159. }
  160. /**
  161. * Retrieves a list of this action's conditions.
  162. *
  163. * @return A list of this action's conditions
  164. */
  165. public List<ActionCondition> getConditions() {
  166. return conditions;
  167. }
  168. /**
  169. * Sets this action's conditions.
  170. *
  171. * @param conditions A list of conditions to use
  172. */
  173. public void setConditions(final List<ActionCondition> conditions) {
  174. this.conditions = conditions;
  175. this.modified = true;
  176. }
  177. /**
  178. * Retrieves the status of this action.
  179. *
  180. * @since 0.6.5
  181. * @return This action's current status
  182. */
  183. public ActionStatus getStatus() {
  184. return status;
  185. }
  186. /**
  187. * Retrieves a plain text description of any error that occurred while
  188. * loading this action.
  189. *
  190. * @since 0.6.5
  191. * @return This action's error message, or null if no error was encountered
  192. */
  193. public String getError() {
  194. return error;
  195. }
  196. /**
  197. * Retrieves the type of the error that occurred while loading this action.
  198. *
  199. * @since 0.6.5
  200. * @return This action's error's type, or null if no error was encountered
  201. */
  202. public ActionErrorType getErrorType() {
  203. return errorType;
  204. }
  205. /**
  206. * Retrieves this action's triggers.
  207. *
  208. * @return The triggers used by this action
  209. */
  210. public ActionType[] getTriggers() {
  211. return triggers == null ? triggers : triggers.clone();
  212. }
  213. /**
  214. * Sets this action's triggers.
  215. *
  216. * @param triggers The new triggers to use
  217. */
  218. public void setTriggers(final ActionType[] triggers) {
  219. this.triggers = triggers.clone();
  220. this.modified = true;
  221. }
  222. /**
  223. * Retrieves this action's new format setting.
  224. *
  225. * @return The format that this action will use, or null if no change
  226. */
  227. public String getNewFormat() {
  228. return newFormat;
  229. }
  230. /**
  231. * Sets this action's new format setting.
  232. *
  233. * @param newFormat The new 'new format' setting
  234. */
  235. public void setNewFormat(final String newFormat) {
  236. this.newFormat = newFormat;
  237. this.modified = true;
  238. }
  239. /**
  240. * Retrieves this action's response.
  241. *
  242. * @return The commands that will be executed if this action is triggered
  243. */
  244. public String[] getResponse() {
  245. return response == null ? response : response.clone();
  246. }
  247. /**
  248. * Sets this action's response.
  249. *
  250. * @param response The new response to use
  251. */
  252. public void setResponse(final String[] response) {
  253. this.response = response.clone();
  254. this.modified = true;
  255. }
  256. /**
  257. * Retrieves this action's group name.
  258. *
  259. * @return This action's group name
  260. */
  261. public String getGroup() {
  262. return group;
  263. }
  264. /**
  265. * Sets the group of this action.
  266. *
  267. * @param newGroup The new group for this action
  268. */
  269. public void setGroup(final String newGroup) {
  270. this.group = newGroup;
  271. this.modified = true;
  272. }
  273. /**
  274. * Retrieves this action's name.
  275. *
  276. * @return This action's name
  277. */
  278. public String getName() {
  279. return name;
  280. }
  281. /**
  282. * Sets the name of this action.
  283. *
  284. * @param newName The new name for this action
  285. */
  286. public void setName(final String newName) {
  287. this.name = newName;
  288. this.modified = true;
  289. }
  290. /**
  291. * Retrieves the condition tree used for this action. Condition trees may
  292. * be null, in which case the arguments are conjoined together.
  293. *
  294. * @return This action's condition tree
  295. */
  296. public ConditionTree getConditionTree() {
  297. return conditionTree;
  298. }
  299. /**
  300. * Retrieves a concrete condition tree used for this action. If there is
  301. * no condition tree defined for this action, returns a conjunction tree
  302. * for the arguments.
  303. *
  304. * @since 0.6
  305. * @return A {@link ConditionTree} object for this action
  306. */
  307. public ConditionTree getRealConditionTree() {
  308. return conditionTree == null ? ConditionTree.createConjunction(
  309. conditions.size()) : conditionTree;
  310. }
  311. /**
  312. * Sets the condition tree used for this action.
  313. *
  314. * @param conditionTree The new condition tree to be used
  315. */
  316. public void setConditionTree(final ConditionTree conditionTree) {
  317. this.conditionTree = conditionTree;
  318. this.modified = true;
  319. }
  320. /**
  321. * Retrieves the concurrency group of this action.
  322. *
  323. * @return This action's concurrency group
  324. * @since 0.6.3
  325. */
  326. public String getConcurrencyGroup() {
  327. return concurrencyGroup;
  328. }
  329. /**
  330. * Sets the concurrency group of this action.
  331. *
  332. * @param concurrencyGroup This action's new concurrency group
  333. * @since 0.6.3
  334. */
  335. public void setConcurrencyGroup(final String concurrencyGroup) {
  336. this.concurrencyGroup = (concurrencyGroup == null
  337. || concurrencyGroup.isEmpty()) ? null : concurrencyGroup;
  338. }
  339. /**
  340. * Determines whether or not this action will stop the event execution
  341. * if it is triggered.
  342. *
  343. * @return The stopping preference of this action
  344. * @see #setStopping(boolean)
  345. * @since 0.6.4
  346. */
  347. public boolean isStopping() {
  348. return stop;
  349. }
  350. /**
  351. * Sets the stopping preference of this action. If the stopping preference
  352. * is <code>true</code> then when this action is successfully triggered,
  353. * it will request that execution of the event is stopped. This will
  354. * prevent the default behaviour of the callee being executed.
  355. *
  356. * @param stop The new stopping preference of this action
  357. * @see #isStopping()
  358. * @since 0.6.4
  359. */
  360. public void setStopping(final boolean stop) {
  361. this.stop = stop;
  362. }
  363. /**
  364. * Determine if this model has been modified since it was constructed or
  365. * its modified status was reset.
  366. *
  367. * @return True if this model has been modified, false otherwise
  368. */
  369. public boolean isModified() {
  370. return modified;
  371. }
  372. /**
  373. * Resets the modified status of this model. After a call to
  374. * resetModified(), this model will report that it has not been modified,
  375. * until one of the set* methods is used.
  376. */
  377. public void resetModified() {
  378. this.modified = false;
  379. }
  380. /** {@inheritDoc} */
  381. @Override
  382. public String toString() {
  383. return "[name=" + group + "/" + name + ", triggers="
  384. + Arrays.toString(triggers) + ", response="
  385. + Arrays.toString(response) + ", "
  386. + conditions + ", format='" + newFormat + "']";
  387. }
  388. }