Java Code Examples for org.apache.tinkerpop.gremlin.structure.Property#isPresent()

The following examples show how to use org.apache.tinkerpop.gremlin.structure.Property#isPresent() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LogEntryFactory.java    From timbuctoo with GNU General Public License v3.0 6 votes vote down vote up
public LogEntry createForEdge(Edge edge) throws IllegalArgumentException {
  Property<Integer> revProp = edge.property("rev");
  if (!revProp.isPresent()) {
    String id = edge.value("tim_id");
    throw new IllegalArgumentException(
      String.format("Edge with id '%s' has no property 'rev'. This edge will be ignored.", id)
    );
  }
  Integer rev = revProp.value();

  if (rev > 1) {
    Edge prevEdge = edgeRetriever.getPreviousVersion(edge);

    return new UpdateEdgeLogEntry(edge, prevEdge);
  }
  return new CreateEdgeLogEntry(edge);

}
 
Example 2
Source File: BitsyEdge.java    From bitsy with Apache License 2.0 6 votes vote down vote up
@Override
// THERE ARE TWO MORE COPIES OF THIS CODE IN ELEMENT AND VERTEX
public <T> Iterator<Property<T>> properties(String... propertyKeys) {
    ArrayList<Property<T>> ans = new ArrayList<Property<T>>();

    if (propertyKeys.length == 0) {
    	if (this.properties == null) return Collections.emptyIterator();
    	propertyKeys = this.properties.getPropertyKeys();
    }

    for (String key : propertyKeys) {
        Property<T> prop = property(key);
        if (prop.isPresent()) ans.add(prop);
    }
    return ans.iterator();
}
 
Example 3
Source File: Titan1Element.java    From incubator-atlas with Apache License 2.0 6 votes vote down vote up
@Override
public <T> T getProperty(String propertyName, Class<T> clazz) {

    //add explicit logic to return null if the property does not exist
    //This is the behavior Atlas expects.  Titan 1 throws an exception
    //in this scenario.
    Property p = getWrappedElement().property(propertyName);
    if (p.isPresent()) {
        Object propertyValue= p.value();
        if (propertyValue == null) {
            return null;
        }
        if (AtlasEdge.class == clazz) {
            return (T)graph.getEdge(propertyValue.toString());
        }
        if (AtlasVertex.class == clazz) {
            return (T)graph.getVertex(propertyValue.toString());
        }
        return (T)propertyValue;

    }
    return null;
}
 
Example 4
Source File: PolymorphicTypeResolver.java    From Ferma with Apache License 2.0 6 votes vote down vote up
@Override
public <T> Class<? extends T> resolve(final Element element, final Class<T> kind) {
    final Property<String> nodeClazzProperty = element.<String>property(this.typeResolutionKey);
    final String nodeClazz;
    if( nodeClazzProperty.isPresent() )
        nodeClazz = nodeClazzProperty.value();
    else
        return kind;

    final Class<T> nodeKind = (Class<T>) this.reflectionCache.forName(nodeClazz);

    if (kind.isAssignableFrom(nodeKind) || kind.equals(VertexFrame.class) || kind.equals(EdgeFrame.class) || kind.equals(AbstractVertexFrame.class) || kind.equals(AbstractEdgeFrame.class) || kind.
          equals(Object.class))
        return nodeKind;
    else
        return kind;
}
 
Example 5
Source File: ClassificationService.java    From windup with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Returns the total effort points in all of the {@link ClassificationModel}s associated with the provided {@link FileModel}.
 */
public int getMigrationEffortPoints(FileModel fileModel)
{
    GraphTraversal<Vertex, Vertex> classificationPipeline = new GraphTraversalSource(getGraphContext().getGraph()).V(fileModel.getElement());
    classificationPipeline.in(ClassificationModel.FILE_MODEL);
    classificationPipeline.has(EffortReportModel.EFFORT, P.gt(0));
    classificationPipeline.has(WindupVertexFrame.TYPE_PROP, Text.textContains(ClassificationModel.TYPE));

    int classificationEffort = 0;
    for (Vertex v : classificationPipeline.toList())
    {
        Property<Integer> migrationEffort = v.property(ClassificationModel.EFFORT);
        if (migrationEffort.isPresent())
        {
            classificationEffort += migrationEffort.value();
        }
    }
    return classificationEffort;
}
 
Example 6
Source File: ClusterCountMapReduce.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void map(final Vertex vertex, final MapEmitter<NullObject, Serializable> emitter) {
    final Property<Serializable> cluster = vertex.property(PeerPressureVertexProgram.CLUSTER);
    if (cluster.isPresent()) {
        emitter.emit(NullObject.instance(), cluster.value());
    }
}
 
Example 7
Source File: ArangoDBEdge.java    From arangodb-tinkerpop-provider with Apache License 2.0 5 votes vote down vote up
@Override
public <V> Property<V> property(
	String key,
	V value) {
	logger.info("set property {} = {}", key, value);
	ElementHelper.validateProperty(key, value);
	Property<V> p = property(key);
	if (!p.isPresent()) {
           p = ArangoDBUtil.createArangoDBEdgeProperty(key, value, this);
       }
	else {
		((ArangoDBEdgeProperty<V>) p).value(value);
	}
	return p;
}
 
Example 8
Source File: AbstractElementFrame.java    From Ferma with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T getProperty(final String name, final Class<T> type) {
    final Property<T> nameProperty = getElement().property(name);
    if( !nameProperty.isPresent() )
        return null;
    final T nameValue = nameProperty.value();

    if (type.isEnum()) {
        return (T) Enum.valueOf((Class<Enum>) type, nameValue.toString());
    }
    return nameValue;
}
 
Example 9
Source File: AbstractElementFrame.java    From Ferma with Apache License 2.0 5 votes vote down vote up
@Override
public <T> T getProperty(final String name) {
    final Property<T> property = getElement().<T>property(name);
    if( property.isPresent())
        return property.value();
    else
        return null;
}
 
Example 10
Source File: PolymorphicTypeResolver.java    From Ferma with Apache License 2.0 5 votes vote down vote up
@Override
public Class<?> resolve(final Element element) {
    final Property<String> typeResolutionName = element.<String>property(this.typeResolutionKey);

    if( typeResolutionName.isPresent() )
        return this.reflectionCache.forName(typeResolutionName.value());
    else
        return null;
}
 
Example 11
Source File: MapInAdjacentPropertiesHandler.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Getter
 */
private static Map<String, Serializable> handleGetter(Vertex vertex, Method method, Object[] args,
            MapInAdjacentProperties ann)
{
    if (args != null && args.length != 0)
        throw new WindupException("Method must take no arguments: " + method.getName());

    // Find the map vertex.
    Map<String, Serializable> map = new HashMap<>();
    Iterator<Vertex> it = vertex.vertices(Direction.OUT, ann.label());
    Vertex mapVertex = null;
    if (!it.hasNext())
    {
        // No map yet.
        return map;
    }
    else
    {
        mapVertex = it.next();
        if (it.hasNext())
        {
            // Multiple vertices behind edges with given label.
            log.warning("Found multiple vertices for a map, using only first one; for: " + method.getName());
        }
    }

    Set<String> keys = mapVertex.keys();
    for (String key : keys)
    {
        final Property<Object> val = mapVertex.property(key);
        if (!val.isPresent() || !(val.value() instanceof String))
            log.warning("@InProperties is meant for Map<String,Serializable>, but the value was: " + val.getClass());
        map.put(key, "" + val.value());
    }
    return map;
}
 
Example 12
Source File: GraphComputerTest.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void execute(final Vertex vertex, final Messenger messenger, final Memory memory) {
    if (memory.isInitialIteration()) {
        final Property<TraverserSet> haltedTraversers = vertex.property(TraversalVertexProgram.HALTED_TRAVERSERS);
        if (!haltedTraversers.isPresent()) return;
        final Iterator iterator = haltedTraversers.value().iterator();
        if (iterator.hasNext()) {
            while (iterator.hasNext()) {
                final Traverser t = (Traverser) iterator.next();
                if (!(t.path() instanceof EmptyPath)) {
                    final int pathLength = t.path().size();
                    final List<Pair<Vertex, Integer>> memoryValue = new LinkedList<>();
                    memoryValue.add(Pair.with(vertex, pathLength));
                    memory.add(LENGTHS_KEY, memoryValue);
                }
            }
        }
    } else {
        if (memory.exists(LENGTHS_KEY)) {
            final List<Pair<Vertex, Integer>> lengths = memory.get(LENGTHS_KEY);
            for (final Pair<Vertex, Integer> pair : lengths) {
                if (pair.getValue0().equals(vertex)) {
                    vertex.property(VertexProperty.Cardinality.list, propertyKey, pair.getValue1());
                }
            }
        }
    }
}
 
Example 13
Source File: ClusterPopulationMapReduce.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void map(final Vertex vertex, final MapEmitter<Serializable, Long> emitter) {
    final Property<Serializable> cluster = vertex.property(PeerPressureVertexProgram.CLUSTER);
    if (cluster.isPresent()) {
        emitter.emit(cluster.value(), 1l);
    }
}
 
Example 14
Source File: PageRankMapReduce.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
@Override
public void map(final Vertex vertex, final MapEmitter<Object, Double> emitter) {
    final Property pageRank = vertex.property(PageRankVertexProgram.PAGE_RANK);
    if (pageRank.isPresent()) {
        emitter.emit(vertex.id(), (Double) pageRank.value());
    }
}
 
Example 15
Source File: ElementHelper.java    From tinkerpop with Apache License 2.0 5 votes vote down vote up
/**
 * A standard method for determining if two {@link Property} objects are equal. This method should be used by any
 * {@link Object#equals(Object)} implementation to ensure consistent behavior.
 *
 * @param a the first {@link Property}
 * @param b the second {@link Property}
 * @return true if equal and false otherwise
 */
public static boolean areEqual(final Property a, final Object b) {
    if (a == b)
        return true;
    if (null == b || null == a)
        return false;
    if (!(b instanceof Property))
        return false;
    if (!a.isPresent() && !((Property) b).isPresent())
        return true;
    if (!a.isPresent() && ((Property) b).isPresent() || a.isPresent() && !((Property) b).isPresent())
        return false;
    return a.key().equals(((Property) b).key()) && a.value().equals(((Property) b).value());

}
 
Example 16
Source File: ArangoDBVertexProperty.java    From arangodb-tinkerpop-provider with Apache License 2.0 5 votes vote down vote up
@Override
public <U> Property<U> property(String key, U value) {
	logger.info("property {} = {}", key, value);
	ElementHelper.validateProperty(key, value);
       Property<U> p = property(key);
       if (!p.isPresent()) {
           p = ArangoDBUtil.createArangoDBPropertyProperty(key, value, this);
       } else {
           ((ArangoDBElementProperty<U>) p).value(value);
       }
       return p;
}
 
Example 17
Source File: PageRankMapReduce.java    From titan1withtp3.1 with Apache License 2.0 5 votes vote down vote up
@Override
public void map(final Vertex vertex, final MapEmitter<Object, Double> emitter) {
    final Property pageRank = vertex.property(PageRankVertexProgram.PAGE_RANK);
    if (pageRank.isPresent()) {
        emitter.emit(vertex.id(), (Double) pageRank.value());
    }
}
 
Example 18
Source File: BitsyElement.java    From bitsy with Apache License 2.0 5 votes vote down vote up
@Override
public <T> Iterator<? extends Property<T>> properties(String... propertyKeys) {
    ArrayList<Property<T>> ans = new ArrayList<Property<T>>();
    if (propertyKeys.length == 0) {
    	if (this.properties == null) return Collections.emptyIterator();
    	propertyKeys = this.properties.getPropertyKeys();
    }

    for (String key : propertyKeys) {
    	Property<T> prop = property(key);
    	if (prop.isPresent()) ans.add(prop);
    }

    return ans.iterator();
}
 
Example 19
Source File: GraphTypeManager.java    From windup with Eclipse Public License 1.0 5 votes vote down vote up
private void addTokenProperty(Element el, String propertyName, String propertyValue)
{
    Property<String> val = el.property(propertyName);
    if (!val.isPresent())
        el.property(propertyName, propertyValue);
    else
        el.property(propertyName, val.value() + "|" + propertyValue);
}
 
Example 20
Source File: InvariantsCheck.java    From timbuctoo with GNU General Public License v3.0 4 votes vote down vote up
private boolean isAccepted(Edge edge, String edgeType) {
  String acceptedProp = String.format("%s_accepted", edgeType);
  Property<Boolean> property = edge.property(acceptedProp);
  return property.isPresent() && property.value();
}