root/src/java/src/com/omniti/reconnoiter/esper/DeriveView.java

Revision e8b241b833cdda315cc463b322a89ba888c5e781, 7.2 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 5 years ago)

juggle files around for cleaner builds.

  • Property mode set to 100644
Line 
1 /****************************************************************************
2  * Copyright (C) 2008 EsperTech, Inc. All rights reserved.                  *
3  * Copyright (C) 2009 OmniTI, Inc. All rights reserved.                     *
4  * http://esper.codehaus.org                                                *
5  * http://www.espertech.com                                                 *
6  * ------------------------------------------------------------------------ *
7  * The software in this package is published under the terms of the         *
8  * GPL license a copy of which has been included with this distribution     *
9  * in the license.txt file.                                                 *
10  ****************************************************************************/
11
12 package com.omniti.reconnoiter.esper;
13
14 import com.espertech.esper.collection.SingleEventIterator;
15 import com.espertech.esper.core.service.StatementContext;
16 import com.espertech.esper.epl.expression.ExprNode;
17 import com.espertech.esper.client.EventBean;
18 import com.espertech.esper.client.EventType;
19 import com.espertech.esper.view.CloneableView;
20 import com.espertech.esper.view.DataWindowView;
21 import com.espertech.esper.view.View;
22 import com.espertech.esper.view.ViewSupport;
23 import com.espertech.esper.core.context.util.AgentInstanceContext;
24 import com.espertech.esper.view.stat.StatViewAdditionalProps;
25
26 import java.util.Iterator;
27 import java.util.Arrays;
28 import java.lang.ArithmeticException;
29
30 /**
31  * This view is a moving window extending the specified number of elements into the past.
32  */
33 public class DeriveView extends ViewSupport implements DataWindowView, CloneableView
34 {
35     private StatementContext statementContext;
36     protected AgentInstanceContext agentInstanceContext;
37     protected ExprNode expressionX;
38     protected ExprNode expressionY;
39     protected final EventType eventType;
40     private boolean isDouble;
41     private WeightedValueBean lastWVBean;
42     private NoitDerivePoint lastpoint;
43     protected final StatViewAdditionalProps additionalProps;
44
45     /**
46      * Constructor creates a moving window extending the specified number of elements into the past.
47      * @param size is the specified number of elements into the past
48      * @param viewUpdatedCollection is a collection that the view must update when receiving events
49      * @param deriveViewFactory for copying this view in a group-by
50      */
51     public DeriveView(AgentInstanceContext agentInstanceContext, ExprNode expressionX, ExprNode expressionY, EventType eventType, StatViewAdditionalProps additionalProps)
52     {
53         this.agentInstanceContext = agentInstanceContext;
54         this.statementContext = agentInstanceContext.getStatementContext();
55         this.expressionX = expressionX;
56         this.expressionY = expressionY;
57         this.eventType = eventType;
58         this.additionalProps = additionalProps;
59         isDouble = (expressionY.getExprEvaluator().getType() != double.class || expressionY.getExprEvaluator().getType() != Double.class);
60     }
61
62     public View cloneView()
63     {
64         return new DeriveView(this.agentInstanceContext, this.expressionX, this.expressionY, this.eventType, this.additionalProps);
65     }
66
67     public boolean isEmpty()
68     {
69         return (lastWVBean == null);
70     }
71
72     public static EventType getEventType(com.espertech.esper.event.EventAdapterService eas)
73     {
74         return eas.addBeanType(WeightedValueBean.class.getName(), WeightedValueBean.class, true, true, true);
75     }
76
77     public EventType getEventType()
78     {
79         return getEventType(statementContext.getEventAdapterService());
80     }
81
82     public final void update(EventBean[] newData, EventBean[] oldData)
83     {
84         if(oldData != null)
85             throw new UnsupportedOperationException("Time window does not accept event removal");
86
87         int derivedsize = 0;
88         EventBean[] derivedNew = null;
89         if (newData != null) {
90             EventBean[] derivedWorking = null;
91             int i = 0;
92             derivedsize = newData.length;
93             if (lastpoint == null) derivedsize -= 1;
94             if(derivedsize > 0)
95                 derivedWorking = new EventBean[derivedsize];
96             for ( EventBean pointb : newData ) {
97                 EventBean eventsPerStream[] = { pointb };
98                 Number NX = (Number) expressionX.getExprEvaluator().evaluate(eventsPerStream, true, agentInstanceContext);
99                 if(NX == null) continue;
100                 Number NY = (Number) expressionY.getExprEvaluator().evaluate(eventsPerStream, true, agentInstanceContext);
101                 if(NY == null) continue;
102                 NoitDerivePoint point = new NoitDerivePoint();
103                 point.X = NX.longValue();
104                 if(isDouble)
105                   point.Ydouble = NY.doubleValue();
106                 else
107                   point.Ylong = NY.longValue();
108
109                 if (lastpoint != null) {
110                     try {
111                         NoitDerivePoint sub = subtract(point,lastpoint);
112                         long weight = sub.X;
113                         double value = sub.ror(isDouble);
114                         // Bad things(TM) happen in Esper when value is NaN/Inf
115                         // and things with weight 0 have no place here.
116                         if(weight == 0)
117                           throw new ArithmeticException("future / 0");
118                         if(Double.isNaN(value))
119                           throw new ArithmeticException("Esper Octopus: NaN");
120                         if(Double.isInfinite(value))
121                           throw new ArithmeticException("Esper Octopus: Inf");
122                         lastWVBean = new WeightedValueBean(sub.X, sub.ror(isDouble));
123                         EventBean eb = statementContext.getEventAdapterService().adapterForBean(lastWVBean);
124                         derivedWorking[i++] = eb;
125                     }
126                     catch(Exception e) {
127                         derivedsize--;
128                     }
129                 }
130                 lastpoint = point;
131             }
132
133             if(i > 0) {
134                 derivedNew = new EventBean[i];
135                 for(int j = 0; j<i; j++)
136                     derivedNew[j] = derivedWorking[j];
137             }
138         }
139
140         // If there are child views, call update method
141         if (this.hasViews() && derivedNew != null)
142         {
143             updateChildren(derivedNew, null);
144         }
145     }
146
147     protected NoitDerivePoint subtract(NoitDerivePoint a, NoitDerivePoint b)
148     {
149         NoitDerivePoint v = new NoitDerivePoint();
150         v.X = a.X - b.X;
151         v.Ylong = a.Ylong - b.Ylong;
152         v.Ydouble = a.Ydouble - b.Ydouble;
153         return v;
154     }
155
156     public final Iterator<EventBean> iterator()
157     {
158         return new SingleEventIterator(statementContext.getEventAdapterService().adapterForBean(lastWVBean));
159     }
160
161     public final String toString()
162     {
163         return this.getClass().getName() + " lastpoint=" + lastpoint.X + "," + (isDouble ? lastpoint.Ydouble : lastpoint.Ylong);
164     }
165
166     protected class NoitDerivePoint {
167         public long X;
168         public long Ylong;
169         public double Ydouble;
170         public double ror(boolean isDouble) {
171             if(X == 0) return Double.NaN;
172             if(isDouble) return (Ydouble / (double)X);
173             else return ((double)Ylong / (double)X);
174         }
175     }
176 }
Note: See TracBrowser for help on using the browser.