Source code: org/mandarax/eca/example/PortfolioAction.java
1 /*
2 * Copyright (C) 1998-2002 <a href="mailto:mandarax@jbdietrich.com">Jens Dietrich</a>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18
19 package org.mandarax.eca.example;
20
21 import java.util.*;
22 import javax.mail.*;
23 import javax.mail.internet.*;
24 import org.mandarax.eca.*;
25 import org.mandarax.kernel.*;
26
27 /**
28 * An action that can be performed by a portfolio agent.
29 * @author <A HREF="mailto:j.b.dietrich@massey.ac.nz">Jens Dietrich</A>
30 */
31 public class PortfolioAction implements Action {
32
33 // action types
34 public static final int ALERT = 0;
35 public static final int ALERT_URGENT = 1;
36 public static final int SELL = 2;
37 public static final int BUY = 3;
38 public static final int LOG = 4;
39 public static final int NOTHING = 5;
40
41 private String message = null;
42 private Object target = null;
43 private int actionType = ALERT;
44
45 /**
46 * Constructor.
47 */
48 public PortfolioAction() {
49 super();
50 }
51
52 /**
53 * Constructor. Pass the action type.
54 * @param int the action type
55 */
56 public PortfolioAction(int actionType) {
57 super();
58 if (actionType<0 || actionType>5) throw new IllegalArgumentException();
59 this.actionType = actionType;
60 }
61 /**
62 * Returns the message.
63 * @return a String
64 */
65 public String getMessage() {
66 return message;
67 }
68
69 /**
70 * Sets the message.
71 * @param message The message to set
72 */
73 public void setMessage(String message) {
74 this.message = message;
75 }
76
77 /**
78 * Returns the target.
79 * @return Object
80 */
81 public Object getTarget() {
82 return target;
83 }
84
85 /**
86 * Sets the target.
87 * @param target The target to set
88 */
89 public void setTarget(Object target) {
90 this.target = target;
91 }
92
93 /**
94 * Perform an action.
95 * @param param the action parameters
96 */
97 public void perform(Object[] param) throws ActionException {
98 if (target instanceof ActionMonitor) {
99 ActionMonitor console = (ActionMonitor)target;
100 console.log(this,param);
101 }
102 else throw new ActionException("Target type should be " + ActionMonitor.class);
103
104 // type specific actions
105 if (actionType==ALERT) {
106 alert((Investment)param[0],(Derivation)param[1],false);
107 }
108 if (actionType==ALERT_URGENT) {
109 alert((Investment)param[0],(Derivation)param[1],true);
110 }
111 }
112 /**
113 * Convert the action to a string.
114 * @return a textual representation of the action
115 */
116 public String toString() {
117 if (actionType==ALERT) return "alert";
118 if (actionType==ALERT_URGENT) return "urgent alert";
119 if (actionType==BUY) return "buy";
120 if (actionType==SELL) return "sell";
121 if (actionType==LOG) return "log";
122 if (actionType==NOTHING) return "do nothing";
123 else return super.toString();
124 }
125
126
127 /**
128 * Send an email.
129 * @param subject the subject of the message
130 * @param text the body of the message
131 * @param highPriority whethet to send the message with high priority
132 */
133 private static void mail(String subject,String text,boolean highPriority) throws Exception {
134 ExampleLog.LOG_EXAMPLE.debug("Sending mail via " + Settings.getSmtp() + " to " + Settings.getTo());
135 Session session = Session.getInstance(Settings.getMailProperties(),null);
136 session.setDebug(true);
137 Message msg = new MimeMessage(session);
138 msg.setFrom(new InternetAddress(Settings.getFrom()));
139 msg.addRecipient(Message.RecipientType.TO,new InternetAddress(Settings.getTo()));
140 msg.setSubject(subject);
141 msg.setText(text);
142 if (highPriority) msg.addHeader("X-Priority", "1");
143 Transport.send(msg);
144
145 }
146 /**
147 * Send an alert.
148 * @param inv an investment
149 * @param deriv a derivation
150 * @param boolean urgent
151 */
152 private static void alert(Investment inv,Derivation der,boolean urgent){
153 String subject = "Alert by portfolio agent: " + inv.getSymbol();
154 StringBuffer body = new StringBuffer();
155 body.append("Investment: ");
156 body.append(inv);
157 body.append('\n');
158 body.append('\n');
159 body.append("This alert is based on the following rules:\n");
160 print(der.getRoot(),body);
161 try {
162 mail(subject,body.toString(),urgent);
163 }
164 catch (Exception x) {
165 ExampleLog.LOG_EXAMPLE.error("Exception sending mail",x);
166 }
167 }
168 /**
169 * Print a derivation root.
170 * @param node the node
171 * @param buf the buffer to print on
172 * @param indent the indent
173 */
174 private static void print(DerivationNode node,StringBuffer buf) {
175 Clause c = node.getAppliedClause();
176 if (c!=null) {
177 buf.append(c);
178 buf.append("\n");
179 }
180 for (Iterator iter = node.getSubNodes().iterator();iter.hasNext();) {
181 DerivationNode nextNode = (DerivationNode)iter.next();
182 if (nextNode.getState()!=DerivationNode.FAILED) {
183 print(nextNode,buf);
184 }
185 }
186 }
187
188 /**
189 * Returns the actionType.
190 * @return int
191 */
192 public int getActionType() {
193 return actionType;
194 }
195
196 /**
197 * Sets the actionType.
198 * @param actionType The actionType to set
199 */
200 public void setActionType(int actionType) {
201 this.actionType = actionType;
202 }
203
204 }