I am trying to expand on an example from the Esper documentation for the where enumeration method, and having issues. Here is the example in question:
select items.where(i => i.location.x = 0 and i.location.y = 0) as zeroloc
from LocationReport
What I would like to do seems pretty simple. Instead of selecting items that match this expression :
I want to select LocationReports that contain at least one item that matches the expression.
Do it over a time_batch window (emphasized textnon-batched time window is a possibility as well).
So every n number of seconds I would receive a collection of LocationReports in which each report contains at least one zero location in its items List.
For Reference, here is the structure of the Java objects used in the Esper example:
public class LocationReport { List items; ...
public class Item { String assetId; // passenger or luggage asset
id Location location; // (x,y) location boolean luggage; //
true if this item is a luggage piece String assetIdPassenger; // if
the item is luggage, contains passenger associated ...
public class Location { int x; int y; ...
Background detail : Assume LocationReport is the actual object I am interested in... Using EPL like in the example above, the where logic works, but the problem is that, in returning only the items member, I do not see the LocationReport class it came from, which contains other properties besides items that my UpdateListener needs.
Also, probably not relevant, but in my case, I am receiving a high rate of messages where many LocationReports are duplicates (or close enough to be considered duplicates), and my where clause will need to make that determination and only forward "new" messages.
Thanks!
You could add the "*" to the select and that gives you the event objects alongside. select *, items.where(...) from LocationReport
You could add "output every N seconds" to output. Add "#time(...)" for the time window.
Related
I am writing my first app in Android Studio, I am a self-taught novice. When I write data into the subscript of an array I have created as a user-defined class the value is written into an adjacent subscript as well! Have traced to some code where I move data down one position in the array, thought I could do this in one operation, but it seems this messes something up and I need to copy each member of the class individually.
Here is my class
class LeaderboardClass
{
public String DateTime;
public String UserName;
public long Milliseconds; //0 denotes not in use
}
Here is my array declaration
LeaderboardClass[] LeaderboardData = new LeaderboardClass[LeaderboardEntries];
I want to move some data from subscript j to subscript j+1
I tried
LeaderboardData[j + 1] = LeaderboardData[j];
I thought this would copy all the data from subscript j to j+1
Then when I subsequently write to the array I (subscript i) I get the correct entry I made, plus a duplicate entry in the adjacent subscript i+1.
When I rewrite the above to:
LeaderboardData[j + 1].UserName = LeaderboardData[j].UserName;
LeaderboardData[j + 1].DateTime = LeaderboardData[j].DateTime;
LeaderboardData[j + 1].Milliseconds = LeaderboardData[j].Milliseconds;
Everything else behaves as expected. So I was wondering exactly what is happening with my first (presumably incorrect) code?
Thanks.
In Java, there's a difference between primitive values and objects (instances of classes): Primitives are stored by value whereas objects are stored by reference. This means that your code would work as you expect if you were using integers. However, since you are using a class, the array merely stores the references to those objects. Hence, when you do LeaderboardData[j + 1] = LeaderboardData[j]; you are merely copying the reference of that object. Therefore, LeaderboardData[j + 1]and LeaderboardData[j] will point to the same object.
Sidenote: If you run your program with a debugger, you can actually see this in action:
The number behind the # denotes the reference number and if you look closely, you can see that the objects at indices 8 and 9 both have the reference #716.
To fix this, I would suggest that you use lists instead of arrays as they allow you to remove and add new entries. The standard list implementation is an ArrayList but in your use-case, a LinkedList might be more efficient.
Lastly, a closing notes on your code: For variable names (like DateTime, UserName or LeaderboardData should always start with a lowercase letter to distinguish them from classes. That way, you can avoid lots of confusion - especially because Java also has a built-in class called DateTime.
I'm a java Beginner and I've created a program where you can type in some food in a TableView and the details of the respective food you can type in a GripPane. One of the Details you have to type in is the quantity of the food, and another is the Calories per piece. Now I would like to create a button and a field. Or Maybe just a field that shows all calories of the food in the Table view. So it should multiplicate the quantity with the calories, for every food and add them all together. For a Total of Calories. Now I have no idea how to do that. Could somebody help me with step-by-step instructions? Not sure if it makes sense to add some code to the program. By the way, I use Eclipse on Windows and SceneBuilder. Thanks for every help.
Cheers Blarg
The first piece of advice from my side would be to try writing some code on your own! That way you learn and you wouldn't need to copy and paste somebody else's code.
And secondly, this is how I would approach it:
Create the fields as you described below in the Scene Builder and give them all id (names) so that we can access them in our controller (I am supposing you know how that works).
Add a button so that the user can click to perform the calculation
When the button is clicked, you can get all the information from each TextBox and create a Food Object with all the information. Performing the calculation is a rather simple task that can be done by converting the data received from the TextBoxes into numbers and multiplying
public void addFoodItemIntoTable()
{
...
String quantityOfFoodStr = quantityTextBox.getText();
int quantityOfFood = Integer.parseInt(quantityOfFoodStr);
String caloriesOfFoodStr = caloriesTextBox.getText();
double caloriesOfFood = Double.parseDouble(caloriesOfFoodStr);
double total = quantityOfFood * caloriesOfFood;
...
}
After adding all the elements in your TableView (Check this). You can easily get the total of the field by iterating all the elements of your table and adding them into a variable.
Example:
double total = 0;
for(Food currentFood : foodTable.getItems())
{
total = total + currentFood.getTotalCalculation(); // The naming should not be correct... Change it to whatever you find suitable
}
Good luck!
I'm parsing out a bunch of employee incident reports for reporting purposes.
The incident reports themselves are free text, and I have to categorize the injuries by body location. I'm trying to avoid if{}elseif{}elseif{}....}else{}.
Example incident reports:
Employee slipped on wet stairs and injured her knee and right arm, and struck her head on the handrail.
Should add "knee", "arm", and "head" to affected area.
Employee was lifting boxes without approved protective equipment resulting in a back strain.
Should add "back" to affected area.
While attempting to unjam copier, employee got right index finger caught in machinery resulting in a 1-inch cut.
Should add "finger" to affected area.
Right now, I have:
private static StaffInjuryData setAffectedAreas(String incident, StaffInjuryData sid){
incident = incident.toUpperCase(); //eliminate case issues
if(incident.contains("HEAD")){
sid.addAffectedArea("HEAD");
}else if(incident.contains("FACE")){
sid.addAffectedArea("FACE");
}else if(incident.contains("EYE")){
sid.addAffectedArea("EYE");
}else if(incident.contains("NOSE")){
sid.addAffectedArea("NOSE");
}
//etc, etc, etc
return sid;
}
Is there an easier/more efficient way then if-elseif-ad inifinitum to do this?
One approach is to construct a regular expression from the individual body parts, use it for searching the string, and add the individual matches to the list:
Pattern bodyParts = Pattern.compile("\\b(head|face|eye|nose)\\b", Pattern.CASE_INSENSITIVE);
Use of \b on both ends prevents partial matches, e.g. finding "head" in the text containing "forehead" or "eye" inside an "eyelid".
This Q&A explains how to search text using regex in Java.
Add a Set<String> as parameter where you provide all expected keyword :
private static StaffInjuryData setAffectedAreas(String incident, StaffInjuryData sid, Set<String> keywords){
incident = incident.toUpperCase(); //eliminate case issues
for (String keyword : keywords){
if(incident.contains(keyword)){
sid.addAffectedArea(keyword);
}
}
return sid;
}
Perhaps creating a list containing all parts {neck,shoulder,back,etc} and then checking if the entry contains any of those values?
you might be able to create some sort of container (like a list or set) with all of the different parts (IE Head, Face, Eye, Nose, Finger, etc), split the string using the .split() method, and then compare each part of that string to each item in your container.
This might be easier, but could possibly be less efficient
This is one of the sample document which I saved in my bucket,
{
"id": "639882607203778560",
"text": "How does Andy Reid describe the no WR touchdown stat?",
"name": "chiefs",
"createdAt": 1441394876000,
}
I need to fetch the documents for given name and the date range. So this is the view I created for it,
function (doc, meta) {
if (doc._class == "com.link.data" && doc.createdAt && doc.name) {
emit([doc.createdAt,doc.name], null);
}
}
This will give me the all documents for given date range but it doesn't filter based on name. I have all the documents with other names also. How can I achieve this? Also what is the correct implementation for java?
This is the my current impl and I want to do this without using N1ql.
query.setRange(ComplexKey.of(1441376400000L, name), ComplexKey.of(1441396800000L,name));
I tried to add range as a startKey and endKey.Then put the name as a key in couchbase UI and it doesn't work.
Disclaimer: I dont have a lot of experience with compound/complex keys in CB.
I believe what you're asking for cannot be done with a single view: Sorting on date and then filtering on a specific name. You can sort on range and then group-by name, but you'd still get all the various names in the bucket (as you've already noticed).
What you can do is use two separate views and then intersect the results: get the doc-ids with the name you want, get the docs in the range you want and find intersections in your java code. Since views are only 'eventually consistent', your results are just as good as with a single view request, so the only thing you're wasting here is bandwidth and a little time, but not result-precision.
"given" means exact match?
If you want to get result with exact matched name with date range, try emit() with:
emit([doc.name, doc.createdAt], null);
the key array itself, 1st arg in emit() function, is the sort order.
I have a table written in GWT from a List list. What i want it is to obtain the sum of a certain group of elements.
The problem with this, it is that the value I want to do the sum with its calculated, and thus not obtainable unless you calculate it before generating the List.
I was wondering if it was somehow possible to achieve this through DOM manipulation. And if so, how?.
I will show you an example:
DataA DataB DataC DataD
---------------------------------
aaaaa bbbbb ccccc 12
aaaa1 bbbb1 cccc1 15
aaa11 bbb11 ccc11 17
I want to get the sum of "DataD" column, but i dont know how can i do it.
Thank you in advance for your time,
Kind regards,
Elaborate: DataD column value it is calculated and the value comes from another system and its placed into the table through a third party program; thus i cannot get its value and use it into a sum to get the value i want.
You should be able to add some event handler to the table's widget so that whenever data is added/changed in the widget, you adjust the total. Please give more information about what widgets you are using to represent the table.
EDIT:
now that we know you are using an HTMLLayoutContainer, I assume DataD is being poopulated at a place beyond your controll in code. What you can do is add event handlers to handle the add remove etc events. For example, you could do something like below:
//Your container
HtmlLayoutContainer c = new HtmlLayoutContainer(templates.getTemplate());
c.addAddHandler(new AddEvent.AddHandler() {
#Override
public void onAdd(AddEvent event) {
//Do proper exception handling, check if it is the right widget for dataD, do the needed calculations etc
sum += Double.parseDouble(((HTML)event.getWidget()).getHTML());
}
});
c.addRemoveHandler(new RemoveEvent.RemoveHandler() {
#Override
public void onRemove(RemoveEvent event) {
//handle all conditions like in onAdd in AddHandler above
sum -= Double.parseDouble(((HTML)event.getWidget()).getHTML());
}
});
You can look into the API to see what exactl events match your needs best (http://dev.sencha.com/deploy/gxt-3.0.0/javadoc/gxt/com/sencha/gxt/widget/core/client/container/HtmlLayoutContainer.html)