]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.simulation/src/org/simantics/simulation/export/CSVItemsQuery.java
Obey Indexing.isDependenciesIndexingDisabled
[simantics/platform.git] / bundles / org.simantics.simulation / src / org / simantics / simulation / export / CSVItemsQuery.java
1 package org.simantics.simulation.export;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
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;
24
25 /**
26  * This query finds CSV item datas
27  *
28  * @author toni.kalajainen@semantum.fi
29  */
30 public class CSVItemsQuery implements Read<List<CSVItemsQuery.CSVItem>> {
31         
32         String contentUri;
33         
34         /**
35          * Create query using content uris.
36          * 
37          * Label format, use ""
38          * 
39          * @param contentUris uri to Chart or Subscription
40          */
41         public CSVItemsQuery(String contentUri) {
42                 this.contentUri = contentUri;
43         }
44         
45         @Override
46         public List<CSVItem> perform(ReadGraph graph) throws DatabaseException {
47                 List<CSVItem> result = new ArrayList<CSVItem>();
48                 Resource resource = graph.getResource(contentUri);
49                 
50                 Layer0 L0 = Layer0.getInstance(graph);
51                 ModelingResources MOD = ModelingResources.getInstance(graph);  
52                 
53                 List<Resource> subscriptionItems = new ArrayList<Resource>();
54                 
55                 if ( graph.isInstanceOf(resource, MOD.Subscription ) ) {
56                         subscriptionItems.addAll( graph.syncRequest( new ObjectsWithType(resource, L0.ConsistsOf, MOD.Subscription_Item) ) );
57                 } else {
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) ));
62                         }
63                 }
64                 
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 );
73                                 
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;                      
77                         
78 //            Resource experiment = graph.syncRequest(new PossibleExperiment(subscriptionItem));
79 //            String runIdentifier = ""; // experiment.getIdentifier();
80                         
81                         item.variableReference = rvi.toPossibleString(graph, configuration);
82             if (item.variableReference == null) continue;
83
84                 item.label = removeVariablePrefixPath( item.variableReference );                                
85                 item.unit = graph.getPossibleRelatedValue( subscriptionItem, MOD.Subscription_Item_Unit, Bindings.STRING );
86                 result.add( item );
87                 }
88                                 
89                 return result;
90         }
91         
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('#');
97         if (propIndex == -1)
98             return rvi;
99         int prevSlash = rvi.lastIndexOf('/', propIndex);
100         if (prevSlash == -1)
101             return rvi;
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) + ")" : "");
105     }
106         
107     static Pair<String, Integer> attributeKey(String key, int start) {
108         int iy = key.lastIndexOf('/');
109         boolean isIndexed = iy >= start;
110         if (isIndexed) {
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());
114         }
115         return Pair.make(key.substring(start), null);
116     }
117     
118     public static class CSVItem {
119         public String modelUri;
120         public String label;
121         public String variableReference;
122         public String unit;
123     }
124     
125 }
126