Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/mandarax/eca/example/GenerateKB.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.lang.reflect.Method;
22  
23  import org.mandarax.kernel.*;
24  import org.mandarax.kernel.meta.JFunction;
25  import org.mandarax.lib.math.DoubleArithmetic;
26  import org.mandarax.lib.math.IntArithmetic;
27  import org.mandarax.reference.AbstractKnowledgeBase;
28  import org.mandarax.reference.AdvancedKnowledgeBase;
29  import org.mandarax.util.LogicFactorySupport;
30  import org.mandarax.xkb.XKBManager;
31  import org.mandarax.xkb.framework.XKBDriver_1_1;
32  
33  /**
34   * Generates the kb (as xml file) from a script.
35   * @author <A HREF="mailto:j.b.dietrich@massey.ac.nz">Jens Dietrich</A>
36   */
37  public class GenerateKB {
38    
39    /**
40     * Generate the kb.
41     */
42    public static void main(String[] args) throws Exception {
43      KnowledgeBase kb = createKB();
44      XKBManager xkbManager = new XKBManager();
45      xkbManager.setDriver(new XKBDriver_1_1());
46      xkbManager.exportKnowledgeBase(new java.io.File(PortfolioAgent.XML_SOURCE),kb);
47    }
48      
49    /**
50     * Utility method to create a JFunction.
51     * @param name a function name.
52     * @param clazz the declaring class
53     * @return a JFunction
54     */
55    private static JFunction createFunction(String name,Class clazz) {
56      Class[] param = {};
57      return createFunction(name,clazz,param);
58    }
59  
60    /**
61     * Utility method to create a JFunction.
62     * @param name a function name.
63     * @param clazz the declaring class
64     * @param clazz1 a parameter class
65     * @return a JFunction
66     */
67    private static JFunction createFunction(String name,Class clazz,Class clazz1) {
68      Class[] param = {clazz1};
69      return createFunction(name,clazz,param);
70    }  
71  
72    /**
73     * Utility method to create a JFunction.
74     * @param name a function name.
75     * @param clazz the declaring class
76     * @param clazzes parameter classes
77     * @return a JFunction
78     */
79    private static JFunction createFunction(String name,Class clazz,Class[] clazzes) {
80      try {
81        Method m = clazz.getMethod(name,clazzes);
82        return new JFunction(m);
83      }
84      catch (Exception x) {
85        ExampleLog.LOG_EXAMPLE.error("Cannot create JFunction",x);
86      }
87      return null;
88    }
89  
90    /**
91     * Create a knowledge base.
92     * @return a knowledge base
93     */
94    private static KnowledgeBase createKB() {
95      
96      KnowledgeBase kb = new AdvancedKnowledgeBase();
97      
98      // initialize predicates
99      Predicate doAction = PortfolioAgent.doAction;    
100     Predicate isCritical = new SimplePredicate("is critical",new Class[]{Investment.class});
101     Predicate isRisk = new SimplePredicate("is risk",new Class[]{Investment.class});
102     
103     // initialize functions 
104     Function getName = createFunction("getName",Investment.class);
105     Function getNumber = createFunction("getNumber",Investment.class);
106     Function getDateOfPurchase = createFunction("getDateOfPurchase",Investment.class);
107     Function getAgeInMonth = createFunction("getAgeInMonth",Investment.class);
108     Function getAgeInYears = createFunction("getAgeInYears",Investment.class);
109     Function getRecentAbsChange = createFunction("getRecentAbsChange",Investment.class);
110     Function getRecentRelChange = createFunction("getRecentRelChange",Investment.class);
111     Function getSymbol = createFunction("getSymbol",Investment.class);
112     Function getValue = createFunction("getValue",Investment.class);
113     Function getRelativeValue = createFunction("getRelativeValue",Portfolio.class,Investment.class);
114     
115     // initialize actions
116     PortfolioAction actAlert = new PortfolioAction(PortfolioAction.ALERT);
117     PortfolioAction actAlertUrgent = new PortfolioAction(PortfolioAction.ALERT_URGENT);
118     PortfolioAction actBuy = new PortfolioAction(PortfolioAction.BUY);
119     PortfolioAction actSell = new PortfolioAction(PortfolioAction.SELL);
120     PortfolioAction actLog = new PortfolioAction(PortfolioAction.LOG);
121     PortfolioAction actDoNothing = new PortfolioAction(PortfolioAction.NOTHING);
122     
123     // initialize knowledge base
124     LogicFactorySupport lfs = new LogicFactorySupport();
125     
126   
127     // sell critical investments if kept for more then 1 year (for tax reasons)
128     Rule r1 = lfs.rule(
129       lfs.fact(
130         isCritical,
131         "an investment"
132       ),
133       lfs.fact(
134         IntArithmetic.LESS_THAN,
135         new Integer(1),
136         lfs.cplx(
137           getAgeInYears,
138           "an investment"
139         )
140       ),
141       lfs.fact(
142         doAction,
143         actSell,
144         "an investment"
145       )    
146     );
147     kb.add(r1);
148     
149     // for other critical investments send urgent alert
150     Rule r2 = lfs.rule(
151       lfs.fact(
152         isCritical,
153         "an investment"
154       ),
155       lfs.fact(
156         doAction,
157         actAlertUrgent,
158         "an investment"
159       )    
160     );    
161     kb.add(r2);
162     
163     // send alert if investment is at risk    
164     Rule r3 = lfs.rule(
165       lfs.fact(
166         isRisk,
167         "an investment"
168       ),
169       lfs.fact(
170         doAction,
171         actAlert,
172         "an investment"
173       )    
174     );
175     kb.add(r3);
176     
177     
178     // if value drops more than 5% investment is critical
179     Rule r4 = lfs.rule(
180       lfs.fact(
181         DoubleArithmetic.LESS_THAN,
182         lfs.cplx(
183           getRecentRelChange,
184           "an investment"
185         ),
186         new Double("-5")
187       ),
188       lfs.fact(
189         isCritical,
190         "an investment"
191       )    
192     );    
193     kb.add(r4);    
194 
195     // if value drops more than 3% AND investment represents more than 10% of portfolio then investment is critical
196     Rule r5 = lfs.rule(
197       lfs.fact(
198         DoubleArithmetic.LESS_THAN,
199         lfs.cplx(
200           getRecentRelChange,
201           "an investment"
202         ),
203         new Double("-3")
204       ),
205       lfs.fact(
206         DoubleArithmetic.GREATER_THAN,
207         lfs.cplx(
208           getRelativeValue,
209           new Portfolio(),
210           "an investment"
211         ),
212         new Double(10)        
213       ),
214       lfs.fact(
215         isCritical,
216         "an investment"
217       )    
218     );    
219     kb.add(r5);      
220 
221     // if value drops more than 3% investment is at risk
222     Rule r6 = lfs.rule(
223       lfs.fact(
224         DoubleArithmetic.LESS_THAN,
225         lfs.cplx(
226           getRecentRelChange,
227           "an investment"
228         ),
229         new Double("-3")
230       ),
231       lfs.fact(
232         isRisk,
233         "an investment"
234       )    
235     );    
236     kb.add(r6);  
237             
238     
239     // default action
240     Fact f = lfs.fact(
241       doAction,
242       actLog,
243       "an investment"
244     );
245     kb.add(f);  
246     
247     return kb;
248   }
249 
250 
251 
252 }