/******************************************************************************* * Copyright (c) 2014, 2016 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 * Semantum Oy *******************************************************************************/ package org.simantics.r; import org.rosuda.REngine.Rserve.RserveException; import org.simantics.databoard.Bindings; import org.simantics.db.ReadGraph; import org.simantics.db.Resource; import org.simantics.db.exception.DatabaseException; import org.simantics.db.layer0.variable.NodeManagerVariableBuilder; import org.simantics.db.layer0.variable.NodeSupport; import org.simantics.db.layer0.variable.StandardGraphChildVariable; import org.simantics.db.layer0.variable.Variable; import org.simantics.db.layer0.variable.VariableNode; import org.simantics.layer0.Layer0; import org.simantics.r.scl.RSession; import org.simantics.r.scl.RSessionConfiguration; import org.simantics.r.scl.RSessionManager; import org.simantics.r.scl.variable.RVariableNode; import org.simantics.simulator.variable.NodeManager; public class RVariableBuilder extends NodeManagerVariableBuilder { @Override public Variable buildChild(ReadGraph graph, Variable parent, VariableNode node, Resource child) throws DatabaseException { Layer0 L0 = Layer0.getInstance(graph); RResource R = RResource.getInstance(graph); String name = graph.getRelatedValue(child, L0.HasName, Bindings.STRING); RSession session = RSessionManager.getSession(name); if(session == null) { Resource conf = graph.getSingleObject(child, L0.PartOf); RSessionConfiguration configuration = new RSessionConfiguration( graph.getRelatedValue(conf, R.SessionConfiguration_host, Bindings.STRING), graph.getRelatedValue(conf, R.SessionConfiguration_port, Bindings.INTEGER), graph.getRelatedValue(conf, R.SessionConfiguration_username, Bindings.STRING), graph.getRelatedValue(conf, R.SessionConfiguration_password, Bindings.STRING)); try { session = RSessionManager.getOrCreateSession(configuration, name); } catch (RserveException e) { throw new DatabaseException(e); } } NodeSupport support = getNodeSupport(graph, name); NodeManager nodeManager = session.getNodeManager(); return new StandardGraphChildVariable(parent, new VariableNode(support, nodeManager), child); } @Override protected NodeSupport getNodeSupport(ReadGraph graph, String sessionName) throws DatabaseException { return RSessionManager.getOrCreateNodeSupport(sessionName); } @Override protected Object getRoot(ReadGraph graph, NodeSupport support, String sessionName) throws DatabaseException { return support.manager; } }