From: miettinen Date: Fri, 17 Jan 2014 06:28:17 +0000 (+0000) Subject: Unit equivalents to Sysdyn unit parser (refs #4628). X-Git-Tag: 1.8.1~161 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=97e6269352f0a3340e50d5dcb8308966fd0db96b;p=simantics%2Fsysdyn.git Unit equivalents to Sysdyn unit parser (refs #4628). Allow %, $, £, and euro symbols, as well as greek letters in unit parser (refs #4425). git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@28650 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ontology/graph.tg b/org.simantics.sysdyn.ontology/graph.tg index 27edbee8..afa595a6 100644 Binary files a/org.simantics.sysdyn.ontology/graph.tg and b/org.simantics.sysdyn.ontology/graph.tg differ diff --git a/org.simantics.sysdyn.ontology/graph/Validation.pgraph b/org.simantics.sysdyn.ontology/graph/Validation.pgraph index 6d4fb228..cfa53e0b 100644 --- a/org.simantics.sysdyn.ontology/graph/Validation.pgraph +++ b/org.simantics.sysdyn.ontology/graph/Validation.pgraph @@ -34,7 +34,6 @@ VALIDATIONS.Units.unitValidator : L0.Function VALIDATIONS.Units.moduleInterfaceExtension : L0.Function - SYSDYN.Variable @VALIDATIONS.constraint VALIDATIONS.ExpressionConstraint @@ -67,11 +66,15 @@ SYSDYN.Variable VALIDATIONS.Dependencies.missingDependencyValidator VALIDATIONS.Functions.baseRealizationFunction +VALIDATIONS.Units.UnitIssueSource + >-- VALIDATIONS.Units.UnitIssueSource.allowEquivalents >-- L0.Boolean >() { diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/EquivalentUnitsWidget.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/EquivalentUnitsWidget.java new file mode 100644 index 00000000..13106559 --- /dev/null +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/properties/widgets/EquivalentUnitsWidget.java @@ -0,0 +1,143 @@ +/******************************************************************************* + * Copyright (c) 2014 Association for Decentralized Information Management in + * Industry THTH ry. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * VTT Technical Research Centre of Finland - initial API and implementation + *******************************************************************************/ +package org.simantics.sysdyn.ui.properties.widgets; + +import org.eclipse.jface.viewers.ISelection; +import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.swt.SWT; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.simantics.browsing.ui.swt.widgets.impl.SelectionListenerImpl; +import org.simantics.browsing.ui.swt.widgets.impl.Widget; +import org.simantics.browsing.ui.swt.widgets.impl.WidgetSupport; +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.PossibleObjectWithType; +import org.simantics.db.common.request.ReadRequest; +import org.simantics.db.exception.DatabaseException; +import org.simantics.db.management.ISessionContext; +import org.simantics.issues.ontology.IssueResource; +import org.simantics.operation.Layer0X; +import org.simantics.sysdyn.SysdynResource; +import org.simantics.utils.ui.ISelectionUtils; + +/** + * Widget for equivalent units selection. + * @author Tuomas Miettinen + * + */ +public class EquivalentUnitsWidget implements Widget{ + + Resource model = null; + org.simantics.browsing.ui.swt.widgets.Button unitEquivalents; + + public EquivalentUnitsWidget(Composite parent, WidgetSupport support, int style) { + support.register(this); + unitEquivalents = new org.simantics.browsing.ui.swt.widgets.Button(parent, support, style |= SWT.CHECK); + unitEquivalents.setText("Unit equivalents"); + unitEquivalents.getWidget().setToolTipText("Supported equivalent units:\n" + + "$, $s, dollar, dollars, usd\n"+ + "\u20ac, \u20acs, eur, euro, euros, e, ecu\n"+ + "£, £s, pound, pounds, gbp\n"+ + "Unit, Units\n"+ + "Person, People, Persons\n"+ + "second, seconds, sec, s\n"+ + "minute, minutes, min\n"+ + "hour, hours, h, hr\n"+ + "day, days, d\n"+ + "month, months, mon, mth, mo, mos\n"+ + "year, years, a, y, yr"); + + } + + @Override + public void setInput(ISessionContext context, Object input) { + if(input instanceof ISelection) { + ISelection selection = (ISelection)input; + if(selection instanceof IStructuredSelection) { + Resource resource = ISelectionUtils.filterSingleSelection(selection, Resource.class); + if(resource != null) { + model = resource; + } + } + } + + if(model == null) return; + + try { + context.getSession().syncRequest(new ReadRequest() { + + @Override + public void run(ReadGraph graph) throws DatabaseException { + // Determine if unit validation is enabled. + SysdynResource sr = SysdynResource.getInstance(graph); + final Resource unitIssueSource = graph.syncRequest( + new PossibleObjectWithType(model, + Layer0X.getInstance(graph).Activates, + sr.Validations_Units_UnitIssueSource)); + IssueResource ISSUE = IssueResource.getInstance(graph); + final Boolean issueSource_active = graph.getPossibleRelatedValue(unitIssueSource, ISSUE.IssueSource_active, Bindings.BOOLEAN); + + Boolean result = false; + if(unitIssueSource != null) { + result = graph.getPossibleRelatedValue(unitIssueSource, sr.Validations_Units_UnitIssueSource_allowEquivalents, Bindings.BOOLEAN); + } + + final boolean enable = result; + final Button button = getWidget(); + button.getDisplay().asyncExec(new Runnable() { + + @Override + public void run() { + if(button.isDisposed()) return; + + // Is unit validation is enabled, enable the button. + getWidget().setEnabled(unitIssueSource!=null && issueSource_active); + if(Boolean.TRUE.equals(enable)) + button.setSelection(true); + else + button.setSelection(false); + } + }); + } + }); + } catch (DatabaseException e) { + e.printStackTrace(); + } + + unitEquivalents.addSelectionListener(new SelectionListenerImpl(context) { + + @Override + public void apply(WriteGraph graph, Resource model) throws DatabaseException { + SysdynResource sr = SysdynResource.getInstance(graph); + Resource unitIssueSource = graph.syncRequest( + new PossibleObjectWithType(model, + Layer0X.getInstance(graph).Activates, + sr.Validations_Units_UnitIssueSource)); + if(unitIssueSource == null) + return; + + Boolean result = graph.getPossibleRelatedValue(unitIssueSource, sr.Validations_Units_UnitIssueSource_allowEquivalents, Bindings.BOOLEAN); + if(result == null) + result = false; + graph.claimLiteral(unitIssueSource, sr.Validations_Units_UnitIssueSource_allowEquivalents, Boolean.FALSE.equals(result)); + } + }); + } + + public Button getWidget() { + return unitEquivalents.getWidget(); + } + +} diff --git a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/UnitFunction.java b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/UnitFunction.java index 24e3c80c..ff07b394 100644 --- a/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/UnitFunction.java +++ b/org.simantics.sysdyn.ui/src/org/simantics/sysdyn/ui/validation/UnitFunction.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013-2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy - initial API and implementation + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.ui.validation; @@ -36,6 +37,7 @@ import org.simantics.sysdyn.manager.SysdynModelManager; import org.simantics.sysdyn.representation.IndependentVariable; import org.simantics.sysdyn.representation.expressions.IExpression; import org.simantics.sysdyn.representation.utils.UnitUtils; +import org.simantics.sysdyn.utils.ModelUtils; public class UnitFunction { @@ -113,6 +115,7 @@ public class UnitFunction { if(configuration == null) return Collections.emptyList(); + SysdynModel model = ModelUtils.getModel(graph, module); String result = null; ArrayList issues = new ArrayList(); for(Resource outputRelation : graph.getObjects(module, SR.Variable_isTailOf)) { @@ -123,7 +126,7 @@ public class UnitFunction { continue; String left = graph.getPossibleRelatedValue(output, SR.Variable_unit); String right = graph.getPossibleRelatedValue(reference, SR.Variable_unit); - result = UnitUtils.matchUnits(left, right); + result = UnitUtils.matchUnits(left, right, UnitUtils.allowEquivalents(graph, model)); if(result != null) issues.add(new ModuleStandardIssue(SR.Validations_ModuleOutputUnitWarning, module, output, reference)); } @@ -136,7 +139,7 @@ public class UnitFunction { continue; String left = graph.getPossibleRelatedValue(variable, SR.Variable_unit); String right = graph.getPossibleRelatedValue(reference, SR.Variable_unit); - result = UnitUtils.matchUnits(left, right); + result = UnitUtils.matchUnits(left, right, UnitUtils.allowEquivalents(graph, model)); if(result != null) issues.add(new ModuleStandardIssue(SR.Validations_ModuleInputUnitWarning, module, variable, reference)); } @@ -226,7 +229,8 @@ public class UnitFunction { if(right == null || right.isEmpty()) return "No unit defined for " + NameUtils.getSafeName(graph, rightResource); - String result = UnitUtils.matchUnits(left, right); + SysdynModel model = ModelUtils.getModel(graph, leftResource); + String result = UnitUtils.matchUnits(left, right, UnitUtils.allowEquivalents(graph, model)); if(result != null) result = prefix + result + suffix; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/TestParser.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/TestParser.java index f12f8281..95393832 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/TestParser.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/TestParser.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013-2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy - initial API and implementation + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn; @@ -28,24 +29,25 @@ public class TestParser { public static void main(String[] args) { HashMap units = new HashMap(); - - doScenario(scenario1(units), units); - doScenario(scenario2(units), units); - doScenario(scenario3(units), units); - doScenario(scenario4(units), units); - doScenario(scenario5(units), units); - doScenario(scenario6(units), units); - doScenario(scenario7(units), units); - doScenario(scenario8(units), units); - doScenario(scenario9(units), units); - doScenario(scenario10(units), units); - doScenario(scenario11(units), units); - doScenario(scenario12(units), units); - doScenario(scenario13(units), units); + boolean allowEquivalents = true; + + doScenario(scenario1(units), units, allowEquivalents); + doScenario(scenario2(units), units, allowEquivalents); + doScenario(scenario3(units), units, allowEquivalents); + doScenario(scenario4(units), units, allowEquivalents); + doScenario(scenario5(units), units, allowEquivalents); + doScenario(scenario6(units), units, allowEquivalents); + doScenario(scenario7(units), units, allowEquivalents); + doScenario(scenario8(units), units, allowEquivalents); + doScenario(scenario9(units), units, allowEquivalents); + doScenario(scenario10(units), units, allowEquivalents); + doScenario(scenario11(units), units, allowEquivalents); + doScenario(scenario12(units), units, allowEquivalents); + doScenario(scenario13(units), units, allowEquivalents); } - private static void doScenario(String expression, HashMap units) { + private static void doScenario(String expression, HashMap units, boolean allowEquivalents) { System.out.println("-----------------------------------"); System.out.println("Expression: " + expression); System.out.println("Units: "); @@ -58,7 +60,7 @@ public class TestParser { UnitCheckingNode node = (UnitCheckingNode) parser.expr(); try { - UnitResult u = node.getUnits(units); + UnitResult u = node.getUnits(units, allowEquivalents); System.out.println("Result: " + u.getCleanFullUnit()); } catch (UnitCheckingException e) { e.printStackTrace(); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/UnitUtils.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/UnitUtils.java index 758cc8b2..4949b478 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/UnitUtils.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/utils/UnitUtils.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013-2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy - initial API and implementation + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.representation.utils; @@ -16,12 +17,15 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Set; +import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; +import org.simantics.db.common.request.PossibleObjectWithType; import org.simantics.db.exception.DatabaseException; import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; import org.simantics.db.exception.ServiceException; import org.simantics.db.layer0.request.PossibleModel; +import org.simantics.operation.Layer0X; import org.simantics.sysdyn.SysdynResource; import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.representation.Configuration; @@ -48,10 +52,12 @@ public class UnitUtils { HashMap units = UnitUtils.findUnits(graph, model, configuration, components); try { - node.getUnits(units); + node.getUnits(units, allowEquivalents(graph, model)); } catch (UnitCheckingException e) { return e.getMessage(); - } + } catch (DatabaseException e) { + e.printStackTrace(); + } } catch (ParseException e) { e.printStackTrace(); @@ -59,6 +65,30 @@ public class UnitUtils { return null; } + /** + * Determine if the Unit Equivalents checkbox is selected + * @param graph + * @param model SysdynModel of which configuration is queried + * @return true iff equivalent units are allowed + * @throws DatabaseException + */ + public static boolean allowEquivalents(ReadGraph graph, SysdynModel model) throws DatabaseException { + Resource modelResource = graph.syncRequest(new PossibleModel(model.getConfigurationResource())); + SysdynResource sr = SysdynResource.getInstance(graph); + Resource unitIssueSource = graph.syncRequest( + new PossibleObjectWithType(modelResource, + Layer0X.getInstance(graph).Activates, + sr.Validations_Units_UnitIssueSource)); + if(unitIssueSource == null) + return false; + + Boolean result = graph.getPossibleRelatedValue(unitIssueSource, sr.Validations_Units_UnitIssueSource_allowEquivalents, Bindings.BOOLEAN); + if(result == null) + result = false; + + return result; + } + public static String matchUnits(ReadGraph graph, SysdynModel model, Configuration configuration, String unit, String expression) { if(unit == null) return "Unit not defined"; @@ -77,14 +107,17 @@ public class UnitUtils { leftReader.close(); try { - UnitResult rightUnits = right.getUnits(units); - UnitResult leftUnits = left.getUnits(null); + boolean allowEquivalents = allowEquivalents(graph, model); + UnitResult rightUnits = right.getUnits(units, allowEquivalents); + UnitResult leftUnits = left.getUnits(null, allowEquivalents); if(!rightUnits.equals(leftUnits)) return leftUnits.getCleanFullUnit() + " != " + rightUnits.getCleanFullUnit(); } catch (UnitCheckingException e) { return e.getMessage(); - } + } catch (DatabaseException e) { + e.printStackTrace(); + } } catch (ParseException e) { e.printStackTrace(); @@ -180,7 +213,7 @@ public class UnitUtils { } - public static String matchUnits(String left, String right) throws DatabaseException { + public static String matchUnits(String left, String right, boolean allowEquivalents) throws DatabaseException { if(left == null || right == null || left.isEmpty() || right.isEmpty()) return "No unit defined"; @@ -197,8 +230,8 @@ public class UnitUtils { rightReader.close(); try { - UnitResult leftUnits = leftNode.getUnits(null); - UnitResult rightUnits = rightNode.getUnits(null); + UnitResult leftUnits = leftNode.getUnits(null, allowEquivalents); + UnitResult rightUnits = rightNode.getUnits(null, allowEquivalents); if(!rightUnits.equals(leftUnits)) return leftUnits.getCleanFullUnit() + " != " + rightUnits.getCleanFullUnit(); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitCheckingNode.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitCheckingNode.java index 399d04d8..2233c88e 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitCheckingNode.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitCheckingNode.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013-2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy - initial API and implementation + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.unitParser; @@ -22,15 +23,15 @@ public class UnitCheckingNode extends SimpleNode { super(id); } - public UnitResult getUnits(HashMap units) throws UnitCheckingException{ - UnitResult result = new UnitResult(); + public UnitResult getUnits(HashMap units, boolean allowEquivalents) throws UnitCheckingException{ + UnitResult result = new UnitResult(allowEquivalents); if(jjtGetNumChildren() == 0){ String node = printNode(); result.append(node); } else { for(int i = 0; i < jjtGetNumChildren(); i++) { - result.appendResult(((UnitCheckingNode)jjtGetChild(i)).getUnits(units)); + result.appendResult(((UnitCheckingNode)jjtGetChild(i)).getUnits(units, allowEquivalents)); } } return result; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.jjt b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.jjt index 60acd1a6..8227241b 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.jjt +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.jjt @@ -44,7 +44,7 @@ TOKEN: | "*" | "/" | ".*" | "./" | "^" | ".^" | "=" | ":=" -| +| | { matchedToken.image = matchedToken.image.substring(1,matchedToken.image.length()-1); } | diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/AddOp.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/AddOp.java index 9c3374c8..026b8a1c 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/AddOp.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/AddOp.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013-2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy - initial API and implementation + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.unitParser.nodes; @@ -20,6 +21,7 @@ import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType; /** * See UnitCheckingNodeFactory for mapping * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class AddOp extends UnitCheckingNode { @@ -29,8 +31,8 @@ public class AddOp extends UnitCheckingNode { } @Override - public UnitResult getUnits(HashMap units) throws UnitCheckingException { - UnitResult result = super.getUnits(units); + public UnitResult getUnits(HashMap units, boolean allowEquivalents) throws UnitCheckingException { + UnitResult result = super.getUnits(units, allowEquivalents); result.setUnitType(UnitType.OPERATOR); return result; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Arithmetic.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Arithmetic.java index d844bb7c..396bcb1d 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Arithmetic.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Arithmetic.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013-2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy - initial API and implementation + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.unitParser.nodes; @@ -20,6 +21,7 @@ import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType; /** * See UnitCheckingNodeFactory for mapping * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class Arithmetic extends UnitCheckingNode { @@ -30,8 +32,8 @@ public class Arithmetic extends UnitCheckingNode { @Override - public UnitResult getUnits(HashMap units) throws UnitCheckingException { - UnitResult result = new UnitResult(); + public UnitResult getUnits(HashMap units, boolean allowEquivalents) throws UnitCheckingException { + UnitResult result = new UnitResult(allowEquivalents); UnitCheckingNode base = null; UnitCheckingNode operator = null; @@ -39,12 +41,12 @@ public class Arithmetic extends UnitCheckingNode { for(int i = 0; i < jjtGetNumChildren(); i++) { candidateNode = ((UnitCheckingNode)jjtGetChild(i)); - UnitResult candidateUnits = candidateNode.getUnits(units); + UnitResult candidateUnits = candidateNode.getUnits(units, allowEquivalents); if(candidateUnits.getUnitType() == UnitType.OPERATOR) { continue; } else if(base == null || result.getUnitType() == UnitType.SCALAR || result.getUnitType() == UnitType.ANY) { base = ((UnitCheckingNode)jjtGetChild(i)); - result.appendResult(base.getUnits(units)); + result.appendResult(base.getUnits(units, allowEquivalents)); continue; } else if(candidateUnits.getUnitType() == UnitType.SCALAR) { continue; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ArrayDefinition.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ArrayDefinition.java index 4b46e718..7b300b21 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ArrayDefinition.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ArrayDefinition.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013-2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy - initial API and implementation + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.unitParser.nodes; @@ -21,6 +22,7 @@ import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType; /** * See UnitCheckingNodeFactory for mapping * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class ArrayDefinition extends UnitCheckingNode { @@ -31,8 +33,8 @@ public class ArrayDefinition extends UnitCheckingNode { @Override - public UnitResult getUnits(HashMap units) throws UnitCheckingException { - UnitResult result = new UnitResult(); + public UnitResult getUnits(HashMap units, boolean allowEquivalents) throws UnitCheckingException { + UnitResult result = new UnitResult(allowEquivalents); result.setUnitType(UnitType.SCALAR); UnitCheckingNode base = null; @@ -41,13 +43,13 @@ public class ArrayDefinition extends UnitCheckingNode { FunctionArguments functionArguments = (FunctionArguments) jjtGetChild(0); for(UnitCheckingNode candidateNode : gatherExpressions(functionArguments, new ArrayList())) { - UnitResult candidateUnits = candidateNode.getUnits(units); + UnitResult candidateUnits = candidateNode.getUnits(units, allowEquivalents); if(candidateUnits.getUnitType() == UnitType.SCALAR) { continue; } else if(base == null) { base = candidateNode; UnitType oldUnitType = result.getUnitType(); - result.appendResult(base.getUnits(units)); + result.appendResult(base.getUnits(units, allowEquivalents)); /* * Make sure unit type persist diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ComponentReferenceFull.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ComponentReferenceFull.java index f5399282..c110fcb2 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ComponentReferenceFull.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ComponentReferenceFull.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013-2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy - initial API and implementation + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.unitParser.nodes; @@ -23,6 +24,7 @@ import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType; /** * See UnitCheckingNodeFactory for mapping * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class ComponentReferenceFull extends UnitCheckingNode { @@ -31,12 +33,12 @@ public class ComponentReferenceFull extends UnitCheckingNode { super(id); } - protected UnitResult parseUnits(String units) { + protected UnitResult parseUnits(String units, boolean allowEquivalents) { StringReader sr = new StringReader(units); UnitParser parser = new UnitParser(sr); try { UnitCheckingNode node = (UnitCheckingNode) parser.expr(); - return node.getUnits(null); + return node.getUnits(null, allowEquivalents); } catch (ParseException e) { e.printStackTrace(); } catch (UnitCheckingException e) { @@ -46,11 +48,11 @@ public class ComponentReferenceFull extends UnitCheckingNode { } @Override - public UnitResult getUnits(HashMap units) throws UnitCheckingException { + public UnitResult getUnits(HashMap units, boolean allowEquivalents) throws UnitCheckingException { String node = printNode(); if("dmnl".equals(node)) { - UnitResult result = new UnitResult(); + UnitResult result = new UnitResult(allowEquivalents); result.setUnitType(UnitType.DMNL); return result; } @@ -59,10 +61,10 @@ public class ComponentReferenceFull extends UnitCheckingNode { if(!units.containsKey(node) || units.get(node) == null) throw new UnitCheckingException("No units defined for " + node); else { - return parseUnits(units.get(node)); + return parseUnits(units.get(node), allowEquivalents); } } else { - UnitResult result = new UnitResult(); + UnitResult result = new UnitResult(allowEquivalents); result.addDivident(node); result.append(node); return result; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Divide.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Divide.java index 4c2ec48e..2c090ea2 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Divide.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Divide.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013-2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy - initial API and implementation + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.unitParser.nodes; @@ -20,6 +21,7 @@ import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType; /** * See UnitCheckingNodeFactory for mapping * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class Divide extends UnitCheckingNode { @@ -29,8 +31,8 @@ public class Divide extends UnitCheckingNode { } @Override - public UnitResult getUnits(HashMap units) throws UnitCheckingException { - UnitResult result = super.getUnits(units); + public UnitResult getUnits(HashMap units, boolean allowEquivalents) throws UnitCheckingException { + UnitResult result = super.getUnits(units, allowEquivalents); result.setUnitType(UnitType.OPERATOR); return result; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Factor.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Factor.java index 6e5b1fe8..369f67b6 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Factor.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Factor.java @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy - initial API and implementation + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.unitParser.nodes; @@ -31,8 +32,8 @@ public class Factor extends UnitCheckingNode { } @Override - public UnitResult getUnits(HashMap units) throws UnitCheckingException { - UnitResult result = new UnitResult(); + public UnitResult getUnits(HashMap units, boolean allowEquivalents) throws UnitCheckingException { + UnitResult result = new UnitResult(allowEquivalents); UnitCheckingNode current = null; UnitCheckingNode operator = null; @@ -40,10 +41,10 @@ public class Factor extends UnitCheckingNode { for(int i = 0; i < jjtGetNumChildren(); i++) { current = ((UnitCheckingNode)jjtGetChild(i)); - UnitResult currentUnits = current.getUnits(units); + UnitResult currentUnits = current.getUnits(units, allowEquivalents); if(currentUnits.getUnitType() == UnitType.ANY) { - result = new UnitResult(); + result = new UnitResult(allowEquivalents); result.setUnitType(UnitType.ANY); return result; } else if(currentUnits.getUnitType() == UnitType.OPERATOR) { @@ -58,7 +59,7 @@ public class Factor extends UnitCheckingNode { UnitType unitType = currentUnits.getUnitType(); if(unitType == UnitType.SCALAR) { int exponent; - UnitResult baseUnits = base.getUnits(units); + UnitResult baseUnits = base.getUnits(units, allowEquivalents); try { exponent = Integer.valueOf(currentUnits.getFullUnit()); } catch (NumberFormatException e) { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ForIndex.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ForIndex.java index 0d934f12..4a185f7b 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ForIndex.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/ForIndex.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013-2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy - initial API and implementation + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.unitParser.nodes; @@ -19,6 +20,7 @@ import org.simantics.sysdyn.unitParser.UnitCheckingNode; /** * See UnitCheckingNodeFactory for mapping * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class ForIndex extends UnitCheckingNode { @@ -28,8 +30,8 @@ public class ForIndex extends UnitCheckingNode { } @Override - public UnitResult getUnits(HashMap units) throws UnitCheckingException { - return new UnitResult(); + public UnitResult getUnits(HashMap units, boolean allowEquivalents) throws UnitCheckingException { + return new UnitResult(allowEquivalents); } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/FunctionCall.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/FunctionCall.java index e165cc56..a99be2ec 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/FunctionCall.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/FunctionCall.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013-2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy - initial API and implementation + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.unitParser.nodes; @@ -20,6 +21,7 @@ import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType; /** * See UnitCheckingNodeFactory for mapping * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class FunctionCall extends UnitCheckingNode { @@ -29,8 +31,8 @@ public class FunctionCall extends UnitCheckingNode { } @Override - public UnitResult getUnits(HashMap units) throws UnitCheckingException { - UnitResult result = new UnitResult(); + public UnitResult getUnits(HashMap units, boolean allowEquivalents) throws UnitCheckingException { + UnitResult result = new UnitResult(allowEquivalents); result.setUnitType(UnitType.ANY); return result; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/IfThenElse.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/IfThenElse.java index 3529a15e..4234acaa 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/IfThenElse.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/IfThenElse.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013-2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy - initial API and implementation + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.unitParser.nodes; @@ -19,6 +20,7 @@ import org.simantics.sysdyn.unitParser.UnitCheckingNode; /** * See UnitCheckingNodeFactory for mapping * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class IfThenElse extends UnitCheckingNode { @@ -29,14 +31,14 @@ public class IfThenElse extends UnitCheckingNode { @Override - public UnitResult getUnits(HashMap units) throws UnitCheckingException { + public UnitResult getUnits(HashMap units, boolean allowEquivalents) throws UnitCheckingException { UnitResult base = null; UnitCheckingNode baseNode = null; UnitCheckingNode candidateNode = null; for(int i = 0; i < jjtGetNumChildren(); i++) { candidateNode = ((UnitCheckingNode)jjtGetChild(i)); - UnitResult candidateUnits = candidateNode.getUnits(units); + UnitResult candidateUnits = candidateNode.getUnits(units, allowEquivalents); if(!(candidateNode instanceof Condition)) { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Multiplication.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Multiplication.java index 53cdc783..43b13aae 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Multiplication.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Multiplication.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013-2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy - initial API and implementation + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.unitParser.nodes; @@ -20,6 +21,7 @@ import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType; /** * See UnitCheckingNodeFactory for mapping * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class Multiplication extends UnitCheckingNode { @@ -29,8 +31,8 @@ public class Multiplication extends UnitCheckingNode { } @Override - public UnitResult getUnits(HashMap units) throws UnitCheckingException { - UnitResult result = super.getUnits(units); + public UnitResult getUnits(HashMap units, boolean allowEquivalents) throws UnitCheckingException { + UnitResult result = super.getUnits(units, allowEquivalents); result.setUnitType(UnitType.OPERATOR); return result; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Power.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Power.java index 14799461..ab1d9adf 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Power.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Power.java @@ -29,8 +29,8 @@ public class Power extends UnitCheckingNode { } @Override - public UnitResult getUnits(HashMap units) throws UnitCheckingException { - UnitResult result = super.getUnits(units); + public UnitResult getUnits(HashMap units, boolean allowEquivalents) throws UnitCheckingException { + UnitResult result = super.getUnits(units, allowEquivalents); result.setUnitType(UnitType.OPERATOR); return result; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/RelOp.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/RelOp.java index 57098ba1..d7a62ade 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/RelOp.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/RelOp.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013-2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy - initial API and implementation + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.unitParser.nodes; @@ -20,6 +21,7 @@ import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType; /** * See UnitCheckingNodeFactory for mapping * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class RelOp extends UnitCheckingNode { @@ -29,8 +31,8 @@ public class RelOp extends UnitCheckingNode { } @Override - public UnitResult getUnits(HashMap units) throws UnitCheckingException { - UnitResult result = super.getUnits(units); + public UnitResult getUnits(HashMap units, boolean allowEquivalents) throws UnitCheckingException { + UnitResult result = super.getUnits(units, allowEquivalents); result.setUnitType(UnitType.OPERATOR); return result; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Relation.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Relation.java index 6d8711c1..2fd22f3b 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Relation.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Relation.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013-2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy - initial API and implementation + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.unitParser.nodes; @@ -19,6 +20,7 @@ import org.simantics.sysdyn.unitParser.UnitCheckingNode; /** * See UnitCheckingNodeFactory for mapping * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class Relation extends UnitCheckingNode { @@ -28,8 +30,8 @@ public class Relation extends UnitCheckingNode { } @Override - public UnitResult getUnits(HashMap units) throws UnitCheckingException { - UnitResult result = new UnitResult(); + public UnitResult getUnits(HashMap units, boolean allowEquivalents) throws UnitCheckingException { + UnitResult result = new UnitResult(allowEquivalents); UnitCheckingNode base = null; UnitCheckingNode operator = null; @@ -40,14 +42,14 @@ public class Relation extends UnitCheckingNode { base = ((UnitCheckingNode)jjtGetChild(1)); if(base instanceof Arithmetic) { - result.appendResult(base.getUnits(units)); + result.appendResult(base.getUnits(units, allowEquivalents)); for(int i = 2; i < jjtGetNumChildren(); i = i + 2) { candidateNode = ((UnitCheckingNode)jjtGetChild(i)); if(!(candidateNode instanceof Value)) { operator = ((UnitCheckingNode)jjtGetChild(i-1)); - UnitResult candidateUnits = candidateNode.getUnits(units); + UnitResult candidateUnits = candidateNode.getUnits(units, allowEquivalents); if(!result.equals(candidateUnits)) { result.equals(candidateUnits); throw new UnitCheckingException("Not equals exception: " + diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Term.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Term.java index 77f9c409..344cf7eb 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Term.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Term.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013-2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy - initial API and implementation + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.unitParser.nodes; @@ -21,6 +22,7 @@ import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType; /** * See UnitCheckingNodeFactory for mapping * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class Term extends UnitCheckingNode { @@ -30,8 +32,8 @@ public class Term extends UnitCheckingNode { } @Override - public UnitResult getUnits(HashMap units) throws UnitCheckingException { - UnitResult result = new UnitResult(); + public UnitResult getUnits(HashMap units, boolean allowEquivalents) throws UnitCheckingException { + UnitResult result = new UnitResult(allowEquivalents); UnitCheckingNode current = null; UnitCheckingNode operator = null; @@ -39,10 +41,10 @@ public class Term extends UnitCheckingNode { for(int i = 0; i < jjtGetNumChildren(); i++) { current = ((UnitCheckingNode)jjtGetChild(i)); - UnitResult currentUnits = current.getUnits(units); + UnitResult currentUnits = current.getUnits(units, allowEquivalents); if(currentUnits.getUnitType() == UnitType.ANY) { - result = new UnitResult(); + result = new UnitResult(allowEquivalents); result.setUnitType(UnitType.ANY); return result; } else if(currentUnits.getUnitType() == UnitType.OPERATOR) { diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/UnitResult.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/UnitResult.java index 72fcb73d..74cf4dd9 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/UnitResult.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/UnitResult.java @@ -28,6 +28,7 @@ import java.util.Iterator; * clean representation of the unit. ( e.g. m * s / m -> s ) * * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class UnitResult { @@ -39,7 +40,47 @@ public class UnitResult { private StringBuilder fullUnit = new StringBuilder(); private UnitType unitType; - public UnitResult() { + // Equivalent forms of units: + private final Equivalent[] EQUIVALENTS = { + new Equivalent("$","$s","dollar","dollars","usd"), + new Equivalent("\u20ac","\u20acs","eur","euro","euros","e","ecu"), + new Equivalent("£","£s","pound","pounds","gbp"), + new Equivalent("Unit","Units"), + new Equivalent("Person","People","Persons"), + new Equivalent("second","seconds","sec","s"), + new Equivalent("minute","minutes","min"), + new Equivalent("hour","hours","h","hr"), + new Equivalent("day","days","d"), + new Equivalent("month","months","mon","mth","mo","mos"), + new Equivalent("year","years","a","y","yr") + }; + private boolean allowEquivalents; + + private class Equivalent { + public ArrayList forms; + + Equivalent(String ... forms) { + this.forms = new ArrayList(); + for (String f : forms) + this.forms.add(f); + } + + public boolean contains(String form) { + for (String f : forms) + if (f.equalsIgnoreCase(form)) + return true; + return false; + } + + public String getDefault() { + if (forms.size() > 0) + return forms.get(0); + else return null; + } + } + + public UnitResult(boolean allowEquivalents) { + this.allowEquivalents = allowEquivalents; setUnitType(UnitType.NORMAL); } @@ -159,6 +200,22 @@ public class UnitResult { if(copyDividers1.size() != copyDividers2.size()) return false; + if (allowEquivalents) { + // Replace and sort all once again. + for(int i = 0; i < copyDividents1.size(); i++) { + copyDividents1.set(i, getDefaultForm(copyDividents1.get(i))); + copyDividents2.set(i, getDefaultForm(copyDividents2.get(i))); + Collections.sort(copyDividents1); + Collections.sort(copyDividents2); + } + for(int i = 0; i < copyDividers1.size(); i++) { + copyDividers1.set(i, getDefaultForm(copyDividers1.get(i))); + copyDividers2.set(i, getDefaultForm(copyDividers2.get(i))); + Collections.sort(copyDividers1); + Collections.sort(copyDividers2); + } + } + for(int i = 0; i < copyDividents1.size(); i++) { String a = copyDividents1.get(i); String b = copyDividents2.get(i); @@ -166,7 +223,6 @@ public class UnitResult { return false; } } - for(int i = 0; i < copyDividers1.size(); i++) { String a = copyDividers1.get(i); String b = copyDividers2.get(i); @@ -178,7 +234,19 @@ public class UnitResult { return true; } - public void reduceUnitLists(ArrayList originalDividents, ArrayList copyDividents, ArrayList copyDividers) { + /** + * Get the default form of a unit (e.g. dollars -> $). + * @param unit + * @return default form of unit + */ + private String getDefaultForm(String unit) { + for (Equivalent e : EQUIVALENTS) + if (e.contains(unit)) + return e.getDefault(); + return unit; + } + + public void reduceUnitLists(ArrayList originalDividents, ArrayList copyDividents, ArrayList copyDividers) { for(String s : originalDividents) { if(copyDividers.contains(s)) { copyDividers.remove(s); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Value.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Value.java index 1ba0fc00..7e26c569 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Value.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/nodes/Value.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2013 Association for Decentralized Information Management in + * Copyright (c) 2013-2014 Association for Decentralized Information Management in * Industry THTH ry. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 @@ -8,6 +8,7 @@ * * Contributors: * Semantum Oy - initial API and implementation + * VTT Technical Research Centre of Finland *******************************************************************************/ package org.simantics.sysdyn.unitParser.nodes; @@ -20,6 +21,7 @@ import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType; /** * See UnitCheckingNodeFactory for mapping * @author Teemu Lempinen + * @author Tuomas Miettinen * */ public class Value extends UnitCheckingNode { @@ -29,8 +31,8 @@ public class Value extends UnitCheckingNode { } @Override - public UnitResult getUnits(HashMap units) throws UnitCheckingException { - UnitResult result = super.getUnits(units); + public UnitResult getUnits(HashMap units, boolean allowEquivalents) throws UnitCheckingException { + UnitResult result = super.getUnits(units, allowEquivalents); result.setUnitType(UnitType.SCALAR); return result; }