From 2e2c23034487fc2f7ec0525872ead579dd7fab31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Moritz=20Schweppenh=C3=A4user?= Date: Fri, 31 May 2024 12:40:42 +0200 Subject: [PATCH] chore(checkstyle): getting rid of more checkstyle warnings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * cleaned SQLiteWriter * cleaned ClientServerChannel * cleaned IPResolver * documented Vector3d class Signed-off-by: Moritz Schweppenhäuser --- .../database/persistence/SQLiteWriter.java | 283 ++++++++++-------- .../org/eclipse/mosaic/lib/math/Vector3d.java | 273 ++++++++++++++++- .../lib/coupling/ClientServerChannel.java | 51 ++-- .../lib/objects/addressing/IpResolver.java | 3 + 4 files changed, 457 insertions(+), 153 deletions(-) diff --git a/lib/mosaic-database/src/main/java/org/eclipse/mosaic/lib/database/persistence/SQLiteWriter.java b/lib/mosaic-database/src/main/java/org/eclipse/mosaic/lib/database/persistence/SQLiteWriter.java index f77ec78a6..fbedbd5aa 100644 --- a/lib/mosaic-database/src/main/java/org/eclipse/mosaic/lib/database/persistence/SQLiteWriter.java +++ b/lib/mosaic-database/src/main/java/org/eclipse/mosaic/lib/database/persistence/SQLiteWriter.java @@ -54,6 +54,9 @@ public class SQLiteWriter { */ final MosaicVersion stable; + /** + * Constructor for the {@link SQLiteWriter} reading the properties to retrieve the stable database version. + */ public SQLiteWriter() { // determine stable version to check against try (InputStream propertiesStream = this.getClass().getResourceAsStream("/database.properties")) { @@ -94,27 +97,7 @@ public void saveToFile(@Nonnull Database database, @Nonnull String fileName) { if (goSave) { try { Statement statement = sqlite.connect(); - log.debug("setting version and properties..."); - saveProperties(database); - log.info("Saving {} nodes...", database.getNodes().size()); - saveNodes(database); - log.info("Saving {} ways...", database.getWays().size()); - saveWays(database); - saveWayNodes(database); - log.info("Saving {} connections...", database.getConnections().size()); - saveConnections(database); - saveConnectionNodes(database); - log.info("Saving {} restrictions...", database.getRestrictions().size()); - saveRestrictions(database); - log.info("Saving {} routes...", database.getRoutes().size()); - saveRoutes(database); - log.info("Saving {} roundabouts...", database.getRoundabouts().size()); - saveRoundabouts(database); - log.info("Saving {} buildings...", database.getBuildings().size()); - saveBuildings(database); - log.info("Create Indices"); - createIndices(); - log.info("Database saved"); + executeSaves(database); sqlite.disconnect(statement); } catch (SQLException sqle) { log.error("error while trying to write database content: {}", sqle.getMessage()); @@ -122,6 +105,30 @@ public void saveToFile(@Nonnull Database database, @Nonnull String fileName) { } } + private void executeSaves(@Nonnull Database database) throws SQLException { + log.debug("setting version and properties..."); + saveProperties(database); + log.info("Saving {} nodes...", database.getNodes().size()); + saveNodes(database); + log.info("Saving {} ways...", database.getWays().size()); + saveWays(database); + saveWayNodes(database); + log.info("Saving {} connections...", database.getConnections().size()); + saveConnections(database); + saveConnectionNodes(database); + log.info("Saving {} restrictions...", database.getRestrictions().size()); + saveRestrictions(database); + log.info("Saving {} routes...", database.getRoutes().size()); + saveRoutes(database); + log.info("Saving {} roundabouts...", database.getRoundabouts().size()); + saveRoundabouts(database); + log.info("Saving {} buildings...", database.getBuildings().size()); + saveBuildings(database); + log.info("Create Indices"); + createIndices(); + log.info("Database saved"); + } + /** * Clears the database of all known/used tables. @@ -166,26 +173,39 @@ private void initTables() throws SQLException { // create tables // integrity statement.executeUpdate("CREATE TABLE " + TABLES.PROPERTIES + " (id TEXT UNIQUE, value TEXT)"); - statement.executeUpdate("INSERT INTO " + TABLES.PROPERTIES + "(id, value) VALUES ('" + Database.PROPERTY_VERSION + "', '" + Database.VERSION_UNKNOWN + "')"); - statement.executeUpdate("INSERT INTO " + TABLES.PROPERTIES + "(id, value) VALUES ('" + Database.PROPERTY_IMPORT_ORIGIN + "', '')"); + statement.executeUpdate("INSERT INTO " + TABLES.PROPERTIES + + "(id, value) VALUES ('" + Database.PROPERTY_VERSION + "', '" + Database.VERSION_UNKNOWN + "')"); + statement.executeUpdate("INSERT INTO " + TABLES.PROPERTIES + + "(id, value) VALUES ('" + Database.PROPERTY_IMPORT_ORIGIN + "', '')"); // pure network - statement.executeUpdate("CREATE TABLE " + TABLES.NODE + " (id TEXT, lat REAL, lon REAL, ele REAL, is_traffic_light BOOLEAN, is_intersection BOOLEAN, is_generated BOOLEAN)"); - statement.executeUpdate("CREATE TABLE " + TABLES.WAY + " (id TEXT, name TEXT, type TEXT, speed REAL, lanesForward INTEGER, lanesBackward INTEGER, oneway BOOLEAN)"); - statement.executeUpdate("CREATE TABLE " + TABLES.WAY_CONSISTS_OF + " (way_id TEXT, node_id TEXT, sequence_number INTEGER)"); - statement.executeUpdate("CREATE TABLE " + TABLES.CONNECTION + " (id TEXT, way_id TEXT, lanes INTEGER, length REAL)"); - statement.executeUpdate("CREATE TABLE " + TABLES.CONNECTION_CONSISTS_OF + " (connection_id TEXT, node_id TEXT, sequence_number INTEGER)"); + statement.executeUpdate("CREATE TABLE " + TABLES.NODE + + " (id TEXT, lat REAL, lon REAL, ele REAL, is_traffic_light BOOLEAN, is_intersection BOOLEAN, is_generated BOOLEAN)"); + statement.executeUpdate("CREATE TABLE " + + TABLES.WAY + " (id TEXT, name TEXT, type TEXT, speed REAL, lanesForward INTEGER, lanesBackward INTEGER, oneway BOOLEAN)"); + statement.executeUpdate("CREATE TABLE " + + TABLES.WAY_CONSISTS_OF + " (way_id TEXT, node_id TEXT, sequence_number INTEGER)"); + statement.executeUpdate("CREATE TABLE " + + TABLES.CONNECTION + " (id TEXT, way_id TEXT, lanes INTEGER, length REAL)"); + statement.executeUpdate("CREATE TABLE " + + TABLES.CONNECTION_CONSISTS_OF + " (connection_id TEXT, node_id TEXT, sequence_number INTEGER)"); // turn restrictions - statement.executeUpdate("CREATE TABLE " + TABLES.RESTRICTION + " (id TEXT, source_way_id TEXT, via_node_id TEXT, target_way_id TEXT, type TEXT)"); + statement.executeUpdate("CREATE TABLE " + TABLES.RESTRICTION + + " (id TEXT, source_way_id TEXT, via_node_id TEXT, target_way_id TEXT, type TEXT)"); // traffic signals - statement.executeUpdate("CREATE TABLE " + TABLES.TRAFFIC_SIGNALS + " (id TEXT, ref_node_id TEXT, phases TEXT, timing TEXT, from_way_id TEXT, via0_way_id TEXT, via1_way_id TEXT, to_way_id TEXT, lanes_from TEXT, lanes_via0 TEXT, lanes_via1 TEXT, lanes_to TEXT)"); + statement.executeUpdate("CREATE TABLE " + TABLES.TRAFFIC_SIGNALS + + " (id TEXT, ref_node_id TEXT, phases TEXT, timing TEXT, from_way_id TEXT, via0_way_id TEXT, via1_way_id TEXT," + + " to_way_id TEXT, lanes_from TEXT, lanes_via0 TEXT, lanes_via1 TEXT, lanes_to TEXT)"); // vehicle data - statement.executeUpdate("CREATE TABLE " + TABLES.ROUTE + " (id TEXT, sequence_number INTEGER, connection_id TEXT)"); + statement.executeUpdate("CREATE TABLE " + TABLES.ROUTE + + " (id TEXT, sequence_number INTEGER, connection_id TEXT)"); // roundabouts statement.executeUpdate("CREATE TABLE " + TABLES.ROUNDABOUT + " (id TEXT)"); - statement.executeUpdate("CREATE TABLE " + TABLES.ROUNDABOUT_CONSISTS_OF + " (roundabout_id TEXT, node_id TEXT, sequence_number INTEGER)"); + statement.executeUpdate("CREATE TABLE " + + TABLES.ROUNDABOUT_CONSISTS_OF + " (roundabout_id TEXT, node_id TEXT, sequence_number INTEGER)"); // buildings and its corners statement.executeUpdate("CREATE TABLE " + TABLES.BUILDING + " (id TEXT, name TEXT, height REAL)"); - statement.executeUpdate("CREATE TABLE " + TABLES.BUILDING_CONSISTS_OF + " (building_id TEXT, lat REAL, lon REAL, sequence_number INTEGER)"); + statement.executeUpdate("CREATE TABLE " + + TABLES.BUILDING_CONSISTS_OF + " (building_id TEXT, lat REAL, lon REAL, sequence_number INTEGER)"); // Connection Details (like parking lots) statement.executeUpdate("CREATE TABLE " + TABLES.CONNECTION_DETAILS + " (id TEXT, connection TEXT, type TEXT, value TEXT)"); @@ -241,24 +261,28 @@ private void saveNodes(Database database) { String statement = "INSERT INTO " + TABLES.NODE + "(" + columns + ") VALUES (?, ?, ?, ?, ?, ?, ?)"; try (PreparedStatement prep = sqlite.getConnection().prepareStatement(statement)) { boolean autoCommit = sqlite.getConnection().getAutoCommit(); - sqlite.getConnection().setAutoCommit(false); - for (Node node : database.getNodes()) { - prep.setString(1, node.getId()); - prep.setDouble(2, node.getPosition().getLongitude()); - prep.setDouble(3, node.getPosition().getLatitude()); - prep.setDouble(4, node.getPosition().getAltitude()); - prep.setBoolean(5, node.getClass().equals(TrafficLightNode.class)); - prep.setBoolean(6, node.isIntersection()); - prep.setBoolean(7, node.isGenerated()); - prep.executeUpdate(); - } - sqlite.getConnection().commit(); + batchSaveNodes(database, prep); sqlite.getConnection().setAutoCommit(autoCommit); } catch (SQLException sqle) { log.error("error while trying to persist nodes: {}", sqle.getMessage()); } } + private void batchSaveNodes(Database database, PreparedStatement prep) throws SQLException { + sqlite.getConnection().setAutoCommit(false); + for (Node node : database.getNodes()) { + prep.setString(1, node.getId()); + prep.setDouble(2, node.getPosition().getLongitude()); + prep.setDouble(3, node.getPosition().getLatitude()); + prep.setDouble(4, node.getPosition().getAltitude()); + prep.setBoolean(5, node.getClass().equals(TrafficLightNode.class)); + prep.setBoolean(6, node.isIntersection()); + prep.setBoolean(7, node.isGenerated()); + prep.executeUpdate(); + } + sqlite.getConnection().commit(); + } + /** * Saves all {@link Way}s to the SQLite file. Be aware that references(/relations) to other * objects are saved separately! @@ -270,24 +294,28 @@ private void saveWays(Database database) { String statement = "INSERT INTO " + TABLES.WAY + "(" + columns + ") VALUES (?, ?, ?, ?, ?, ?, ?)"; try (PreparedStatement prep = sqlite.getConnection().prepareStatement(statement)) { boolean autoCommit = sqlite.getConnection().getAutoCommit(); - sqlite.getConnection().setAutoCommit(false); - for (Way way : database.getWays()) { - prep.setString(1, way.getId()); - prep.setString(2, way.getName()); - prep.setString(3, way.getType()); - prep.setDouble(4, way.getMaxSpeedInMs()); - prep.setInt(5, way.getNumberOfLanesForward()); - prep.setInt(6, way.getNumberOfLanesBackward()); - prep.setBoolean(7, way.isOneway()); - prep.executeUpdate(); - } - sqlite.getConnection().commit(); + batchSaveWays(database, prep); sqlite.getConnection().setAutoCommit(autoCommit); } catch (SQLException sqle) { log.error("error while trying to persist ways: {}", sqle.getMessage()); } } + private void batchSaveWays(Database database, PreparedStatement prep) throws SQLException { + sqlite.getConnection().setAutoCommit(false); + for (Way way : database.getWays()) { + prep.setString(1, way.getId()); + prep.setString(2, way.getName()); + prep.setString(3, way.getType()); + prep.setDouble(4, way.getMaxSpeedInMs()); + prep.setInt(5, way.getNumberOfLanesForward()); + prep.setInt(6, way.getNumberOfLanesBackward()); + prep.setBoolean(7, way.isOneway()); + prep.executeUpdate(); + } + sqlite.getConnection().commit(); + } + /** * Saves all {@link Roundabout}s to the SQLite file. References(/relations) to other * objects NOT are saved separately! @@ -341,21 +369,9 @@ private void saveRoundabouts(Database database) { private void saveWayNodes(Database database) { String columns = "way_id, node_id, sequence_number"; String statement = "INSERT INTO " + TABLES.WAY_CONSISTS_OF + "(" + columns + ") VALUES (?, ?, ?)"; - int sequenceNumber; try (PreparedStatement prep = sqlite.getConnection().prepareStatement(statement)) { boolean autoCommit = sqlite.getConnection().getAutoCommit(); - sqlite.getConnection().setAutoCommit(false); - for (Way way : database.getWays()) { - sequenceNumber = 0; - for (Node node : way.getNodes()) { - prep.setString(1, way.getId()); - prep.setString(2, node.getId()); - prep.setInt(3, sequenceNumber); - prep.executeUpdate(); - sequenceNumber++; - } - } - sqlite.getConnection().commit(); + batchSaveWayNodes(database, prep); sqlite.getConnection().setAutoCommit(autoCommit); } catch (SQLException sqle) { log.error("error while trying to persist ways <--> node relations: {}", @@ -363,6 +379,22 @@ private void saveWayNodes(Database database) { } } + private void batchSaveWayNodes(Database database, PreparedStatement prep) throws SQLException { + int sequenceNumber; + sqlite.getConnection().setAutoCommit(false); + for (Way way : database.getWays()) { + sequenceNumber = 0; + for (Node node : way.getNodes()) { + prep.setString(1, way.getId()); + prep.setString(2, node.getId()); + prep.setInt(3, sequenceNumber); + prep.executeUpdate(); + sequenceNumber++; + } + } + sqlite.getConnection().commit(); + } + /** * Saves all {@link Connection}s to the SQLite file. Be aware that references(/relations) to * other objects are saved separately! @@ -374,21 +406,25 @@ private void saveConnections(Database database) { String statement = "INSERT INTO " + TABLES.CONNECTION + "(" + columns + ") VALUES (?, ?, ?, ?)"; try (PreparedStatement prep = sqlite.getConnection().prepareStatement(statement)) { boolean autoCommit = sqlite.getConnection().getAutoCommit(); - sqlite.getConnection().setAutoCommit(false); - for (Connection connection : database.getConnections()) { - prep.setString(1, connection.getId()); - prep.setString(2, connection.getWay().getId()); - prep.setInt(3, connection.getLanes()); - prep.setDouble(4, connection.getLength()); - prep.executeUpdate(); - } - sqlite.getConnection().commit(); + batchSaveConnections(database, prep); sqlite.getConnection().setAutoCommit(autoCommit); } catch (SQLException sqle) { log.error("error while trying to persist connections: {}", sqle.getMessage()); } } + private void batchSaveConnections(Database database, PreparedStatement prep) throws SQLException { + sqlite.getConnection().setAutoCommit(false); + for (Connection connection : database.getConnections()) { + prep.setString(1, connection.getId()); + prep.setString(2, connection.getWay().getId()); + prep.setInt(3, connection.getLanes()); + prep.setDouble(4, connection.getLength()); + prep.executeUpdate(); + } + sqlite.getConnection().commit(); + } + /** * Saves all references/relations between {@link Connection}s and {@link Node}s to the SQLite * file. @@ -398,21 +434,9 @@ private void saveConnections(Database database) { private void saveConnectionNodes(Database database) { String columns = "connection_id, node_id, sequence_number"; String statement = "INSERT INTO " + TABLES.CONNECTION_CONSISTS_OF + "(" + columns + ") VALUES (?, ?, ?)"; - int sequenceNumber; try (PreparedStatement prep = sqlite.getConnection().prepareStatement(statement)) { boolean autoCommit = sqlite.getConnection().getAutoCommit(); - sqlite.getConnection().setAutoCommit(false); - for (Connection connection : database.getConnections()) { - sequenceNumber = 0; - for (Node node : connection.getNodes()) { - prep.setString(1, connection.getId()); - prep.setString(2, node.getId()); - prep.setInt(3, sequenceNumber); - prep.executeUpdate(); - sequenceNumber++; - } - } - sqlite.getConnection().commit(); + batchSaveConnectionNoes(database, prep); sqlite.getConnection().setAutoCommit(autoCommit); } catch (SQLException sqle) { log.error("error while trying to persist connection <--> node relations: {}", @@ -420,6 +444,22 @@ private void saveConnectionNodes(Database database) { } } + private void batchSaveConnectionNoes(Database database, PreparedStatement prep) throws SQLException { + int sequenceNumber; + sqlite.getConnection().setAutoCommit(false); + for (Connection connection : database.getConnections()) { + sequenceNumber = 0; + for (Node node : connection.getNodes()) { + prep.setString(1, connection.getId()); + prep.setString(2, node.getId()); + prep.setInt(3, sequenceNumber); + prep.executeUpdate(); + sequenceNumber++; + } + } + sqlite.getConnection().commit(); + } + /** * Save all {@link Restriction}s to the SQLite file. * @@ -431,22 +471,26 @@ private void saveRestrictions(Database database) { try (PreparedStatement prep = sqlite.getConnection().prepareStatement(statement)) { boolean autoCommit = sqlite.getConnection().getAutoCommit(); - sqlite.getConnection().setAutoCommit(false); - for (Restriction restriction : database.getRestrictions()) { - prep.setString(1, restriction.getId()); - prep.setString(2, restriction.getSource().getId()); - prep.setString(3, restriction.getVia().getId()); - prep.setString(4, restriction.getTarget().getId()); - prep.setString(5, restriction.getType().name()); - prep.executeUpdate(); - } - sqlite.getConnection().commit(); + batchSaveRestrictions(database, prep); sqlite.getConnection().setAutoCommit(autoCommit); } catch (SQLException sqle) { log.error("error while trying to persist restriction; {}", sqle.getMessage()); } } + private void batchSaveRestrictions(Database database, PreparedStatement prep) throws SQLException { + sqlite.getConnection().setAutoCommit(false); + for (Restriction restriction : database.getRestrictions()) { + prep.setString(1, restriction.getId()); + prep.setString(2, restriction.getSource().getId()); + prep.setString(3, restriction.getVia().getId()); + prep.setString(4, restriction.getTarget().getId()); + prep.setString(5, restriction.getType().name()); + prep.executeUpdate(); + } + sqlite.getConnection().commit(); + } + /** * Saves all {@link Building}s to the SQLite file. Basic attributes are stored in the table `Building`. * Furthermore, for each building, a list of corner points are stored in the table `BuildingConsistsOf`. @@ -475,7 +519,8 @@ private void saveBuildings(Database database) { } // Store corner list of building in database - final String cornerStatement = "INSERT INTO " + TABLES.BUILDING_CONSISTS_OF + "(building_id, lat, lon, sequence_number) VALUES (?, ?, ?, ?)"; + final String cornerStatement = "INSERT INTO " + TABLES.BUILDING_CONSISTS_OF + + "(building_id, lat, lon, sequence_number) VALUES (?, ?, ?, ?)"; try (PreparedStatement prep = sqlite.getConnection().prepareStatement(cornerStatement)) { final boolean autoCommit = sqlite.getConnection().getAutoCommit(); @@ -507,26 +552,30 @@ private void saveBuildings(Database database) { private void saveRoutes(Database database) { String columns = "id, sequence_number, connection_id"; String statement = "INSERT INTO " + TABLES.ROUTE + "(" + columns + ") VALUES (?, ?, ?)"; - int sequenceNumber; try (PreparedStatement prep = sqlite.getConnection().prepareStatement(statement)) { boolean autoCommit = sqlite.getConnection().getAutoCommit(); - sqlite.getConnection().setAutoCommit(false); - for (Route route : database.getRoutes()) { - sequenceNumber = 0; - for (Connection connection : route.getConnections()) { - prep.setString(1, route.getId()); - prep.setInt(2, sequenceNumber); - prep.setString(3, connection.getId()); - - prep.executeUpdate(); - sequenceNumber++; - } - } - sqlite.getConnection().commit(); + batchSaveRoutes(database, prep); sqlite.getConnection().setAutoCommit(autoCommit); } catch (SQLException sqle) { log.error("error while trying to persist routes: {}", sqle.getMessage()); } } + private void batchSaveRoutes(Database database, PreparedStatement prep) throws SQLException { + int sequenceNumber; + sqlite.getConnection().setAutoCommit(false); + for (Route route : database.getRoutes()) { + sequenceNumber = 0; + for (Connection connection : route.getConnections()) { + prep.setString(1, route.getId()); + prep.setInt(2, sequenceNumber); + prep.setString(3, connection.getId()); + + prep.executeUpdate(); + sequenceNumber++; + } + } + sqlite.getConnection().commit(); + } + } diff --git a/lib/mosaic-geomath/src/main/java/org/eclipse/mosaic/lib/math/Vector3d.java b/lib/mosaic-geomath/src/main/java/org/eclipse/mosaic/lib/math/Vector3d.java index fae546eba..f07b26837 100644 --- a/lib/mosaic-geomath/src/main/java/org/eclipse/mosaic/lib/math/Vector3d.java +++ b/lib/mosaic-geomath/src/main/java/org/eclipse/mosaic/lib/math/Vector3d.java @@ -32,24 +32,63 @@ public class Vector3d implements Serializable { public double y; public double z; + /** + * Constructor for {@link Vector3d}, which initializes it as V_0. + *
+     *     |0|
+     * v = |0|
+     *     |0|
+     * 
+ */ public Vector3d() { x = 0; y = 0; z = 0; } + /** + * Constructor for {@link Vector3d}, initializing it with the given coordinates. + *
+     *     |x|
+     * v = |y|
+     *     |z|
+     * 
+ * + * @param x X-Coordinate + * @param y Y-Coordinate + * @param z Z-Coordinate + */ public Vector3d(double x, double y, double z) { this.x = x; this.y = y; this.z = z; } + /** + * Constructor for {@link Vector3d}, + * initializing it with a copy of the coordinates from another given {@link Vector3d}. + *
+     *  Vector3d(v_j):
+     *         |x_j|
+     *     v = |y_j|
+     *         |z_j|
+     * 
+ * + * @param v {@link Vector3d} to be copied + **/ public Vector3d(Vector3d v) { x = v.x; y = v.y; z = v.z; } + /** + * Sets the coordinates of the {@link Vector3d} to the given value, + * setting it to a copy of the coordinates from another given {@link Vector3d}. + * + * @param v {@link Vector3d} to be copied + * @return the vector on which {@code set} has been called + */ public Vector3d set(Vector3d v) { x = v.x; y = v.y; @@ -57,6 +96,14 @@ public Vector3d set(Vector3d v) { return this; } + /** + * Sets the coordinates of the {@link Vector3d} to the given values. + * + * @param x X-Coordinate + * @param y Y-Coordinate + * @param z Z-Coordinate + * @return the vector on which {@code set} has been called + */ public Vector3d set(double x, double y, double z) { this.x = x; this.y = y; @@ -64,6 +111,12 @@ public Vector3d set(double x, double y, double z) { return this; } + /** + * Sets all entries of the {@link Vector3d} to the given value. + * + * @param f given value + * @return the vector on which {@code set} has been called + */ public Vector3d set(double f) { x = f; y = f; @@ -71,6 +124,18 @@ public Vector3d set(double f) { return this; } + /** + * Multiplies a scalar to the {@link Vector3d}. + *
+     *  v_i.multiply(f):
+     *      x_i = x_i * f
+     *      y_i = y_i * f
+     *      z_i = z_i * f
+     * 
+ * + * @param f scalar of type double + * @return the vector on which {@code multiply} has been called + */ public Vector3d multiply(double f) { x *= f; y *= f; @@ -78,6 +143,20 @@ public Vector3d multiply(double f) { return this; } + /** + * Multiplies a scalar to a {@link Vector3d}, + * storing the multiplication-result into a given vector. + *
+     *  v_i.multiply(f, v_r):
+     *      x_r = x_i * f
+     *      y_r = y_i * f
+     *      z_r = z_i * f
+     * 
+ * + * @param f scalar of type double + * @param result the {@link Vector3d} where the multiplication result should be saved to + * @return the result-vector + */ public Vector3d multiply(double f, Vector3d result) { result.x = x * f; result.y = y * f; @@ -85,6 +164,18 @@ public Vector3d multiply(double f, Vector3d result) { return result; } + /** + * Coordinate-wise multiplication of two {@link Vector3d vectors}. + *
+     *  v_i.scale(v_j):
+     *      x_i = x_i * x_j
+     *      y_i = y_i * y_j
+     *      z_i = z_i * z_j
+     * 
+ * + * @param scale the {@link Vector3d} to be scaled with + * @return the vector on which {@code scale} has been called + */ public Vector3d scale(Vector3d scale) { x *= scale.x; y *= scale.y; @@ -92,6 +183,20 @@ public Vector3d scale(Vector3d scale) { return this; } + /** + * Coordinate-wise multiplication of two {@link Vector3d vectors}, + * storing the scale-result into a given vector. + *
+     *  v_i.scale(v_j, v_r):
+     *      x_r = x_i * x_j
+     *      y_r = y_i * y_j
+     *      z_r = z_i * z_j
+     * 
+ * + * @param scale the {@link Vector3d} to be scaled with + * @param result the {@link Vector3d} where the scale-result should be saved to + * @return the result-vector + */ public Vector3d scale(Vector3d scale, Vector3d result) { result.x = x * scale.x; result.y = y * scale.y; @@ -99,6 +204,18 @@ public Vector3d scale(Vector3d scale, Vector3d result) { return result; } + /** + * Coordinate-wise addition of two {@link Vector3d vectors}. + *
+     *  v_i.add(v_j):
+     *      x_i = x_i + x_j
+     *      y_i = y_i + y_j
+     *      z_i = z_i + z_j
+     * 
+ * + * @param v the {@link Vector3d} to be added + * @return the vector on which {@code add} has been called + */ public Vector3d add(Vector3d v) { x += v.x; y += v.y; @@ -106,6 +223,20 @@ public Vector3d add(Vector3d v) { return this; } + /** + * Coordinate-wise addition of two {@link Vector3d vectors}, + * storing the addition-result into a given vector. + *
+     *  v_i.add(v_j, v_r):
+     *      x_r = x_i + x_j
+     *      y_r = y_i + y_j
+     *      z_r = z_i + z_j
+     * 
+ * + * @param v the {@link Vector3d} to be added + * @param result the {@link Vector3d} where the addition-result should be saved to + * @return the result-vector + */ public Vector3d add(Vector3d v, Vector3d result) { result.x = x + v.x; result.y = y + v.y; @@ -113,6 +244,18 @@ public Vector3d add(Vector3d v, Vector3d result) { return result; } + /** + * Coordinate-wise subtraction of two {@link Vector3d vectors}. + *
+     *  v_i.subtract(v_j):
+     *      x_i = x_i - x_j
+     *      y_i = y_i - y_j
+     *      z_i = z_i - z_j
+     * 
+ * + * @param v the {@link Vector3d} to be subtracted + * @return the vector on which {@code subtract} has been called + */ public Vector3d subtract(Vector3d v) { x -= v.x; y -= v.y; @@ -120,6 +263,20 @@ public Vector3d subtract(Vector3d v) { return this; } + /** + * Coordinate-wise subtraction of two {@link Vector3d vectors}, + * storing the subtraction-result into a given vector. + *
+     *  v_i.subtract(v_j, v_r):
+     *      x_r = x_i - x_j
+     *      y_r = y_i - y_j
+     *      z_r = z_i - z_j
+     * 
+ * + * @param v the {@link Vector3d} to be subtracted + * @param result the {@link Vector3d} where the subtraction-result should be saved to + * @return the result-vector + */ public Vector3d subtract(Vector3d v, Vector3d result) { result.x = x - v.x; result.y = y - v.y; @@ -128,10 +285,33 @@ public Vector3d subtract(Vector3d v, Vector3d result) { } + /** + * The dot product of two {@link Vector3d} resulting in a scalar. + *
+     *  v_i.dot(v_j): = x_i * x_j + y_i * y_j + z_i * z_j
+     * 
+ * + * @param v the {@link Vector3d} to be used for the dot product + * @return the resulting scalar + */ public double dot(Vector3d v) { return x * v.x + y * v.y + z * v.z; } + /** + * The cross-product of two {@link Vector3d}, + * storing the subtraction-result into a given vector. + *
+     *  v_i.cross(v_j, v_r):
+     *      x_r = y_i * z_j - z_i * y_j
+     *      y_r = z_i * x_j - x_i * z_j
+     *      z_r = x_i * y_j - y_i * x_j
+     * 
+ * + * @param v the {@link Vector3d} to be used for the cross-product + * @param result the {@link Vector3d} where the cross-product should be saved to + * @return the resulting {@link Vector3d} + */ public Vector3d cross(Vector3d v, Vector3d result) { result.x = y * v.z - z * v.y; result.y = z * v.x - x * v.z; @@ -142,7 +322,7 @@ public Vector3d cross(Vector3d v, Vector3d result) { /** * Calculates and returns the angle between the current and the given vector in radians. * - * @param v + * @param v the given vector * @return angle in radians */ public double angle(Vector3d v) { @@ -150,6 +330,11 @@ public double angle(Vector3d v) { return Math.acos(a); } + /** + * Normalizes the {@link Vector3d} by dividing it by its {@link #magnitude()}. + * + * @return the normalized {@link Vector3d} + */ public Vector3d norm() { double mag = magnitude(); if (!MathUtils.isFuzzyZero(mag)) { @@ -159,6 +344,13 @@ public Vector3d norm() { } } + /** + * Normalizes the {@link Vector3d} by dividing it by its {@link #magnitude()}, + * storing the result into a given vector. + * + * @param result the {@link Vector3d} where the normalization should be saved to + * @return the normalized {@link Vector3d} + */ public Vector3d norm(Vector3d result) { double mag = magnitude(); if (!MathUtils.isFuzzyZero(mag)) { @@ -176,10 +368,30 @@ public double magnitudeSqr() { return x * x + y * y + z * z; } + /** + * Method to determine the Euclidean distance between the two points represented by two {@link Vector3d Vector3ds}, + * resulting in a scalar value. + *
+     * v_i.distanceTo(v_j) = sqrt((x_j - x_i)^2 + (y_j - y_i)^2 + (z_j - z_i)^2)
+     * 
+ * + * @param v the {@link Vector3d} to which the distance should be determined + * @return the resulting Euclidean distance + */ public double distanceTo(Vector3d v) { return Math.sqrt(distanceSqrTo(v)); } + /** + * Method to determine the sqquared Euclidean distance between the two points represented by two {@link Vector3d Vector3ds}, + * resulting in a scalar value. + *
+     * v_i.distanceSqrTo(v_j) = (x_j - x_i)^2 + (y_j - y_i)^2 + (z_j - z_i)^2
+     * 
+ * + * @param v the {@link Vector3d} to which the squared distance should be determined + * @return the resulting squared Euclidean distance + */ public double distanceSqrTo(Vector3d v) { double dx = (v.x - x); double dy = (v.y - y); @@ -188,32 +400,35 @@ public double distanceSqrTo(Vector3d v) { } /** - * Rotates this vector by the specified angle (in degrees) around the given axis. + * Rotates this {@link Vector3d} by the specified angle (in degrees) around the given axis. * * @param angleDeg Rotation angle in degrees * @param axis Rotation axis + * @return the rotated {@link Vector3d} */ public Vector3d rotateDeg(double angleDeg, Vector3d axis) { return rotate(Math.toRadians(angleDeg), axis.x, axis.y, axis.z); } /** - * Rotates this vector by the specified angle (in radians) around the given axis. + * Rotates this {@link Vector3d} by the specified angle (in radians) around the given axis. * * @param angle Rotation angle in radians * @param axis Rotation axis + * @return the rotated {@link Vector3d} */ public Vector3d rotate(double angle, Vector3d axis) { return rotate(angle, axis.x, axis.y, axis.z); } /** - * Rotates this vector by the specified angle (in radians) around the given axis. + * Rotates this {@link Vector3d} by the specified angle (in radians) around the given axis. * * @param angle Rotation angle in radians * @param axisX Rotation axis x-component * @param axisY Rotation axis y-component * @param axisZ Rotation axis z-component + * @return the rotated {@link Vector3d} */ public Vector3d rotate(double angle, double axisX, double axisY, double axisZ) { double c = Math.cos(angle); @@ -232,38 +447,82 @@ public Vector3d rotate(double angle, double axisX, double axisY, double axisZ) { return this; } + /** + * Writes the {@link Vector3d} to an array using doubles, + * given an offset as start-index. + * + * @param result the array where the result should be saved to + * @param offset the start-index for writing + */ public void toArray(double[] result, int offset) { result[offset] = x; result[offset + 1] = y; result[offset + 2] = z; } + /** + * Writes the {@link Vector3d} to an array using floats, + * given an offset as start-index. + * + * @param result the array where the result should be saved to + * @param offset the start-index for writing + */ public void toArray(float[] result, int offset) { result[offset] = (float) x; result[offset + 1] = (float) y; result[offset + 2] = (float) z; } + /** + * Conversion of {@link Vector3d} to a {@link CartesianPoint} within MOSAIC's coordinate system. + * + * @return the resulting {@link CartesianPoint} + */ public CartesianPoint toCartesian() { return toCartesian(new MutableCartesianPoint()); } + /** + * Conversion of {@link Vector3d} to a {@link CartesianPoint} within MOSAIC's coordinate system, + * storing the results in a given {@link MutableCartesianPoint}. + * + * @param result a {@link MutableCartesianPoint} where the conversion will be saved to + * @return the resulting {@link CartesianPoint} + */ public MutableCartesianPoint toCartesian(MutableCartesianPoint result) { return result.set(x, -z, y); } + /** + * Conversion of {@link Vector3d} to a {@link GeoPoint} using the underlying {@link GeoProjection}. + * + * @return the resulting {@link GeoPoint} + */ public GeoPoint toGeo() { return GeoProjection.getInstance().vectorToGeographic(this); } + /** + * Projection of {@link Vector3d} to a {@link GeoPoint} using the underlying {@link GeoProjection}, + * storing the result in a given {@link MutableGeoPoint}. + * + * @param result the {@link MutableGeoPoint} where the projection will be saved to + * @return the resulting {@link MutableGeoPoint} + */ public MutableGeoPoint toGeo(MutableGeoPoint result) { return GeoProjection.getInstance().vectorToGeographic(this, result); } + /** + * Equal-check for two {@link Vector3d Vector3ds} using {@link MathUtils#isFuzzyEqual(double, double)}. + * + * @param other the {@link Vector3d} to be compared + * @return {@code true} if all coordinates of both {@link Vector3d Vector3ds} are fuzzy-equal, else {@link false} + */ public boolean isFuzzyEqual(Vector3d other) { - return MathUtils.isFuzzyEqual(x, other.x) && - MathUtils.isFuzzyEqual(y, other.y) && - MathUtils.isFuzzyEqual(z, other.z); + return MathUtils.isFuzzyEqual(x, other.x) + && MathUtils.isFuzzyEqual(y, other.y) + && MathUtils.isFuzzyEqual(z, other.z); } @Override diff --git a/lib/mosaic-network/src/main/java/org/eclipse/mosaic/lib/coupling/ClientServerChannel.java b/lib/mosaic-network/src/main/java/org/eclipse/mosaic/lib/coupling/ClientServerChannel.java index abe356f4f..47081fe09 100644 --- a/lib/mosaic-network/src/main/java/org/eclipse/mosaic/lib/coupling/ClientServerChannel.java +++ b/lib/mosaic-network/src/main/java/org/eclipse/mosaic/lib/coupling/ClientServerChannel.java @@ -128,6 +128,7 @@ public static final class CMD { /** * Allowed address types. + * FIXME: Is this still needed? */ public static final class ADDRESSTYPE { @@ -163,7 +164,7 @@ public static final class ADDRESSTYPE { final private OutputStream out; /** - * Logger + * Logger (not yet used). *///TODO: implement usage final private Logger log; @@ -206,8 +207,6 @@ public ClientServerChannel(InetAddress host, int port, Logger log) throws IOExce /** * Closes the channel. - * - * @throws java.io.IOException */ public void close() throws IOException { this.socket.close(); @@ -223,10 +222,10 @@ private ReceiveMessage readMessage() throws IOException { /** * Reads a message from the incoming channel. + * TODO: ChannelID (and length) not yet treated * * @return The read message. - * @throws java.io.IOException - */ //TODO: ChannelID (and length) not yet treated + */ public ReceiveMessageContainer readMessage(IdTransformer idTransformer) throws IOException { ReceiveMessage receiveMessage = this.readMessage(); V2xReceiverInformation recInfo = new V2xReceiverInformation(receiveMessage.getTime()).signalStrength(receiveMessage.getRssi()); @@ -241,7 +240,6 @@ public ReceiveMessageContainer readMessage(IdTransformer idTran * Reads a port from the incoming stream. * * @return the read port as int - * @throws java.io.IOException */ public int readPortBody() throws IOException { PortExchange portExchange = Validate.notNull(PortExchange.parseDelimitedFrom(in), "Could not read port."); @@ -252,7 +250,6 @@ public int readPortBody() throws IOException { * Reads a time from the incoming stream. * * @return the read time as long - * @throws java.io.IOException */ public long readTimeBody() throws IOException { TimeMessage timeMessage = Validate.notNull(TimeMessage.parseDelimitedFrom(in), "Could not read time."); @@ -260,15 +257,13 @@ public long readTimeBody() throws IOException { } /** - * Reads a single command from the input stream - * blocking + * Reads a single command from the input stream blocking. * * @return the read command - * @throws java.io.IOException */ public int readCommand() throws IOException { CommandMessage commandMessage = Validate.notNull(CommandMessage.parseDelimitedFrom(in), "Could not read command."); - return ProtobufCMDToCMD(commandMessage.getCommandType()); + return protobufCmdToCmd(commandMessage.getCommandType()); } //#################################################################### @@ -281,7 +276,6 @@ public int readCommand() throws IOException { * @param startTime the first point in time simulated by the simulator * @param endTime the last timestep simulated by the simulator * @return command returned by the federate - * @throws IOException Communication error. */ public int writeInitBody(long startTime, long endTime) throws IOException { writeCommand(CMD.INIT); //Announce INIT message @@ -297,7 +291,6 @@ public int writeInitBody(long startTime, long endTime) throws IOException { * @param time time at which the node is added * @param nodes a list of ids and positions * @return command returned by the federate - * @throws IOException Communication error. */ public int writeAddNodeMessage(long time, List nodes) throws IOException { writeCommand(CMD.UPDATE_NODE); //Announce UPDATE_NODE message @@ -318,7 +311,6 @@ public int writeAddNodeMessage(long time, List nodes) throws * @param time the time at which he RSU is added * @param rsus list of ids and positions * @return command returned by the federate - * @throws IOException Communication error. */ public int writeAddRsuNodeMessage(long time, List rsus) throws IOException { writeCommand(CMD.UPDATE_NODE); @@ -340,7 +332,6 @@ public int writeAddRsuNodeMessage(long time, List rsus) throw * @param time time at which the positions are updated * @param nodes a list of ids and positions * @return command returned by the federate - * @throws IOException Communication error. */ public int writeUpdatePositionsMessage(long time, List nodes) throws IOException { writeCommand(CMD.UPDATE_NODE); @@ -361,7 +352,6 @@ public int writeUpdatePositionsMessage(long time, List nodes) * @param time time at which the nodes are removed * @param ids list of IDs to remove * @return command returned by the federate - * @throws IOException Communication error. */ public int writeRemoveNodesMessage(long time, List ids) throws IOException { writeCommand(CMD.UPDATE_NODE); @@ -377,7 +367,7 @@ public int writeRemoveNodesMessage(long time, List ids) throws IOExcept return readCommand(); } - // @param channelId the channelID //TODO:make enum from + // @param channelId the channelID //TODO: make enum from /** * Write send message header to stream. @@ -389,13 +379,17 @@ public int writeRemoveNodesMessage(long time, List ids) throws IOExcept * @param msgLength length of the message * @param dac DestinationAddressContainer with the destination address of the sender and additional information * @return command returned by the federate - * @throws IOException Communication error. */ public int writeSendMessage(long time, int srcNodeId, int msgId, long msgLength, DestinationAddressContainer dac) throws IOException { writeCommand(CMD.MSG_SEND); - SendMessageMessage.Builder sendMess = SendMessageMessage.newBuilder(); //Add message details to the builder - sendMess.setTime(time).setNodeId(srcNodeId).setChannelId(translateChannel(dac.getAdhocChannelId())).setMessageId(msgId).setLength(msgLength); + //Add message details to the builder + SendMessageMessage.Builder sendMess = SendMessageMessage.newBuilder() + .setTime(time) + .setNodeId(srcNodeId) + .setChannelId(translateChannel(dac.getAdhocChannelId())) + .setMessageId(msgId) + .setLength(msgLength); ByteBuffer buffer = ByteBuffer.allocate(Long.SIZE); buffer.put(dac.getAddress().getIPv4Address().getAddress()); //make an int32 out of the byte array @@ -448,7 +442,6 @@ public int writeSendMessage(long time, int srcNodeId, * @param externalId the external (federate-internal) ID of the node * @param configuration the actual configuration * @return command returned by the federate - * @throws IOException */ public int writeConfigMessage(long time, int msgID, int externalId, AdHocConfiguration configuration) throws IOException { writeCommand(CMD.CONF_RADIO); @@ -467,7 +460,8 @@ public int writeConfigMessage(long time, int msgID, int externalId, AdHocConfigu default: throw new RuntimeException("Illegal number of radios in configuration: " + configuration.getRadioMode().toString()); } - if (configuration.getRadioMode() == AdHocConfiguration.RadioMode.SINGLE || configuration.getRadioMode() == AdHocConfiguration.RadioMode.DUAL) { + if (configuration.getRadioMode() == AdHocConfiguration.RadioMode.SINGLE + || configuration.getRadioMode() == AdHocConfiguration.RadioMode.DUAL) { ConfigureRadioMessage.RadioConfiguration.Builder radioConfig1 = ConfigureRadioMessage.RadioConfiguration.newBuilder(); radioConfig1.setReceivingMessages(false); //!!Semantic in Java: true -> only routing radioConfig1.setIpAddress(inet4ToInt(configuration.getConf0().getNewIP())); //Semantic in federates: false -> only routing @@ -505,7 +499,6 @@ public int writeConfigMessage(long time, int msgID, int externalId, AdHocConfigu * Command: advance time. * * @param time point in time up to which advance is granted - * @throws IOException Communication error. */ public void writeAdvanceTimeMessage(long time) throws IOException { writeCommand(CMD.ADVANCE_TIME); @@ -521,12 +514,12 @@ public void writeAdvanceTimeMessage(long time) throws IOException { * @throws IOException Communication error. */ public void writeCommand(int cmd) throws IOException { - CommandType protobufCMD = cmdToProtobufCMD(cmd); - if (protobufCMD == CommandType.UNDEF) { + CommandType protobufCmd = cmdToProtobufCmd(cmd); + if (protobufCmd == CommandType.UNDEF) { return; } CommandMessage.Builder commandMessage = CommandMessage.newBuilder(); - commandMessage.setCommandType(protobufCMD); + commandMessage.setCommandType(protobufCmd); commandMessage.build().writeDelimitedTo(out); } @@ -548,8 +541,8 @@ private int inet4ToInt(Inet4Address ip) { return buffer.getInt(); } - private int ProtobufCMDToCMD(CommandType protoCMD) { - switch (protoCMD) { + private int protobufCmdToCmd(CommandType protoCmd) { + switch (protoCmd) { case INIT: return CMD.INIT; case SHUT_DOWN: @@ -581,7 +574,7 @@ private int ProtobufCMDToCMD(CommandType protoCMD) { } } - private CommandType cmdToProtobufCMD(int cmd) { + private CommandType cmdToProtobufCmd(int cmd) { switch (cmd) { case CMD.INIT: return CommandType.INIT; diff --git a/lib/mosaic-objects/src/main/java/org/eclipse/mosaic/lib/objects/addressing/IpResolver.java b/lib/mosaic-objects/src/main/java/org/eclipse/mosaic/lib/objects/addressing/IpResolver.java index 6a7470672..fe824da5a 100644 --- a/lib/mosaic-objects/src/main/java/org/eclipse/mosaic/lib/objects/addressing/IpResolver.java +++ b/lib/mosaic-objects/src/main/java/org/eclipse/mosaic/lib/objects/addressing/IpResolver.java @@ -128,6 +128,8 @@ public Inet4Address getNetMask() { } /** + * Method to look up an {@link Inet4Address} for a given MOSAIC-style host name. + * * @param hostname the units ID veh_0; rsu_1; tl_2; cs_3 etc. * @return the hosts Inet4Address if it is registered or null if not */ @@ -138,6 +140,7 @@ public Inet4Address lookup(String hostname) { } /** + * Method to look up a MOSAIC-style host name for a given {@link Inet4Address}. * @param address the Inet4Address to lookup * @return name of the host belonging to the given address or null if none found */