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.db.layer0.adapter.impl;
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.Collections;
17 import java.util.List;
20 import org.simantics.databoard.Bindings;
21 import org.simantics.db.ReadGraph;
22 import org.simantics.db.Resource;
23 import org.simantics.db.common.primitiverequest.Adapter;
24 import org.simantics.db.common.procedure.adapter.TransientCacheListener;
25 import org.simantics.db.common.request.ObjectsWithType;
26 import org.simantics.db.common.request.TernaryRead;
27 import org.simantics.db.exception.DatabaseException;
28 import org.simantics.db.layer0.adapter.Instances;
29 import org.simantics.db.layer0.genericrelation.IndexQueries;
30 import org.simantics.db.layer0.util.Layer0Utils;
31 import org.simantics.db.request.ReadExt;
32 import org.simantics.db.request.RequestFlags;
33 import org.simantics.db.service.CollectionSupport;
34 import org.simantics.layer0.Layer0;
35 import org.simantics.operation.Layer0X;
36 import org.simantics.scl.runtime.function.Function;
38 import gnu.trove.set.hash.THashSet;
41 * @author Antti Villberg
42 * @author Tuukka Lehtonen
44 public class EntityInstances implements Instances {
46 private static final boolean TRACE_QUERIES = false;
48 private final Resource type;
50 public EntityInstances(Resource type) {
55 public Collection<Resource> find(ReadGraph graph, Resource index) throws DatabaseException {
56 return find(graph, index, "");
60 * A (cacheable) query to optimize single index queries for immutable
61 * indexes such as ontologies.
63 public static class QueryIndex extends TernaryRead<Resource, Resource, String, List<Resource>> implements ReadExt {
65 public QueryIndex(Resource index, Resource type, String filter) {
66 super(index, type, filter);
70 public List<Resource> perform(ReadGraph graph)
71 throws DatabaseException {
72 Resource type = parameter2;
74 Layer0 L0 = Layer0.getInstance(graph);
75 Layer0X L0X = Layer0X.getInstance(graph);
76 String typeName = graph.getRelatedValue(type, L0.HasName);
77 if (typeName.isEmpty())
78 return Collections.emptyList();
80 @SuppressWarnings({ "unchecked", "rawtypes" })
81 Function dependencyResources = graph.syncRequest(new Adapter(L0X.DependencyResources, Function.class), TransientCacheListener.<Function>instance());
83 StringBuilder filtersb = new StringBuilder();
84 filtersb.append("Types:*").append( IndexQueries.escape( typeName, true ) );
85 if (parameter3.length() > 0)
86 filtersb.append(" AND ").append( parameter3 );
87 String filter = filtersb.toString();
90 System.out.println("EntityInstances.QueryIndex: finding " + filter + " from index " + graph.getPossibleURI(parameter));
91 //new Exception("EntityInstances: finding " + filter + " from index " + graph.getPossibleURI(parameter)).printStackTrace();
94 @SuppressWarnings("unchecked")
95 List<Resource> results = (List<Resource>)dependencyResources.apply(graph, parameter, filter);
96 if (results == null || results.isEmpty())
97 return Collections.emptyList();
100 System.out.println(" EntityInstances.QueryIndex: got " + results.size() + " results");
102 // // TreeSet to keep the results in deterministic order.
103 // Set<Resource> resultSet = new TreeSet<Resource>();
104 // for (Map<String, Object> entry : results) {
105 // Resource res = (Resource)entry.get("Resource");
106 // if (res != null && !resultSet.contains(res))
107 // resultSet.add(res);
110 CollectionSupport coll = graph.getService(CollectionSupport.class);
111 List<Resource> result = coll.createList();
113 for (Resource res : Layer0Utils.sortByCluster(graph, results)) {
114 if (graph.isInstanceOf(res, type))
119 System.out.println(" EntityInstances.QueryIndex: got " + results.size() + " unique type-matching results");
126 public String toString() {
127 return "QueryIndex " + parameter + " " + parameter2 + " " + parameter3;
131 public boolean isImmutable(ReadGraph graph) throws DatabaseException {
132 return graph.isImmutable(parameter);
136 public int getType() {
137 return RequestFlags.IMMEDIATE_UPDATE;
142 private List<Resource> findRec(ReadGraph graph, Resource index, String filter, Set<Resource> visited) throws DatabaseException {
144 if(!visited.add(index)) return Collections.emptyList();
146 CollectionSupport coll = graph.getService(CollectionSupport.class);
148 List<Resource> indexResult = graph.syncRequest(new QueryIndex(index, type, filter), TransientCacheListener.<List<Resource>>instance());
150 Layer0 L0 = Layer0.getInstance(graph);
151 Collection<Resource> linkedRoots = graph.syncRequest(new ObjectsWithType(index, L0.IsLinkedTo, L0.IndexRoot));
152 if (linkedRoots.isEmpty())
155 List<Resource> result = indexResult;
156 for (Resource dep : linkedRoots) {
157 Collection<Resource> linkedIndexResults = findRec(graph, dep, filter, visited);
158 if (linkedIndexResults.isEmpty())
160 if (result == indexResult) {
161 result = coll.createList();
162 result.addAll(indexResult);
165 result.addAll(linkedIndexResults);
173 public Collection<Resource> find(ReadGraph graph, Resource index, String filter) throws DatabaseException {
174 CollectionSupport coll = graph.getService(CollectionSupport.class);
176 THashSet<Resource> visited = new THashSet<>();
177 List<Resource> rec = findRec(graph, index, filter, visited);
178 for(Resource global : Layer0Utils.listGlobalOntologies(graph)) {
179 if(!visited.add(global)) continue;
180 List<Resource> rs = graph.syncRequest(new QueryIndex(global, type, filter), TransientCacheListener.<List<Resource>>instance());
181 if(rec.isEmpty() && !rs.isEmpty()) {
182 // TODO: rec could be an immutable empty list
183 rec = new ArrayList<Resource>();
187 Collection<Resource> result = coll.asSortedList(rec);
192 public Collection<Resource> findByName(ReadGraph graph, Resource model,
193 String name) throws DatabaseException {
194 Layer0 L0 = Layer0.getInstance(graph);
195 CollectionSupport coll = graph.getService(CollectionSupport.class);
196 List<Resource> results = coll.createList();
197 for(Resource match : find(graph, model, name)) {
198 if(name.equals(graph.getPossibleRelatedValue(match, L0.HasName, Bindings.STRING))) results.add(match);