diff options
author | Adrien Hopkins <ahopk127@my.yorku.ca> | 2021-08-26 08:38:34 -0500 |
---|---|---|
committer | Adrien Hopkins <ahopk127@my.yorku.ca> | 2021-08-26 08:38:34 -0500 |
commit | 3eab1082f65687085b04f59605673696353d5ede (patch) | |
tree | 7adfc4e411b52ecf3183a78823b0b4b2152e9b90 /docs | |
parent | 1cb69cfdcb18bbafdbc792174697732e7cf359e7 (diff) |
Finished sections on prefixes and UnitDatabase
Diffstat (limited to 'docs')
-rw-r--r-- | docs/design.org | 21 | ||||
-rw-r--r-- | docs/design.pdf | bin | 0 -> 148366 bytes | |||
-rw-r--r-- | docs/design.tex | 129 |
3 files changed, 147 insertions, 3 deletions
diff --git a/docs/design.org b/docs/design.org index 41713eb..1453327 100644 --- a/docs/design.org +++ b/docs/design.org @@ -1,11 +1,12 @@ #+TITLE: 7Units Design Document #+SUBTITLE: For version 0.3.1 -#+DATE: 2021 August 24 +#+DATE: 2021 August 26 #+LaTeX_HEADER: \usepackage[a4paper, lmargin=25mm, rmargin=25mm, tmargin=25mm, bmargin=25mm]{geometry} #+LaTeX_HEADER: \usepackage{xurl} +#+LaTeX: \newpage * Introduction - 7Units is a program that can convert between units. This document details the internal design of 7Units, for current and future developers. + 7Units is a program that can convert between units. This document details the internal design of 7Units, intended to be used by current and future developers. The frontend code is currently subject to change, so it is not included in the current version of this document. * Unit System Design @@ -34,8 +35,22 @@ - BritishImperial :: A static utility class with instances of common units in the British Imperial system (not to be confused with the US Customary system, which is also called "Imperial"; it has the same unit names but the values of a few units are different). This class and the US Customary is divided into static classes for each dimension, such as ~BritishImperial.Length~. - USCustomary :: A static utility class with instances of common units in the US Customary system (not to be confused with the British Imperial system; it has the same unit names but the values of a few units are different). ** Prefixes - + A ~UnitPrefix~ is a simple object that can multiply a ~LinearUnit~ by a value. It can calculate a new name for the unit by combining its name and the unit's name (symbols are done similarly). It can do multiplication, division and exponentation with a number, as well as multiplication and division with another prefix; all of these work by changing the prefix's multiplier. ** The Unit Database + The ~UnitDatabase~ class stores all of the unit, prefix and dimension data used by this program. It is not a representation of an actual database, just a class that stores lots of data. + + Units are stored using a custom ~Map~ implementation (~PrefixedUnitMap~) which maps unit names to units. It is backed by two maps: one for units (without prefixes) and one for prefixes. It is programmed to include prefixes (so if units includes "metre" and prefixes includes "kilo", this map will include "kilometre", mapping it to a unit representing a kilometre). It is immutable, but you can modify the underlying maps, which is reflected in the ~PrefixedUnitMap~. Other than that, it is a normal map implementation. + + Prefixes and dimensions are stored in normal maps. +*** Parsing Expressions + Each ~UnitDatabase~ instance has four [[*ExpressionParser][ExpressionParser]] instances associated with it, for four types of expressions: unit, unit value, prefix and dimension. They are mostly similar, with operators corresponding to each operation of the corresponding class (~LinearUnit~, ~LinearUnitValue~, ~UnitPrefix~, ~ObjectProduct<BaseDimension>~). Unit and unit value expressions use linear units; nonlinear units can be used with a special syntax (like "degC(20)") and are immediately converted to a linear unit representing their base (Kelvin in this case) before operating. +*** Parsing Files + There are two types of data files: unit and dimension. + + Unit files contain data about units and prefixes. Each line contains the name of a unit or prefix (prefixes end in a dash, units don't) followed by an expression which defines it, separated by one or more space characters (this behaviour is defined by the static regular expression ~NAME_EXPRESSION~). Unit files are parsed line by line, each line being run through the ~addUnitOrPrefixFromLine~ method, which splits a line into name and expression, determines whether it's a unit or a prefix, and parses the expression. Because all units are defined by others, base units need to be defined with a special expression "!"; *these units should be added to the database before parsing the file*. + + Dimension files are similar, only for dimensions instead of units and prefixes. +#+LaTeX: \newpage * Utility Classes 7Units has a few general "utility" classes. They aren't directly related to units, but are used in the units system. ** ObjectProduct diff --git a/docs/design.pdf b/docs/design.pdf Binary files differnew file mode 100644 index 0000000..c906158 --- /dev/null +++ b/docs/design.pdf diff --git a/docs/design.tex b/docs/design.tex new file mode 100644 index 0000000..05d2368 --- /dev/null +++ b/docs/design.tex @@ -0,0 +1,129 @@ +% Created 2021-08-26 Thu 08:35 +% Intended LaTeX compiler: pdflatex +\documentclass[11pt]{article} +\usepackage[utf8]{inputenc} +\usepackage[T1]{fontenc} +\usepackage{graphicx} +\usepackage{grffile} +\usepackage{longtable} +\usepackage{wrapfig} +\usepackage{rotating} +\usepackage[normalem]{ulem} +\usepackage{amsmath} +\usepackage{textcomp} +\usepackage{amssymb} +\usepackage{capt-of} +\usepackage{hyperref} +\usepackage[a4paper, lmargin=25mm, rmargin=25mm, tmargin=25mm, bmargin=25mm]{geometry} +\usepackage{xurl} +\date{2021 August 26} +\title{7Units Design Document\\\medskip +\large For version 0.3.1} +\hypersetup{ + pdfauthor={}, + pdftitle={7Units Design Document}, + pdfkeywords={}, + pdfsubject={}, + pdfcreator={Emacs 27.1 (Org mode 9.4.6)}, + pdflang={English}} +\begin{document} + +\maketitle +\tableofcontents + +\newpage + +\section{Introduction} +\label{sec:org9766985} +7Units is a program that can convert between units. This document details the internal design of 7Units, intended to be used by current and future developers. + +The frontend code is currently subject to change, so it is not included in the current version of this document. +\section{Unit System Design} +\label{sec:org9482112} +Any code related to the backend unit system is stored in the \texttt{sevenUnits.unit} package. +\subsection{Dimensions} +\label{sec:org29ea1a0} +Dimensions represent what a unit is measuring, such as length, time, or energy. Dimensions are represented as an \hyperref[sec:org22ea189]{ObjectProduct}<BaseDimension>, where \texttt{BaseDimension} is a very simple class (its only properties are a name and a symbol) which represents the dimension of a base unit; these base dimensions can be multiplied to create all other Dimensions. +\subsection{Unit Classes} +\label{sec:org492fabc} +Units are internally represented by the abstract class \texttt{Unit}. All units have an \hyperref[sec:org22ea189]{ObjectProduct}<BaseUnit> (referred to as the base) that they are based on, a dimension (ObjectProduct<BaseDimension>), one or more names and a symbol (these last two bits of data are contained in the \texttt{NameSymbol} class). The dimension is calculated from the base unit when needed; the variable is just a cache. It has two constructors: a package-private one used to make \texttt{BaseUnit} instances, and a protected one used to make general units (for other subclasses of \texttt{Unit}). All unit classes are immutable. + +Units also have two conversion functions - one which converts from a value expressed in this unit to its base unit, and another which converts from a value expressed in the base unit to this unit. In \texttt{Unit}, they are defined as two abstract methods. This allows you to convert from any unit to any other (as long as they have the same base, i.e. you aren't converting metres to pounds). To convert from A to B, first convert from A to its base, then convert from the base to B. + +\texttt{BaseUnit} represents a unit that all other units are defined by. All of the units used by this system are defined by seven SI \texttt{BaseUnit} instances (metre, second, kilogram, ampere, kelvin, mole, candela; this is what 7Units is named after) and two non-SI \texttt{BaseUnit} instances (US dollar and bit). Because base units are themselves units (and should be able to be used as units), \texttt{BaseUnit} is a subclass of \texttt{Unit}, using its own package-private constructor. + +However, most units are instances of \texttt{LinearUnit}, another subclass of \texttt{Unit}. \texttt{LinearUnit} represents a unit that is \emph{a product of a base unit and a constant called the \textbf{conversion factor}}. Most units you've ever used fall under this definition, the only common exceptions are degrees Celsius and Fahrenheit. This simplicity allows the \texttt{LinearUnit} to do many things: +\begin{itemize} +\item It can implement conversion to and from the base as multiplying and dividing respectively by the conversion factor +\item You can easily create new units by multiplying or dividing a \texttt{LinearUnit} by a number (for example, kilometre = metre * 1000). This can be easily implemented as multiplying this unit's conversion factor by the multiplier and returning a new \texttt{LinearUnit} with that conversion factor factor. +\item You can add or subtract two \texttt{LinearUnit} instances to create a third (as long as they have the same base) by adding or subtracting the conversion factor. +\item You can multiply or divide any two \texttt{LinearUnit} instances to create a third by multiplying or dividing the bases and conversion factors. +\item Note that any operations will return a unit without name(s) or a symbol. All unit classes have a \texttt{withName} method that returns a copy of them with different names and/or a different symbol (all of this info is contained in the \texttt{NameSymbol} class) +\end{itemize} + +There are a few more classes which play small roles in the unit system: +\begin{description} +\item[{Unitlike}] A class that is like a unit, but its "value" can be any class. The only use of this class right now is to implement \texttt{MultiUnit}, a combination of units (like "foot + inch", commonly used in North America for measuring height); its "value" is a list of numbers. +\item[{FunctionalUnit}] A convenience class that implements the two conversion functions of \texttt{Unit} using \texttt{DoubleUnaryOperator} instances. This is used internally to implement degrees Celsius and Fahrenheit. There is also a version of this for \texttt{Unitlike}, \texttt{FunctionalUnitlike}. +\item[{UnitValue}] A value expressed as a certain unit (such as "7 inches"). This class is used by the simple unit converter to represent units. You can convert them between units. There are also versions of this for \texttt{LinearUnit} and \texttt{Unitlike}. +\item[{Metric}] A static utility class with instances of all of the SI named units, the 9 base dimensions, SI prefixes, some common prefixed units like the kilometre, and a few non-SI units used commonly with them. +\item[{BritishImperial}] A static utility class with instances of common units in the British Imperial system (not to be confused with the US Customary system, which is also called "Imperial"; it has the same unit names but the values of a few units are different). This class and the US Customary is divided into static classes for each dimension, such as \texttt{BritishImperial.Length}. +\item[{USCustomary}] A static utility class with instances of common units in the US Customary system (not to be confused with the British Imperial system; it has the same unit names but the values of a few units are different). +\end{description} +\subsection{Prefixes} +\label{sec:org96c3f4a} +A \texttt{UnitPrefix} is a simple object that can multiply a \texttt{LinearUnit} by a value. It can calculate a new name for the unit by combining its name and the unit's name (symbols are done similarly). It can do multiplication, division and exponentation with a number, as well as multiplication and division with another prefix; all of these work by changing the prefix's multiplier. +\subsection{The Unit Database} +\label{sec:org96ca2c9} +The \texttt{UnitDatabase} class stores all of the unit, prefix and dimension data used by this program. It is not a representation of an actual database, just a class that stores lots of data. + +Units are stored using a custom \texttt{Map} implementation (\texttt{PrefixedUnitMap}) which maps unit names to units. It is backed by two maps: one for units (without prefixes) and one for prefixes. It is programmed to include prefixes (so if units includes "metre" and prefixes includes "kilo", this map will include "kilometre", mapping it to a unit representing a kilometre). It is immutable, but you can modify the underlying maps, which is reflected in the \texttt{PrefixedUnitMap}. Other than that, it is a normal map implementation. + +Prefixes and dimensions are stored in normal maps. +\subsubsection{Parsing Expressions} +\label{sec:orgbd5591a} +Each \texttt{UnitDatabase} instance has four \hyperref[sec:org616a257]{ExpressionParser} instances associated with it, for four types of expressions: unit, unit value, prefix and dimension. They are mostly similar, with operators corresponding to each operation of the corresponding class (\texttt{LinearUnit}, \texttt{LinearUnitValue}, \texttt{UnitPrefix}, \texttt{ObjectProduct<BaseDimension>}). Unit and unit value expressions use linear units; nonlinear units can be used with a special syntax (like "degC(20)") and are immediately converted to a linear unit representing their base (Kelvin in this case) before operating. +\subsubsection{Parsing Files} +\label{sec:org1627c0d} +There are two types of data files: unit and dimension. + +Unit files contain data about units and prefixes. Each line contains the name of a unit or prefix (prefixes end in a dash, units don't) followed by an expression which defines it, separated by one or more space characters (this behaviour is defined by the static regular expression \texttt{NAME\_EXPRESSION}). Unit files are parsed line by line, each line being run through the \texttt{addUnitOrPrefixFromLine} method, which splits a line into name and expression, determines whether it's a unit or a prefix, and parses the expression. Because all units are defined by others, base units need to be defined with a special expression "!"; \textbf{these units should be added to the database before parsing the file}. + +Dimension files are similar, only for dimensions instead of units and prefixes. +\newpage +\section{Utility Classes} +\label{sec:org99dedb2} +7Units has a few general "utility" classes. They aren't directly related to units, but are used in the units system. +\subsection{ObjectProduct} +\label{sec:org22ea189} +An \texttt{ObjectProduct} represents a "product" of elements of some type. The units system uses them to represent coherent units as a product of base units, and dimensions as a product of base dimensions. + +Internally, it is represented using a map mapping objects to their exponents in the product. For example, the unit "kg m\textsuperscript{2} / s\textsuperscript{2}" (i.e. a Joule) would be represented with a map like \texttt{[kg: 1, m: 2, s: -2]}. +\subsection{ExpressionParser} +\label{sec:org616a257} +The \texttt{ExpressionParser} class is used to parse the unit, prefix and dimension expressions that are used throughout 7Units. An expression is something like "(2 m + 30 J / N) * 8 s)". Each instance represents a type of expression, containing a way to obtain values (such as numbers or units) from the text and operations that can be done on these values (such as addition, subtraction or multiplication). Each operation also has a priority, which controls the order of operations (i.e. multiplication gets a higher priority than addition). + +\texttt{ExpressionParser} has a parameterized type \texttt{T}, which represents the type of the value used in the expression. The expression parser currently only supports one type of value per expression; in the expressions used by 7Units numbers are treated as a kind of unit or prefix. Operators are represented by internal types; the system distinguishes between unary operators (those that take a single value, like negation) and binary operators (those that take 2 values, like +, -, * or /). + +Expressions are parsed in 2 steps: +\begin{enumerate} +\item Convert the expression to \href{https://en.wikipedia.org/wiki/Reverse\_Polish\_notation}{Reverse Polish Notation}, where operators come \textbf{after} the values they operate on, and brackets and the order of operations are not necessary. For example, "2 + 5" becomes "\texttt{2 5 +}", "(1 + 2) * 3" becomes "\texttt{1 2 + 3 *}" and the example expression earlier becomes "\texttt{2 m * 30 J * N / + 8 s * *}". This makes it simple to evaluate - early calculators used RPN for a good reason! +\item Evaluate the RPN expression. This can be done simply with a for loop and a stack. For each token in the expression, the progam does the following: +\begin{itemize} +\item if it is a number or unit, add it to the stack. +\item if it is a unary operator, take one value from the stack, apply the operator to it, and put the result into the stack. +\item if it is a binary operator, take two values from the stack, apply the operator to them, and put the result into the stack. +\end{itemize} +After evaluating the last token, there should be one value left in the stack - the answer. If there isn't, the original expression was malformed. +\end{enumerate} +\subsection{Math Classes} +\label{sec:orgd04995c} +There are two simple math classes in 7Units: +\begin{description} +\item[{\texttt{UncertainDouble}}] Like a \texttt{double}, but with an uncertainty (e.g. \(2.0 \pm 0.4\)). The operations are like those of the regular Double, only they also calculate the uncertainty of the final value. They also have "exact" versions to help interoperation between \texttt{double} and \texttt{UncertainDouble}. +\item[{\texttt{DecimalComparison}}] A static utility class that contains a few alternate equals() methods for \texttt{double} and \texttt{UncertainDouble}. These methods allow a slight (configurable) difference between values to still be considered equal, to fight roundoff error. +\end{description} +\subsection{Collection Classes} +\label{sec:org00dd440} +The \texttt{ConditionalExistenceCollections} class contains wrapper implementations of \texttt{Collection}, \texttt{Iterator}, \texttt{Map} and \texttt{Set}. These implementations ignore elements that do not pass a certain condition - if an element fails the condition, \texttt{contains} will return false, the iterator will skip past it, it won't be counted in \texttt{size}, etc. even if it exists in the original collection. Effectively, any element of the original collection that fails the test does not exist. +\end{document} |