protected void checkPathLegal(Path targetPath) { if (!context.entityClass.getSimpleName().equals(targetPath.getSegment().getElementType().getSimpleName())) { throw new IllegalArgumentException("Current position in the inventory traversal expects entities of type " + context.entityClass.getSimpleName() + " which is incompatible with the provided path: " + targetPath); } }
@SuppressWarnings("unchecked") public static Filter[] pathTo(CanonicalPath path) { if (!path.isDefined()) { return new Filter[0]; } List<Filter> fs = new ArrayList<>(); for (Path.Segment s : path.getPath()) { fs.add(Related.by(Relationships.WellKnown.contains)); fs.add(With.type(Entity.entityTypeFromSegmentType(s.getElementType()))); fs.add(With.id(s.getElementId())); } if (fs.size() < 2) { return new Filter[0]; } else { //remove the first 'contains' defined in the loop above List<Filter> ret = fs.subList(1, fs.size()); return ret.toArray(new Filter[ret.size()]); } }
/** * Returns a child builder of a pre-existing child. * @param childPath the path to the child * @return the child builder or null */ public ChildBuilder<This> getChild(Path.Segment childPath) { Map<EntityType, Set<FullNode>> myChildren = children.get(myPath); if (myChildren == null) { return null; } EntityType childType = EntityType.of(childPath.getElementType()); Set<FullNode> childrenOfType = myChildren.get(childType); return childrenOfType.stream().filter(child -> child.getEntity().getId().equals(childPath.getElementId())) .findAny().map(child -> { RelativePath rp = myPath.modified().extend(childPath).get(); return new ChildBuilder<>(castThis(), rp, blueprints, children); }).orElse(null); }
@Override protected EntityAndPendingNotifications<BE, MetadataPack> wireUpNewEntity(BE entity, MetadataPack.Blueprint blueprint, CanonicalPath parentPath, BE parent, Transaction<BE> tx) { Set<Notification<?, ?>> newRels = new HashSet<>(); blueprint.getMembers().forEach((p) -> { try { BE member = tx.find(p); BE rel = tx.relate(entity, member, incorporates.name(), null); Relationship r = tx.convert(rel, Relationship.class); newRels.add(new Notification<>(r, r, created())); } catch (ElementNotFoundException e) { throw new EntityNotFoundException(p.getSegment().getElementType().getSimpleName(), Query.filters(Query.to(p))); } }); MetadataPack entityObject = tx.convert(entity, MetadataPack.class); return new EntityAndPendingNotifications<>(entity, entityObject, newRels); }
@SuppressWarnings("unchecked") public void visit(GraphTraversal<?, ?> query, With.CanonicalPaths filter, QueryTranslationState state) { String prop = chooseBasedOnDirection(__cp, __targetCp, __sourceCp, state.getComingFrom()).name(); String typeName = null; String[] typeNames = null; if (filter.getPaths().length == 1) { typeName = Constants.Type.of(filter.getPaths()[0].getSegment().getElementType()).name(); query.has(prop, filter.getPaths()[0].toString()); } else { typeNames = Stream.of(filter.getPaths()).map(p -> p.getSegment().getElementType()) .toArray(String[]::new); String[] paths = Stream.of(filter.getPaths()).map(Object::toString).toArray(String[]::new); query.has(prop, P.within(paths)); } goBackFromEdges(query, state); if (typeNames == null) { query.hasLabel(typeName); } else { query.hasLabel(P.within(typeNames)); } }
/** * Returns a child builder of a pre-existing child. * @param childPath the path to the child * @return the child builder or null */ public ChildBuilder<This> getChild(Path.Segment childPath) { Map<EntityType, Set<FullNode>> myChildren = children.get(myPath); if (myChildren == null) { return null; } EntityType childType = EntityType.of(childPath.getElementType()); Set<FullNode> childrenOfType = myChildren.get(childType); return childrenOfType.stream().filter(child -> child.getEntity().getId().equals(childPath.getElementId())) .findAny().map(child -> { RelativePath rp = myPath.modified().extend(childPath).get(); return new ChildBuilder<>(castThis(), rp, blueprints, children); }).orElse(null); }
private Set<SyncHash.Tree> sortByType(Collection<SyncHash.Tree> col) { Set<SyncHash.Tree> set = new TreeSet<>((a, b) -> { InventoryStructure.EntityType aType = InventoryStructure.EntityType.of(a.getPath().getSegment().getElementType()); InventoryStructure.EntityType bType = InventoryStructure.EntityType.of(b.getPath().getSegment().getElementType()); int ret = aType.ordinal() - bType.ordinal(); if (ret == 0) { //this is actually not important.. we only need to make sure we have a total ordering and that //the entities are sorted by their type.. ret = a.getHash().compareTo(b.getHash()); } return ret; }); set.addAll(col); return set; }
@Override public Element find(CanonicalPath path) throws ElementNotFoundException { Iterator<? extends Element> it; if (SegmentType.rl.equals(path.getSegment().getElementType())) { //__eid is globally unique for relationships it = context.getGraph().traversal().E().has(__eid.name(), path.getSegment().getElementId()); } else { it = context.getGraph().traversal().V() .hasLabel(Constants.Type.of(path.getSegment().getElementType()).name()) .has(__cp.name(), path.toString()); } if (!it.hasNext()) { throw new ElementNotFoundException(); } return it.next(); }
@SuppressWarnings("unchecked") @Override protected String getProposedId(Transaction<BE> tx, MetadataPack.Blueprint blueprint) { Iterator<? extends Entity<? extends Entity.Blueprint, ?>> members = blueprint.getMembers().stream() .map((p) -> { SegmentType type = p.getSegment().getElementType(); Class<?> cls = Entity.entityTypeFromSegmentType(type); try { BE e = tx.find(p); return (Entity<? extends Entity.Blueprint, ?>) tx.convert(e, cls); } catch (ElementNotFoundException ex) { throw new EntityNotFoundException(cls, Query.filters(Query.to(p))); } }).iterator(); return IdentityHash.of(members, context.inventory.keepTransaction(tx)); }
/** * Constructs a new entity * * @param name the human readable name of the entity, can be null * @param path the path of the entity, must not be null * @param properties the additional user-defined properties, can be null */ Entity(String name, CanonicalPath path, Map<String, Object> properties) { super(path, properties); this.name = name; if (!segmentTypeFromType(this.getClass()).equals(path.getSegment().getElementType())) { throw new IllegalArgumentException("Invalid path specified. Trying to create " + this.getClass().getSimpleName() + " but the path points to " + path.getSegment().getElementType().getSimpleName()); } }
/** * Constructs a new entity * * @param name the human readable name of the entity, can be null * @param path the path of the entity, must not be null * @param properties the additional user-defined properties, can be null */ Entity(String name, CanonicalPath path, Map<String, Object> properties) { super(path, properties); this.name = name; if (!segmentTypeFromType(this.getClass()).equals(path.getSegment().getElementType())) { throw new IllegalArgumentException("Invalid path specified. Trying to create " + this.getClass().getSimpleName() + " but the path points to " + path.getSegment().getElementType().getSimpleName()); } }
boolean canBeIdentityRoot() { return canBeIdentityRoot(path.getElementType()); }
public ElementTypes<?, ?, ?> byPath(Path path) { return bySegment(path.getSegment().getElementType()); }
TraversalContext<BE, E> proceedTo(Path path) { if (!AbstractElement.segmentTypeFromType(entityClass).equals(path.getSegment().getElementType())) { throw new IllegalArgumentException("Path doesn't point to the type of element currently being accessed."); } return replacePath(Util.extendTo(this, path)); }
private static String segmentToString(Path.Segment segment) { return (segment.getElementType() == null ? "" : (segment.getElementType().getSerialized() + ";")) + segment.getElementId(); } }
private void convertToPipeline(RelativePath path, GraphTraversal<?, ?> pipeline) { for (Path.Segment s : path.getPath()) { if (SegmentType.up.equals(s.getElementType())) { pipeline.in(Relationships.WellKnown.contains.name()); } else { pipeline.out(Relationships.WellKnown.contains.name()) .hasLabel(Constants.Type.of(s.getElementType()).name()); apply(s, pipeline); } } }
static <BE> Relationship associationWith(TraversalContext<BE, ?> context, SegmentType sourceEntityType, Relationships.WellKnown relationship, Path path) throws RelationNotFoundException { return inTx(context, tx -> { Query sourceQuery = context.sourcePath.extend().filter().with(type(sourceEntityType)).get(); Query targetQuery = Util.queryTo(context.sourcePath, path); SegmentType targetType = path.getSegment().getElementType(); return Util.getAssociation(tx, sourceQuery, sourceEntityType, targetQuery, targetType, relationship.name()); }); }
public static boolean canIncorporate(CanonicalPath entityPath) { SegmentType entityType = entityPath.getSegment().getElementType(); SegmentType parentType = entityPath.up().getSegment().getElementType(); return SegmentType.t.equals(parentType) && (SegmentType.rt.equals(entityType) || SegmentType.mt.equals(entityType)); }
private void apply(Path.Segment segment, GraphTraversal<?, ?> pipeline) { pipeline.has(__type.name(), Constants.Type.of(Entity.typeFromSegmentType(segment.getElementType())).name()); pipeline.has(__eid.name(), segment.getElementId()); }
public static boolean canIncorporate(CanonicalPath entityPath) { SegmentType entityType = entityPath.getSegment().getElementType(); SegmentType parentType = entityPath.up().getSegment().getElementType(); return SegmentType.t.equals(parentType) && (SegmentType.rt.equals(entityType) || SegmentType.mt.equals(entityType)); }