From: lempinen Date: Tue, 30 Apr 2013 07:08:37 +0000 (+0000) Subject: Unit validation for functions (UnitType.Any) and array definitions. (refs #4263) X-Git-Tag: 1.8.1~316 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=eee540a1e85d4accff2a49ee08c7940a2eb4f00f;p=simantics%2Fsysdyn.git Unit validation for functions (UnitType.Any) and array definitions. (refs #4263) git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@27300 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/org.simantics.sysdyn.ontology/graph.tg b/org.simantics.sysdyn.ontology/graph.tg index 44425342..9140f080 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/Sysdyn.pgraph b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph index f04d001f..50113eb9 100644 --- a/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph +++ b/org.simantics.sysdyn.ontology/graph/Sysdyn.pgraph @@ -45,10 +45,12 @@ SYSDYN.SysdynModel -- SYSDYN.SysdynModel.solver --> L0.String -- SYSDYN.SysdynModel.variableFilter --> L0.String -- SYSDYN.SysdynModel.fmuFile --> L0.ByteArray -- SYSDYN.SysdynModel.timeUnit --> L0.String -- SYSDYN.SysdynModel.exeFile --> L0.ByteArray >() { + + @Override + public Map perform(ReadGraph graph, final Resource input) throws DatabaseException { + + Map map = new HashMap(); + map.put("year", "year"); + map.put("month", "month"); + map.put("day", "day"); + map.put("hour", "hour"); + map.put("min", "min"); + map.put("s", "s"); + return map; + } + }); + + timeUnit.setSelectionFactory(new ReadFactoryImpl() { + + @Override + public String perform(ReadGraph graph, final Resource input) throws DatabaseException { + String s = graph.getPossibleRelatedValue(input, SysdynResource.getInstance(graph).SysdynModel_timeUnit); + return s != null ? s : "month"; + } + }); + + timeUnit.addModifyListener(new ComboStringPropertyModifier() { + + @Override + public void applyText(WriteGraph graph, Resource input, String text) + throws DatabaseException { + graph.claimLiteral(input, SysdynResource.getInstance(graph).SysdynModel_timeUnit, text); + } + }); // Scrolled composite settings 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 de8b9eee..f8e5839b 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 @@ -78,7 +78,7 @@ public class UnitFunction { Object expr = sm.getMapping().get(expression); if(expr != null && var != null && expr instanceof IExpression && var instanceof IndependentVariable) { - String result = ((IExpression)expr).matchUnits(graph, sm.getMapping()); + String result = ((IExpression)expr).validateUnits(graph, sm); if(result != null) { Issue issue = new StandardIssue(SR.Validations_UnitWarning, independentVariable, expression, unitResource); issues.add(issue); @@ -117,7 +117,7 @@ public class UnitFunction { } Object expr = sm.getMapping().get(contexts.get(1)); - String result = ((IExpression)expr).matchUnits(graph, sm.getMapping()); + String result = ((IExpression)expr).validateUnits(graph, sm); return result; } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/DelayExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/DelayExpression.java index 6a296c80..59121d64 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/DelayExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/DelayExpression.java @@ -11,13 +11,16 @@ *******************************************************************************/ package org.simantics.sysdyn.representation.expressions; +import org.simantics.db.ReadGraph; import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedValue; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.representation.IndependentVariable; import org.simantics.sysdyn.representation.Variability; import org.simantics.sysdyn.representation.utils.FormatUtils; import org.simantics.sysdyn.representation.utils.IndexUtils; +import org.simantics.sysdyn.representation.utils.UnitUtils; /** * Representation of a delay expression. The order of the @@ -252,5 +255,19 @@ public class DelayExpression extends Expression { return "This + is + not + a + parameter + at + any + time"; } + + @Override + public String validateUnits(ReadGraph graph, SysdynModel model) { + if(parent.getUnit() == null) + return "Unit not defined for " + parent.getName(); + + String result = UnitUtils.matchUnits(graph, model, parent.getParentConfiguration(), parent.getUnit(), equation); + if(result == null) + result = UnitUtils.matchUnits(graph, model, parent.getParentConfiguration(), parent.getUnit(), initialValue); + if(result == null) + result = UnitUtils.matchUnits(graph, model, parent.getParentConfiguration(), UnitUtils.getTimeUnit(graph, model), delayTime); + + return result; + } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java index 9f5f28f0..9a326e47 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/Expression.java @@ -13,10 +13,10 @@ package org.simantics.sysdyn.representation.expressions; import org.simantics.db.ReadGraph; import org.simantics.layer0.Layer0; -import org.simantics.objmap.IMapping; import org.simantics.objmap.annotations.RelatedElement; import org.simantics.objmap.annotations.RelatedValue; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.representation.IndependentVariable; import org.simantics.sysdyn.representation.utils.UnitUtils; @@ -63,12 +63,12 @@ public abstract class Expression implements IExpression { } @Override - public String matchUnits() { - return UnitUtils.matchUnits(null, null, parent, getExpression()); + public String validateUnits() { + return validateUnits(null, null); } @Override - public String matchUnits(ReadGraph graph, IMapping mapping) { - return UnitUtils.matchUnits(graph, mapping, parent, getExpression()); + public String validateUnits(ReadGraph graph, SysdynModel model) { + return UnitUtils.matchUnits(graph, model, parent.getParentConfiguration(), parent.getUnit(), getExpression()); } } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/IExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/IExpression.java index 48ecdaa4..78f21bac 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/IExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/IExpression.java @@ -12,7 +12,7 @@ package org.simantics.sysdyn.representation.expressions; import org.simantics.db.ReadGraph; -import org.simantics.objmap.IMapping; +import org.simantics.sysdyn.manager.SysdynModel; /** @@ -63,15 +63,15 @@ public interface IExpression { * Match the units of this expression to units of its variable * @return null if match, Error message otherwise */ - String matchUnits(); + String validateUnits(); /** * Match the units of this expression to units of its variable. * * Requests units of referred variables for request listening * @param graph ReadGraph - * @param mapping ObjMap mapping + * @param model SysdynModel (with mapping) * @return null if match, ERror message otherwise */ - String matchUnits(ReadGraph graph, IMapping mapping); + String validateUnits(ReadGraph graph, SysdynModel model); } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/WithLookupExpression.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/WithLookupExpression.java index c98ee79a..2343eb8c 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/WithLookupExpression.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/representation/expressions/WithLookupExpression.java @@ -11,12 +11,15 @@ *******************************************************************************/ package org.simantics.sysdyn.representation.expressions; +import org.simantics.db.ReadGraph; import org.simantics.objmap.annotations.GraphType; import org.simantics.objmap.annotations.RelatedValue; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.representation.utils.FormatUtils; import org.simantics.sysdyn.representation.utils.IndexUtils; import org.simantics.sysdyn.representation.utils.SheetFormatUtils; +import org.simantics.sysdyn.representation.utils.UnitUtils; /** * Representation of a withlookup expression @@ -46,4 +49,16 @@ public class WithLookupExpression extends Expression { return "interpolate(" + equation + ", " + lookupTable + ")"; } + @Override + public String validateUnits(ReadGraph graph, SysdynModel model) { + if(parent.getUnit() == null) + return "Unit not defined for " + parent.getName(); + + String result = UnitUtils.expressionUnitsValid(graph, model, parent.getParentConfiguration(), equation); + if(result == null) { + result = UnitUtils.expressionUnitsValid(graph, model, parent.getParentConfiguration(), equation); + } + + return result; + } } 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 85365768..eca1fe34 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 @@ -20,8 +20,9 @@ import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.ManyObjectsForFunctionalRelationException; import org.simantics.db.exception.ServiceException; -import org.simantics.objmap.IMapping; +import org.simantics.simulation.ontology.SimulationResource; import org.simantics.sysdyn.SysdynResource; +import org.simantics.sysdyn.manager.SysdynModel; import org.simantics.sysdyn.representation.Configuration; import org.simantics.sysdyn.representation.IElement; import org.simantics.sysdyn.representation.Module; @@ -35,9 +36,30 @@ import org.simantics.sysdyn.unitParser.nodes.UnitResult; public class UnitUtils { - public static String matchUnits(ReadGraph graph, IMapping mapping, Variable variable, String expression) { - if(variable.getUnit() == null) - return "Unit not defined for " + variable.getName(); + public static String expressionUnitsValid(ReadGraph graph, SysdynModel model, Configuration configuration, String expression) { + try { + StringReader reader = new StringReader(expression); + UnitParser parser = new UnitParser(reader); + UnitCheckingNode node = (UnitCheckingNode) parser.expr(); + reader.close(); + Set components = UnitUtils.findComponents(node); + HashMap units = UnitUtils.findUnits(graph, model, configuration, components); + + try { + node.getUnits(units); + } catch (UnitCheckingException e) { + return e.getMessage(); + } + + } catch (ParseException e) { + e.printStackTrace(); + } + return null; + } + + public static String matchUnits(ReadGraph graph, SysdynModel model, Configuration configuration, String unit, String expression) { + if(unit == null) + return "Unit not defined"; try { StringReader rightReader = new StringReader(expression); @@ -45,9 +67,9 @@ public class UnitUtils { UnitCheckingNode right = (UnitCheckingNode) rightParser.expr(); rightReader.close(); Set components = findComponents(right); - HashMap units = findUnits(graph, mapping, variable, components); + HashMap units = findUnits(graph, model, configuration, components); - StringReader leftReader = new StringReader(variable.getUnit()); + StringReader leftReader = new StringReader(unit); UnitParser leftParser = new UnitParser(leftReader); UnitCheckingNode left = (UnitCheckingNode) leftParser.expr(); leftReader.close(); @@ -62,7 +84,6 @@ public class UnitUtils { return e.getMessage(); } -// node.dump(""); } catch (ParseException e) { e.printStackTrace(); } @@ -71,16 +92,15 @@ public class UnitUtils { } - private static HashMap findUnits(ReadGraph graph, IMapping mapping, Variable variable, Set components) { + public static HashMap findUnits(ReadGraph graph, SysdynModel model, Configuration configuration, Set components) { HashMap units = new HashMap(); - Configuration configuration = variable.getParentConfiguration(); for(String component : components) { Variable var = getElement(configuration, component); if(var != null) { // Support listening, if graph and mapping exists - if(graph != null && mapping != null) { - Resource varResource = mapping.inverseGet(var); + if(graph != null && model != null) { + Resource varResource = model.getMapping().inverseGet(var); if(varResource != null) { try { graph.getPossibleRelatedValue(varResource, SysdynResource.getInstance(graph).Variable_unit); @@ -99,11 +119,30 @@ public class UnitUtils { } } - + units.put("time", getTimeUnit(graph, model)); return units; } + public static String getTimeUnit(ReadGraph graph, SysdynModel model) { + try { + Resource modelResource = graph.getPossibleObject(model.getConfigurationResource(), SimulationResource.getInstance(graph).IsConfigurationOf); + if(modelResource != null) { + String timeUnit = graph.getPossibleRelatedValue(modelResource, SysdynResource.getInstance(graph).SysdynModel_timeUnit); + if(timeUnit == null) + timeUnit = "month"; + return timeUnit; + } + + } catch (ManyObjectsForFunctionalRelationException e) { + e.printStackTrace(); + } catch (ServiceException e) { + e.printStackTrace(); + } + + return null; + } + private static Variable getElement(Configuration configuration, String name) { String[] elements = name.split("\\."); String element = elements[0]; @@ -122,7 +161,7 @@ public class UnitUtils { return null; } - private static Set findComponents(UnitCheckingNode node) { + public static Set findComponents(UnitCheckingNode node) { HashSet components = new HashSet(); addComponents(node, components); return components; 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 e80e60f6..399d04d8 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitCheckingNode.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitCheckingNode.java @@ -11,7 +11,6 @@ *******************************************************************************/ package org.simantics.sysdyn.unitParser; -import java.util.ArrayList; import java.util.HashMap; import org.simantics.sysdyn.unitParser.nodes.UnitResult; @@ -30,22 +29,13 @@ public class UnitCheckingNode extends SimpleNode { String node = printNode(); result.append(node); } else { - for(UnitCheckingNode node : getChildren()) { - result.appendResult(node.getUnits(units)); + for(int i = 0; i < jjtGetNumChildren(); i++) { + result.appendResult(((UnitCheckingNode)jjtGetChild(i)).getUnits(units)); } } return result; } - protected ArrayList getChildren() { - ArrayList children = new ArrayList(); - for(int i = 0; i < jjtGetNumChildren(); i++) { - children.add((UnitCheckingNode)jjtGetChild(i)); - } - return children; - } - - public String printNode() { StringBuilder sb = new StringBuilder(); Token token = jjtGetFirstToken(); diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitCheckingNodeFactory.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitCheckingNodeFactory.java index 434b982b..e3db8e8f 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitCheckingNodeFactory.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitCheckingNodeFactory.java @@ -16,12 +16,16 @@ import java.util.HashMap; import org.simantics.sysdyn.unitParser.nodes.AddOp; import org.simantics.sysdyn.unitParser.nodes.Arithmetic; +import org.simantics.sysdyn.unitParser.nodes.ArrayDefinition; import org.simantics.sysdyn.unitParser.nodes.ComponentIdentity; import org.simantics.sysdyn.unitParser.nodes.ComponentReference; import org.simantics.sysdyn.unitParser.nodes.ComponentReferenceFull; import org.simantics.sysdyn.unitParser.nodes.Condition; import org.simantics.sysdyn.unitParser.nodes.Divide; +import org.simantics.sysdyn.unitParser.nodes.Expression; import org.simantics.sysdyn.unitParser.nodes.ForIndex; +import org.simantics.sysdyn.unitParser.nodes.FunctionArguments; +import org.simantics.sysdyn.unitParser.nodes.FunctionCall; import org.simantics.sysdyn.unitParser.nodes.IfThenElse; import org.simantics.sysdyn.unitParser.nodes.Multiplication; import org.simantics.sysdyn.unitParser.nodes.RelOp; @@ -38,6 +42,7 @@ public class UnitCheckingNodeFactory { constructors.put("term", Term.class); constructors.put("arithmetic_expression", Arithmetic.class); constructors.put("ifthenelse", IfThenElse.class); + constructors.put("array_definition", ArrayDefinition.class); constructors.put("component_reference_full", ComponentReferenceFull.class); constructors.put("value", Value.class); @@ -54,6 +59,10 @@ public class UnitCheckingNodeFactory { constructors.put("component_identity", ComponentIdentity.class); constructors.put("condition", Condition.class); constructors.put("for_index", ForIndex.class); + constructors.put("function_call", FunctionCall.class); + constructors.put("function_arguments", FunctionArguments.class); + constructors.put("expression", Expression.class); + } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.java index 1579b6cd..eff84e43 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.java @@ -789,9 +789,7 @@ public class UnitParser/*@bgen(jjtree)*/implements UnitParserTreeConstants, Unit jj_consume_token(65); break; case 62: - jj_consume_token(62); - function_arguments(); - jj_consume_token(63); + array_definition(); break; case 35: jj_consume_token(35); @@ -855,6 +853,38 @@ public class UnitParser/*@bgen(jjtree)*/implements UnitParserTreeConstants, Unit } } + final public void array_definition() throws ParseException { + /*@bgen(jjtree) array_definition */ + UnitCheckingNode jjtn000 = (UnitCheckingNode)UnitCheckingNodeFactory.jjtCreate(JJTARRAY_DEFINITION); + boolean jjtc000 = true; + jjtree.openNodeScope(jjtn000); + jjtn000.jjtSetFirstToken(getToken(1)); + try { + jj_consume_token(62); + function_arguments(); + jj_consume_token(63); + } catch (Throwable jjte000) { + if (jjtc000) { + jjtree.clearNodeScope(jjtn000); + jjtc000 = false; + } else { + jjtree.popNode(); + } + if (jjte000 instanceof RuntimeException) { + {if (true) throw (RuntimeException)jjte000;} + } + if (jjte000 instanceof ParseException) { + {if (true) throw (ParseException)jjte000;} + } + {if (true) throw (Error)jjte000;} + } finally { + if (jjtc000) { + jjtree.closeNodeScope(jjtn000, true); + jjtn000.jjtSetLastToken(getToken(0)); + } + } + } + final public void function_call() throws ParseException { /*@bgen(jjtree) function_call */ UnitCheckingNode jjtn000 = (UnitCheckingNode)UnitCheckingNodeFactory.jjtCreate(JJTFUNCTION_CALL); @@ -1539,8 +1569,7 @@ void named_arguments() : { default: jj_la1[40] = jj_gen; if (jj_2_3(2147483647)) { - name(); - function_call_args(); + function_call(); } else { switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case IDENT: @@ -1633,19 +1662,6 @@ void named_arguments() : { finally { jj_save(2, xla); } } - private boolean jj_3_2() { - if (jj_3R_13()) return true; - return false; - } - - private boolean jj_3R_11() { - if (jj_scan_token(IDENT)) return true; - Token xsp; - xsp = jj_scanpos; - if (jj_3R_14()) jj_scanpos = xsp; - return false; - } - private boolean jj_3R_14() { if (jj_scan_token(66)) return true; if (jj_3R_11()) return true; @@ -1669,6 +1685,19 @@ void named_arguments() : { return false; } + private boolean jj_3_2() { + if (jj_3R_13()) return true; + return false; + } + + private boolean jj_3R_11() { + if (jj_scan_token(IDENT)) return true; + Token xsp; + xsp = jj_scanpos; + if (jj_3R_14()) jj_scanpos = xsp; + return false; + } + private boolean jj_3_1() { if (jj_3R_11()) return true; if (jj_3R_12()) return true; diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.jj b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.jj index c5d0883e..789c7474 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.jj +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.jj @@ -573,7 +573,7 @@ void primary() : {/*@bgen(jjtree) primary */ /*| "(" output_expression_list() ")"*/ // Not needed, replaced with following: | parenthesis_expression() | "[" expression_list() ( ";" expression_list() )* "]" - | "{" function_arguments() "}" + | array_definition() | "end"/*@bgen(jjtree)*/ } catch (Throwable jjte000) { if (jjtc000) { @@ -631,6 +631,39 @@ void component_reference_full() : {/*@bgen(jjtree) component_reference_full */ /*@egen*/ } +void array_definition() : {/*@bgen(jjtree) array_definition */ + UnitCheckingNode jjtn000 = (UnitCheckingNode)UnitCheckingNodeFactory.jjtCreate(JJTARRAY_DEFINITION); + boolean jjtc000 = true; + jjtree.openNodeScope(jjtn000); + jjtn000.jjtSetFirstToken(getToken(1)); +/*@egen*/ +} {/*@bgen(jjtree) array_definition */ + try { +/*@egen*/ + "{" function_arguments() "}"/*@bgen(jjtree)*/ + } catch (Throwable jjte000) { + if (jjtc000) { + jjtree.clearNodeScope(jjtn000); + jjtc000 = false; + } else { + jjtree.popNode(); + } + if (jjte000 instanceof RuntimeException) { + throw (RuntimeException)jjte000; + } + if (jjte000 instanceof ParseException) { + throw (ParseException)jjte000; + } + throw (Error)jjte000; + } finally { + if (jjtc000) { + jjtree.closeNodeScope(jjtn000, true); + jjtn000.jjtSetLastToken(getToken(0)); + } + } +/*@egen*/ +} + void function_call() : {/*@bgen(jjtree) function_call */ UnitCheckingNode jjtn000 = (UnitCheckingNode)UnitCheckingNodeFactory.jjtCreate(JJTFUNCTION_CALL); boolean jjtc000 = true; @@ -1127,7 +1160,7 @@ void subscript() : {/*@bgen(jjtree) subscript */ } {/*@bgen(jjtree) subscript */ try { /*@egen*/ ":" - | LOOKAHEAD( name() parenthesis_open() ) name() function_call_args() + | LOOKAHEAD( name() parenthesis_open() ) function_call() | rangeIndex() ( ":" rangeIndex())?/*@bgen(jjtree)*/ } catch (Throwable jjte000) { if (jjtc000) { 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 e7f87938..9e8521e1 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.jjt +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParser.jjt @@ -158,7 +158,7 @@ void primary() : { /*| "(" output_expression_list() ")"*/ // Not needed, replaced with following: | parenthesis_expression() | "[" expression_list() ( ";" expression_list() )* "]" - | "{" function_arguments() "}" + | array_definition() | "end" } @@ -167,6 +167,11 @@ void component_reference_full() : { component_reference() } +void array_definition() : { +} { + "{" function_arguments() "}" +} + void function_call() : { } { name() function_call_args() @@ -264,7 +269,7 @@ void array_subscripts() : { void subscript() : { } { ":" - | LOOKAHEAD( name() parenthesis_open() ) name() function_call_args() + | LOOKAHEAD( name() parenthesis_open() ) function_call() | rangeIndex() ( ":" rangeIndex())? } diff --git a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParserTreeConstants.java b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParserTreeConstants.java index d790ceb2..62b026d4 100644 --- a/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParserTreeConstants.java +++ b/org.simantics.sysdyn/src/org/simantics/sysdyn/unitParser/UnitParserTreeConstants.java @@ -21,24 +21,25 @@ public interface UnitParserTreeConstants public int JJTDIVIDE = 15; public int JJTPRIMARY = 16; public int JJTCOMPONENT_REFERENCE_FULL = 17; - public int JJTFUNCTION_CALL = 18; - public int JJTPARENTHESIS_EXPRESSION = 19; - public int JJTVALUE = 20; - public int JJTPARENTHESIS_OPEN = 21; - public int JJTPARENTHESIS_CLOSE = 22; - public int JJTNAME = 23; - public int JJTCOMPONENT_REFERENCE = 24; - public int JJTCOMPONENT_IDENTITY = 25; - public int JJTFUNCTION_CALL_ARGS = 26; - public int JJTFUNCTION_ARGUMENTS = 27; - public int JJTFOR_INDICES = 28; - public int JJTFOR_INDEX = 29; - public int JJTNAMED_ARGUMENT = 30; - public int JJTOUTPUT_EXPRESSION_LIST = 31; - public int JJTEXPRESSION_LIST = 32; - public int JJTARRAY_SUBSCRIPTS = 33; - public int JJTSUBSCRIPT = 34; - public int JJTRANGEINDEX = 35; + public int JJTARRAY_DEFINITION = 18; + public int JJTFUNCTION_CALL = 19; + public int JJTPARENTHESIS_EXPRESSION = 20; + public int JJTVALUE = 21; + public int JJTPARENTHESIS_OPEN = 22; + public int JJTPARENTHESIS_CLOSE = 23; + public int JJTNAME = 24; + public int JJTCOMPONENT_REFERENCE = 25; + public int JJTCOMPONENT_IDENTITY = 26; + public int JJTFUNCTION_CALL_ARGS = 27; + public int JJTFUNCTION_ARGUMENTS = 28; + public int JJTFOR_INDICES = 29; + public int JJTFOR_INDEX = 30; + public int JJTNAMED_ARGUMENT = 31; + public int JJTOUTPUT_EXPRESSION_LIST = 32; + public int JJTEXPRESSION_LIST = 33; + public int JJTARRAY_SUBSCRIPTS = 34; + public int JJTSUBSCRIPT = 35; + public int JJTRANGEINDEX = 36; public String[] jjtNodeName = { @@ -60,6 +61,7 @@ public interface UnitParserTreeConstants "divide", "primary", "component_reference_full", + "array_definition", "function_call", "parenthesis_expression", "value", @@ -80,4 +82,4 @@ public interface UnitParserTreeConstants "rangeIndex", }; } -/* JavaCC - OriginalChecksum=e2f99f7df8969b486d8da5159d18ad81 (do not edit this line) */ +/* JavaCC - OriginalChecksum=9313cac9e0951f8836c120d51f6c620b (do not edit this line) */ 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 a24ef52d..d844bb7c 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 @@ -40,13 +40,14 @@ public class Arithmetic extends UnitCheckingNode { for(int i = 0; i < jjtGetNumChildren(); i++) { candidateNode = ((UnitCheckingNode)jjtGetChild(i)); UnitResult candidateUnits = candidateNode.getUnits(units); - if(candidateUnits.getUnitType() == UnitType.OPERATOR || candidateUnits.getUnitType() == UnitType.ANY) { - result.setUnitType(candidateUnits.getUnitType()); + if(candidateUnits.getUnitType() == UnitType.OPERATOR) { continue; - } else if(base == null) { + } else if(base == null || result.getUnitType() == UnitType.SCALAR || result.getUnitType() == UnitType.ANY) { base = ((UnitCheckingNode)jjtGetChild(i)); result.appendResult(base.getUnits(units)); continue; + } else if(candidateUnits.getUnitType() == UnitType.SCALAR) { + continue; } else { operator = ((UnitCheckingNode)jjtGetChild(i-1)); if(!result.equals(candidateUnits)) { @@ -57,7 +58,7 @@ public class Arithmetic extends UnitCheckingNode { } } } - +// System.out.println("Arithmetic: " + printNode() + " [" + result.getFullUnit() + " : " + result.getUnitType() + "]"); return result; } 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 d771dc3d..0f8ec704 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 @@ -18,6 +18,7 @@ import org.simantics.sysdyn.unitParser.ParseException; import org.simantics.sysdyn.unitParser.UnitCheckingException; import org.simantics.sysdyn.unitParser.UnitCheckingNode; import org.simantics.sysdyn.unitParser.UnitParser; +import org.simantics.sysdyn.unitParser.nodes.UnitResult.UnitType; /** * See UnitCheckingNodeFactory for mapping @@ -47,7 +48,13 @@ public class ComponentReferenceFull extends UnitCheckingNode { @Override public UnitResult getUnits(HashMap units) throws UnitCheckingException { String node = printNode(); - node = printNode(); + + if("dmnl".equals(node)) { + UnitResult result = new UnitResult(); + result.setUnitType(UnitType.DMNL); + return result; + } + if(units != null) { if(!units.containsKey(node)) throw new UnitCheckingException("No units defined for " + node); 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 77408c00..33a37fd3 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 @@ -40,7 +40,11 @@ public class Term extends UnitCheckingNode { current = ((UnitCheckingNode)jjtGetChild(i)); UnitResult currentUnits = current.getUnits(units); - if(currentUnits.getUnitType() == UnitType.OPERATOR) { + if(currentUnits.getUnitType() == UnitType.ANY) { + result = new UnitResult(); + result.setUnitType(UnitType.ANY); + return result; + } else if(currentUnits.getUnitType() == UnitType.OPERATOR) { continue; } else if(base == null) { base = current; @@ -50,24 +54,26 @@ public class Term extends UnitCheckingNode { operator = ((UnitCheckingNode)jjtGetChild(i-1)); if(operator instanceof Multiplication) { - if(currentUnits.getUnitType() != UnitType.ANY) { + UnitType unitType = currentUnits.getUnitType(); + if(unitType != UnitType.SCALAR && unitType != UnitType.DMNL) { result.append(operator.printNode()); result.appendResult(currentUnits); } - } else if(operator instanceof Divide){ + } else if(operator instanceof Divide) { result.append(operator.printNode()); result.addAllDividers(currentUnits.getDividers()); result.addAllDividers(currentUnits.getDividents()); - result.setUnitType(currentUnits.getUnitType()); - if(currentUnits.getUnitType() == UnitType.ANY) + UnitType unitType = currentUnits.getUnitType(); + result.setUnitType(unitType); + if(unitType == UnitType.SCALAR || unitType == UnitType.DMNL) result.append("1"); else result.append(currentUnits.getFullUnit()); } } } - +// System.out.println("Term: " + printNode() + " [" + result.getFullUnit() + " : " + result.getUnitType() + "]"); return result; } 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 27851346..72fcb73d 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 @@ -32,12 +32,16 @@ import java.util.Iterator; */ public class UnitResult { - public enum UnitType {NORMAL, ANY, DMNL, OPERATOR}; + public enum UnitType {NORMAL, ANY, DMNL, OPERATOR, SCALAR}; private ArrayList dividers = new ArrayList(); private ArrayList dividents = new ArrayList(); private StringBuilder fullUnit = new StringBuilder(); - private UnitType unitType = UnitType.NORMAL; + private UnitType unitType; + + public UnitResult() { + setUnitType(UnitType.NORMAL); + } public void appendResult(UnitResult result) { addAllDividents(result.getDividents()); @@ -139,6 +143,9 @@ public class UnitResult { if(getUnitType() == UnitType.ANY || other.getUnitType() == UnitType.ANY) return true; + if(getUnitType() != other.getUnitType()) + return true; + ArrayList copyDividers1 = new ArrayList(dividers); ArrayList copyDividents1 = new ArrayList(dividents); reduceUnitLists(dividents, copyDividents1, copyDividers1); @@ -190,4 +197,9 @@ public class UnitResult { public void setUnitType(UnitType unitType) { this.unitType = unitType; } + + @Override + public String toString() { + return getCleanFullUnit(); + } } 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 3da4c149..1ba0fc00 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 @@ -31,7 +31,7 @@ public class Value extends UnitCheckingNode { @Override public UnitResult getUnits(HashMap units) throws UnitCheckingException { UnitResult result = super.getUnits(units); - result.setUnitType(UnitType.ANY); + result.setUnitType(UnitType.SCALAR); return result; } }