1 /*******************************************************************************
\r
2 * Copyright (c) 2012, 2013 Association for Decentralized Information Management in
\r
4 * All rights reserved. This program and the accompanying materials
\r
5 * are made available under the terms of the Eclipse Public License v1.0
\r
6 * which accompanies this distribution, and is available at
\r
7 * http://www.eclipse.org/legal/epl-v10.html
\r
10 * VTT Technical Research Centre of Finland - initial API and implementation
\r
11 *******************************************************************************/
\r
12 package org.simantics.objmap.structural;
\r
14 import java.util.ArrayList;
\r
15 import java.util.List;
\r
17 import org.simantics.db.ReadGraph;
\r
18 import org.simantics.db.Resource;
\r
19 import org.simantics.db.exception.DatabaseException;
\r
20 import org.simantics.layer0.Layer0;
\r
23 * An object representing structural Resource.
\r
26 * @author Marko Luukkainen <marko.luukkainen@vtt.fi>
\r
29 public class StructuralResource {
\r
31 private Resource resource;
\r
32 private List<Resource> context = new ArrayList<Resource>(1);
\r
34 private Resource typeResource = null;
\r
36 public StructuralResource(Resource resource) {
\r
37 assert(resource != null);
\r
38 this.resource = resource;
\r
40 public StructuralResource(ReadGraph g, Resource resource, Resource context) throws DatabaseException {
\r
41 assert(resource != null);
\r
42 this.resource = resource;
\r
43 this.context.add(context);
\r
47 public StructuralResource(ReadGraph g, Resource resource, Resource... context) throws DatabaseException {
\r
48 assert(resource != null);
\r
49 this.resource = resource;
\r
50 for (Resource r : context)
\r
51 this.context.add(r);
\r
55 public StructuralResource(ReadGraph g, Resource resource, List<Resource> context) throws DatabaseException {
\r
56 assert(resource != null);
\r
57 this.resource = resource;
\r
58 for (Resource r : context)
\r
59 this.context.add(r);
\r
62 public StructuralResource(ReadGraph g, Resource resource, List<Resource> context, Resource context2) throws DatabaseException {
\r
63 assert(resource != null);
\r
64 this.resource = resource;
\r
65 for (Resource r : context)
\r
66 this.context.add(r);
\r
67 this.context.add(context2);
\r
71 private void resolveType(ReadGraph g) throws DatabaseException {
\r
72 if (this.context.contains(resource)) {
\r
73 Layer0 l0 = Layer0.getInstance(g);
\r
74 typeResource = g.getSingleObject(resource, l0.InstanceOf);
\r
80 * The Resource in the DB.
\r
83 public Resource getResource() {
\r
88 * Context in which this resource is accessed. Each context resource represents a structural model instance.
\r
91 public List<Resource> getContext() {
\r
96 * If the resource is structural model instance, this returns the type Resource. Otherwise returns null.
\r
99 public Resource getTypeResource() {
\r
100 return typeResource;
\r
104 * Returns true, if the resource is structural,
\r
107 public boolean isStructural() {
\r
108 return context.size() > 0;
\r
112 * Returns true is the Resource is root of Structural Model instance.
\r
113 * In this case the resource instance is editable.
\r
117 public boolean isStructuralRoot() {
\r
118 return (context.size() == 1 && context.get(0).equals(resource));
\r
122 * Returns true, the resource is structural model instance.
\r
125 public boolean isStructuralInstance() {
\r
126 return typeResource != null;
\r
130 public int hashCode() {
\r
131 int hashCode = resource.hashCode();
\r
132 for (Resource ctx : context)
\r
133 hashCode += ctx.hashCode();
\r
138 public boolean equals(Object obj) {
\r
143 if (obj.getClass() != getClass())
\r
145 StructuralResource other = (StructuralResource)obj;
\r
146 if (!resource.equals(other.resource))
\r
148 if (context.size() != other.context.size())
\r
150 for (int i = 0; i < context.size(); i++) {
\r
151 if (!context.get(i).equals(other.context.get(i)))
\r
158 public String toString() {
\r
159 String s = "Res: " + resource + " Context:";
\r
160 for (Resource ctx : context)
\r
162 if (typeResource != null)
\r
163 s+= " Type: " + typeResource;
\r