]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.modeling/src/org/simantics/modeling/typicals/rules/InstanceOfRule.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.modeling / src / org / simantics / modeling / typicals / rules / InstanceOfRule.java
1 /*******************************************************************************
2  * Copyright (c) 2013 Association for Decentralized Information Management in
3  * 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.modeling.typicals.rules;
13
14 import java.util.Collection;
15
16 import org.simantics.db.Resource;
17 import org.simantics.db.WriteGraph;
18 import org.simantics.db.common.utils.NameUtils;
19 import org.simantics.db.exception.DatabaseException;
20 import org.simantics.layer0.Layer0;
21 import org.simantics.modeling.typicals.ITypicalSynchronizationRule;
22 import org.simantics.modeling.typicals.TypicalInfo;
23
24 /**
25  * Synchronizes L0.InstanceOf relations from template to instance.
26  * 
27  * @author Tuukka Lehtonen
28  */
29 public enum InstanceOfRule implements ITypicalSynchronizationRule {
30
31     INSTANCE;
32
33     public static InstanceOfRule getInstance() {
34         return INSTANCE;
35     }
36
37     @Override
38     public boolean synchronize(WriteGraph graph, Resource template, Resource instance, TypicalInfo info) throws DatabaseException {
39         boolean changed = false;
40         Layer0 L0 = Layer0.getInstance(graph);
41         Collection<Resource> templateInstanceOf = graph.getObjects(template, L0.InstanceOf);
42         Collection<Resource> instanceInstanceOf = graph.getObjects(instance, L0.InstanceOf);
43
44         for (Resource instanceOf : templateInstanceOf) {
45             if (!graph.hasStatement(instance, L0.InstanceOf, instanceOf)) {
46                 graph.claim(instance, L0.InstanceOf, instanceOf);
47                 info.messageLog.add("\t\t\tadd type  " + NameUtils.getSafeName(graph, instanceOf));
48                 changed = true;
49             }
50         }
51         for (Resource instanceOf : instanceInstanceOf) {
52             if (!templateInstanceOf.contains(instanceOf) && graph.hasStatement(instance, L0.InstanceOf, instanceOf)) {
53                 graph.deny(instance, L0.InstanceOf, instanceOf);
54                 info.messageLog.add("\t\t\tremove type " + NameUtils.getSafeName(graph, instanceOf));
55                 changed = true;
56             }
57         }
58
59         return changed;
60     }
61
62 }