Source code: org/mandarax/eca/example/PortfolioAgent.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.beans.*;
22 import java.lang.reflect.Method;
23 import java.util.*;
24 import org.mandarax.eca.*;
25 import org.mandarax.eca.mail.POPEventSource;
26 import org.mandarax.kernel.*;
27 import org.mandarax.kernel.meta.JFunction;
28 import org.mandarax.lib.math.*;
29 import org.mandarax.reference.*;
30 import org.mandarax.util.LogicFactorySupport;
31 import org.mandarax.xkb.XKBManager;
32 import org.mandarax.xkb.framework.XKBDriver_1_1;
33
34 /**
35 * An agent using a set of ECA rules.
36 * @author <A HREF="mailto:j.b.dietrich@massey.ac.nz">Jens Dietrich</A>
37 */
38 public class PortfolioAgent implements org.mandarax.eca.EventListener,ExampleLog{
39 public static final String XML_SOURCE = "portfolioagent.xml";
40 private org.mandarax.kernel.KnowledgeBase kb = null;
41 private EventSource eventSource = null;
42 private Portfolio portfolio = new Portfolio();
43 static Predicate doAction = new SimplePredicate("do Action",new Class[]{PortfolioAction.class,Investment.class});
44 private Predicate isCritical = null;
45 private Predicate isRisk = null;
46 private Function getPriceChange = null;
47 private Object target = null;
48 private PortfolioAction lastAction = null;
49 private Derivation lastDerivation = null;
50 private transient PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
51
52 /**
53 * Constructor.
54 */
55 public PortfolioAgent() {
56 super();
57 initialize(); }
58 /**
59 * Initialize the agent.
60 */
61 private void initialize() {
62 // initialize kb
63 initKB();
64
65 // start listening to events
66 eventSource = new POPEventSource(Settings.getPop(),Settings.getPopUser(),Settings.getPopPassword(),Settings.getMailProperties(),1000*30);
67 eventSource.add(this);
68 }
69
70 /*
71 * Add a property change listener.
72 * @param l a property change listener
73 */
74 public void addPropertyChangeListener(PropertyChangeListener l) {
75 propertyChangeSupport.addPropertyChangeListener(l);
76 }
77
78 /*
79 * Remove a property change listener.
80 * @param l a property change listener
81 */
82 public void removePropertyChangeListener(PropertyChangeListener l) {
83 propertyChangeSupport.removePropertyChangeListener(l);
84 }
85 /**
86 * Event listener method.
87 * @param e an event
88 */
89 public void consume(Event e) {
90
91 this.LOG_EXAMPLE.debug("Incoming event from " + e.getSource());
92
93 // ignore parameters, do all possible actions for any event !
94 LogicFactorySupport lfs = new LogicFactorySupport();
95 InferenceEngine ie = new ResolutionInferenceEngine();
96
97 for (Iterator iter = portfolio.getInvestments().iterator();iter.hasNext();) {
98 Investment investment = (Investment)iter.next();
99
100 Query query = lfs.query(
101 lfs.fact(
102 doAction,
103 "action",
104 investment
105 ),
106 "get appropriate actions"
107 );
108 try {
109 ResultSet result = ie.query(query,kb,ie.ONE,ie.TRY_NEXT);
110 result.next();
111 PortfolioAction action = (PortfolioAction)result.getResult(PortfolioAction.class,"action");
112 Derivation derivation = result.getProof();
113 LOG_EXAMPLE.debug("Action " + action + " for investment " + investment);
114 action.setTarget(target);
115 action.perform(new Object[]{investment,derivation});
116 setLastDerivation(derivation);
117 setLastAction(action);
118 }
119 catch (Exception x) {
120 LOG_EXAMPLE.error("Exception executing query for investment " + investment,x);
121 }
122 }
123
124 }
125
126 /**
127 * Initialize the knowledge base.
128 */
129 private void initKB() {
130 XKBManager xkbManager = new XKBManager();
131 xkbManager.setDriver(new XKBDriver_1_1());
132 try {
133 kb = xkbManager.importKnowledgeBase(new java.io.File(XML_SOURCE));
134 }
135 catch (Exception x) {
136 LOG_EXAMPLE.error("Cannot import kb from "+XML_SOURCE,x);
137 }
138
139 }
140
141 /**
142 * Returns the action target.
143 * @return Object
144 */
145 public Object getTarget() {
146 return target;
147 }
148
149 /**
150 * Sets the action target.
151 * @param target The target to set
152 */
153 public void setTarget(Object target) {
154 this.target = target;
155 }
156
157 /**
158 * Returns the lastAction.
159 * @return PortfolioAction
160 */
161 public PortfolioAction getLastAction() {
162 return lastAction;
163 }
164
165 /**
166 * Returns the lastDerivation.
167 * @return Derivation
168 */
169 public Derivation getLastDerivation() {
170 return lastDerivation;
171 }
172
173 /**
174 * Sets the lastAction.
175 * @param lastAction The lastAction to set
176 */
177 public void setLastAction(PortfolioAction lastAction) {
178 PortfolioAction old = this.lastAction;
179 this.lastAction = lastAction;
180 this.propertyChangeSupport.firePropertyChange("lastAction",null,lastAction);
181 }
182
183 /**
184 * Sets the lastDerivation.
185 * @param lastDerivation The lastDerivation to set
186 */
187 public void setLastDerivation(Derivation lastDerivation) {
188 this.lastDerivation = lastDerivation;
189 }
190
191 /**
192 * Returns the kb.
193 * @return org.mandarax.kernel.KnowledgeBase
194 */
195 public org.mandarax.kernel.KnowledgeBase getKb() {
196 return kb;
197 }
198
199 /**
200 * Returns the eventSource.
201 * @return EventSource
202 */
203 public EventSource getEventSource() {
204 return eventSource;
205 }
206
207 /**
208 * Returns the portfolio.
209 * @return the Portfolio
210 */
211 public Portfolio getPortfolio() {
212 return portfolio;
213 }
214
215 }