1 /*******************************************************************************
2 * Copyright (c) 2017 Association for Decentralized Information Management in
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
12 *******************************************************************************/
13 package org.simantics.charts.internal;
15 import java.util.Collections;
16 import java.util.List;
17 import java.util.Optional;
19 import org.simantics.databoard.type.Datatype;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.exception.DatabaseException;
23 import org.simantics.db.layer0.request.PossibleActiveVariableFromVariable;
24 import org.simantics.db.layer0.variable.RVI;
25 import org.simantics.db.layer0.variable.Variable;
26 import org.simantics.db.layer0.variable.VariableReference;
27 import org.simantics.db.layer0.variable.Variables;
28 import org.simantics.modeling.utils.VariableReferences;
31 * @author Tuukka Lehtonen
32 * @since 1.28.1, 1.29.0
34 public class VariableUtils {
36 public static Datatype getDatatype(ReadGraph graph, Resource resource, RVI rvi) throws DatabaseException {
37 Variable configuration = Variables.getConfigurationContext(graph, resource);
38 Variable active = graph.syncRequest(new PossibleActiveVariableFromVariable(configuration));
39 Variable var = rvi.resolve(graph, active != null ? active : configuration);
40 return var.getDatatype(graph);
47 * @return <code>null</code> if the value is valid JSON but does not contain
48 * a variable reference
49 * @throws DatabaseException
50 * if the value fails to resolve as either JSON or an RVI string
52 public static List<VariableReference> getVariableReferencesFromString(ReadGraph graph, Resource targetModel, String source) throws DatabaseException {
55 Optional<Variable> v = JsonUtils.tryParseJsonPropertyVariable(graph, source);
57 return graph.syncRequest(VariableReferences.variablesToReferences(targetModel, Collections.singletonList(v.get())));
58 // JSON, but no variable info in it.
60 } catch (DatabaseException e) {
62 RVI rvi = RVI.fromResourceFormat(graph, source);
63 return Collections.singletonList(new VariableReference(rvi, getDatatype(graph, targetModel, rvi), null));