]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/TGRepresentationUtils.java
Always write model activation information in activations VG
[simantics/platform.git] / bundles / org.simantics.db.layer0 / src / org / simantics / db / layer0 / util / TGRepresentationUtils.java
1 /*******************************************************************************
2  * Copyright (c) 2016 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.db.layer0.util;
13
14 import java.util.Collections;
15 import java.util.List;
16 import java.util.Map;
17 import java.util.Set;
18
19 import org.simantics.datatypes.literal.GUID;
20 import org.simantics.db.ReadGraph;
21 import org.simantics.db.Resource;
22 import org.simantics.db.Statement;
23 import org.simantics.db.common.request.PossibleIndexRoot;
24 import org.simantics.db.exception.DatabaseException;
25 import org.simantics.db.layer0.adapter.impl.EntityInstances.QueryIndex;
26 import org.simantics.db.layer0.genericrelation.IndexQueries;
27 import org.simantics.db.layer0.util.ConsistsOfProcess.ConsistsOfProcessEntry;
28 import org.simantics.db.layer0.util.DomainProcessor3.ExclusionDecision;
29 import org.simantics.db.layer0.util.TransferableGraphConfiguration2.SeedSpec;
30 import org.simantics.layer0.Layer0;
31 import org.simantics.scl.runtime.function.Function1;
32 import org.simantics.utils.datastructures.Pair;
33
34 /**
35  * @author Antti Villberg
36  * @author Tuukka Lehtonen
37  * @since 1.22
38  */
39 public class TGRepresentationUtils {
40
41     public static boolean findByIdentifier(ReadGraph graph, Resource targetIndex, Resource source) throws DatabaseException {
42         Layer0 L0 = Layer0.getInstance(graph);
43         GUID guid = graph.getPossibleRelatedValue(source, L0.identifier, GUID.BINDING);
44         if(guid != null) {
45             List<Resource> exist = graph.syncRequest(new QueryIndex(targetIndex, L0.Entity, "GUID:" + IndexQueries.quoteTerm(guid.indexString())));
46             return !exist.isEmpty();
47         }
48         return false;
49     }
50
51     public static Function1<Statement, ExclusionDecision> computeExclusionFunction(ReadGraph graph, Resource[] resources, Map<String,Object> hints) throws DatabaseException {
52         Resource targetResource = (Resource)hints.get(ClipboardUtils.HINT_TARGET_RESOURCE);
53         if(targetResource != null) {
54             Resource targetRoot = graph.syncRequest(new PossibleIndexRoot(targetResource));
55             if(targetRoot != null) {
56                 for(Resource r : resources) {
57                     Resource sourceRoot = graph.syncRequest(new PossibleIndexRoot(r));
58                     // We are copying a complete index root => no need to change guids
59                     if(r.equals(sourceRoot))
60                         continue;
61                     if(targetRoot.equals(sourceRoot)) {
62                         // Copy into same index root => guid rewrite is needed
63                         return new GUIDExclusionFunction(graph);
64                     }
65                     // First try the root GUID
66                     if(findByIdentifier(graph, targetRoot, r))
67                         return new GUIDExclusionFunction(graph);
68
69                     // The root is OK - check everything beneath
70                     Pair<List<ConsistsOfProcessEntry>,Set<Resource>> pair = ConsistsOfProcess.walk(graph, Collections.singleton(SeedSpec.internal(r)), true); 
71                     List<ConsistsOfProcessEntry> entries = pair.first;
72                     for(ConsistsOfProcessEntry entry : entries) {
73                         if(findByIdentifier(graph, targetRoot, entry.resource))
74                             return new GUIDExclusionFunction(graph);
75                     }
76                 }
77             }
78         }
79
80         return null;
81     }
82
83 }