1 /*******************************************************************************
2 * Copyright (c) 2007 VTT Technical Research Centre of Finland and others.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
9 * VTT Technical Research Centre of Finland - initial API and implementation
10 *******************************************************************************/
11 package org.simantics.project.management;
13 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.List;
16 import java.util.TreeMap;
17 import java.util.regex.Pattern;
19 import org.simantics.db.ReadGraph;
20 import org.simantics.db.Resource;
21 import org.simantics.db.exception.DatabaseException;
22 import org.simantics.db.request.Read;
23 import org.simantics.layer0.DatabaseManagementResource;
24 import org.simantics.layer0.Layer0;
27 * A description of an graph bundle
29 * @author Toni Kalajainen <toni.kalajainen@vtt.fi>
31 public class OntologySpec {
33 /** Versioned Id pattern */
34 static String ID_PATTERN_STRING = "[a-zA-Z_0-9]+(?:\\.[a-zA-Z_0-9]+)*";
35 static String VERSION_PATTERN_STRING = "(\\d+).(\\d+).(\\d+).([a-zA-Z_0-9\\-]+)";
36 static Pattern ID_PATTERN = Pattern.compile(ID_PATTERN_STRING);
37 static Pattern VERSION_PATTERN = Pattern.compile(VERSION_PATTERN_STRING);
38 static Pattern VERSIONED_ID_PATTERN = Pattern.compile("(" + ID_PATTERN_STRING + ")/" + VERSION_PATTERN_STRING + "");
42 /** OSGi versioned ID - id/major.minor.micro(.qualifier) */
46 * Create new bundle spec
48 * @param name optional name
51 public OntologySpec(String name, String versionedId) {
52 if (!VERSIONED_ID_PATTERN.matcher(versionedId).matches())
53 throw new IllegalArgumentException("not versioned id");
54 this.versionedId = versionedId;
55 this.name = name == null ? versionedId : name;
58 public String getName() {
62 public String getVersionedId() {
67 public int hashCode() {
68 return versionedId.hashCode();
72 public boolean equals(Object obj) {
73 if (obj == null) return false;
74 if (obj instanceof OntologySpec == false) return false;
75 OntologySpec other = (OntologySpec) obj;
76 return other.versionedId.equals(versionedId);
80 public String toString() {
81 return name + "("+versionedId+")";
84 /** A query that reads all bundles from the database, result is ordered by versioned id */
85 static Read<List<OntologySpec>> QUERY = new Read<List<OntologySpec>>() {
87 public List<OntologySpec> perform(ReadGraph g) throws DatabaseException {
88 DatabaseManagementResource DatabaseManagement = DatabaseManagementResource.getInstance(g);
89 Layer0 L0 = Layer0.getInstance(g);
90 Collection<Resource> tgs = g.getObjects(DatabaseManagement.InstalledGraphBundles, L0.ConsistsOf);
91 TreeMap<String, OntologySpec> result = new TreeMap<String, OntologySpec>();
92 for (Resource tg : tgs) {
93 if ( !g.isInstanceOf(tg, DatabaseManagement.GraphBundle) ) continue;
95 String name = g.getPossibleRelatedValue(tg, L0.HasName);
96 String versionedId = g.getPossibleRelatedValue(tg, DatabaseManagement.HasVersionedId);
97 OntologySpec bs = new OntologySpec(name, versionedId);
98 result.put( bs.versionedId, bs );
99 } catch (IllegalArgumentException iae) {
103 return new ArrayList<OntologySpec>(result.values());