Revise the download and match logic so more motors are downloaded correctly. The DownloadResponse now holds on to all the results supplied from Thrustcurve so we can process them a little better. The TCMissingMotorDownloadAction chooses the motor which has the best match (giving preference to matching digest, desgination and finally RockSim files). The TCSearchAction records into the database all the motors which have a unique designation.
This commit is contained in:
parent
f72a2fe02e
commit
60b1477c26
@ -1,28 +1,27 @@
|
|||||||
package net.sf.openrocket.android.thrustcurve;
|
package net.sf.openrocket.android.thrustcurve;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
|
||||||
public class DownloadResponse {
|
public class DownloadResponse {
|
||||||
|
|
||||||
private Map<Integer,MotorBurnFile> data = new HashMap<Integer,MotorBurnFile>();
|
private Map<Integer,List<MotorBurnFile>> data = new HashMap<Integer,List<MotorBurnFile>>();
|
||||||
|
|
||||||
private String error = null;
|
private String error = null;
|
||||||
|
|
||||||
public void add( MotorBurnFile mbd ) {
|
public void add( MotorBurnFile mbd ) {
|
||||||
MotorBurnFile currentData = data.get(mbd.getMotorId());
|
List<MotorBurnFile> currentData = data.get(mbd.getMotorId());
|
||||||
if ( currentData == null || currentData.getThrustCurveMotor() == null ) {
|
if ( currentData == null ) {
|
||||||
data.put(mbd.getMotorId(),mbd);
|
currentData = new ArrayList<MotorBurnFile>();
|
||||||
} else {
|
data.put(mbd.getMotorId(), currentData);
|
||||||
// Prefer RASP motors.
|
|
||||||
if ( "RockSim".equals(mbd.getFiletype()) && !"RockSim".equals(currentData.getFiletype()) ) {
|
|
||||||
data.put(mbd.getMotorId(), mbd);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
currentData.add(mbd);
|
||||||
}
|
}
|
||||||
|
|
||||||
public MotorBurnFile getData(Integer motor_id) {
|
public List<MotorBurnFile> getData(Integer motor_id) {
|
||||||
return data.get(motor_id);
|
return data.get(motor_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,8 +75,8 @@ public class DownloadResponseParser {
|
|||||||
AndroidLogWrapper.d(DownloadResponseParser.class,"base64: " + ex.getMessage());
|
AndroidLogWrapper.d(DownloadResponseParser.class,"base64: " + ex.getMessage());
|
||||||
}
|
}
|
||||||
currentMotor.decodeFile( s );
|
currentMotor.decodeFile( s );
|
||||||
|
ret.add((MotorBurnFile)currentMotor.clone());
|
||||||
}
|
}
|
||||||
ret.add((MotorBurnFile)currentMotor.clone());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
@ -81,11 +81,4 @@ public class MotorBurnFile {
|
|||||||
return thrustCurveMotor;
|
return thrustCurveMotor;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @param thrustCurveMotor the thrustCurveMotor to set
|
|
||||||
*/
|
|
||||||
public void setThrustCurveMotor(ThrustCurveMotor thrustCurveMotor) {
|
|
||||||
this.thrustCurveMotor = thrustCurveMotor;
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,10 +1,12 @@
|
|||||||
package net.sf.openrocket.android.thrustcurve;
|
package net.sf.openrocket.android.thrustcurve;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import net.sf.openrocket.android.util.AndroidLogWrapper;
|
import net.sf.openrocket.android.util.AndroidLogWrapper;
|
||||||
|
import net.sf.openrocket.motor.ThrustCurveMotor;
|
||||||
import net.sf.openrocket.motor.ThrustCurveMotorPlaceholder;
|
import net.sf.openrocket.motor.ThrustCurveMotorPlaceholder;
|
||||||
|
|
||||||
public class TCMissingMotorDownloadAction extends TCQueryAction {
|
public class TCMissingMotorDownloadAction extends TCQueryAction {
|
||||||
@ -48,7 +50,7 @@ public class TCMissingMotorDownloadAction extends TCQueryAction {
|
|||||||
|
|
||||||
handler.post( new UpdateMessage("Looking for " + motor.getManufacturer() + " " + motor.getDesignation()));
|
handler.post( new UpdateMessage("Looking for " + motor.getManufacturer() + " " + motor.getDesignation()));
|
||||||
|
|
||||||
SearchResponse res = new ThrustCurveAPI().doSearch(request);
|
SearchResponse res = ThrustCurveAPI.doSearch(request);
|
||||||
|
|
||||||
int total = res.getResults().size();
|
int total = res.getResults().size();
|
||||||
int count = 1;
|
int count = 1;
|
||||||
@ -72,9 +74,10 @@ public class TCMissingMotorDownloadAction extends TCQueryAction {
|
|||||||
|
|
||||||
AndroidLogWrapper.d(TCQueryAction.class, mi.toString());
|
AndroidLogWrapper.d(TCQueryAction.class, mi.toString());
|
||||||
|
|
||||||
MotorBurnFile b = new ThrustCurveAPI().downloadData(mi.getMotor_id());
|
List<MotorBurnFile> listOfMotors = ThrustCurveAPI.downloadData(mi.getMotor_id());
|
||||||
|
|
||||||
writeMotor( mi, b);
|
ThrustCurveMotor bestMatch = ThrustCurveAPI.findBestMatch(motor, listOfMotors);
|
||||||
|
writeMotor( mi, bestMatch);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import net.sf.openrocket.android.db.DbAdapter;
|
|||||||
import net.sf.openrocket.android.motor.ExtendedThrustCurveMotor;
|
import net.sf.openrocket.android.motor.ExtendedThrustCurveMotor;
|
||||||
import net.sf.openrocket.android.util.AndroidLogWrapper;
|
import net.sf.openrocket.android.util.AndroidLogWrapper;
|
||||||
import net.sf.openrocket.android.util.ProgressDialogFragment;
|
import net.sf.openrocket.android.util.ProgressDialogFragment;
|
||||||
|
import net.sf.openrocket.motor.ThrustCurveMotor;
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.os.AsyncTask;
|
import android.os.AsyncTask;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
@ -95,14 +96,14 @@ public abstract class TCQueryAction extends Fragment {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void writeMotor( TCMotor mi, MotorBurnFile b) throws Exception {
|
protected void writeMotor( TCMotor mi, ThrustCurveMotor thrustCurveMotor) throws Exception {
|
||||||
|
|
||||||
DbAdapter mDbHelper = new DbAdapter(getActivity());
|
DbAdapter mDbHelper = new DbAdapter(getActivity());
|
||||||
mDbHelper.open();
|
mDbHelper.open();
|
||||||
try {
|
try {
|
||||||
ExtendedThrustCurveMotor m = new ExtendedThrustCurveMotor();
|
ExtendedThrustCurveMotor m = new ExtendedThrustCurveMotor();
|
||||||
|
|
||||||
m.setThrustCurveMotor( b.getThrustCurveMotor() );
|
m.setThrustCurveMotor( thrustCurveMotor );
|
||||||
|
|
||||||
// Convert impulse class. ThrustCurve puts mmx, 1/4a and 1/2a as A.
|
// Convert impulse class. ThrustCurve puts mmx, 1/4a and 1/2a as A.
|
||||||
m.setImpulseClass(mi.getImpulse_class());
|
m.setImpulseClass(mi.getImpulse_class());
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
package net.sf.openrocket.android.thrustcurve;
|
package net.sf.openrocket.android.thrustcurve;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import net.sf.openrocket.android.util.AndroidLogWrapper;
|
import net.sf.openrocket.android.util.AndroidLogWrapper;
|
||||||
|
import net.sf.openrocket.motor.ThrustCurveMotor;
|
||||||
|
|
||||||
public class TCSearchAction extends TCQueryAction {
|
public class TCSearchAction extends TCQueryAction {
|
||||||
|
|
||||||
@ -22,7 +25,7 @@ public class TCSearchAction extends TCQueryAction {
|
|||||||
protected String doInBackground(Void... params) {
|
protected String doInBackground(Void... params) {
|
||||||
try {
|
try {
|
||||||
handler.post( new UpdateMessage("Quering Thrustcurve"));
|
handler.post( new UpdateMessage("Quering Thrustcurve"));
|
||||||
SearchResponse res = new ThrustCurveAPI().doSearch(searchRequest);
|
SearchResponse res = ThrustCurveAPI.doSearch(searchRequest);
|
||||||
|
|
||||||
int total = res.getResults().size();
|
int total = res.getResults().size();
|
||||||
int count = 1;
|
int count = 1;
|
||||||
@ -46,9 +49,13 @@ public class TCSearchAction extends TCQueryAction {
|
|||||||
|
|
||||||
AndroidLogWrapper.d(TCQueryAction.class, mi.toString());
|
AndroidLogWrapper.d(TCQueryAction.class, mi.toString());
|
||||||
|
|
||||||
MotorBurnFile b = new ThrustCurveAPI().downloadData(mi.getMotor_id());
|
List<MotorBurnFile> b = ThrustCurveAPI.downloadData(mi.getMotor_id());
|
||||||
|
List<ThrustCurveMotor> motors = ThrustCurveAPI.extractAllMotors(b);
|
||||||
writeMotor( mi, b);
|
if ( motors != null && motors.size() > 0 ) {
|
||||||
|
for( ThrustCurveMotor motor : motors ) {
|
||||||
|
writeMotor( mi, motor);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if ( total < res.getMatches() ) {
|
if ( total < res.getMatches() ) {
|
||||||
dismiss();
|
dismiss();
|
||||||
|
@ -6,15 +6,21 @@ import java.io.OutputStream;
|
|||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.net.URLConnection;
|
import java.net.URLConnection;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import net.sf.openrocket.android.util.AndroidLogWrapper;
|
import net.sf.openrocket.android.util.AndroidLogWrapper;
|
||||||
|
import net.sf.openrocket.motor.ThrustCurveMotor;
|
||||||
|
import net.sf.openrocket.motor.ThrustCurveMotorPlaceholder;
|
||||||
|
|
||||||
|
|
||||||
public class ThrustCurveAPI {
|
public abstract class ThrustCurveAPI {
|
||||||
|
|
||||||
private String url_base = "http://www.thrustcurve.org/servlets/";
|
private static String url_base = "http://www.thrustcurve.org/servlets/";
|
||||||
|
|
||||||
public SearchResponse doSearch( SearchRequest request ) throws MalformedURLException, IOException {
|
public static SearchResponse doSearch( SearchRequest request ) throws MalformedURLException, IOException {
|
||||||
|
|
||||||
String requestString = request.toString();
|
String requestString = request.toString();
|
||||||
|
|
||||||
@ -41,7 +47,7 @@ public class ThrustCurveAPI {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public MotorBurnFile downloadData( Integer motor_id ) throws MalformedURLException, IOException {
|
public static List<MotorBurnFile> downloadData( Integer motor_id ) throws MalformedURLException, IOException {
|
||||||
|
|
||||||
if ( motor_id == null ) {
|
if ( motor_id == null ) {
|
||||||
return null;
|
return null;
|
||||||
@ -70,9 +76,73 @@ public class ThrustCurveAPI {
|
|||||||
DownloadResponse downloadResponse = DownloadResponseParser.parse(is);
|
DownloadResponse downloadResponse = DownloadResponseParser.parse(is);
|
||||||
AndroidLogWrapper.d(ThrustCurveAPI.class,downloadResponse.toString());
|
AndroidLogWrapper.d(ThrustCurveAPI.class,downloadResponse.toString());
|
||||||
|
|
||||||
MotorBurnFile mbf = downloadResponse.getData(motor_id);
|
return downloadResponse.getData(motor_id);
|
||||||
|
|
||||||
return mbf;
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* look through the listOfMotors to find the one which best matches the motor requested.
|
||||||
|
*
|
||||||
|
* The algorithm uses a score based method. Each entry in listOfMotors is assigned a score
|
||||||
|
* and the element with the highest score is returned. The score is computed as follows:
|
||||||
|
*
|
||||||
|
* 1) if the element matches the digest of the requested motor eactly, score += 1000
|
||||||
|
* 1) if the element matches the designation in the requested motor exactly, score = 100
|
||||||
|
* 2) if the element is a RockSim file score += 10
|
||||||
|
*
|
||||||
|
* @param motor
|
||||||
|
* @param listOfMotors
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static ThrustCurveMotor findBestMatch( ThrustCurveMotorPlaceholder motor, List<MotorBurnFile> listOfMotors ) {
|
||||||
|
|
||||||
|
ThrustCurveMotor bestMatch = null;
|
||||||
|
int bestScore = -1;
|
||||||
|
|
||||||
|
final String wantedDesignation = motor.getDesignation();
|
||||||
|
final String wantedDigest = motor.getDigest();
|
||||||
|
|
||||||
|
for ( MotorBurnFile entry : listOfMotors ) {
|
||||||
|
int entryScore = 0;
|
||||||
|
ThrustCurveMotor entryMotor = entry.getThrustCurveMotor();
|
||||||
|
|
||||||
|
if ("RockSim".equals(entry.getFiletype()) ) {
|
||||||
|
entryScore += 10;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( wantedDigest != null && wantedDigest.equals( entryMotor.getDigest() ) ) {
|
||||||
|
entryScore += 1000;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( wantedDesignation != null && wantedDesignation.equals(entryMotor.getDesignation())) {
|
||||||
|
entryScore += 100;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( entryScore > bestScore ) {
|
||||||
|
bestScore = entryScore;
|
||||||
|
bestMatch = entry.getThrustCurveMotor();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
return bestMatch;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Extract all unique motors from the list based on designation.
|
||||||
|
* @param listOfMotors
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static List<ThrustCurveMotor> extractAllMotors( List<MotorBurnFile> listOfMotors ) {
|
||||||
|
|
||||||
|
Map<String, ThrustCurveMotor> motorsByDesignation = new HashMap<String,ThrustCurveMotor>();
|
||||||
|
|
||||||
|
for( MotorBurnFile entry : listOfMotors ) {
|
||||||
|
ThrustCurveMotor motor = entry.getThrustCurveMotor();
|
||||||
|
motorsByDesignation.put( motor.getDesignation(), motor);
|
||||||
|
}
|
||||||
|
|
||||||
|
return new ArrayList<ThrustCurveMotor>(motorsByDesignation.values());
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user