private void applyDirectoryEntityJsonObjectEqualityToQuery(Query graphQuery, String propertyName, JSONObject value0, User user) { String directoryEntityId = value0.optString("directoryEntityId", null); if (directoryEntityId != null) { graphQuery.has(propertyName, Compare.EQUAL, directoryEntityId); } else if (value0.optBoolean("currentUser", false)) { directoryEntityId = directoryRepository.getDirectoryEntityId(user); graphQuery.has(propertyName, Compare.EQUAL, directoryEntityId); } else { throw new VisalloException("Invalid directory entity JSONObject filter:\n" + value0.toString(2)); } }
private void updateQueryWithDataTypeFilter(Query graphQuery, JSONObject obj, User user, SearchOptions searchOptions) { String dataType = obj.getString("dataType"); String predicateString = obj.optString("predicate"); PropertyType propertyType = PropertyType.valueOf(dataType); try { if ("has".equals(predicateString)) { graphQuery.has(PropertyType.getTypeClass(propertyType)); } else if ("hasNot".equals(predicateString)) { graphQuery.hasNot(PropertyType.getTypeClass(propertyType)); } else if ("in".equals(predicateString)) { JSONArray values = obj.getJSONArray("values"); graphQuery.has(PropertyType.getTypeClass(propertyType), Contains.IN, JSONUtil.toList(values)); } else { JSONArray values = obj.getJSONArray("values"); Object value0 = jsonValueToObject(values, propertyType, 0); if (PropertyType.GEO_LOCATION.equals(propertyType)) { GeoCompare geoComparePredicate = GeoCompare.valueOf(predicateString.toUpperCase()); graphQuery.has(GeoShape.class, geoComparePredicate, value0); } else { throw new UnsupportedOperationException("Data type queries are not yet supported for type: " + dataType); } } } catch (ParseException ex) { throw new VisalloException("Could not update query with filter:\n" + obj.toString(2), ex); } }
private void applyDoubleEqualityToQuery(Query graphQuery, JSONObject obj, Object value0) throws ParseException { String propertyName = obj.getString("propertyName"); JSONObject metadata = obj.has("metadata") ? obj.getJSONObject("metadata") : null; if (metadata != null && metadata.has("http://visallo.org#inputPrecision") && value0 instanceof Double) { double doubleParam = (double) value0; int inputPrecision = Math.max(metadata.getInt("http://visallo.org#inputPrecision"), 0); double lowerBound = Precision.round(doubleParam, inputPrecision, BigDecimal.ROUND_DOWN); double upperBound = Precision.equals(doubleParam, lowerBound, Precision.EPSILON) ? lowerBound + Math.pow(10, -inputPrecision) : Precision.round(doubleParam, inputPrecision, BigDecimal.ROUND_UP); graphQuery.has(propertyName, Compare.GREATER_THAN_EQUAL, (lowerBound - Precision.EPSILON)); graphQuery.has(propertyName, Compare.LESS_THAN, (upperBound + Precision.EPSILON)); } else { graphQuery.has(propertyName, Compare.EQUAL, value0); } }
String propertyName = obj.getString("propertyName"); if ("has".equals(predicateString)) { graphQuery.has(propertyName); } else if ("hasNot".equals(predicateString)) { graphQuery.hasNot(propertyName); } else if ("in".equals(predicateString)) { graphQuery.has(propertyName, Contains.IN, JSONUtil.toList(obj.getJSONArray("values"))); } else { PropertyType propertyDataType = PropertyType.convert(obj.optString("propertyDataType")); graphQuery.has(propertyName, TextPredicate.CONTAINS, value0); } else if (PropertyType.DATE.equals(propertyDataType)) { applyDateToQuery(graphQuery, obj, predicateString, values, searchOptions); } else if (PropertyType.BOOLEAN.equals(propertyDataType)) { graphQuery.has(propertyName, Compare.EQUAL, value0); } else if (PropertyType.GEO_LOCATION.equals(propertyDataType)) { GeoCompare geoComparePredicate = GeoCompare.valueOf(predicateString.toUpperCase()); graphQuery.has(propertyName, geoComparePredicate, value0); } else if ("<".equals(predicateString)) { graphQuery.has(propertyName, Compare.LESS_THAN, value0); } else if (">".equals(predicateString)) { graphQuery.has(propertyName, Compare.GREATER_THAN, value0); } else if ("range".equals(predicateString)) { graphQuery.has(propertyName, Compare.GREATER_THAN_EQUAL, value0); graphQuery.has(propertyName, Compare.LESS_THAN_EQUAL, jsonValueToObject(values, propertyDataType, 1)); } else if ("=".equals(predicateString) || "equal".equals(predicateString)) { if (PropertyType.DIRECTORY_ENTITY.equals(propertyDataType) && value0 instanceof JSONObject) { applyDoubleEqualityToQuery(graphQuery, obj, value0);
@Override public void addConceptTypeFilterToQuery(Query query, Collection<ElementTypeFilter> filters, String workspaceId) { checkNotNull(query, "query cannot be null"); checkNotNull(filters, "filters cannot be null"); if (filters.isEmpty()) { return; } Set<String> conceptIds = new HashSet<>(filters.size()); for (ElementTypeFilter filter : filters) { Concept concept = getConceptByIRI(filter.iri, workspaceId); checkNotNull(concept, "Could not find concept with IRI: " + filter.iri); conceptIds.add(concept.getIRI()); if (filter.includeChildNodes) { Set<Concept> childConcepts = getConceptAndAllChildren(concept, workspaceId); conceptIds.addAll(childConcepts.stream().map(Concept::getIRI).collect(Collectors.toSet())); } } query.has(VisalloProperties.CONCEPT_TYPE.getPropertyName(), Contains.IN, conceptIds); }
@Override public User findByPasswordResetToken(String token) { QueryResultsIterable<Vertex> userVertices = graph.query(authorizations) .has(UserVisalloProperties.PASSWORD_RESET_TOKEN.getPropertyName(), token) .has(VisalloProperties.CONCEPT_TYPE.getPropertyName(), userConceptId) .vertices(); Vertex user = singleOrDefault(userVertices, null); return createFromVertex(user); }
@Override public User findByUsername(String username) { username = formatUsername(username); Iterable<Vertex> vertices = graph.query(authorizations) .has(UserVisalloProperties.USERNAME.getPropertyName(), username) .has(VisalloProperties.CONCEPT_TYPE.getPropertyName(), userConceptId) .vertices(); Vertex userVertex = singleOrDefault(vertices, null); if (userVertex == null) { return null; } userVertexCache.put(userVertex.getId(), userVertex); return createFromVertex(userVertex); }
@Override public Iterable<Relationship> getRelationshipsByIRI(List<String> relationshipIRIs, String workspaceId) { QueryResultsIterable<Vertex> vertices = getGraph().query(getAuthorizations(workspaceId)) .has(VisalloProperties.CONCEPT_TYPE.getPropertyName(), OntologyRepository.TYPE_RELATIONSHIP) .has(OntologyProperties.ONTOLOGY_TITLE.getPropertyName(), Contains.IN, relationshipIRIs) .vertices(); return transformRelationships(vertices, workspaceId); }
@Override protected List<Concept> findLoadedConceptsByIntent(String intent, String workspaceId) { QueryResultsIterable<Vertex> vertices = getGraph().query(getAuthorizations(workspaceId)) .has(VisalloProperties.CONCEPT_TYPE.getPropertyName(), OntologyRepository.TYPE_CONCEPT) .has(OntologyProperties.INTENT.getPropertyName(), intent) .vertices(); return transformConcepts(vertices, workspaceId); }
@Override public Iterable<Concept> getConceptsByIRI(List<String> conceptIRIs, String workspaceId) { QueryResultsIterable<Vertex> vertices = getGraph().query(getAuthorizations(workspaceId)) .has(VisalloProperties.CONCEPT_TYPE.getPropertyName(), OntologyRepository.TYPE_CONCEPT) .has(OntologyProperties.ONTOLOGY_TITLE.getPropertyName(), Contains.IN, conceptIRIs) .vertices(); return transformConcepts(vertices, workspaceId); }
@Override protected List<Relationship> findLoadedRelationshipsByIntent(String intent, String workspaceId) { QueryResultsIterable<Vertex> vertices = getGraph().query(getAuthorizations(workspaceId)) .has(VisalloProperties.CONCEPT_TYPE.getPropertyName(), OntologyRepository.TYPE_RELATIONSHIP) .has(OntologyProperties.INTENT.getPropertyName(), intent) .vertices(); return transformRelationships(vertices, workspaceId); }
@Override public Iterable<OntologyProperty> getPropertiesByIRI(List<String> propertyIRIs, String workspaceId) { QueryResultsIterable<Vertex> vertices = getGraph().query(getAuthorizations(workspaceId)) .has(VisalloProperties.CONCEPT_TYPE.getPropertyName(), OntologyRepository.TYPE_PROPERTY) .has(OntologyProperties.ONTOLOGY_TITLE.getPropertyName(), Contains.IN, propertyIRIs) .vertices(); return transformProperties(vertices, workspaceId); }
@Override public List<OntologyProperty> getPropertiesByIntent(String intent, String workspaceId) { QueryResultsIterable<Vertex> vertices = getGraph().query(getAuthorizations(workspaceId)) .has(VisalloProperties.CONCEPT_TYPE.getPropertyName(), OntologyRepository.TYPE_PROPERTY) .has(OntologyProperties.INTENT.getPropertyName(), intent) .vertices(); return transformProperties(vertices, workspaceId); }
@Override public Iterable<User> findByStatus(int skip, int limit, UserStatus status) { QueryResultsIterable<Vertex> userVertices = graph.query(authorizations) .has(VisalloProperties.CONCEPT_TYPE.getPropertyName(), userConceptId) .has(UserVisalloProperties.STATUS.getPropertyName(), status.toString()) .skip(skip) .limit(limit) .vertices(); return new ConvertingIterable<Vertex, User>(userVertices) { @Override protected User convert(Vertex vertex) { return createFromVertex(vertex); } }; }
public JSONObject getAverages(int minutes, Graph graph, Authorizations authorizations) { Date minutesAgo = new Date(System.currentTimeMillis() - minutes * 60 * 1000); Query q = graph.query(authorizations) .has(VisalloProperties.CONCEPT_TYPE.getPropertyName(), PingOntology.IRI_CONCEPT_PING) .has(PingOntology.CREATE_DATE.getPropertyName(), Compare.GREATER_THAN, minutesAgo) .limit(0); q.addAggregation(new StatisticsAggregation(PingOntology.SEARCH_TIME_MS.getPropertyName(), PingOntology.SEARCH_TIME_MS.getPropertyName())); q.addAggregation(new StatisticsAggregation(PingOntology.RETRIEVAL_TIME_MS.getPropertyName(), PingOntology.RETRIEVAL_TIME_MS.getPropertyName())); q.addAggregation(new StatisticsAggregation(PingOntology.GRAPH_PROPERTY_WORKER_WAIT_TIME_MS.getPropertyName(), PingOntology.GRAPH_PROPERTY_WORKER_WAIT_TIME_MS.getPropertyName())); q.addAggregation(new StatisticsAggregation(PingOntology.LONG_RUNNING_PROCESS_WAIT_TIME_MS.getPropertyName(), PingOntology.LONG_RUNNING_PROCESS_WAIT_TIME_MS.getPropertyName())); QueryResultsIterable<Vertex> vertices = q.vertices(); StatisticsResult searchTimeAgg = vertices.getAggregationResult(PingOntology.SEARCH_TIME_MS.getPropertyName(), StatisticsResult.class); StatisticsResult retrievalTimeAgg = vertices.getAggregationResult(PingOntology.RETRIEVAL_TIME_MS.getPropertyName(), StatisticsResult.class); StatisticsResult gpwWaitTimeAgg = vertices.getAggregationResult(PingOntology.GRAPH_PROPERTY_WORKER_WAIT_TIME_MS.getPropertyName(), StatisticsResult.class); StatisticsResult lrpWaitTimeAgg = vertices.getAggregationResult(PingOntology.LONG_RUNNING_PROCESS_WAIT_TIME_MS.getPropertyName(), StatisticsResult.class); JSONObject json = new JSONObject(); json.put("pingCount", searchTimeAgg.getCount()); json.put("averageSearchTime", searchTimeAgg.getAverage()); json.put("averageRetrievalTime", retrievalTimeAgg.getAverage()); json.put("graphPropertyWorkerCount", gpwWaitTimeAgg.getCount()); json.put("averageGraphPropertyWorkerWaitTime", gpwWaitTimeAgg.getAverage()); json.put("longRunningProcessCount", lrpWaitTimeAgg.getCount()); json.put("averageLongRunningProcessWaitTime", lrpWaitTimeAgg.getAverage()); return json; }
@Test public void testGraphQueryForEdgesUsingInOutVertexIds() { Vertex v1 = graph.addVertex("v1", VISIBILITY_A, AUTHORIZATIONS_A); Vertex v2 = graph.addVertex("v2", VISIBILITY_A, AUTHORIZATIONS_A); Vertex v3 = graph.addVertex("v3", VISIBILITY_A, AUTHORIZATIONS_A); graph.addEdge("e1", v1, v2, LABEL_LABEL1, VISIBILITY_A, AUTHORIZATIONS_A); graph.addEdge("e2", v1, v3, LABEL_LABEL1, VISIBILITY_A, AUTHORIZATIONS_A); graph.addEdge("e3", v3, v1, LABEL_LABEL1, VISIBILITY_A, AUTHORIZATIONS_A); graph.flush(); QueryResultsIterable<Edge> edges = graph.query(AUTHORIZATIONS_A) .has(Edge.OUT_VERTEX_ID_PROPERTY_NAME, "v1") .has(Edge.IN_VERTEX_ID_PROPERTY_NAME, "v2") .edges(); assertEdgeIdsAnyOrder(edges, "e1"); edges = graph.query(AUTHORIZATIONS_A) .has(Edge.OUT_VERTEX_ID_PROPERTY_NAME, "v1") .edges(); assertEdgeIdsAnyOrder(edges, "e1", "e2"); }
@Test public void testGraphQueryForEdgesUsingInOutVertexIds() { Vertex v1 = graph.addVertex("v1", VISIBILITY_A, AUTHORIZATIONS_A); Vertex v2 = graph.addVertex("v2", VISIBILITY_A, AUTHORIZATIONS_A); Vertex v3 = graph.addVertex("v3", VISIBILITY_A, AUTHORIZATIONS_A); graph.addEdge("e1", v1, v2, LABEL_LABEL1, VISIBILITY_A, AUTHORIZATIONS_A); graph.addEdge("e2", v1, v3, LABEL_LABEL1, VISIBILITY_A, AUTHORIZATIONS_A); graph.addEdge("e3", v3, v1, LABEL_LABEL1, VISIBILITY_A, AUTHORIZATIONS_A); graph.flush(); QueryResultsIterable<Edge> edges = graph.query(AUTHORIZATIONS_A) .has(Edge.OUT_VERTEX_ID_PROPERTY_NAME, "v1") .has(Edge.IN_VERTEX_ID_PROPERTY_NAME, "v2") .edges(); assertEdgeIdsAnyOrder(edges, "e1"); edges = graph.query(AUTHORIZATIONS_A) .has(Edge.OUT_VERTEX_ID_PROPERTY_NAME, "v1") .edges(); assertEdgeIdsAnyOrder(edges, "e1", "e2"); }
.has("name", Compare.EQUAL, "Joe") .vertices(); Assert.assertEquals(1, count(vertices));
.has("name", Compare.EQUAL, "Joe") .vertices(); Assert.assertEquals(1, count(vertices));