]> git.gag.com Git - debian/openrocket/commitdiff
Revise the download and match logic so more motors are downloaded correctly. The...
authorkruland2607 <kruland2607@180e2498-e6e9-4542-8430-84ac67f01cd8>
Tue, 21 Feb 2012 02:55:33 +0000 (02:55 +0000)
committerkruland2607 <kruland2607@180e2498-e6e9-4542-8430-84ac67f01cd8>
Tue, 21 Feb 2012 02:55:33 +0000 (02:55 +0000)
git-svn-id: https://openrocket.svn.sourceforge.net/svnroot/openrocket/trunk@435 180e2498-e6e9-4542-8430-84ac67f01cd8

android/src/net/sf/openrocket/android/thrustcurve/DownloadResponse.java
android/src/net/sf/openrocket/android/thrustcurve/DownloadResponseParser.java
android/src/net/sf/openrocket/android/thrustcurve/MotorBurnFile.java
android/src/net/sf/openrocket/android/thrustcurve/TCMissingMotorDownloadAction.java
android/src/net/sf/openrocket/android/thrustcurve/TCQueryAction.java
android/src/net/sf/openrocket/android/thrustcurve/TCSearchAction.java
android/src/net/sf/openrocket/android/thrustcurve/ThrustCurveAPI.java

index 4532610442134f9855e9ad169775f0a36db1df79..99d09762373dee29be672654616f39877b9c00fa 100644 (file)
@@ -1,28 +1,27 @@
 package net.sf.openrocket.android.thrustcurve;\r
 \r
+import java.util.ArrayList;\r
 import java.util.HashMap;\r
+import java.util.List;\r
 import java.util.Map;\r
 \r
 \r
 public class DownloadResponse {\r
 \r
-       private Map<Integer,MotorBurnFile> data = new HashMap<Integer,MotorBurnFile>();\r
+       private Map<Integer,List<MotorBurnFile>> data = new HashMap<Integer,List<MotorBurnFile>>();\r
        \r
        private String error = null;\r
        \r
        public void add( MotorBurnFile mbd ) {\r
-               MotorBurnFile currentData = data.get(mbd.getMotorId());\r
-               if ( currentData == null || currentData.getThrustCurveMotor() == null ) {\r
-                       data.put(mbd.getMotorId(),mbd);\r
-               } else {\r
-                       // Prefer RASP motors.\r
-                       if ( "RockSim".equals(mbd.getFiletype()) && !"RockSim".equals(currentData.getFiletype()) ) {\r
-                               data.put(mbd.getMotorId(), mbd);\r
-                       }\r
+               List<MotorBurnFile> currentData = data.get(mbd.getMotorId());\r
+               if ( currentData == null ) {\r
+                       currentData = new ArrayList<MotorBurnFile>();\r
+                       data.put(mbd.getMotorId(), currentData);\r
                }\r
+               currentData.add(mbd);\r
        }\r
 \r
-       public MotorBurnFile getData(Integer motor_id) {\r
+       public List<MotorBurnFile> getData(Integer motor_id) {\r
                return data.get(motor_id);\r
        }\r
        \r
index a38a4e04287b4a628a83bf42b76af20a209e8449..abf9d99f87a6a1e6952b49e8da98e9fed5c1164f 100644 (file)
@@ -75,8 +75,8 @@ public class DownloadResponseParser {
                                                                AndroidLogWrapper.d(DownloadResponseParser.class,"base64: " + ex.getMessage());\r
                                                        }\r
                                                        currentMotor.decodeFile( s );\r
+                                                       ret.add((MotorBurnFile)currentMotor.clone());\r
                                                }\r
-                                               ret.add((MotorBurnFile)currentMotor.clone());\r
                                        }\r
                                }\r
                                );\r
index a4290768598e1cc047d6a203dcfefafd076fec3d..85fdb33125ba4dda01cea40eab45054a705df458 100644 (file)
@@ -81,11 +81,4 @@ public class MotorBurnFile {
                return thrustCurveMotor;\r
        }\r
 \r
-       /**\r
-        * @param thrustCurveMotor the thrustCurveMotor to set\r
-        */\r
-       public void setThrustCurveMotor(ThrustCurveMotor thrustCurveMotor) {\r
-               this.thrustCurveMotor = thrustCurveMotor;\r
-       }\r
-       \r
 }\r
index ce580e9589fe7256d1f925637495a49cb42fe89b..aafaf1bd5b326662682d10e35cb1fe5ee34ce624 100644 (file)
@@ -1,10 +1,12 @@
 package net.sf.openrocket.android.thrustcurve;\r
 \r
+import java.util.List;\r
 import java.util.Set;\r
 import java.util.regex.Matcher;\r
 import java.util.regex.Pattern;\r
 \r
 import net.sf.openrocket.android.util.AndroidLogWrapper;\r
+import net.sf.openrocket.motor.ThrustCurveMotor;\r
 import net.sf.openrocket.motor.ThrustCurveMotorPlaceholder;\r
 \r
 public class TCMissingMotorDownloadAction extends TCQueryAction {\r
@@ -48,7 +50,7 @@ public class TCMissingMotorDownloadAction extends TCQueryAction {
 \r
                                handler.post( new UpdateMessage("Looking for " + motor.getManufacturer() + " " + motor.getDesignation()));\r
 \r
-                               SearchResponse res = new ThrustCurveAPI().doSearch(request);\r
+                               SearchResponse res = ThrustCurveAPI.doSearch(request);\r
 \r
                                int total = res.getResults().size();\r
                                int count = 1;\r
@@ -72,9 +74,10 @@ public class TCMissingMotorDownloadAction extends TCQueryAction {
 \r
                                        AndroidLogWrapper.d(TCQueryAction.class, mi.toString());\r
 \r
-                                       MotorBurnFile b = new ThrustCurveAPI().downloadData(mi.getMotor_id());\r
+                                       List<MotorBurnFile> listOfMotors = ThrustCurveAPI.downloadData(mi.getMotor_id());\r
 \r
-                                       writeMotor( mi, b);\r
+                                       ThrustCurveMotor bestMatch = ThrustCurveAPI.findBestMatch(motor, listOfMotors);\r
+                                       writeMotor( mi, bestMatch);\r
 \r
                                }\r
                        }\r
index bccacd49513961235de418ae882880fd7487db84..7516f7f118f5efbf355c29dc4e76e2ed66ec193f 100644 (file)
@@ -4,6 +4,7 @@ import net.sf.openrocket.android.db.DbAdapter;
 import net.sf.openrocket.android.motor.ExtendedThrustCurveMotor;\r
 import net.sf.openrocket.android.util.AndroidLogWrapper;\r
 import net.sf.openrocket.android.util.ProgressDialogFragment;\r
+import net.sf.openrocket.motor.ThrustCurveMotor;\r
 import android.app.Activity;\r
 import android.os.AsyncTask;\r
 import android.os.Bundle;\r
@@ -95,14 +96,14 @@ public abstract class TCQueryAction extends Fragment {
                }\r
        }\r
 \r
-       protected void writeMotor( TCMotor mi, MotorBurnFile b) throws Exception {\r
+       protected void writeMotor( TCMotor mi, ThrustCurveMotor thrustCurveMotor) throws Exception {\r
 \r
                DbAdapter mDbHelper = new DbAdapter(getActivity());\r
                mDbHelper.open();\r
                try {\r
                        ExtendedThrustCurveMotor m = new ExtendedThrustCurveMotor();\r
 \r
-                       m.setThrustCurveMotor( b.getThrustCurveMotor() );\r
+                       m.setThrustCurveMotor( thrustCurveMotor );\r
 \r
                        // Convert impulse class.  ThrustCurve puts mmx, 1/4a and 1/2a as A.\r
                        m.setImpulseClass(mi.getImpulse_class());\r
index 1354e872fd83cbdfe11e55728d8f7ec1fa3c26d3..fc05f4d058cb3e0297855b480d186b26eb661425 100644 (file)
@@ -1,6 +1,9 @@
 package net.sf.openrocket.android.thrustcurve;\r
 \r
+import java.util.List;\r
+\r
 import net.sf.openrocket.android.util.AndroidLogWrapper;\r
+import net.sf.openrocket.motor.ThrustCurveMotor;\r
 \r
 public class TCSearchAction extends TCQueryAction {\r
 \r
@@ -22,7 +25,7 @@ public class TCSearchAction extends TCQueryAction {
                protected String doInBackground(Void... params) {\r
                        try {\r
                                handler.post( new UpdateMessage("Quering Thrustcurve"));\r
-                               SearchResponse res = new ThrustCurveAPI().doSearch(searchRequest);\r
+                               SearchResponse res = ThrustCurveAPI.doSearch(searchRequest);\r
 \r
                                int total = res.getResults().size();\r
                                int count = 1;\r
@@ -46,9 +49,13 @@ public class TCSearchAction extends TCQueryAction {
 \r
                                        AndroidLogWrapper.d(TCQueryAction.class, mi.toString());\r
 \r
-                                       MotorBurnFile b = new ThrustCurveAPI().downloadData(mi.getMotor_id());\r
-\r
-                                       writeMotor( mi, b);\r
+                                       List<MotorBurnFile> b = ThrustCurveAPI.downloadData(mi.getMotor_id());\r
+                                       List<ThrustCurveMotor> motors = ThrustCurveAPI.extractAllMotors(b);\r
+                                       if ( motors != null && motors.size() > 0 ) {\r
+                                               for( ThrustCurveMotor motor : motors ) {\r
+                                                       writeMotor( mi, motor);\r
+                                               }\r
+                                       }\r
                                }\r
                                if ( total < res.getMatches() ) {\r
                                        dismiss();\r
index 2f38a4224f4331920b267ba16e0fd090fdcfe5b2..bcb567da058c3db92ef2dab8f568e378cecefa2b 100644 (file)
@@ -6,15 +6,21 @@ import java.io.OutputStream;
 import java.net.MalformedURLException;\r
 import java.net.URL;\r
 import java.net.URLConnection;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.List;\r
+import java.util.Map;\r
 \r
 import net.sf.openrocket.android.util.AndroidLogWrapper;\r
+import net.sf.openrocket.motor.ThrustCurveMotor;\r
+import net.sf.openrocket.motor.ThrustCurveMotorPlaceholder;\r
 \r
 \r
-public class ThrustCurveAPI {\r
+public abstract class ThrustCurveAPI {\r
 \r
-       private String url_base = "http://www.thrustcurve.org/servlets/";\r
+       private static String url_base = "http://www.thrustcurve.org/servlets/";\r
        \r
-       public SearchResponse doSearch( SearchRequest request ) throws MalformedURLException, IOException {\r
+       public static SearchResponse doSearch( SearchRequest request ) throws MalformedURLException, IOException {\r
                \r
                String requestString = request.toString();\r
                \r
@@ -41,7 +47,7 @@ public class ThrustCurveAPI {
         return result;\r
        }\r
 \r
-       public MotorBurnFile downloadData( Integer motor_id ) throws MalformedURLException, IOException {\r
+       public static List<MotorBurnFile> downloadData( Integer motor_id ) throws MalformedURLException, IOException {\r
 \r
                if ( motor_id == null ) {\r
                        return null;\r
@@ -70,9 +76,73 @@ public class ThrustCurveAPI {
                DownloadResponse downloadResponse = DownloadResponseParser.parse(is);\r
                AndroidLogWrapper.d(ThrustCurveAPI.class,downloadResponse.toString());\r
 \r
-               MotorBurnFile mbf = downloadResponse.getData(motor_id);\r
-\r
-               return mbf;\r
+               return downloadResponse.getData(motor_id);\r
 \r
     }\r
+       \r
+       /**\r
+        * look through the listOfMotors to find the one which best matches the motor requested.\r
+        * \r
+        * The algorithm uses a score based method.  Each entry in listOfMotors is assigned a score\r
+        * and the element with the highest score is returned.  The score is computed as follows:\r
+        * \r
+        * 1) if the element matches the digest of the requested motor eactly, score += 1000\r
+        * 1) if the element matches the designation in the requested motor exactly, score = 100\r
+        * 2) if the element is a RockSim file score += 10\r
+        * \r
+        * @param motor\r
+        * @param listOfMotors\r
+        * @return\r
+        */\r
+       public static ThrustCurveMotor findBestMatch( ThrustCurveMotorPlaceholder motor, List<MotorBurnFile> listOfMotors ) {\r
+               \r
+               ThrustCurveMotor bestMatch = null;\r
+               int bestScore = -1;\r
+               \r
+               final String wantedDesignation = motor.getDesignation();\r
+               final String wantedDigest = motor.getDigest();\r
+               \r
+               for ( MotorBurnFile entry : listOfMotors ) {\r
+                       int entryScore = 0;\r
+                       ThrustCurveMotor entryMotor = entry.getThrustCurveMotor();\r
+                       \r
+                       if ("RockSim".equals(entry.getFiletype()) ) {\r
+                               entryScore += 10;\r
+                       }\r
+\r
+                       if ( wantedDigest != null && wantedDigest.equals( entryMotor.getDigest() ) ) {\r
+                               entryScore += 1000;\r
+                       }\r
+                       \r
+                       if ( wantedDesignation != null && wantedDesignation.equals(entryMotor.getDesignation())) {\r
+                               entryScore += 100;\r
+                       }\r
+                       \r
+                       if ( entryScore > bestScore ) {\r
+                               bestScore = entryScore;\r
+                               bestMatch = entry.getThrustCurveMotor();\r
+                       }\r
+                       \r
+               }\r
+               \r
+               return bestMatch;\r
+       }\r
+       \r
+       /**\r
+        * Extract all unique motors from the list based on designation.\r
+        * @param listOfMotors\r
+        * @return\r
+        */\r
+       public static List<ThrustCurveMotor> extractAllMotors( List<MotorBurnFile> listOfMotors ) {\r
+               \r
+               Map<String, ThrustCurveMotor> motorsByDesignation = new HashMap<String,ThrustCurveMotor>();\r
+               \r
+               for( MotorBurnFile entry : listOfMotors ) {\r
+                       ThrustCurveMotor motor = entry.getThrustCurveMotor();\r
+                       motorsByDesignation.put( motor.getDesignation(), motor);\r
+               }\r
+               \r
+               return new ArrayList<ThrustCurveMotor>(motorsByDesignation.values());\r
+               \r
+       }\r
 }\r