X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.scenegraph.profile%2Fsrc%2Forg%2Fsimantics%2Fscenegraph%2Fprofile%2FProfileUtils.java;h=92326248f26007b4eccfda3772ff72d99c6155a5;hb=08dc7080753f3ea35985e76e9effb9d3ff92c3b5;hp=b35ccd9f2064b425bbece1d43566f5b2ea4a09ad;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/ProfileUtils.java b/bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/ProfileUtils.java index b35ccd9f2..92326248f 100644 --- a/bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/ProfileUtils.java +++ b/bundles/org.simantics.scenegraph.profile/src/org/simantics/scenegraph/profile/ProfileUtils.java @@ -1,25 +1,84 @@ -package org.simantics.scenegraph.profile; - -import java.util.Collections; -import java.util.List; - -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.exception.DatabaseException; -import org.simantics.diagram.stubs.DiagramResource; - -public class ProfileUtils { - - public static List getProfileChildren(ReadGraph graph, Resource profile) throws DatabaseException { - DiagramResource DIA = DiagramResource.getInstance(graph); - Resource entries = graph.getPossibleObject(profile, DIA.HasEntries); - if(entries == null) return Collections.emptyList(); - return getProfileChildrenFromEntries(graph, entries); - } - - public static List getProfileChildrenFromEntries(ReadGraph graph, Resource entries) throws DatabaseException { - DiagramResource DIA = DiagramResource.getInstance(graph); - return graph.getRelatedValue2(entries, DIA.Profile_children, entries); - } - -} +package org.simantics.scenegraph.profile; + +import java.util.Collections; +import java.util.List; +import java.util.UUID; + +import org.simantics.databoard.Bindings; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.WriteGraph; +import org.simantics.db.common.request.ObjectsWithType; +import org.simantics.db.common.request.PossibleIndexRoot; +import org.simantics.db.exception.DatabaseException; +import org.simantics.diagram.stubs.DiagramResource; +import org.simantics.layer0.Layer0; +import org.simantics.simulation.ontology.SimulationResource; + +public class ProfileUtils { + + public static List getProfileChildren(ReadGraph graph, Resource profile) throws DatabaseException { + DiagramResource DIA = DiagramResource.getInstance(graph); + Resource entries = graph.getPossibleObject(profile, DIA.HasEntries); + if(entries == null) return Collections.emptyList(); + return getProfileChildrenFromEntries(graph, entries); + } + + public static List getProfileChildrenFromEntries(ReadGraph graph, Resource entries) throws DatabaseException { + DiagramResource DIA = DiagramResource.getInstance(graph); + return graph.getRelatedValue2(entries, DIA.Profile_children, entries); + } + + public static Resource getPossibleProfileActivationState(ReadGraph graph, Resource runtimeDiagram, Resource profile) throws DatabaseException { + + Layer0 L0 = Layer0.getInstance(graph); + DiagramResource DIA = DiagramResource.getInstance(graph); + Resource conf = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration); + if(conf == null) return null; + Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(conf)); + if(indexRoot == null) return null; + // Find existing state + for(Resource state : graph.syncRequest(new ObjectsWithType(indexRoot, L0.ConsistsOf, DIA.ProfileActivationState))) { + Resource ref = graph.getPossibleObject(state, DIA.ProfileActivationState_HasProfile); + if(profile.equals(ref)) return state; + } + return null; + + } + + public static Resource claimProfileActivationState(WriteGraph graph, Resource runtimeDiagram, Resource runtimeProfile, Resource entry) throws DatabaseException { + Layer0 L0 = Layer0.getInstance(graph); + DiagramResource DIA = DiagramResource.getInstance(graph); + Resource conf = graph.getPossibleObject(runtimeDiagram, DIA.RuntimeDiagram_HasConfiguration); + if(conf == null) return null; + Resource indexRoot = graph.syncRequest(new PossibleIndexRoot(conf)); + if(indexRoot == null) return null; + // Find existing state + for(Resource state : graph.syncRequest(new ObjectsWithType(indexRoot, L0.ConsistsOf, DIA.ProfileActivationState))) { + Resource profile = graph.getPossibleObject(state, DIA.ProfileActivationState_HasProfile); + if(profile.equals(runtimeProfile)) return state; + } + // Create new state + Resource state = graph.newResource(); + graph.claim(state, L0.InstanceOf, DIA.ProfileActivationState); + graph.claimLiteral(state, L0.HasName, L0.String, UUID.randomUUID().toString(), Bindings.STRING); + graph.claim(state, DIA.ProfileActivationState_HasProfile, runtimeProfile); + graph.claim(indexRoot, L0.ConsistsOf, state); + return state; + } + + public static boolean isActive(ReadGraph graph, Resource runtimeDiagram, Resource profile, Resource entry) throws DatabaseException { + DiagramResource DIA = DiagramResource.getInstance(graph); + SimulationResource SIMU = SimulationResource.getInstance(graph); + if(graph.isInstanceOf(entry, DIA.ProfileEntry)) { + if(graph.isImmutable(profile)) { + Resource state = getPossibleProfileActivationState(graph, runtimeDiagram, profile); + if(state != null && graph.hasStatement(state, SIMU.IsActive, entry)) return true; + } else { + if(graph.hasStatement(profile, SIMU.IsActive, entry)) return true; + } + } + return false; + } + +}