]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.datatypes.ontology/src/org/simantics/datatypes/SIGenerator.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.datatypes.ontology / src / org / simantics / datatypes / SIGenerator.java
1 package org.simantics.datatypes;
2
3 import java.io.File;
4 import java.io.PrintStream;
5 import java.io.UnsupportedEncodingException;
6 import java.net.MalformedURLException;
7 import java.net.URL;
8 import java.net.URLDecoder;
9 import java.util.ArrayList;
10
11 public class SIGenerator {
12
13         public static class Unit {
14                 
15                 public String name;
16                 public String shorthand;
17                 
18                 public Unit(String name, String shorthand) {
19                         this.name = name;
20                         this.shorthand = shorthand;
21                 }
22                 
23                 public Unit withPrefix(String namePrefix, String shorthandPrefix) {
24                         return new Unit(namePrefix+name, shorthandPrefix+shorthand);
25                 }
26                 
27         }
28         
29         public static final Unit[] baseUnits = {
30                 new Unit("Metre", "m"),
31                 // Easier this way..
32                 new Unit("gram", "g"),
33                 new Unit("Second", "s"),
34                 new Unit("Ampere", "A"),
35                 new Unit("Kelvin", "K"),
36                 new Unit("Candela", "cd"),
37                 new Unit("Mole", "mol"),
38         };
39         
40         public static final Unit[] baseUnitsPrefixed = prefix(baseUnits);
41         
42         public static final Unit[] derivedUnits = {
43                 new Unit("Hertz", "hz"),
44                 new Unit("Radian", "rad"),
45                 new Unit("Steradian", "sr"),
46                 new Unit("Newton", "N"),
47                 new Unit("Pascal", "Pa"),
48                 new Unit("Joule", "J"),
49                 new Unit("Watt", "W"),
50                 new Unit("Coulomb", "C"),
51                 new Unit("Volt", "V"),
52                 new Unit("Farad", "F"),
53                 new Unit("Ohm", "ohm"),
54                 new Unit("Siemens", "S"),
55                 new Unit("Weber", "Wb"),
56                 new Unit("Tesla", "T"),
57                 new Unit("Henry", "H"),
58                 new Unit("Celsius", "C"),
59                 new Unit("Lumen", "lm"),
60                 new Unit("Lux", "lx"),
61                 new Unit("Becquerel", "Bq"),
62                 new Unit("Gray", "Gy"),
63                 new Unit("Sievert", "Sv"),
64                 new Unit("Katal", "kat")
65         };
66
67         public static final Unit[] derivedUnitsPrefixed = prefix(derivedUnits);
68
69         public static final Unit[] scalableCompoundUnits = {
70                 new Unit("gramPerSecond", "g/s"),
71                 new Unit("gramPerLitre", "g/l"),
72                 new Unit("gramPerCubicMetre", "g/m3"),
73                 new Unit("NewtonPerMetre", "N/m"),
74                 new Unit("JoulePerKilogram", "J/kg"),
75                 new Unit("MetrePerSecond", "m/s"),
76                 new Unit("MolePerLitre", "mol/l"),
77                 new Unit("MolePerKilogram", "mol/kg"),
78         };
79
80         public static final Unit[] scalableCompoundUnitsPrefixed = prefix(scalableCompoundUnits);
81
82         public static final Unit[] compoundUnits = {
83                 new Unit("SquareMetre", "m2"),
84                 new Unit("CubicMetre", "m3"),
85         };
86
87         public static final Unit[] specials = {
88                 new Unit("MassPercentage", "w-%"),
89                 new Unit("Percentage", "%"),
90                 new Unit("Degree", "deg"),
91                 new Unit("Minute", "min"),
92                 new Unit("Hour", "h"),
93                 new Unit("Litre", "l"),
94         };
95         
96         static Unit[] prefix(Unit[] units) {
97                 ArrayList<Unit> result = new ArrayList<Unit>();
98                 for(Unit unit : units) {
99                         result.add(unit.withPrefix("Tera", "T"));
100                         result.add(unit.withPrefix("Giga", "G"));
101                         result.add(unit.withPrefix("Mega", "M"));
102                         result.add(unit.withPrefix("Kilo", "k"));
103                         result.add(unit.withPrefix("Hecto", "h"));
104                         result.add(unit.withPrefix("", ""));
105                         result.add(unit.withPrefix("Centi", "c"));
106                         result.add(unit.withPrefix("Milli", "m"));
107                         result.add(unit.withPrefix("Micro", "u"));
108                 }
109                 return result.toArray(new Unit[result.size()]);
110         }
111         
112         static void print(PrintStream out, Unit unit) {
113                 out.println("SI." + unit.name + " <T SI.Unit");
114                 out.println("SI." + unit.name + ".Double <T L0.Double");
115                 out.println("  @L0.assert L0.HasDataType $Double(unit=\"" + unit.shorthand + "\")");
116                 out.println("SI." + unit.name + ".Float <T L0.Float");
117                 out.println("  @L0.assert L0.HasDataType $Float(unit=\"" + unit.shorthand + "\")");
118                 out.println("");
119         }
120         
121         static void create(File path) {
122                 
123                 try {
124
125                         PrintStream out = new PrintStream(path + "/graph/SI.pgraph");
126                         out.println("L0 = <http://www.simantics.org/Layer0-1.1>");
127                         out.println("DATA = <http://www.simantics.org/Datatypes-1.1>");
128                         out.println("SI = DATA.SI : L0.Library");
129                         out.println("    L0.HasResourceClass \"org.simantics.datatypes.SIResource\"");
130                         out.println("");
131                         out.println("SI.Unit <T L0.Entity");
132                         out.println("");
133                         out.println("// SI base units");
134                         out.println("");
135                         
136                         for(Unit unit : baseUnitsPrefixed) {
137                                 print(out, unit);
138                         }
139
140                         out.println("// SI derived units");
141                         out.println("");
142
143                         for(Unit unit : derivedUnitsPrefixed) {
144                                 print(out, unit);
145                         }
146
147                         out.println("// SI scalable compound units");
148                         out.println("");
149
150                         for(Unit unit : scalableCompoundUnitsPrefixed) {
151                                 print(out, unit);
152                         }
153                         
154                         out.println("// SI compound units");
155                         out.println("");
156
157                         for(Unit unit : compoundUnits) {
158                                 print(out, unit);
159                         }
160
161                         out.println("// Special units");
162                         out.println("");
163
164                         for(Unit unit : specials) {
165                                 print(out, unit);
166                         }
167                         
168                 } catch (Exception e) {
169                         
170                 }
171                 
172         }
173         
174         public static void main(String[] args) {
175                 
176         URL classLocation = SIGenerator.class.getResource(".");
177         if (classLocation != null) {
178             if (classLocation.getProtocol().equals("file")) {
179                 try {
180                     URL resource = new URL(classLocation, "../../../..");
181                     File path = new File(URLDecoder.decode(resource.getPath(), "UTF-8"));
182                     System.err.println("path=" + path.getAbsolutePath());
183                     create(path);
184                 } catch (MalformedURLException e) {
185                     e.printStackTrace();
186                 } catch (UnsupportedEncodingException e) {
187                     e.printStackTrace();
188                 }
189             }
190         }
191                 
192         }
193         
194 }