import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
-import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.simantics.db.common.procedure.adapter.DirectStatementProcedure;
import org.simantics.db.common.request.IsParent;
import org.simantics.db.common.request.ObjectsWithType;
+import org.simantics.db.common.request.PossibleChild;
import org.simantics.db.common.request.PossibleObjectWithType;
import org.simantics.db.common.request.PossibleOwner;
+import org.simantics.db.common.request.ResourceRead;
+import org.simantics.db.common.request.RuntimeEnvironmentRequest;
+import org.simantics.db.exception.AdaptionException;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.exception.InvalidResourceReferenceException;
import org.simantics.db.service.ClusterUID;
import org.simantics.db.service.SerialisationSupport;
import org.simantics.db.service.XSupport;
import org.simantics.layer0.Layer0;
+import org.simantics.scl.compiler.environment.Environments;
+import org.simantics.scl.compiler.runtime.RuntimeEnvironment;
+import org.simantics.scl.compiler.top.SCLExpressionCompilationException;
+import org.simantics.scl.compiler.types.Type;
import org.simantics.utils.datastructures.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
return owners;
}
+ /**
+ * This method didn't have a clear specification and therefore should not be used. Use instead
+ * the methods in the class {@link NearestOwnerFinder}.
+ */
+ @Deprecated
public static Resource getNearestOwner(ReadGraph graph, Collection<Resource> resources) throws DatabaseException {
- return getNearestOwner(graph, resources, null);
- }
-
- private static Resource getNearestOwner(ReadGraph graph, Collection<Resource> resources, THashSet<Resource> infiniteRecursionBreaker) throws DatabaseException {
-
- Layer0 L0 = Layer0.getInstance(graph);
-
- // These are resources that are linked to some of the input resources by IsComposedOf
- Set<Resource> directOwners = new HashSet<Resource>();
-
- // These are resources that refer to some of the input resources which don't have an owner
- Set<Resource> referringResources = new HashSet<Resource>();
-
- for(Resource r : resources) {
-
- Collection<Resource> owners = graph.getObjects(r, L0.IsOwnedBy);
-
- // TODO: getObjects returns duplicate entries (https://www.simantics.org/redmine/issues/4885) and therefore direct is Set<Resource>. Fix getObjects to not return duplicate entries
- if (owners.size() > 1) {
- owners = new HashSet<Resource>(owners);
- if (owners.size() > 1) {
- LOGGER.error("Multiple owners for {}, id: {}", graph.getPossibleURI(r), r);
- for (Resource r2 : owners)
- LOGGER.error(" owner: {}, id: {}", graph.getPossibleURI(r2), r2);
- return null;
- }
- }
-
- if (owners.isEmpty()) {
- // If there are no owners, collect resources referring to this resource by IsRelatedTo
- for(Statement stm : graph.getStatements(r, L0.IsWeaklyRelatedTo)) {
- Resource inverse = graph.getPossibleInverse(stm.getPredicate());
- if(inverse != null) {
- if(graph.isSubrelationOf(inverse, L0.IsRelatedTo)) {
- // Filter away tags
- if(r.equals(stm.getObject()))
- continue;
- referringResources.add(stm.getObject());
- }
- }
- }
- }
- else
- directOwners.addAll(owners);
- }
-
- if(!directOwners.isEmpty()) {
- Iterator<Resource> iter = directOwners.iterator();
- Resource common = iter.next();
- while (iter.hasNext()) {
- Resource other = iter.next();
- common = commonAncestor(graph, common, other);
- if (common == null)
- return null; // The
- }
- referringResources.add(common);
- }
- if(referringResources.isEmpty())
- return null;
-
- if(!Collections.disjoint(referringResources, resources)) {
- LOGGER.error("Overlapping owners:");
- for(Resource r : resources)
- LOGGER.error(" resource {}", NameUtils.getSafeName(graph, r, true));
- for(Resource r : referringResources)
- LOGGER.error(" owner {}", NameUtils.getSafeName(graph, r, true));
- return null;
- }
-
- if(referringResources.size() == 1)
- return referringResources.iterator().next();
-
- // Prevent recursion on current input resources
- if(infiniteRecursionBreaker == null)
- infiniteRecursionBreaker = new THashSet<>(resources);
- else {
- if(!Collections.disjoint(infiniteRecursionBreaker, resources)) {
- infiniteRecursionBreaker.retainAll(resources);
- LOGGER.error("Resources have a cyclic reference loop preventing the discovery their owner. {}", infiniteRecursionBreaker);
- return null;
- }
-
- infiniteRecursionBreaker.addAll(resources);
- }
-
- return getNearestOwner(graph, referringResources, infiniteRecursionBreaker);
-
+ if(resources.size() == 1)
+ return NearestOwnerFinder.getNearestOwner(graph, resources.iterator().next());
+ else
+ return NearestOwnerFinder.getNearestOwnerFromDirectOwners(graph, resources);
}
public static Resource getClusterSetForNewResource(ReadGraph graph, Resource ... resources) throws DatabaseException {
if(resources.length == 1) return getClusterSetForNewResource(graph, resources[0]);
- Resource owner = getNearestOwner(graph, CollectionUtils.toList(resources));
+ Resource owner = NearestOwnerFinder.getNearestOwnerFromDirectOwners(graph, CollectionUtils.toList(resources));
if(owner == null) return null;
return getClusterSetForNewResource(graph, owner, new HashSet<Resource>());
if(resources.size() == 1) return getClusterSetForNewResource(graph, resources.iterator().next());
- Resource owner = getNearestOwner(graph, resources);
+ Resource owner = NearestOwnerFinder.getNearestOwnerFromDirectOwners(graph, resources);
return getClusterSetForNewResource(graph, owner, new HashSet<Resource>());
}
return xs.isClusterLoaded(clusterUID);
}
-
+ public static void setImmutable(ReadGraph graph, Resource resource, boolean value) throws DatabaseException {
+ XSupport xs = graph.getService(XSupport.class);
+ xs.setImmutable(resource, value);
+ }
+
+ public static Type getSCLType(ReadGraph graph, RuntimeEnvironment runtimeEnvironment, String typeText) throws DatabaseException {
+ try {
+ return Environments.getType(runtimeEnvironment.getEnvironment(), typeText);
+ } catch (SCLExpressionCompilationException e) {
+ throw new DatabaseException(e);
+ }
+ }
+
+ public static Type getSCLType(ReadGraph graph, Resource resource, String typeText) throws DatabaseException {
+ try {
+ RuntimeEnvironment runtimeEnvironment = graph.syncRequest(new RuntimeEnvironmentRequest(resource));
+ return Environments.getType(runtimeEnvironment.getEnvironment(), typeText);
+ } catch (SCLExpressionCompilationException e) {
+ throw new DatabaseException(e);
+ }
+ }
+
+ public static Resource getPossibleChild(ReadGraph graph, Resource resource, String name) throws DatabaseException {
+ return graph.sync(new PossibleChild(resource, name));
+ }
+
+ public static Resource getPossibleChild(ReadGraph graph, Resource resource, Resource type, String name) throws DatabaseException {
+ Resource child = graph.sync(new PossibleChild(resource, name));
+ if(child == null) return null;
+ if(!graph.isInstanceOf(child, type)) return null;
+ return child;
+ }
+
+ public static String getEnumerationValueName(ReadGraph graph, Resource resource) throws DatabaseException {
+ Layer0 L0 = Layer0.getInstance(graph);
+ String label = graph.getPossibleRelatedValue(resource, L0.HasLabel, Bindings.STRING);
+ if(label != null)
+ return label;
+ return safeName(graph, resource);
+ }
+
+ private static String safeName(ReadGraph graph, Resource value) throws DatabaseException {
+ return graph.syncRequest(new StringAdapterRequest(value));
+ }
+
+ public static class StringAdapterRequest extends ResourceRead<String> {
+ public StringAdapterRequest(Resource resource) {
+ super(resource);
+ }
+ @Override
+ public String perform(ReadGraph graph) throws DatabaseException {
+ try {
+ return graph.adapt(resource, String.class);
+ } catch (AdaptionException e) {
+ return NameUtils.getSafeName(graph, resource);
+ }
+ }
+ }
+
}