211 lines
8.2 KiB
Java
211 lines
8.2 KiB
Java
/*
|
|
* Copyright (c) 2013-2015, Willem Cazander
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without modification, are permitted provided
|
|
* that the following conditions are met:
|
|
*
|
|
* * Redistributions of source code must retain the above copyright notice, this list of conditions and the
|
|
* following disclaimer.
|
|
* * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and
|
|
* the following disclaimer in the documentation and/or other materials provided with the distribution.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
|
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
|
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
|
* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
|
|
* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
package net.forwardfire.unitxc;
|
|
|
|
import java.nio.charset.Charset;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Paths;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.Iterator;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
|
|
import org.junit.jupiter.api.Assertions;
|
|
import org.junit.jupiter.api.Test;
|
|
|
|
import net.forwardfire.unitxc.converter.UnitXConverterParameterValue;
|
|
import net.forwardfire.unitxc.model.UnitXCType;
|
|
import net.forwardfire.unitxc.model.UnitXCGroup;
|
|
import net.forwardfire.unitxc.model.UnitXCGroupJump;
|
|
import net.forwardfire.unitxc.model.UnitXCGroupJumpParameter;
|
|
import net.forwardfire.unitxc.model.UnitXCGroupQuantity;
|
|
import net.forwardfire.unitxc.model.UnitXConverterResult;
|
|
import net.forwardfire.unitxc.model.step.RuleStepCodePrinter;
|
|
import net.forwardfire.unitxc.model.step.RuleStepCodeStyle;
|
|
import net.forwardfire.unitxc.model.step.UnitXConverterStep;
|
|
|
|
public class UnitXCConverterStepTest {
|
|
|
|
@Test
|
|
public void testFac() throws Exception {
|
|
UnitXCManager unitManager = UnitXCFactory.createManager();
|
|
|
|
Assertions.assertNotNull(unitManager);
|
|
Assertions.assertNotNull(unitManager.getConfig());
|
|
Assertions.assertNotNull(unitManager.getConfig().getUnitGroups());
|
|
Assertions.assertFalse(unitManager.getConfig().getUnitGroups().isEmpty());
|
|
|
|
long ts = 0l;
|
|
for (UnitXCGroup typeGroup:unitManager.getConfig().getUnitGroups()) {
|
|
UnitXCType type = unitManager.getUnitType(typeGroup.getBaseTypeId());
|
|
if (type == null) {
|
|
throw new IllegalStateException("No base type for: "+typeGroup.getId()+" needs: "+typeGroup.getBaseTypeId());
|
|
}
|
|
long tc = typeGroup.getUnitTypeSize();
|
|
ts += tc;
|
|
String groupLine = String.format("%-40s %-20d %-10s", typeGroup.getName().getValue(""),tc,type.getId());
|
|
System.out.println(groupLine);
|
|
}
|
|
System.out.println("\ntotalTypes: "+ts);
|
|
System.out.println("");
|
|
|
|
Set<String> paraSet = new HashSet<>();
|
|
for (UnitXCGroup typeGroup:unitManager.getConfig().getUnitGroups()) {
|
|
for (UnitXCGroupJump jumpGroup:typeGroup.getGroupJumps()) {
|
|
for (UnitXCGroupJumpParameter jumpPara:jumpGroup.getJumpParameters()) {
|
|
System.out.println("jumpPara: "+jumpPara.getId()+" as: "+jumpPara.getUnitGroupId());
|
|
if (paraSet.contains(jumpPara.getId())) {
|
|
//throw new IllegalStateException("duplicate jump parameter id found: "+jumpPara.getId());
|
|
}
|
|
paraSet.add(jumpPara.getId());
|
|
}
|
|
}
|
|
}
|
|
System.out.println("\ntotalTypes: "+ts);
|
|
System.out.println("");
|
|
|
|
UnitXConverterResult result2 = unitManager.getConverter().convertStepped(20.0, "m/s/s", "km/s/s");
|
|
System.out.println("20m/s = "+result2.getResultValue()+" km/h");
|
|
|
|
UnitXConverterResult result = unitManager.getConverter().convertStepped(211.0, "k°F", "m°C");
|
|
System.out.println("result: "+result);
|
|
|
|
//result = unitManager.getConverter().convertStepped(10.763, "ft²", "in²");
|
|
//xmlDriver.marshal(result, System.out);
|
|
//System.out.println("");
|
|
|
|
//result = unitManager.getConverter().convertStepped(10.763, "ft³", "in³");
|
|
//xmlDriver.marshal(result, System.out);
|
|
|
|
|
|
//result = unitManager.getConverter().convertStepped(1079000000, "km/h", "m/s");
|
|
//xmlDriver.marshal(result, System.out);
|
|
|
|
|
|
//try (OutputStream out = new FileOutputStream("/tmp/unitxc.conf.xml")) {
|
|
// jaxbDriver.getUnitXCConfig().marshal(unitManager.getConfig(), out);
|
|
//}
|
|
|
|
//System.out.println("GROUP:");
|
|
//jaxbDriver.getUnitXCGroup().marshal(unitManager.getUnitGroup("length"), System.out);
|
|
|
|
Map<String,UnitXConverterParameterValue> para = new HashMap<>();
|
|
para.put("metre_per_second_second", new UnitXConverterParameterValue(2, unitManager.getUnitType("minute")));
|
|
result = unitManager.getConverter().convertStepped(50, "km/h", "mm",para);
|
|
//System.out.println("CONVERT-RESULT: "+result);
|
|
|
|
long totalUnits=0;
|
|
StringBuilder buf = new StringBuilder();
|
|
buf.append("digraph G {\n");
|
|
|
|
//buf.append("\tsize = \"4,4\"\n");
|
|
for (UnitXCGroup typeGroup:unitManager.getConfig().getUnitGroups()) {
|
|
UnitXCType baseType = unitManager.getUnitType(typeGroup.getBaseTypeId());
|
|
System.out.println("group: "+typeGroup.getId());
|
|
|
|
// print info
|
|
buf.append("\t"+typeGroup.getId());
|
|
buf.append("[");
|
|
if (typeGroup.getDerivedFrom().isEmpty()) {
|
|
buf.append("shape=box,style=filled,color=\".9 0.5 0.9\",");
|
|
}
|
|
StringBuilder label = new StringBuilder();
|
|
label.append("label=\"");
|
|
label.append(typeGroup.getId()+"\\n");
|
|
label.append(baseType.getId()+"\\n");
|
|
label.append(baseType.getName()+"\\n");
|
|
Iterator<UnitXCGroupQuantity> q = typeGroup.getQuantities().iterator();
|
|
label.append("(");
|
|
while (q.hasNext()) {
|
|
UnitXCGroupQuantity gq = q.next();
|
|
label.append(gq.getId());
|
|
//label.append(gq.getName());
|
|
if (q.hasNext()) {
|
|
label.append(",");
|
|
}
|
|
}
|
|
label.append(")");
|
|
label.append("\"");
|
|
|
|
buf.append(label+",labelloc=b");
|
|
buf.append("];\n");
|
|
|
|
// print unit per group
|
|
buf.append("\t"+typeGroup.getId()+" -> "+typeGroup.getId());
|
|
buf.append("[");
|
|
buf.append("color=red,label=\""+typeGroup.getUnitTypeSize()+"\"");
|
|
buf.append("];\n");
|
|
|
|
totalUnits += typeGroup.getUnitTypeSize();
|
|
|
|
// print relations
|
|
for (String groupFrom:typeGroup.getDerivedFrom()) {
|
|
buf.append("\t"+groupFrom+" -> "+typeGroup.getId());
|
|
buf.append("[");
|
|
buf.append("dir=none");
|
|
buf.append("];\n");
|
|
}
|
|
|
|
//for (UnitXCGroupJump jumpGroup:typeGroup.getGroupJumps()) {
|
|
// System.out.println("jumpGruop: "+jumpGroup.getId()+" to: "+jumpGroup.getUnitGroupId());
|
|
// buf.append("\t"+typeGroup.getId()+" -> "+jumpGroup.getUnitGroupId());
|
|
// buf.append("[");
|
|
// buf.append("];\n");
|
|
// buf.append("\t"+typeGroup.getId()+" -> "+jumpGroup.getId());
|
|
// buf.append(";\n");
|
|
//
|
|
// buf.append("\t"+jumpGroup.getId()+" -> "+jumpGroup.getUnitGroupId());
|
|
// buf.append(";\n");
|
|
|
|
//for (UnitXCGroupJumpParameter jumpPara:jumpGroup.getJumpParameters()) {
|
|
|
|
//buf.append("\t"+jumpPara.getUnitGroupId()+"_"+(x++)+" -> "+jumpGroup.getId());
|
|
//buf.append(";\n");
|
|
|
|
//buf.append("\t"+typeGroup.getId()+" -> "+jumpPara.getId());
|
|
//buf.append(";\n");
|
|
|
|
//buf.append("\t"+jumpPara.getId()+" -> "+jumpGroup.getUnitGroupId());
|
|
//buf.append(";\n");
|
|
//}
|
|
//}
|
|
}
|
|
|
|
buf.append("Stats[shape=box,color=\".9 0.2 0.8\",label=\"unit groups: "+unitManager.getConfig().getUnitGroups().size()+"\\ntotal units: "+totalUnits+"\",labelloc=b];\n");
|
|
|
|
buf.append("}\n");
|
|
System.out.println("dot: \n"+buf.toString());
|
|
Files.write(Paths.get("target/groups.dot"), buf.toString().getBytes(Charset.forName("UTF-8")));
|
|
|
|
List<UnitXConverterStep> steps = new ArrayList<>();
|
|
result.getResultSteps().forEach(v -> steps.add(v.getConvertStep()));
|
|
|
|
String code = RuleStepCodePrinter.print(RuleStepCodeStyle.JAVASCRIPT, steps, result.getStartTypeId()+"_to_"+result.getResultTypeId());
|
|
System.out.println("\n\ncode: \n"+code);
|
|
}
|
|
}
|