private String getParentVertexId(Vertex vertex, Authorizations authorizations) { Iterable<String> parentIds = vertex.getVertexIds(Direction.OUT, LabelName.IS_A.toString(), authorizations); return parentIds == null ? null : Iterables.getOnlyElement(parentIds, null); }
private Relationship toVertexiumRelationship(String parentIRI, Vertex relationshipVertex, List<OntologyProperty> properties, Map<String, String> relatedVertexIdToIriMap, String workspaceId) { Authorizations authorizations = getAuthorizations(workspaceId); Set<String> domainVertexIds = IterableUtils.toSet(relationshipVertex.getVertexIds(Direction.IN, LabelName.HAS_EDGE.toString(), authorizations)); List<String> domainIris = domainVertexIds.stream().map(relatedVertexIdToIriMap::get).collect(Collectors.toList()); Set<String> rangeVertexIds = IterableUtils.toSet(relationshipVertex.getVertexIds(Direction.OUT, LabelName.HAS_EDGE.toString(), authorizations)); List<String> rangeIris = rangeVertexIds.stream().map(relatedVertexIdToIriMap::get).collect(Collectors.toList()); Set<String> inverseOfVertexIds = IterableUtils.toSet(relationshipVertex.getVertexIds(Direction.OUT, LabelName.INVERSE_OF.toString(), getAuthorizations(workspaceId))); List<String> inverseOfIRIs = inverseOfVertexIds.stream().map(relatedVertexIdToIriMap::get).collect(Collectors.toList()); return createRelationship(parentIRI, relationshipVertex, inverseOfIRIs, domainIris, rangeIris, properties, workspaceId); }
private List<String> getAssociatedElements(String elementType) { Iterable<Vertex> vertices = vertex.getVertices(Direction.BOTH, LabelName.HAS_PROPERTY.toString(), vertex.getAuthorizations()); List<String> result = StreamSupport.stream(vertices.spliterator(), false) .filter(v -> elementType.equals(VisalloProperties.CONCEPT_TYPE.getPropertyValue(v))) .map(OntologyProperties.ONTOLOGY_TITLE::getPropertyValue) .collect(Collectors.toList()); CloseableUtils.closeQuietly(vertices); return result; } }
@Override protected void getOrCreateInverseOfRelationship(Relationship fromRelationship, Relationship inverseOfRelationship) { checkNotNull(fromRelationship, "fromRelationship is required"); checkNotNull(fromRelationship, "inverseOfRelationship is required"); VertexiumRelationship fromRelationshipSg = (VertexiumRelationship) fromRelationship; VertexiumRelationship inverseOfRelationshipSg = (VertexiumRelationship) inverseOfRelationship; Vertex fromVertex = fromRelationshipSg.getVertex(); checkNotNull(fromVertex, "fromVertex is required"); Vertex inverseVertex = inverseOfRelationshipSg.getVertex(); checkNotNull(inverseVertex, "inverseVertex is required"); User user = getSystemUser(); findOrAddEdge(fromVertex, inverseVertex, LabelName.INVERSE_OF.toString(), user, null); findOrAddEdge(inverseVertex, fromVertex, LabelName.INVERSE_OF.toString(), user, null); }
private Vertex getParentVertex(Vertex vertex, String workspaceId) { try { return Iterables.getOnlyElement(vertex.getVertices(Direction.OUT, LabelName.IS_A.toString(), getAuthorizations(workspaceId)), null); } catch (IllegalArgumentException iae) { throw new IllegalStateException(String.format( "Unexpected number of parents for concept %s", OntologyProperties.TITLE.getPropertyValue(vertex) ), iae); } }
private List<Concept> transformConcepts(Iterable<Vertex> vertices, String workspaceId) { Authorizations authorizations = getAuthorizations(workspaceId); List<Vertex> filtered = StreamSupport.stream(vertices.spliterator(), false) .filter(vertex -> VisalloProperties.CONCEPT_TYPE.getPropertyValue(vertex, "").equals(TYPE_CONCEPT)) .collect(Collectors.toList()); Map<String, String> parentVertexIdToIRI = buildParentIdToIriMap(filtered, authorizations); List<String> allPropertyVertexIds = filtered.stream() .flatMap(vertex -> StreamSupport.stream(vertex.getVertexIds(Direction.OUT, LabelName.HAS_PROPERTY.toString(), authorizations).spliterator(), false) ).distinct().collect(Collectors.toList()); List<OntologyProperty> ontologyProperties = transformProperties(getGraph().getVertices(allPropertyVertexIds, authorizations), workspaceId); Map<String, OntologyProperty> ontologyPropertiesByVertexId = ontologyProperties.stream() .collect(Collectors.toMap( ontologyProperty -> ((VertexiumOntologyProperty) ontologyProperty).getVertex().getId(), ontologyProperty -> ontologyProperty )); return filtered.stream().map(vertex -> { String parentVertexId = getParentVertexId(vertex, authorizations); String parentIRI = parentVertexId == null ? null : parentVertexIdToIRI.get(parentVertexId); Iterable<String> propertyVertexIds = vertex.getVertexIds(Direction.OUT, LabelName.HAS_PROPERTY.toString(), authorizations); List<OntologyProperty> conceptProperties = StreamSupport.stream(propertyVertexIds.spliterator(), false) .map(ontologyPropertiesByVertexId::get) .filter(Objects::nonNull) .collect(Collectors.toList()); return createConcept(vertex, conceptProperties, parentIRI, workspaceId); }).collect(Collectors.toList()); }
.flatMap(vertex -> StreamUtils.stream( vertex.getVertexIds(Direction.OUT, LabelName.IS_A.toString(), authorizations), vertex.getVertexIds(Direction.IN, LabelName.HAS_EDGE.toString(), authorizations), vertex.getVertexIds(Direction.OUT, LabelName.HAS_EDGE.toString(), authorizations), vertex.getVertexIds(Direction.OUT, LabelName.INVERSE_OF.toString(), authorizations) StreamSupport.stream(vertex.getVertexIds(Direction.OUT, LabelName.HAS_PROPERTY.toString(), authorizations).spliterator(), false) ).distinct().collect(Collectors.toList()); List<OntologyProperty> ontologyProperties = transformProperties(getGraph().getVertices(allPropertyVertexIds, authorizations), workspaceId); String parentIRI = parentVertexId == null ? null : relatedVertexIdToIriMap.get(parentVertexId); Iterable<String> propertyVertexIds = vertex.getVertexIds(Direction.OUT, LabelName.HAS_PROPERTY.toString(), authorizations); List<OntologyProperty> properties = StreamSupport.stream(propertyVertexIds.spliterator(), false) .map(ontologyPropertiesByVertexId::get)
@Override protected List<Concept> getChildConcepts(Concept concept, String workspaceId) { Vertex conceptVertex = ((VertexiumConcept) concept).getVertex(); return toConcepts(conceptVertex.getVertices(Direction.IN, LabelName.IS_A.toString(), getAuthorizations(workspaceId)), workspaceId); }
@Override protected List<Relationship> getChildRelationships(Relationship relationship, String workspaceId) { Vertex relationshipVertex = ((VertexiumRelationship) relationship).getVertex(); return transformRelationships(relationshipVertex.getVertices(Direction.IN, LabelName.IS_A.toString(), getAuthorizations(workspaceId)), workspaceId); }
/** * Overridable so subclasses can supply a custom implementation of OntologyProperty. */ protected OntologyProperty createOntologyProperty( Vertex propertyVertex, ImmutableList<String> dependentPropertyIris, PropertyType propertyType, String workspaceId ) { if (propertyType.equals(PropertyType.EXTENDED_DATA_TABLE)) { Authorizations authorizations = getAuthorizations(workspaceId); VertexiumExtendedDataTableOntologyProperty result = new VertexiumExtendedDataTableOntologyProperty(propertyVertex, dependentPropertyIris, workspaceId); Iterable<String> tablePropertyIris = propertyVertex.getVertexIds(Direction.OUT, LabelName.HAS_PROPERTY.toString(), authorizations); for (String tablePropertyIri : tablePropertyIris) { result.addProperty(tablePropertyIri.substring(VertexiumOntologyRepository.ID_PREFIX_PROPERTY.length())); } return result; } else { return new VertexiumOntologyProperty(propertyVertex, dependentPropertyIris, workspaceId); } }
@Override public void addDomainConceptsToRelationshipType(String relationshipIri, List<String> conceptIris, User user, String workspaceId) { checkPrivileges(user, workspaceId); try (GraphUpdateContext ctx = graphRepository.beginGraphUpdate(Priority.LOW, user, getAuthorizations(workspaceId))) { ctx.setPushOnQueue(false); VertexiumRelationship relationship = (VertexiumRelationship) getRelationshipByIRI(relationshipIri, workspaceId); Vertex relationshipVertex = relationship.getVertex(); if (!isPublic(workspaceId) && relationship.getSandboxStatus() != SandboxStatus.PRIVATE) { throw new UnsupportedOperationException("Sandboxed updating of domain iris is not currently supported for published relationships"); } Iterable<Concept> concepts = getConceptsByIRI(conceptIris, workspaceId); for (Concept concept : concepts) { checkNotNull(concept, "concepts cannot have null values"); findOrAddEdge(ctx, ((VertexiumConcept) concept).getVertex(), relationshipVertex, LabelName.HAS_EDGE.toString()); } } }
@Override public void addRangeConceptsToRelationshipType(String relationshipIri, List<String> conceptIris, User user, String workspaceId) { checkPrivileges(user, workspaceId); try (GraphUpdateContext ctx = graphRepository.beginGraphUpdate(Priority.LOW, user, getAuthorizations(workspaceId))) { ctx.setPushOnQueue(false); VertexiumRelationship relationship = (VertexiumRelationship) getRelationshipByIRI(relationshipIri, workspaceId); Vertex relationshipVertex = relationship.getVertex(); if (!isPublic(workspaceId) && relationship.getSandboxStatus() != SandboxStatus.PRIVATE) { throw new UnsupportedOperationException("Sandboxed updating of range iris is not currently supported for published relationships"); } Iterable<Concept> concepts = getConceptsByIRI(conceptIris, workspaceId); for (Concept concept : concepts) { checkNotNull(concept, "concepts cannot have null values"); findOrAddEdge(ctx, relationshipVertex, ((VertexiumConcept) concept).getVertex(), LabelName.HAS_EDGE.toString()); } } }
@Override public void updatePropertyDomainIris(OntologyProperty property, Set<String> domainIris, User user, String workspaceId) { VertexiumOntologyProperty vertexiumProperty = (VertexiumOntologyProperty) property; if (!isPublic(workspaceId) && property.getSandboxStatus() != SandboxStatus.PRIVATE) { throw new UnsupportedOperationException("Sandboxed updating of domain iris is not currently supported for published properties"); } Iterable<EdgeVertexPair> existingConcepts = vertexiumProperty.getVertex().getEdgeVertexPairs(Direction.BOTH, LabelName.HAS_PROPERTY.toString(), getAuthorizations(workspaceId)); for (EdgeVertexPair existingConcept : existingConcepts) { String conceptIri = OntologyProperties.ONTOLOGY_TITLE.getPropertyValue(existingConcept.getVertex()); if (!domainIris.remove(conceptIri)) { getGraph().softDeleteEdge(existingConcept.getEdge(), getAuthorizations(workspaceId)); } } for (String domainIri : domainIris) { Vertex domainVertex; Concept concept = getConceptByIRI(domainIri, workspaceId); if (concept != null) { domainVertex = ((VertexiumConcept) concept).getVertex(); } else { Relationship relationship = getRelationshipByIRI(domainIri, workspaceId); if (relationship != null) { domainVertex = ((VertexiumRelationship) relationship).getVertex(); } else { throw new VisalloException("Could not find domain with IRI " + domainIri); } } findOrAddEdge(domainVertex, ((VertexiumOntologyProperty) property).getVertex(), LabelName.HAS_PROPERTY.toString(), user, workspaceId); } }
for (Concept domainConcept : domainConcepts) { if (!relationship.getDomainConceptIRIs().contains(domainConcept.getIRI())) { findOrAddEdge(ctx, ((VertexiumConcept) domainConcept).getVertex(), relationshipVertex, LabelName.HAS_EDGE.toString()); findOrAddEdge(ctx, relationshipVertex, ((VertexiumConcept) rangeConcept).getVertex(), LabelName.HAS_EDGE.toString()); findOrAddEdge(ctx, ((VertexiumConcept) domainConcept).getVertex(), relationshipVertex, LabelName.HAS_EDGE.toString()); findOrAddEdge(ctx, relationshipVertex, ((VertexiumConcept) rangeConcept).getVertex(), LabelName.HAS_EDGE.toString()); findOrAddEdge(ctx, relationshipVertex, ((VertexiumRelationship) parent).getVertex(), LabelName.IS_A.toString());
findOrAddEdge(ctx, ((VertexiumConcept) concept).getVertex(), propertyVertex, LabelName.HAS_PROPERTY.toString()); findOrAddEdge(ctx, ((VertexiumRelationship) relationship).getVertex(), propertyVertex, LabelName.HAS_PROPERTY.toString()); findOrAddEdge(ctx, extendedDataTableVertex, propertyVertex, LabelName.HAS_PROPERTY.toString()); extendedDataTableProperty.addProperty(propertyIri);
} else { concept = createConcept(vertex, null, parent.getIRI(), workspaceId); findOrAddEdge(ctx, ((VertexiumConcept) concept).getVertex(), ((VertexiumConcept) parent).getVertex(), LabelName.IS_A.toString());