1 package org.drools.jsr94.benchmark;
2
3 /*
4 $Id: BenchmarkTestBase.java,v 1.2 2003/03/27 20:42:02 tdiesler Exp $
5
6 Copyright 2002 (C) The Werken Company. All Rights Reserved.
7
8 Redistribution and use of this software and associated documentation
9 ("Software"), with or without modification, are permitted provided
10 that the following conditions are met:
11
12 1. Redistributions of source code must retain copyright
13 statements and notices. Redistributions must also contain a
14 copy of this document.
15
16 2. Redistributions in binary form must reproduce the
17 above copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
20
21 3. The name "drools" must not be used to endorse or promote
22 products derived from this Software without prior written
23 permission of The Werken Company. For written permission,
24 please contact bob@werken.com.
25
26 4. Products derived from this Software may not be called "drools"
27 nor may "drools" appear in their names without prior written
28 permission of The Werken Company. "drools" is a registered
29 trademark of The Werken Company.
30
31 5. Due credit should be given to The Werken Company.
32 (http://drools.werken.com/).
33
34 THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS
35 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
36 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
37 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
38 THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
39 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
45 OF THE POSSIBILITY OF SUCH DAMAGE.
46
47 */
48
49 import junit.framework.TestCase;
50
51 import javax.rules.RuleServiceProvider;
52 import javax.rules.StatelessRuleSession;
53 import javax.rules.admin.RuleAdministrator;
54 import java.io.*;
55 import java.util.*;
56
57 /***
58 * The base class for JSR94 benchmark tests.
59 *
60 * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler</a>
61 */
62 public abstract class BenchmarkTestBase extends TestCase
63 {
64
65 protected RuleServiceProvider ruleServiceProvider;
66 protected RuleAdministrator ruleAdministrator;
67 protected StatelessRuleSession statelessRuleSession;
68
69 private long start, end;
70
71 /*** setup the timer. */
72 protected void setUp() throws Exception
73 {
74 super.setUp();
75 start = System.currentTimeMillis();
76 }
77
78 /*** stop the timer. */
79 protected void tearDown() throws Exception
80 {
81 super.tearDown();
82 end = System.currentTimeMillis();
83 System.out.println( "Elapsed time: " + ( end - start ) + "ms" );
84 }
85
86 /***
87 * Convert the facts from the <code>InputStream</code> to a list of objects.
88 */
89 protected List getInputObjects( String filename ) throws IOException
90 {
91 List list = new ArrayList();
92
93 InputStream inputStream = new FileInputStream( filename );
94 BufferedReader br = new BufferedReader( new InputStreamReader( inputStream ) );
95
96 Map guests = new HashMap();
97
98 String line = null;
99 while (( line = br.readLine() ) != null)
100 {
101 if (line.trim().length() == 0 || line.trim().startsWith( ";" )) continue;
102 StringTokenizer st = new StringTokenizer( line, "() " );
103 String type = st.nextToken();
104
105 if ("guest".equals( type ))
106 {
107 if (!"name".equals( st.nextToken() )) throw new IOException( "expected 'name' in: " + line );
108 String name = st.nextToken();
109 if (!"sex".equals( st.nextToken() )) throw new IOException( "expected 'sex' in: " + line );
110 String sex = st.nextToken();
111 if (!"hobby".equals( st.nextToken() )) throw new IOException( "expected 'hobby' in: " + line );
112 String hobby = st.nextToken();
113
114 Guest guest = (Guest) guests.get( name );
115 if (guest == null)
116 {
117 guest = new Guest( name, sex.charAt( 0 ) );
118 guests.put( name, guest );
119 list.add( guest );
120 }
121 guest.addHobby( hobby );
122 }
123
124 if ("last_seat".equals( type ))
125 {
126 if (!"seat".equals( st.nextToken() )) throw new IOException( "expected 'seat' in: " + line );
127 list.add( new LastSeat( new Integer( st.nextToken() ).intValue() ) );
128 }
129
130 if ("context".equals( type ))
131 {
132 if (!"state".equals( st.nextToken() )) throw new IOException( "expected 'state' in: " + line );
133 list.add( new Context( st.nextToken() ) );
134 }
135 }
136 inputStream.close();
137
138 return list;
139 }
140
141 /*** Verify that each guest has at least one common hobby with the one before him/her. */
142 protected int validateResults( List inList, List outList )
143 {
144
145 int seatCount = 0;
146 Guest lastGuest = null;
147 Iterator it = outList.iterator();
148 while (it.hasNext())
149 {
150 Object obj = it.next();
151 if (!( obj instanceof Seat )) continue;
152
153 Seat seat = (Seat) obj;
154 if (lastGuest == null)
155 lastGuest = guest4Seat( inList, seat );
156
157 Guest guest = guest4Seat( inList, seat );
158
159 boolean hobbyFound = false;
160 for (int i = 0; !hobbyFound && i < lastGuest.getHobbies().size(); i++)
161 {
162 String hobby = (String) lastGuest.getHobbies().get( i );
163 if (guest.getHobbies().contains( hobby ))
164 {
165 hobbyFound = true;
166 }
167 }
168
169 if (!hobbyFound) fail( "seat: " + seat.getSeat() + " no common hobby " + lastGuest + " -> " + guest );
170 seatCount++;
171 }
172
173 return seatCount;
174 }
175
176 /*** Gets the Guest object from the inList base on the guest name of the seat. */
177 private Guest guest4Seat( List inList, Seat seat )
178 {
179
180 Iterator it = inList.iterator();
181 while (it.hasNext())
182 {
183 Object obj = it.next();
184 if (!( obj instanceof Guest )) continue;
185 Guest guest = (Guest) obj;
186 if (guest.getName().equals( seat.getName() ))
187 return guest;
188 }
189
190 return null;
191 }
192 }
This page was automatically generated by Maven