From 055d5d62a81a2c6cb595c2578838a48d2fb46195 Mon Sep 17 00:00:00 2001 From: villberg Date: Tue, 26 Aug 2014 05:49:22 +0000 Subject: [PATCH] refs #5224 git-svn-id: https://www.simantics.org/svn/simantics/sysdyn/trunk@30134 ac1ea38d-2e2b-0410-8846-a27921b304fc --- .../src/fi/semantum/sysdyn/solver/Array.java | 17 ++++ .../semantum/sysdyn/solver/Environment.java | 98 ++++++++++++++++++- 2 files changed, 112 insertions(+), 3 deletions(-) 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 d0ceff4f..4e65b150 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 @@ -433,6 +433,23 @@ public class Array implements IExpression { return result; } + interface Modifier { + Object modify(Object o); + } + + public Array copy(Modifier modifier) { + Array result = new Array(); + for(Object element : elements) { + if(element instanceof Array) { + Array sub = (Array)element; + result.addElement(sub.copy(modifier)); + } else { + result.addElement(modifier.modify(element)); + } + } + return result; + } + public boolean isVector() { return isVector; } 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 d3cfce24..d9ab2c96 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 @@ -11,6 +11,7 @@ *******************************************************************************/ package fi.semantum.sysdyn.solver; +import fi.semantum.sysdyn.solver.Array.Modifier; import gnu.trove.list.array.TIntArrayList; import gnu.trove.map.hash.TObjectIntHashMap; @@ -271,9 +272,76 @@ final public class Environment implements IEnvironment, ISystem { @Override public Object evaluate(IEnvironment environment, int argc) { Object value = environment.getValue(0); - Double result = (Double)value; - double res = result.intValue(); - return res; + if(value instanceof Double) { + return ((Double)value).intValue(); + } 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(); + } else { + throw new IllegalStateException(); + } + } + + }); + } else { + throw new IllegalStateException(); + } + } + + }); + model.functions.put("ceil", new Fn1(1) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Object value = environment.getValue(0); + if(value instanceof Double) { + return Math.ceil((Double)value); + } else if(value instanceof Array) { + return ((Array)value).copy(new Modifier() { + + @Override + public Object modify(Object o) { + if(o instanceof Double) { + return Math.ceil((Double)o); + } else { + throw new IllegalStateException(); + } + } + + }); + } else { + throw new IllegalStateException(); + } + } + + }); + model.functions.put("floor", new Fn1(1) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Object value = environment.getValue(0); + if(value instanceof Double) { + return Math.floor((Double)value); + } else if(value instanceof Array) { + return ((Array)value).copy(new Modifier() { + + @Override + public Object modify(Object o) { + if(o instanceof Double) { + return Math.floor((Double)o); + } else { + throw new IllegalStateException(); + } + } + + }); + } else { + throw new IllegalStateException(); + } } }); @@ -289,6 +357,30 @@ final public class Environment implements IEnvironment, ISystem { return res; } + }); + model.functions.put("ones", new Fn1(1) { + + @Override + public Object evaluate(IEnvironment environment, int argc) { + Object value = environment.getValue(0); + Double size = (Double)value; + Array res = new Array(); + for(int i=0;i