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

Revision c75a2104258a9079bb6ef2dbe68c66e7102bbf16, 5.5 kB (checked in by Theo Schlossnagle <jesus@omniti.com>, 8 years ago)

step 2. .noit:derive and .noit:counter, refs #210

  • 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 GPL license       *
8  * a copy of which has been included with this distribution in the license.txt file.  *
9  **************************************************************************************/
10 package com.omniti.reconnoiter.esper;
11
12 import com.espertech.esper.collection.SingleEventIterator;
13 import com.espertech.esper.core.StatementContext;
14 import com.espertech.esper.epl.expression.ExprNode;
15 import com.espertech.esper.client.EventBean;
16 import com.espertech.esper.client.EventType;
17 import com.espertech.esper.view.CloneableView;
18 import com.espertech.esper.view.DataWindowView;
19 import com.espertech.esper.view.View;
20 import com.espertech.esper.view.ViewSupport;
21
22 import java.util.Iterator;
23 import java.util.Arrays;
24 import java.math.BigDecimal;
25 import java.math.MathContext;
26
27 /**
28  * This view is a moving window extending the specified number of elements into the past.
29  */
30 public class DeriveView extends ViewSupport implements DataWindowView, CloneableView
31 {
32     private StatementContext statementContext;
33     protected ExprNode expressionX;
34     protected ExprNode expressionY;
35     private boolean  isDouble;
36     private WeightedValueBean lastWVBean;
37     private BigDecimal[] lastpoint;
38
39     /**
40      * Constructor creates a moving window extending the specified number of elements into the past.
41      * @param size is the specified number of elements into the past
42      * @param viewUpdatedCollection is a collection that the view must update when receiving events
43      * @param deriveViewFactory for copying this view in a group-by
44      */
45     public DeriveView(StatementContext statementContext, ExprNode expressionX, ExprNode expressionY)
46     {
47         this.statementContext = statementContext;
48         this.expressionX = expressionX;
49         this.expressionY = expressionY;
50         isDouble = (expressionY.getType() != double.class || expressionY.getType() != Double.class);
51     }
52
53     public View cloneView(StatementContext statementContext)
54     {
55         return new DeriveView(statementContext, this.expressionX, this.expressionY);
56     }
57
58     public boolean isEmpty()
59     {
60         return (lastWVBean == null);
61     }
62
63     public static EventType getEventType(com.espertech.esper.event.EventAdapterService eas)
64     {
65         return eas.addBeanType(WeightedValueBean.class.getName(), WeightedValueBean.class, false);
66     }
67
68     public EventType getEventType()
69     {
70         return getEventType(statementContext.getEventAdapterService());
71     }
72
73     public final void update(EventBean[] newData, EventBean[] oldData)
74     {
75         if(oldData != null)
76             throw new UnsupportedOperationException("Time window does not accept event removal");
77
78         int derivedsize = 0;
79         EventBean[] derivedNew = null;
80         if (newData != null) {
81             int i = 0;
82             derivedsize = newData.length;
83             if (lastpoint == null) derivedsize -= 1;
84             if(derivedsize > 0)
85                 derivedNew = new EventBean[derivedsize];
86             for ( EventBean pointb : newData ) {
87                 EventBean eventsPerStream[] = { pointb };
88                 BigDecimal X = new BigDecimal(((Number) expressionX.evaluate(eventsPerStream, true, statementContext)).toString());
89                 BigDecimal Y;
90                 if(isDouble)
91                   Y = new BigDecimal(((Number) expressionY.evaluate(eventsPerStream, true, statementContext)).doubleValue());
92                 else
93                   Y = new BigDecimal(((Number) expressionY.evaluate(eventsPerStream, true, statementContext)).toString());
94
95                 BigDecimal[] point = new BigDecimal[] {X,Y};
96                 if (lastpoint != null) {
97                     try {
98                         BigDecimal[] sub = subtract(point,lastpoint);
99                         lastWVBean = new WeightedValueBean(sub[0], sub[1].divide(sub[0],MathContext.DECIMAL128));
100                         EventBean eb = statementContext.getEventAdapterService().adapterForBean(lastWVBean);
101                         derivedNew[i++] = eb;
102                     }
103                     catch(Exception e) {
104                         derivedsize--;
105                     }
106                 }
107                 lastpoint = point;
108             }
109         }
110
111         // If there are child views, call update method
112         if (this.hasViews() && derivedNew != null)
113         {
114             updateChildren(derivedNew, null);
115         }
116     }
117
118     protected BigDecimal[] subtract(BigDecimal[] a, BigDecimal[] b)
119     {
120         BigDecimal[] v = new BigDecimal[2];
121         v[0] = a[0].subtract(b[0]);
122         v[1] = a[1].subtract(b[1]);
123         return v;
124     }
125
126     public final Iterator<EventBean> iterator()
127     {
128         return new SingleEventIterator(statementContext.getEventAdapterService().adapterForBean(lastWVBean));
129     }
130
131     public final String toString()
132     {
133         return this.getClass().getName() + " lastpoint=" + lastpoint[0] + "," + lastpoint[1];
134     }
135 }
Note: See TracBrowser for help on using the browser.