import org.simantics.db.common.procedure.adapter.TransientCacheListener;
import org.simantics.db.common.request.ObjectsWithType;
import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.genericrelation.Dependencies;
import org.simantics.db.layer0.genericrelation.IndexQueries;
import org.simantics.db.layer0.util.Layer0Utils;
import org.simantics.db.service.CollectionSupport;
Layer0 L0 = Layer0.getInstance(graph);
HashSet<Resource> results = new HashSet<Resource>();
- String search = "Name:" + name;
+ String search = IndexQueries.quoteTerm(Dependencies.FIELD_NAME, name);
- for(Map<String, Object> entry : find(graph, model, search)) {
- Resource resource = (Resource)entry.get("Resource");
+ for(Resource resource : findResources(graph, model, search)) {
if(name.equals(graph.getPossibleRelatedValue(resource, L0.HasName, Bindings.STRING))) results.add(resource);
}
return results;
HashSet<Resource> results = new HashSet<Resource>();
Layer0 L0 = Layer0.getInstance(graph);
String typeName = graph.getRelatedValue(type, L0.HasName, Bindings.STRING);
- String search = "Types:" + IndexQueries.quoteTerm(typeName);
+ String search = IndexQueries.quoteTerm(Dependencies.FIELD_TYPES, typeName);
- for(Map<String, Object> entry : find(graph, model, search)) {
- Resource resource = (Resource)entry.get("Resource");
+ for(Resource resource : findResources(graph, model, search)) {
if(graph.isInstanceOf(resource, type)) results.add(resource);
}
return results;
HashSet<Resource> results = new HashSet<Resource>();
String typeName = graph.getRelatedValue(type, L0.HasName, Bindings.STRING);
- String search = "Types:" + IndexQueries.quoteTerm(typeName) + " AND Name:" + IndexQueries.quoteTerm(name);
+
+ String search = IndexQueries.and(IndexQueries.quoteTerm(Dependencies.FIELD_TYPES, typeName), IndexQueries.quoteTerm(Dependencies.FIELD_NAME, name));
- for(Map<String, Object> entry : find(graph, model, search)) {
- Resource resource = (Resource)entry.get("Resource");
+ for(Resource resource : findResources(graph, model, search)) {
if(graph.isInstanceOf(resource, type)) results.add(resource);
}
return results;
import org.simantics.scl.runtime.function.UnsaturatedFunction2;
/**
- * dependencies:
- * (ReadGraph, Resource model, String query) -> List<Map<String, Object>>
- * (ReadGraph, Resource model, String query, Integer maxResults) -> List<Map<String, Object>>
+ * dependencyResources:
+ * (ReadGraph, Resource model, String query) -> List<Resource>
+ * (ReadGraph, Resource model, String query, Integer maxResults) -> List<Resource>
*
* @author Antti Villberg
*/
public class DependencyResources extends FunctionImpl4<ReadGraph, Resource, String, Integer, Object> {
- public static final String FIELD_MODEL = "Model";
- public static final String FIELD_PARENT = "Parent";
- public static final String FIELD_RESOURCE = "Resource";
- public static final String FIELD_NAME = "Name";
- public static final String FIELD_TYPES = "Types";
-
protected Resource getIndexRelation(ReadGraph graph) {
return Layer0X.getInstance(graph).DependenciesRelation;
}
}
public static String escapeTerm(String field, String term, boolean escapeWildcards) {
- StringBuilder sb = new StringBuilder();
- return escapeTerm(field, term, escapeWildcards, sb).toString();
+ return escapeTerm(field, term, escapeWildcards, new StringBuilder()).toString();
}
-
+
+ public static StringBuilder quoteTerm(String field, String term, StringBuilder result) {
+ if (field != null)
+ result.append(field).append(':');
+ result.append("\"");
+ result.append(term.replaceAll("(\"|\\\\)", "\\\\$0"));
+ result.append("\"");
+ return result;
+ }
+
public static String quoteTerm(String term) {
- StringBuilder sb = new StringBuilder();
- sb.append("\"");
- sb.append(term.replaceAll("(\"|\\\\)", "\\\\$0"));
- sb.append("\"");
+ return quoteTerm(null, term, new StringBuilder(term.length()*2)).toString();
+ }
+
+ public static String quoteTerm(String field, String term) {
+ return quoteTerm(field, term,
+ new StringBuilder(
+ term.length()*2
+ + (field != null ? field.length() + 1 : 0))
+ ).toString();
+ }
+
+ private static String join(String withString, String... exps) {
+ if (exps.length == 0)
+ return "";
+ StringBuilder sb = new StringBuilder(128);
+ for (int i = 0; i < exps.length - 1; ++i) {
+ sb.append(exps[i]).append(withString);
+ }
+ sb.append(exps[exps.length - 1]);
return sb.toString();
}
+ public static String and(String exp1, String exp2) {
+ return exp1 + " AND " + exp2;
+ }
+
+ public static String and(String... exps) {
+ return join(" AND ", exps);
+ }
+
+ public static String or(String exp1, String exp2) {
+ return exp1 + " OR " + exp2;
+ }
+
+ public static String or(String... exps) {
+ return join(" OR ", exps);
+ }
+
// public static void main(String[] args) {
// System.out.println("esc: " + escape("AND01", true, true));
// System.out.println("esc: " + escape("AND 01", true, true));
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
-import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import org.eclipse.core.runtime.IProgressMonitor;
import org.simantics.db.exception.CancelTransactionException;
import org.simantics.db.exception.DatabaseException;
import org.simantics.db.layer0.genericrelation.Dependencies;
+import org.simantics.db.layer0.genericrelation.IndexQueries;
import org.simantics.db.layer0.request.PossibleModel;
import org.simantics.event.Activator;
import org.simantics.event.ontology.EventResource;
if (model == null)
throw new CancelTransactionException();
- indexFunction = graph.adapt(L0X.Dependencies, Function.class);
+ indexFunction = graph.adapt(L0X.DependencyResources, Function.class);
if (!initialEventsResolved) {
MapList<String, Resource> initialEventsBySource = new MapList<String,Resource>();
monitor.subTask("Resolve events with source " + sourceName);
if (DEBUG)
System.out.println(EventSourceResolver.this + ": resolving source name " + sourceName);
- List<Map<String, Object>> results = (List<Map<String, Object>>) indexFunction.apply(graph, model, "Name:" + sourceName);
- for (Map<String, Object> result : results) {
- Resource r = (Resource) result.get(Dependencies.FIELD_RESOURCE);
+ List<Resource> results = (List<Resource>) indexFunction.apply(graph, model,
+ IndexQueries.quoteTerm(Dependencies.FIELD_NAME, sourceName));
+ for (Resource r : results) {
if (eventSourceFilter != null && !eventSourceFilter.accept(graph, r))
continue;
Resource rModel = graph.sync(new PossibleModel(r));
import org.simantics.db.common.request.UnaryRead;
import org.simantics.db.common.utils.NameUtils;
import org.simantics.db.exception.DatabaseException;
+import org.simantics.db.layer0.genericrelation.Dependencies;
import org.simantics.db.layer0.genericrelation.IndexQueries;
import org.simantics.db.service.GraphChangeListenerSupport;
import org.simantics.layer0.Layer0;
*
* <p>
* The type of the function is expected to be:
- * <code>ReadGraph => Resource -> String -> Integer -> List<Map<String,Object>></code>
+ * <code>ReadGraph => Resource -> String -> Integer -> List<Resource></code>
*
* @author Tuukka Lehtonen
*
synchronized (this) {
- String search = "Name:" + lowercaseName + "*";
+ String search = IndexQueries.escapeTerm(Dependencies.FIELD_NAME_SEARCH, lowercaseName, true) + "*"; //$NON-NLS-1$
@SuppressWarnings("unchecked")
List<Resource> components = (List<Resource>) index.apply(graph, indexRoot, search, Integer.MAX_VALUE);
synchronized (this) {
- String search = "Name:" + proposition + "*";
+ String search = Dependencies.FIELD_NAME_SEARCH + ":" + IndexQueries.escape(proposition.toLowerCase(), true) + "*"; //$NON-NLS-1$ //$NON-NLS-2$
Set<String> reserved = graph.syncRequest(new ComponentsRequest(new Tuple4(indexRoot, index, search, getComparator())), TransientCacheAsyncListener.instance());
if (propositionPreFilter != null)
proposition = propositionPreFilter.apply(proposition);
- String search = "Name:" + IndexQueries.quoteTerm( proposition );
+ String search = IndexQueries.quoteTerm(Dependencies.FIELD_NAME_SEARCH, proposition.toLowerCase());
@SuppressWarnings("unchecked")
List<Resource> components = (List<Resource>) index.apply(graph, indexRoot, search, Integer.MAX_VALUE);