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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * Copyright (c) 2006-2014 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.parsers.CommandParser;
  27. import com.dmdirc.commandparser.parsers.GlobalCommandParser;
  28. import com.dmdirc.interfaces.actions.ActionType;
  29. import java.util.ArrayList;
  30. import java.util.Arrays;
  31. import java.util.List;
  32. import javax.inject.Provider;
  33. /**
  34. * Represents the basic model of an action, and its triggering mechanism.
  35. * Saving and loading are handled by the Action class.
  36. */
  37. public class ActionModel {
  38. /** Provider of global command parsers, for use when triggering window-less actions. */
  39. private final Provider<GlobalCommandParser> globalCommandParserProvider;
  40. /** Factory to use to creator substitutors. */
  41. private final ActionSubstitutorFactory substitutorFactory;
  42. /** The group this action belongs to. */
  43. protected String group;
  44. /** The name of this action. */
  45. protected String name;
  46. /** The ActionTypes that trigger this action. */
  47. protected ActionType[] triggers;
  48. /** The commands to execute if this action is triggered. */
  49. protected String[] response;
  50. /** The change that should be made to the format string, if any. */
  51. protected String newFormat;
  52. /** The conditions for this action. */
  53. protected List<ActionCondition> conditions = new ArrayList<>();
  54. /** The condition tree used for evaluating conditions. */
  55. protected ConditionTree conditionTree;
  56. /** Whether this action has been modified or not. */
  57. protected boolean modified;
  58. /** Whether this action wishes the event to be stopped. */
  59. protected boolean stop;
  60. /** The concurrency group this action belongs to, if any. */
  61. protected String concurrencyGroup;
  62. /** The status of this action. */
  63. protected ActionStatus status = ActionStatus.ACTIVE;
  64. /** A description of any error that occurs while creating the action. */
  65. protected String error;
  66. /** The type of error that occurred, if any. */
  67. protected ActionErrorType errorType;
  68. /**
  69. * Creates a new instance of ActionModel with the specified properties.
  70. *
  71. * @param globalCommandParserProvider Provider of global command parsers for triggering actions.
  72. * @param substitutorFactory Factory to use to create action substitutors.
  73. * @param group The group the action belongs to
  74. * @param name The name of the action
  75. */
  76. public ActionModel(
  77. final Provider<GlobalCommandParser> globalCommandParserProvider,
  78. final ActionSubstitutorFactory substitutorFactory,
  79. final String group,
  80. final String name) {
  81. this.globalCommandParserProvider = globalCommandParserProvider;
  82. this.substitutorFactory = substitutorFactory;
  83. this.group = group;
  84. this.name = name;
  85. }
  86. /**
  87. * Creates a new instance of ActionModel with the specified properties.
  88. *
  89. * @param globalCommandParserProvider Provider of global command parsers for triggering actions.
  90. * @param substitutorFactory Factory to use to create action substitutors.
  91. * @param group The group the action belongs to
  92. * @param name The name of the action
  93. * @param triggers The triggers to use
  94. * @param response The response to use
  95. * @param conditions The conditions to use
  96. * @param conditionTree The condition tree to use
  97. * @param newFormat The new formatter to use
  98. */
  99. public ActionModel(
  100. final Provider<GlobalCommandParser> globalCommandParserProvider,
  101. final ActionSubstitutorFactory substitutorFactory,
  102. final String group, final String name,
  103. final ActionType[] triggers, final String[] response,
  104. final List<ActionCondition> conditions,
  105. final ConditionTree conditionTree, final String newFormat) {
  106. this(globalCommandParserProvider, substitutorFactory, group, name);
  107. this.triggers = triggers.clone();
  108. this.response = response.clone();
  109. this.conditions = conditions;
  110. this.conditionTree = conditionTree;
  111. this.newFormat = newFormat;
  112. this.modified = true;
  113. }
  114. /**
  115. * Triggers this action.
  116. *
  117. * @param serverManager The server manager used to iterate servers
  118. * @param format The format of the message that's going to be displayed.
  119. * @param arguments The arguments from the action that caused this trigger.
  120. * @return True if the execution of the event should be stopped, or false
  121. * if the event may continue
  122. */
  123. @Precondition({
  124. "This action has at least one trigger",
  125. "This action's primary trigger is non-null"
  126. })
  127. public boolean trigger(final ServerManager serverManager, final StringBuffer format,
  128. final Object... arguments) {
  129. assert triggers.length > 0;
  130. assert triggers[0] != null;
  131. if (status != ActionStatus.ACTIVE) {
  132. return false;
  133. }
  134. final ActionSubstitutor sub = substitutorFactory.getActionSubstitutor(triggers[0]);
  135. if (!test(sub, arguments)) {
  136. return false;
  137. }
  138. WritableFrameContainer container = null;
  139. CommandParser cp;
  140. if (arguments.length > 0
  141. && arguments[0] instanceof WritableFrameContainer) {
  142. container = (WritableFrameContainer) arguments[0];
  143. } else if (serverManager.numServers() > 0) {
  144. container = serverManager.getServers().get(0);
  145. }
  146. if (container == null) {
  147. cp = globalCommandParserProvider.get();
  148. } else {
  149. cp = container.getCommandParser();
  150. }
  151. for (String command : response) {
  152. cp.parseCommand(container, sub.doSubstitution(command, arguments));
  153. }
  154. if (newFormat != null && format != null) {
  155. format.setLength(0);
  156. format.append(newFormat);
  157. }
  158. return stop;
  159. }
  160. /**
  161. * Tests to see if this action should be triggered or not.
  162. *
  163. * @param sub The ActionsSubstitutor to use to substitute args
  164. * @param arguments The arguments for the action event
  165. * @return True if the action should be executed, false otherwise
  166. */
  167. public boolean test(final ActionSubstitutor sub,
  168. final Object ... arguments) {
  169. final boolean[] results = new boolean[conditions.size()];
  170. int i = 0;
  171. for (ActionCondition condition : conditions) {
  172. results[i++] = condition.test(sub, arguments);
  173. }
  174. return getRealConditionTree().evaluate(results);
  175. }
  176. /**
  177. * Retrieves a list of this action's conditions.
  178. *
  179. * @return A list of this action's conditions
  180. */
  181. public List<ActionCondition> getConditions() {
  182. return conditions;
  183. }
  184. /**
  185. * Sets this action's conditions.
  186. *
  187. * @param conditions A list of conditions to use
  188. */
  189. public void setConditions(final List<ActionCondition> conditions) {
  190. this.conditions = conditions;
  191. this.modified = true;
  192. }
  193. /**
  194. * Retrieves the status of this action.
  195. *
  196. * @since 0.6.5
  197. * @return This action's current status
  198. */
  199. public ActionStatus getStatus() {
  200. return status;
  201. }
  202. /**
  203. * Retrieves a plain text description of any error that occurred while
  204. * loading this action.
  205. *
  206. * @since 0.6.5
  207. * @return This action's error message, or null if no error was encountered
  208. */
  209. public String getError() {
  210. return error;
  211. }
  212. /**
  213. * Retrieves the type of the error that occurred while loading this action.
  214. *
  215. * @since 0.6.5
  216. * @return This action's error's type, or null if no error was encountered
  217. */
  218. public ActionErrorType getErrorType() {
  219. return errorType;
  220. }
  221. /**
  222. * Retrieves this action's triggers.
  223. *
  224. * @return The triggers used by this action
  225. */
  226. public ActionType[] getTriggers() {
  227. return triggers == null ? triggers : triggers.clone();
  228. }
  229. /**
  230. * Sets this action's triggers.
  231. *
  232. * @param triggers The new triggers to use
  233. */
  234. public void setTriggers(final ActionType[] triggers) {
  235. this.triggers = triggers.clone();
  236. this.modified = true;
  237. }
  238. /**
  239. * Retrieves this action's new format setting.
  240. *
  241. * @return The format that this action will use, or null if no change
  242. */
  243. public String getNewFormat() {
  244. return newFormat;
  245. }
  246. /**
  247. * Sets this action's new format setting.
  248. *
  249. * @param newFormat The new 'new format' setting
  250. */
  251. public void setNewFormat(final String newFormat) {
  252. this.newFormat = newFormat;
  253. this.modified = true;
  254. }
  255. /**
  256. * Retrieves this action's response.
  257. *
  258. * @return The commands that will be executed if this action is triggered
  259. */
  260. public String[] getResponse() {
  261. return response == null ? response : response.clone();
  262. }
  263. /**
  264. * Sets this action's response.
  265. *
  266. * @param response The new response to use
  267. */
  268. public void setResponse(final String[] response) {
  269. this.response = response.clone();
  270. this.modified = true;
  271. }
  272. /**
  273. * Retrieves this action's group name.
  274. *
  275. * @return This action's group name
  276. */
  277. public String getGroup() {
  278. return group;
  279. }
  280. /**
  281. * Sets the group of this action.
  282. *
  283. * @param newGroup The new group for this action
  284. */
  285. public void setGroup(final String newGroup) {
  286. this.group = newGroup;
  287. this.modified = true;
  288. }
  289. /**
  290. * Retrieves this action's name.
  291. *
  292. * @return This action's name
  293. */
  294. public String getName() {
  295. return name;
  296. }
  297. /**
  298. * Sets the name of this action.
  299. *
  300. * @param newName The new name for this action
  301. */
  302. public void setName(final String newName) {
  303. this.name = newName;
  304. this.modified = true;
  305. }
  306. /**
  307. * Retrieves the condition tree used for this action. Condition trees may
  308. * be null, in which case the arguments are conjoined together.
  309. *
  310. * @return This action's condition tree
  311. */
  312. public ConditionTree getConditionTree() {
  313. return conditionTree;
  314. }
  315. /**
  316. * Retrieves a concrete condition tree used for this action. If there is
  317. * no condition tree defined for this action, returns a conjunction tree
  318. * for the arguments.
  319. *
  320. * @since 0.6
  321. * @return A {@link ConditionTree} object for this action
  322. */
  323. public ConditionTree getRealConditionTree() {
  324. return conditionTree == null ? ConditionTree.createConjunction(
  325. conditions.size()) : conditionTree;
  326. }
  327. /**
  328. * Sets the condition tree used for this action.
  329. *
  330. * @param conditionTree The new condition tree to be used
  331. */
  332. public void setConditionTree(final ConditionTree conditionTree) {
  333. this.conditionTree = conditionTree;
  334. this.modified = true;
  335. }
  336. /**
  337. * Retrieves the concurrency group of this action.
  338. *
  339. * @return This action's concurrency group
  340. * @since 0.6.3
  341. */
  342. public String getConcurrencyGroup() {
  343. return concurrencyGroup;
  344. }
  345. /**
  346. * Sets the concurrency group of this action.
  347. *
  348. * @param concurrencyGroup This action's new concurrency group
  349. * @since 0.6.3
  350. */
  351. public void setConcurrencyGroup(final String concurrencyGroup) {
  352. this.concurrencyGroup = (concurrencyGroup == null
  353. || concurrencyGroup.isEmpty()) ? null : concurrencyGroup;
  354. }
  355. /**
  356. * Determines whether or not this action will stop the event execution
  357. * if it is triggered.
  358. *
  359. * @return The stopping preference of this action
  360. * @see #setStopping(boolean)
  361. * @since 0.6.4
  362. */
  363. public boolean isStopping() {
  364. return stop;
  365. }
  366. /**
  367. * Sets the stopping preference of this action. If the stopping preference
  368. * is <code>true</code> then when this action is successfully triggered,
  369. * it will request that execution of the event is stopped. This will
  370. * prevent the default behaviour of the callee being executed.
  371. *
  372. * @param stop The new stopping preference of this action
  373. * @see #isStopping()
  374. * @since 0.6.4
  375. */
  376. public void setStopping(final boolean stop) {
  377. this.stop = stop;
  378. }
  379. /**
  380. * Determine if this model has been modified since it was constructed or
  381. * its modified status was reset.
  382. *
  383. * @return True if this model has been modified, false otherwise
  384. */
  385. public boolean isModified() {
  386. return modified;
  387. }
  388. /**
  389. * Resets the modified status of this model. After a call to
  390. * resetModified(), this model will report that it has not been modified,
  391. * until one of the set* methods is used.
  392. */
  393. public void resetModified() {
  394. this.modified = false;
  395. }
  396. /** {@inheritDoc} */
  397. @Override
  398. public String toString() {
  399. return "[name=" + group + "/" + name + ", triggers="
  400. + Arrays.toString(triggers) + ", response="
  401. + Arrays.toString(response) + ", "
  402. + conditions + ", format='" + newFormat + "']";
  403. }
  404. }