/******************************************************************************* * Copyright (c) 2007 VTT Technical Research Centre of Finland and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * VTT Technical Research Centre of Finland - initial API and implementation *******************************************************************************/ package org.simantics.processeditor.stubs; import org.simantics.db.Resource; import org.simantics.db.ResourceContainer; import org.simantics.db.Statement; import org.simantics.db.Graph; import org.simantics.db.ResourceContainer; import org.simantics.layer0.utils.ResourceDoesNotSatisfyAssumptionException; import org.simantics.layer0.utils.IEntity; import org.simantics.layer0.utils.internal.Entity; import org.simantics.layer0.utils.instantiation.InstanceFactory; import java.util.Collection; import java.util.ArrayList; public class PBSNode extends Entity implements IPBSNode { public PBSNode(Graph graph, ResourceContainer resourceContainer) { super(graph, resourceContainer.getResource()); } public PBSNode(IEntity entity) { super(entity.getGraph(), entity.getResource()); } public Graph getGraph() { return graph; } public static PBSNode createDefault(Graph graph) { return new PBSNode(graph, InstanceFactory.instantiate(graph, org.simantics.processeditor.stubs.Plant3DResource.getInstance(graph).PBSNode)); } public static boolean isInstance(IEntity entity) { Graph graph = entity.getGraph(); Resource type = org.simantics.processeditor.stubs.Plant3DResource.getInstance(graph).PBSNode; return graph.isInstanceOf(entity.getResource(), type); } @Override public Resource getResource() { return resource; } public PBSNode toPBSNode() { return this; } public org.simantics.processeditor.stubs.PBSNode getParentStructure() { Statement[] statements = graph.getStatements(resource, org.simantics.processeditor.stubs.Plant3DResource.getInstance(graph).HasParentStructure); if(statements.length > 1) throw new ResourceDoesNotSatisfyAssumptionException("Wrong number (" + statements.length + ") of statements."); if(statements.length == 0) return null; return new org.simantics.processeditor.stubs.PBSNode(graph, statements[0].getObject()); } public void addParentStructure(ResourceContainer v) { graph.addStatement(resource, org.simantics.processeditor.stubs.Plant3DResource.getInstance(graph).HasParentStructure, v.getResource()); } public void setParentStructure(ResourceContainer v) { Resource predicate = org.simantics.processeditor.stubs.Plant3DResource.getInstance(graph).HasParentStructure; graph.removeStatements(resource, predicate); graph.addStatement(resource, predicate, v.getResource()); } public Collection getChildStructure() { Statement[] statements = graph.getStatements(resource, org.simantics.processeditor.stubs.Plant3DResource.getInstance(graph).HasChildStructure); Collection ret = new ArrayList(statements.length); for(Statement s : statements) ret.add(new org.simantics.processeditor.stubs.PBSNode(graph, s.getObject())); return ret; } public void addChildStructure(ResourceContainer v) { graph.addStatement(resource, org.simantics.processeditor.stubs.Plant3DResource.getInstance(graph).HasChildStructure, v.getResource()); } }