1 package org.simantics.simulation.export;
3 import java.util.ArrayList;
6 import org.simantics.databoard.Bindings;
7 import org.simantics.databoard.Databoard;
8 import org.simantics.databoard.accessor.reference.ChildReference;
9 import org.simantics.databoard.accessor.reference.IndexReference;
10 import org.simantics.databoard.binding.Binding;
11 import org.simantics.db.ReadGraph;
12 import org.simantics.db.Resource;
13 import org.simantics.db.common.request.ObjectsWithType;
14 import org.simantics.db.common.request.PossibleObjectWithType;
15 import org.simantics.db.exception.DatabaseException;
16 import org.simantics.db.layer0.request.PossibleModel;
17 import org.simantics.db.layer0.variable.RVI;
18 import org.simantics.db.layer0.variable.Variable;
19 import org.simantics.db.layer0.variable.Variables;
20 import org.simantics.db.request.Read;
21 import org.simantics.layer0.Layer0;
22 import org.simantics.modeling.ModelingResources;
23 import org.simantics.utils.datastructures.Pair;
26 * This query finds CSV item datas
28 * @author toni.kalajainen@semantum.fi
30 public class CSVItemsQuery implements Read<List<CSVItemsQuery.CSVItem>> {
35 * Create query using content uris.
37 * Label format, use ""
39 * @param contentUris uri to Chart or Subscription
41 public CSVItemsQuery(String contentUri) {
42 this.contentUri = contentUri;
46 public List<CSVItem> perform(ReadGraph graph) throws DatabaseException {
47 List<CSVItem> result = new ArrayList<CSVItem>();
48 Resource resource = graph.getResource(contentUri);
50 Layer0 L0 = Layer0.getInstance(graph);
51 ModelingResources MOD = ModelingResources.getInstance(graph);
53 List<Resource> subscriptionItems = new ArrayList<Resource>();
55 if ( graph.isInstanceOf(resource, MOD.Subscription ) ) {
56 subscriptionItems.addAll( graph.syncRequest( new ObjectsWithType(resource, L0.ConsistsOf, MOD.Subscription_Item) ) );
58 // This is probably Chart. This plugin doesn't have dependency to Chart plugin
59 // We can follow HasSubscriptionItem relation anyway.
60 for (Resource possibleItem : graph.getObjects( resource, L0.ConsistsOf )) {
61 subscriptionItems.addAll( graph.syncRequest( new ObjectsWithType(possibleItem, L0.IsRelatedTo, MOD.Subscription_Item) ));
65 for ( Resource subscriptionItem : subscriptionItems ) {
66 Resource model = graph.syncRequest( new PossibleModel(subscriptionItem) );
67 Resource subscription = graph.syncRequest( new PossibleObjectWithType(subscriptionItem, L0.PartOf, MOD.Subscription) );
68 if (subscription == null) continue;
69 Variable configuration = Variables.getPossibleConfigurationContext(graph, subscription);
70 if (configuration == null) continue;
71 CSVItem item = new CSVItem();
72 if ( model != null) item.modelUri = graph.getURI( model );
74 Binding rviBinding = graph.getService(Databoard.class).getBindingUnchecked( RVI.class );
75 RVI rvi = graph.getPossibleRelatedValue(subscriptionItem, MOD.Subscription_Item_VariableId, rviBinding);
76 if (rvi == null) continue;
78 // Resource experiment = graph.syncRequest(new PossibleExperiment(subscriptionItem));
79 // String runIdentifier = ""; // experiment.getIdentifier();
81 item.variableReference = rvi.toPossibleString(graph, configuration);
82 if (item.variableReference == null) continue;
84 item.label = removeVariablePrefixPath( item.variableReference );
85 item.unit = graph.getPossibleRelatedValue( subscriptionItem, MOD.Subscription_Item_Unit, Bindings.STRING );
92 static String removeVariablePrefixPath(String rvi) {
93 // Apros-specific logics:
94 // 1. remove path prefix
95 // 2. change IndexReferences (i-N) into "(N+1)" Apros-style indexing
96 int propIndex = rvi.indexOf('#');
99 int prevSlash = rvi.lastIndexOf('/', propIndex);
102 Pair<String, Integer> attrKey = attributeKey(rvi, propIndex + 1);
103 return rvi.substring(prevSlash + 1, propIndex + 1) + attrKey.first
104 + (attrKey.second != null ? "(" + (attrKey.second + 1) + ")" : "");
107 static Pair<String, Integer> attributeKey(String key, int start) {
108 int iy = key.lastIndexOf('/');
109 boolean isIndexed = iy >= start;
111 ChildReference child = ChildReference.parsePath(key.substring(iy + 1));
112 if (child instanceof IndexReference)
113 return Pair.make(key.substring(start, iy), ((IndexReference) child).getIndex());
115 return Pair.make(key.substring(start), null);
118 public static class CSVItem {
119 public String modelUri;
121 public String variableReference;