]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/export/DiscoverModels.java
Use ListIndexRoots in migration
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / export / DiscoverModels.java
1 package org.simantics.modeling.export;
2
3 import java.util.Collection;
4
5 import org.simantics.db.ReadGraph;
6 import org.simantics.db.common.utils.traverser.TraverseQueryBuilder;
7 import org.simantics.db.common.utils.traverser.TraverseResult;
8 import org.simantics.db.exception.DatabaseException;
9 import org.simantics.db.request.Read;
10 import org.simantics.export.core.ExportContext;
11 import org.simantics.export.core.error.ExportException;
12 import org.simantics.export.core.intf.DiscoverAction;
13 import org.simantics.export.core.util.ExportQueries;
14 import org.simantics.layer0.Layer0;
15 import org.simantics.simulation.ontology.SimulationResource;
16
17 /**
18  * Discovers all models in context ( session, project )
19  *
20  * @author toni.kalajainen@semantum.fi
21  */
22 public class DiscoverModels implements DiscoverAction {
23
24         public Read<Collection<String>> discoverRequest(
25                         final Collection<String> startLocations) throws ExportException {
26                 return new Read<Collection<String>>() {
27                         public Collection<String> perform(ReadGraph graph) throws DatabaseException {
28                                 
29                                 Layer0 L0 = Layer0.getInstance(graph);
30                                 SimulationResource SIM = SimulationResource.getInstance(graph);  
31                                 TraverseQueryBuilder builder = new TraverseQueryBuilder();
32                                 builder.setStartResources( graph.syncRequest( ExportQueries.toResources(startLocations) ) );
33                                 builder.followRelation( L0.ConsistsOf );
34                                 builder.followAndReturnInstanceOf( SIM.Model );
35                                 TraverseResult traverseResult = graph.syncRequest( builder.build() );
36                                 return graph.syncRequest( ExportQueries.toUris( traverseResult.result ) );
37                         }
38                         
39                 };
40         }
41
42         @Override
43         public Collection<String> discoverContent(ExportContext ctx, Collection<String> startLocations) throws ExportException {                
44                 try {
45                         return ctx.session.syncRequest( discoverRequest(startLocations) );
46                 } catch (DatabaseException e) {
47                         if ( e.getCause() instanceof ExportException ) throw (ExportException) e.getCause();
48                         throw new ExportException( e );
49                 }
50         }
51         
52
53 }