From: villberg Date: Tue, 26 Aug 2014 07:19:58 +0000 (+0000) Subject: refs #5224 X-Git-Tag: v1.29.0~208 X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=commitdiff_plain;h=679b46d1d999d0694c20ab8bbd0a857d8cefb710;p=simantics%2Fsysdyn.git refs #5224 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@30136 ac1ea38d-2e2b-0410-8846-a27921b304fc --- diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java index 4e65b150..8747c518 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Array.java @@ -454,4 +454,55 @@ public class Array implements IExpression { return isVector; } + private Object subscript(IEnvironment environment, IExpression[] subscripts, int position) { + + IExpression e = subscripts[position]; + int index = Utils.getIndex(e.evaluate(environment)); + if(index == -2) { + Array ret = new Array(); + for(Object element : elements) { + // Last position should not be array + if(position == subscripts.length-1) { + if(element instanceof Array) throw new IllegalStateException(); + if(element instanceof IExpression) { + IExpression exp = (IExpression)element; + return exp.evaluate(environment); + } else { + ret.addElement(element); + } + } else { + if(element instanceof Array) { + return ret.addElement(((Array)element).subscript(environment, subscripts, position+1)); + } else { + throw new IllegalStateException(); + } + } + } + return ret; + } else { + Object element = element(index); + // Last position should not be array + if(position == subscripts.length-1) { + if(element instanceof Array) throw new IllegalStateException(); + if(element instanceof IExpression) { + IExpression exp = (IExpression)element; + return exp.evaluate(environment); + } else { + return element; + } + } else { + if(element instanceof Array) { + return ((Array)element).subscript(environment, subscripts, position+1); + } else { + throw new IllegalStateException(); + } + } + } + + } + + public Object subscript(IEnvironment environment, IExpression[] subscripts) { + return subscript(environment, subscripts, 0); + } + } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java index d9ab2c96..84d8afed 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Environment.java @@ -273,14 +273,16 @@ final public class Environment implements IEnvironment, ISystem { public Object evaluate(IEnvironment environment, int argc) { Object value = environment.getValue(0); if(value instanceof Double) { - return ((Double)value).intValue(); + double d = ((Double)value).intValue(); + return d; } else if(value instanceof Array) { return ((Array)value).copy(new Modifier() { @Override public Object modify(Object o) { if(o instanceof Double) { - return ((Double)o).intValue(); + double d = ((Double)o).intValue(); + return d; } else { throw new IllegalStateException(); } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java index f9a17730..95d01166 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/Parser.java @@ -376,7 +376,9 @@ public class Parser { ArrayList os = (ArrayList)os_; for(Object o : os) { VariableDeclaration decl = (VariableDeclaration)o; - function.internals.add(decl); + if("input".equals(decl.direction)) function.inputs.add(decl); + else if ("output".equals(decl.direction)) function.outputs.add(decl); + else function.internals.add(decl); } } } @@ -389,14 +391,14 @@ public class Parser { VariableDeclaration decl = (VariableDeclaration)os_; if("input".equals(decl.direction)) function.inputs.add(decl); else if ("output".equals(decl.direction)) function.outputs.add(decl); - else throw new IllegalStateException(); + else function.internals.add(decl); } else { ArrayList os = (ArrayList)os_; for(Object o : os) { VariableDeclaration decl = (VariableDeclaration)o; if("input".equals(decl.direction)) function.inputs.add(decl); else if ("output".equals(decl.direction)) function.outputs.add(decl); - else throw new IllegalStateException(); + else function.internals.add(decl); } } } diff --git a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/VariableBase.java b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/VariableBase.java index 355c7919..e0a57a6c 100644 --- a/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/VariableBase.java +++ b/fi.semantum.sysdyn.solver/src/fi/semantum/sysdyn/solver/VariableBase.java @@ -130,10 +130,10 @@ public class VariableBase { return result; } - private boolean hasScalarSubscript(IExpression[] subscripts) { - if(subscripts == null) return false; - else return true; - } +// private boolean hasScalarSubscript(IExpression[] subscripts) { +// if(subscripts == null) return false; +// else return true; +// } public int intoArray(IEnvironment env, int index, int d, Array target) { @@ -213,12 +213,8 @@ public class VariableBase { if(isStoredAsArray(environment)) { Array array = (Array)environment.getValue(index); - if(hasScalarSubscript(subscripts)) { - Object o = getArrayIndex(environment, subscripts, array); - if(o instanceof Variable) throw new IllegalStateException(); - if(o instanceof Array) - throw new IllegalStateException(); - return o; + if(subscripts != null) { + return array.subscript(environment, subscripts); } else return array;