Two Laptop Bag*
The moose likes Developer Certification (SCJD/OCMJD) and the fly likes Build a boolean expression from a String : CriteriaFind(String criteria) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Developer Certification (SCJD/OCMJD)
Bookmark "Build a boolean expression from a String : CriteriaFind(String criteria)" Watch "Build a boolean expression from a String : CriteriaFind(String criteria)" New topic
Author

Build a boolean expression from a String : CriteriaFind(String criteria)

raphael Bereh
Ranch Hand

Joined: Jan 15, 2002
Posts: 79
Hi all,
I urgently need your help.
In the FBN assignement, to load data from tha db.db table and make thinks easy enough to use, I have created a Class to encapsulate the data. (the class has the same fields as the table fields,and so I can easily load the database into an object of my class that I then add to a vector : doing so makes searching easy as it is just a mater of comparing user entered data to class fields.) But I am stuck with criteriafind : The user enters the criteria in a text boxe and I get the creteria fron there. Now using a StringTokenizer, I can parse the text to get the different fields and values. Now I want to use these fields and values to build a boolean expression eg if(myflight.Origin.equals("SFO") && myflight.Price < 1000 && ...)). I have no idea of how to do that easily (I don't want to have to embricated if statements to build a boolean expression for each possible search criteria). I have already tried to build the "string" in the expression. This works fine and I can get the expression as a String (ie , after parsing creteria, I will have the String :myflight.Origin.equals("SFO") && myflight.Price < 1000 && ...). How can I go from that string to a boolean that will execute in an "if" statement ?
Thanks for help.


SCJP,SCJD,SCBCD
Geir Morten Hagen
Ranch Hand

Joined: Apr 05, 2002
Posts: 34
Are you sure you want to create a class that encapsulate the data from the database? Remember that this make your design fixed to records of just one format.
I'm not done with my assignment yet, but I have implemented criteriaFind in the following way:
* Parse the key/values and put them in a SortedMap(for later searching)
* Copy the keys and values in the map to two string arrays (for comparing with values in DataItem)
* Loop through the records and search for matches in field names.
* When an equal field name is found, compare the value of value for this field/key with the one from the criteria.
* If all key/value pairs in a DataInfo object matches all key/value pairs in the criteria, return the DataInfo object.
Hope this helps
[ April 08, 2002: Message edited by: Geir Morten Hagen ]
Adam Caldwell
Greenhorn

Joined: Mar 27, 2002
Posts: 17
The requirements only specify that you need to do equality comparisons in the very specific format that they specify.
What this requirement really means is that they want to make sure that you know how to use string tokenizer.
Here's how I implemented it:
1) complexFind(ArrayList). Took an ArrayList of key value pairs (key = integer id of field, value=string to compare to). The implementation of this function was actually copied from find() and modified to look for more than one key. find() was rewritten to call complexFind.
2) criteriaFind(String) tokenized the string and created the ArrayList to pass to complexFind.
-Adam
raphael Bereh
Ranch Hand

Joined: Jan 15, 2002
Posts: 79
Well,
Good question about using an Object to encapsulate data. My reasons :
1- Java is an Object Oriented Language, and as such, any good Java programming should be manipulating data through Objects.
2- There is no problem about supporting a database schema extension or something else, the data object class can always be extended as in any OOP modelling.
3- Using an Object to manipulate data got from the database eases the manipulation and makes these manipulations clear for someone that has to modify the source code. Take a look at the following code for instance and note how easy the data in : origine list box, destination list box(these are parsed to get unique elements in the list), table header(columns):nothing is a "magical" constant or data, but evrything commes fron the database .
//import suncertify.Client.* ;
import java.lang.*;
import java.util.*;
import java.io.*;

public class fbnmodel extends Observable {
public Vector selection , origine, destination , tabledata , columnsize ;
public static Vector tableheader;
public String Status = null;
flightinfo myrec ;
public fbnmodel(){
origine = new Vector();
destination = new Vector();
selection = new Vector();
tabledata = new Vector();
tableheader = new Vector();
columnsize = new Vector();
try{
Data mydata = new Data("db.db");
FieldInfo [] myinfo = mydata.getFieldInfo();
/* get table header information */
for(int k=0; k<myinfo.length; k++){
tableheader.addElement(new String(myinfo[k].getName()));
//columnsize.addElement(new Integer (myinfo[k].getLength()));
}

for(int i=1; i<=mydata.getRecordCount(); i++){
/* get records from database and encapsulate them in a flightinfo object */
DataInfo mydatainfo = mydata.getRecord(i);
int recnbr = mydatainfo.getRecordNumber();
String[] test = mydatainfo.getValues();

selection.addElement(new flightinfo(recnbr, test));
}
} catch ( IOException ex) {
System.err.println(ex.getMessage());
} catch(DatabaseException dex ){
System.err.println(dex.getMessage());
}
for(int j=0; j< selection.size(); j++){
//boolean exists = false ;
myrec = (flightinfo) selection.elementAt(j);
int existso = origine.indexOf(myrec.Origin_airport);
int existsd = destination.indexOf(myrec.Destination_airport);
if(existso == -1){
origine.addElement(new String(myrec.Origin_airport));
}
if(existsd == -1){
destination.addElement(new String(myrec.Destination_airport));
}
}
origine.insertElementAt(new String(" "),0);
destination.insertElementAt(new String(" "),0);

/*for(int k=origine.size(); k>=0; k--){
String temp = (String)origine.elementAt(k);
for(int t=origine.size(); t>=0; t--){
if(temp.equals(origine.elementAt(++t))){
origine.removeElementAt(++t);
}
}
}*/
//setChanged();
//notifyObservers();
}
public Vector getSelection(){
return selection ;
}
public Vector getOrigine(){
return origine ;
}
public Vector getDestination(){
return destination ;
}
public static Vector getTableHeader(){
return tableheader;
}
public Vector getTableData(){
return tabledata ;
}
public Vector getColumnSize(){
return columnsize;
}
public void setStatus(String Message){
Status = Message ;
setChanged();
notifyObservers(new String(Status));
}
}
class flightinfo {
public int recordnumber ;
public String Flight_number ;
public String Origin_airport ;
public String Destination_airport ;
public String Carrier ;
public int Price ;
public String Day ;
public String time ;
public String Duration ;
public int Available_seats ;
public flightinfo(int recnum, String[] values){
this. recordnumber = recnum ;
this.Flight_number = values[0].trim();
this.Origin_airport = values[1].trim();
this.Destination_airport = values[2].trim();
this.Carrier = values[3].trim();
//String test = values[4];
try{
this.Price = Integer.parseInt(values[4].trim() );
} catch(NumberFormatException nex){
System.out.println("Error while parsing the price :" + nex.getMessage());
}
this.Day = values[5].trim();
this.time = values[6].trim();
this.Duration = values[7].trim();
try{
this.Available_seats = Integer.parseInt(values[8].trim());
} catch(NumberFormatException nex2){
System.out.println("Error while parsing number of seats :" + nex2.getMessage());
}
}
}

There are many other reasons and we should discuss about this I think ...
Now , I can see how to store the criteria in a Map but now how to implement the data searching : Remember I am using Object to load the data, so I will have to compare the "query" in the map to some fields of the record.
Need more help to get this clear.
Thanks
:roll:
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Build a boolean expression from a String : CriteriaFind(String criteria)
 
Similar Threads
NX: contractor. JTextField or JComboBox for search creteria?
criteriaFind Attack Plan, Comments Welcomed
criteriaFind parameter question
Searching records on the basis of creteria defined.
Constructing value objects using HQL or Creteria