1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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;
14 import org.simantics.db.ReadGraph;
15 import org.simantics.db.Resource;
16 import org.simantics.db.WriteOnlyGraph;
17 import org.simantics.db.common.WriteBindings;
18 import org.simantics.db.exception.DatabaseException;
19 import org.simantics.db.exception.RuntimeDatabaseException;
20 import org.simantics.db.service.SerialisationSupport;
21 import org.simantics.graph.db.AbstractImportAdvisor2;
22 import org.simantics.graph.db.TransferableGraphImporter;
23 import org.simantics.graph.representation.Root;
24 import org.simantics.layer0.Layer0;
25 import org.simantics.project.management.DatabaseManagement;
26 import org.simantics.project.management.GraphBundle;
28 public class OntologyImportAdvisor extends AbstractImportAdvisor2 {
30 final private GraphBundle tg;
31 final private DatabaseManagement mgmt;
32 final private String rootName;
33 private Resource ontology;
34 private Resource ontologyName;
36 public OntologyImportAdvisor(GraphBundle tg, DatabaseManagement mgmt) {
39 if(tg.getName().contains("@")) {
40 rootName = tg.getName().substring(tg.getName().lastIndexOf("/")+1);
42 rootName = tg.getName().substring(tg.getName().lastIndexOf("/")+1) + "-" + tg.getMajor() + "." + tg.getMinor();
47 public Resource getTarget() {
52 public Resource analyzeRoot(ReadGraph graph, Root root) throws DatabaseException {
57 public Resource createRoot(WriteOnlyGraph graph, Root root) throws DatabaseException {
58 return createRoot(graph, root, null);
62 public Resource createRoot(WriteOnlyGraph graph, Root root, Resource resource) throws DatabaseException {
63 if(resource == null) resource = graph.newResource();
64 addRootInfo(root, root.name, resource);
69 public void beforeWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {
70 ontology = graph.newResource(graph.getRootLibrary());
71 ontologyName = graph.newResource(graph.getRootLibrary());
72 graph.newClusterSet(ontology);
73 graph.setClusterSet4NewResource(ontology);
77 public void afterWrite(WriteOnlyGraph graph, TransferableGraphImporter process) throws DatabaseException {
80 long[] ids = process.getResourceIds(graph.getService(SerialisationSupport.class));
81 tg.setResourceArray(ids);
82 mgmt.createGraphBundle(graph, tg);
83 //graph.flushCluster();
84 } catch (DatabaseException e) {
85 throw new RuntimeDatabaseException(e);
91 public boolean allowImmutableModifications() {
96 public Resource createChild(WriteOnlyGraph graph, TransferableGraphImporter process, Resource parent,
97 String name) throws DatabaseException {
98 if(rootName.equals(name)) {
100 Layer0 L0 = graph.getService(Layer0.class);
101 graph.claim(ontologyName, L0.InstanceOf, null, L0.String);
102 graph.claimValue(ontologyName, name, WriteBindings.STRING);
103 graph.claim(ontology, L0.HasName, L0.NameOf, ontologyName);
108 return process.createChild(graph, parent, null, name);
114 public Resource createChild(WriteOnlyGraph graph, TransferableGraphImporter process, Resource parent, Resource child,
115 String name) throws DatabaseException {
117 if(rootName.equals(name)) {
119 Layer0 L0 = graph.getService(Layer0.class);
120 graph.claim(ontologyName, L0.InstanceOf, null, L0.String);
121 graph.claimValue(ontologyName, name, WriteBindings.STRING);
122 graph.claim(ontology, L0.HasName, L0.NameOf, ontologyName);
127 return process.createChild(graph, parent, null, name);