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