package org.simantics.pythonlink.test; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import java.util.Map; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.NullProgressMonitor; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.simantics.PlatformException; import org.simantics.Simantics; import org.simantics.application.arguments.Arguments; import org.simantics.databoard.Bindings; import org.simantics.databoard.Datatypes; import org.simantics.databoard.type.MapType; import org.simantics.db.ReadGraph; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.request.Read; import org.simantics.pythonlink.Python; import org.simantics.pythonlink.PythonContext; public class TestPythonVariable { static IProgressMonitor progress = new NullProgressMonitor(); PythonContext python; @BeforeClass public static void SetUpClass() throws PlatformException { Simantics.startUpHeadless(Arguments.parse(new String[] {}), progress); } @AfterClass public static void TearDownClass() throws PlatformException { Simantics.shutdown(progress); } @Before public void setUp() throws Exception { python = Python.openPythonContext(); } @After public void tearDown() throws Exception { python.close(); } @Test public void test() throws DatabaseException { Simantics.getSession().syncRequest(new Read() { @Override public Void perform(ReadGraph graph) throws DatabaseException { python.setPythonDoubleArrayVariable("foo", new double[] { 1, 2, 3 }); Variable var = Python.getPythonContextVariable(python); Object value = var.getPossiblePropertyValue(graph, "foo"); assertNotNull(value); assertTrue(value instanceof Object[]); assertArrayEquals(new Object[] {1.0, 2.0, 3.0}, (Object[])value); return null; } }); } @Test public void test2() throws DatabaseException { Simantics.getSession().syncRequest(new Read() { @Override public Void perform(ReadGraph graph) throws DatabaseException { python.setPythonDoubleArrayVariable("foo", new double[] { 1, 2, 3 }); Variable var = Python.getPythonContextVariable(python); Object value = var.getPossiblePropertyValue(graph, "foo", Bindings.DOUBLE_ARRAY); assertNotNull(value); assertTrue(value instanceof double[]); assertArrayEquals(new double[] {1.0, 2.0, 3.0}, (double[])value, 0.0); return null; } }); } @Test public void test3() throws DatabaseException { Simantics.getSession().syncRequest(new Read() { @Override public Void perform(ReadGraph graph) throws DatabaseException { python.executePythonStatement("foo = (1, 2, 3)"); Variable var = Python.getPythonContextVariable(python); Variable foo = var.browse(graph, "#foo"); Object value = foo.getPossibleValue(graph, Bindings.LONG_ARRAY); assertNotNull(value); assertTrue(value instanceof long[]); assertArrayEquals(new long[] {1,2,3}, (long[])value); return null; } }); } @Test public void test4() throws DatabaseException { Simantics.getSession().syncRequest(new Read() { @Override public Void perform(ReadGraph graph) throws DatabaseException { python.executePythonStatement("foo = {1:'foo', 2:'bar'}"); Variable var = Python.getPythonContextVariable(python); Variable foo = var.getPossibleProperty(graph, "foo"); Object value = foo.getPossibleValue(graph, Bindings.getBinding(new MapType(Datatypes.LONG, Datatypes.STRING))); assertNotNull(value); assertTrue(value instanceof Map); assertEquals(2, ((Map)value).size()); assertEquals("foo", ((Map)value).get(1L)); assertEquals("bar", ((Map)value).get(2L)); return null; } }); } }