From db64a85a3a338cd9e59448fa51732a75ffce80f1 Mon Sep 17 00:00:00 2001 From: JoePfeiffer Date: Mon, 9 Nov 2020 13:59:31 -0700 Subject: [PATCH 1/2] add logging to ThrustCurveMotorSetDatabase for debug --- core/src/net/sf/openrocket/file/DatabaseMotorFinder.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core/src/net/sf/openrocket/file/DatabaseMotorFinder.java b/core/src/net/sf/openrocket/file/DatabaseMotorFinder.java index a92e9f8e1..891314cd3 100644 --- a/core/src/net/sf/openrocket/file/DatabaseMotorFinder.java +++ b/core/src/net/sf/openrocket/file/DatabaseMotorFinder.java @@ -3,6 +3,9 @@ package net.sf.openrocket.file; import java.util.Collections; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import net.sf.openrocket.aerodynamics.Warning; import net.sf.openrocket.aerodynamics.WarningSet; import net.sf.openrocket.motor.Motor; @@ -16,6 +19,7 @@ import net.sf.openrocket.startup.Application; * @author Sampo Niskanen */ public class DatabaseMotorFinder implements MotorFinder { + private static final Logger log = LoggerFactory.getLogger(DatabaseMotorFinder.class); /** * Do something when a missing motor is found. @@ -40,6 +44,8 @@ public class DatabaseMotorFinder implements MotorFinder { @Override public Motor findMotor(Type type, String manufacturer, String designation, double diameter, double length, String digest, WarningSet warnings) { + + log.debug("type " + type + ", manufacturer " + manufacturer + ", designation " + designation + ", diameter " + diameter + ", length " + length + ", digest " + digest + ", warnings " + warnings); if (designation == null) { warnings.add(Warning.fromString("No motor specified, ignoring.")); @@ -65,6 +71,9 @@ public class DatabaseMotorFinder implements MotorFinder { // One motor if (motors.size() == 1) { Motor m = motors.get(0); + + log.debug("motor is " + m.getDesignation()); + if (digest != null && !digest.equals(m.getDigest())) { String str = "Motor with designation '" + designation + "'"; if (manufacturer != null) From 495ef3497074342f6c8d70bf1bbebe4e69e3502e Mon Sep 17 00:00:00 2001 From: JoePfeiffer Date: Tue, 17 Nov 2020 11:47:17 -0700 Subject: [PATCH 2/2] Merge old findMotor() and findMotors() methods in ThrustCurveMotorSetDatabase.java Turns out the motor digest hash function can have collisions. Since the digest is stored in the .ork file we don't want to rewrite it as that will cause *lots* of spurious "thrust curves don't match" warnings, and I really don't want to get into looking up by some sort of new digest if it exists else old digest. Since some motor description fields are also stored in the .ork file, I'm effectively using these as a key to resolve collisions. Now that I need to pass them in to the findMotor() method, I'm merging it with findMotors() (which searches based on motor descriptions) New combined method returns all motors that match digest and description; If there are none returns all motors that match digest; If there are none returns all motors that match description (may be empty) Passing a digest and no other description emulates the behavior of the old findMotor() (but it may return more than one motor). Passing description elements and no digest duplicates the behavior of the old findMotors() --- .../database/motor/MotorDatabase.java | 5 +- .../motor/ThrustCurveMotorSetDatabase.java | 71 +++++++++++-------- .../openrocket/file/DatabaseMotorFinder.java | 9 +-- 3 files changed, 43 insertions(+), 42 deletions(-) diff --git a/core/src/net/sf/openrocket/database/motor/MotorDatabase.java b/core/src/net/sf/openrocket/database/motor/MotorDatabase.java index 231027104..9f7dca35e 100644 --- a/core/src/net/sf/openrocket/database/motor/MotorDatabase.java +++ b/core/src/net/sf/openrocket/database/motor/MotorDatabase.java @@ -17,10 +17,7 @@ public interface MotorDatabase { * @param length the length, or NaN. * @return a list of all the matching motors. */ - public List findMotors(Motor.Type type, + public List findMotors(String digest, Motor.Type type, String manufacturer, String designation, double diameter, double length); - - public Motor findMotor(String digest); - } diff --git a/core/src/net/sf/openrocket/database/motor/ThrustCurveMotorSetDatabase.java b/core/src/net/sf/openrocket/database/motor/ThrustCurveMotorSetDatabase.java index 5c3d4b7a4..d04aea975 100644 --- a/core/src/net/sf/openrocket/database/motor/ThrustCurveMotorSetDatabase.java +++ b/core/src/net/sf/openrocket/database/motor/ThrustCurveMotorSetDatabase.java @@ -4,6 +4,9 @@ import java.util.ArrayList; import java.util.Collections; import java.util.List; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import net.sf.openrocket.motor.Motor; import net.sf.openrocket.motor.ThrustCurveMotor; @@ -14,51 +17,59 @@ import net.sf.openrocket.motor.ThrustCurveMotor; * @author Sampo Niskanen */ public class ThrustCurveMotorSetDatabase implements MotorDatabase { + private static final Logger log = LoggerFactory.getLogger(ThrustCurveMotorSetDatabase.class); private final List motorSets = new ArrayList(); @Override - public ThrustCurveMotor findMotor(String digest) { - if (digest == null) { - return null; - } - for (ThrustCurveMotorSet set : motorSets) { - for (ThrustCurveMotor m : set.getMotors()) { - if (digest.equals(m.getDigest())) { - return m; - } - } - } - - return null; - - } - - @Override - public List findMotors(Motor.Type type, String manufacturer, String designation, + public List findMotors(String digest, Motor.Type type, String manufacturer, String designation, double diameter, double length) { - ArrayList results = new ArrayList(); - + ArrayList fullMatches = new ArrayList(); + ArrayList digestMatches = new ArrayList(); + ArrayList descriptionMatches = new ArrayList(); + + // Apply filters to see if we can find any motors that match the given criteria. We'll return + // the most restrictive nonempty list we find, or empty list if no matches at all for (ThrustCurveMotorSet set : motorSets) { for (ThrustCurveMotor m : set.getMotors()) { - boolean match = true; + boolean matchDescription = true; + boolean matchDigest = true; + + // unlike the description, digest must be present in search criteria to get a match + if (digest == null || digest != m.getDigest()) + matchDigest = false; + + // match description if (type != null && type != set.getType()) - match = false; + matchDescription = false; else if (manufacturer != null && !m.getManufacturer().matches(manufacturer)) - match = false; + matchDescription = false; else if (designation != null && !designation.equalsIgnoreCase(m.getDesignation())) - match = false; + matchDescription = false; else if (!Double.isNaN(diameter) && (Math.abs(diameter - m.getDiameter()) > 0.005)) - match = false; + matchDescription = false; else if (!Double.isNaN(length) && (Math.abs(length - m.getLength()) > 0.005)) - match = false; - - if (match) - results.add(m); + matchDescription = false; + + if (matchDigest) + digestMatches.add(m); + + if (matchDescription) + descriptionMatches.add(m); + + if (matchDigest && matchDescription) + fullMatches.add(m); } } - return results; + if (!fullMatches.isEmpty()) + return fullMatches; + + if (!digestMatches.isEmpty()) + return digestMatches; + + return descriptionMatches; + } diff --git a/core/src/net/sf/openrocket/file/DatabaseMotorFinder.java b/core/src/net/sf/openrocket/file/DatabaseMotorFinder.java index 891314cd3..36e83698d 100644 --- a/core/src/net/sf/openrocket/file/DatabaseMotorFinder.java +++ b/core/src/net/sf/openrocket/file/DatabaseMotorFinder.java @@ -54,14 +54,7 @@ public class DatabaseMotorFinder implements MotorFinder { List motors; - { - Motor m = Application.getMotorSetDatabase().findMotor(digest); - if (m != null) { - motors = Collections. singletonList(m); - } else { - motors = Application.getMotorSetDatabase().findMotors(type, manufacturer, designation, diameter, length); - } - } + motors = Application.getMotorSetDatabase().findMotors(digest, type, manufacturer, designation, diameter, length); // No motors if (motors.size() == 0) {