Better support for ontological profiles
[simantics/platform.git] / bundles / org.simantics.scenegraph.profile / src / org / simantics / scenegraph / profile / ProfileUtils.java
1 package org.simantics.scenegraph.profile;
2
3 import java.util.Collections;
4 import java.util.List;
5 import java.util.UUID;
6
7 import org.simantics.databoard.Bindings;
8 import org.simantics.db.ReadGraph;
9 import org.simantics.db.Resource;
10 import org.simantics.db.WriteGraph;
11 import org.simantics.db.common.request.ObjectsWithType;
12 import org.simantics.db.common.request.PossibleIndexRoot;
13 import org.simantics.db.exception.DatabaseException;
14 import org.simantics.diagram.stubs.DiagramResource;
15 import org.simantics.layer0.Layer0;
16 import org.simantics.scenegraph.profile.request.ProfileActiveEntryResources;
17
18 public class ProfileUtils {
19
20         public static List<Resource> getProfileChildren(ReadGraph graph, Resource profile) throws DatabaseException {
21                 DiagramResource DIA = DiagramResource.getInstance(graph);
22                 Resource entries = graph.getPossibleObject(profile, DIA.HasEntries);
23                 if(entries == null) return Collections.emptyList();
24                 return getProfileChildrenFromEntries(graph, entries);
25         }
26
27         public static List<Resource> getProfileChildrenFromEntries(ReadGraph graph, Resource entries) throws DatabaseException {
28                 DiagramResource DIA = DiagramResource.getInstance(graph);
29                 return graph.getRelatedValue2(entries, DIA.Profile_children, entries);
30         }
31
32         public static Resource getPossibleProfileActivationState(ReadGraph graph, Resource runtimeDiagram, Resource profile) throws DatabaseException {
33
34                 Layer0 L0 = Layer0.getInstance(graph);
35                 DiagramResource DIA = DiagramResource.getInstance(graph);
36                 Resource conf = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration);
37                 if(conf == null) return null;
38                 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(conf));
39                 if(indexRoot == null) return null;
40                 // Find existing state
41                 for(Resource state : graph.syncRequest(new ObjectsWithType(indexRoot, L0.ConsistsOf, DIA.ProfileActivationState))) {
42                         Resource ref = graph.getPossibleObject(state, DIA.ProfileActivationState_HasProfile);
43                         if(profile.equals(ref)) return state;
44                 }
45                 return null;
46                 
47         }
48         
49         public static Resource claimProfileActivationState(WriteGraph graph, Resource runtimeDiagram, Resource runtimeProfile, Resource entry) throws DatabaseException {
50                 Layer0 L0 = Layer0.getInstance(graph);
51                 DiagramResource DIA = DiagramResource.getInstance(graph);
52                 Resource conf = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration);
53                 if(conf == null) return null;
54                 Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(conf));
55                 if(indexRoot == null) return null;
56                 // Find existing state
57                 for(Resource state : graph.syncRequest(new ObjectsWithType(indexRoot, L0.ConsistsOf, DIA.ProfileActivationState))) {
58                         Resource profile = graph.getPossibleObject(state, DIA.ProfileActivationState_HasProfile);
59                         if(profile.equals(runtimeProfile)) return state;
60                 }
61                 // Create new state
62                 Resource state = graph.newResource();
63                 graph.claim(state, L0.InstanceOf, DIA.ProfileActivationState);
64                 graph.claimLiteral(state, L0.HasName, L0.String, UUID.randomUUID().toString(), Bindings.STRING);
65                 graph.claim(state, DIA.ProfileActivationState_HasProfile, runtimeProfile);
66                 graph.claim(indexRoot, L0.ConsistsOf, state);
67                 return state;
68         }
69         
70         
71 }