Java Code Examples for com.tinkerpop.blueprints.Element#setProperty()

The following examples show how to use com.tinkerpop.blueprints.Element#setProperty() . 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: TestBigdataGraphEmbeddedTransactional.java    From database with GNU General Public License v2.0 6 votes vote down vote up
private void trySetProperty(final Element element, final String key, final Object value, final boolean allowDataType) {
    boolean exceptionTossed = false;
    try {
        element.setProperty(key, value);
    } catch (Throwable t) {
        exceptionTossed = true;
        if (!allowDataType) {
            assertTrue(t instanceof IllegalArgumentException);
        } else {
            fail("setProperty should not have thrown an exception as this data type is accepted according to the GraphTest settings.\n\n" +
                    "Exception was " + t);
        }
    }

    if (!allowDataType && !exceptionTossed) {
        fail("setProperty threw an exception but the data type should have been accepted.");
    }
}
 
Example 2
Source File: TinkerGraphUtil.java    From SciGraph with Apache License 2.0 6 votes vote down vote up
void copyProperties(PropertyContainer container, Element element) {
  for (String key : container.getPropertyKeys()) {
    Object property = container.getProperty(key);
    if (property.getClass().isArray()) {
      List<Object> propertyList = new ArrayList<>();
      for (int i = 0; i < Array.getLength(property); i++) {
        propertyList.add(Array.get(property, i));
      }
      property = propertyList;
    }
    else if (key.equals(CommonProperties.IRI) && String.class.isAssignableFrom(property.getClass())) {
      property = curieUtil.getCurie((String)property).orElse((String)property);
    }
    element.setProperty(key, property);
  }
}
 
Example 3
Source File: ArrayPropertyTransformer.java    From SciGraph with Apache License 2.0 6 votes vote down vote up
static void transform(Iterable<? extends Element> elements) {
  for (Element element: elements) {
    for (String key: element.getPropertyKeys()) {
      if (PROTECTED_PROPERTY_KEYS.contains(key)) {
        continue;
      } else {
        Object value = element.getProperty(key);
        if (value instanceof Iterable) {
          // Leave it
        } else if (value.getClass().isArray()) {
          element.setProperty(key, Arrays.asList(value));
        } else {
          element.setProperty(key, newArrayList(value));
        }
      }
    }
  }
}
 
Example 4
Source File: KeyIndexableGraphHelper.java    From org.openntf.domino with Apache License 2.0 6 votes vote down vote up
/**
 * For those graphs that do no support automatic reindexing of elements when a key is provided for indexing, this method can be used to simulate that behavior.
 * The elements in the graph are iterated and their properties (for the provided keys) are removed and then added.
 * Be sure that the key indices have been created prior to calling this method so that they can pick up the property mutations calls.
 * Finally, if the graph is a TransactionalGraph, then a 1000 mutation buffer is used for each commit.
 *
 * @param graph    the graph containing the provided elements
 * @param elements the elements to index into the key indices
 * @param keys     the keys of the key indices
 * @return the number of element properties that were indexed
 */
public static long reIndexElements(final Graph graph, final Iterable<? extends Element> elements, final Set<String> keys) {
    final boolean isTransactional = graph instanceof TransactionalGraph;
    long counter = 0;
    for (final Element element : elements) {
        for (final String key : keys) {
            final Object value = element.removeProperty(key);
            if (null != value) {
                counter++;
                element.setProperty(key, value);


                if (isTransactional && (counter % 1000 == 0)) {
                    ((TransactionalGraph) graph).commit();
                }
            }
        }
    }
    if (isTransactional) {
        ((TransactionalGraph) graph).commit();
    }
    return counter;
}
 
Example 5
Source File: TinkerGraphUtil.java    From SciGraph with Apache License 2.0 5 votes vote down vote up
static void copyProperties(Element source, Element target) {
  for (String key : source.getPropertyKeys()) {
    Object property = source.getProperty(key);
    if (property.getClass().isArray()) {
      List<Object> propertyList = new ArrayList<>();
      for (int i = 0; i < Array.getLength(property); i++) {
        propertyList.add(Array.get(property, i));
      }
      property = propertyList;
    }
    target.setProperty(key, property);
  }
}
 
Example 6
Source File: ElementUtils.java    From antiquity with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Copy properties from one element to another.
 * 
 * @param from element to copy properties from
 * @param to element to copy properties to
 * @param excludedKeys the keys that should be excluded from being copied.
 */
public static void copyProps(Element from, Element to, Set<String> excludedKeys) {
    for (String k : from.getPropertyKeys()) {
        if (excludedKeys != null && excludedKeys.contains(k)) {
            continue;
        }

        to.setProperty(k, from.getProperty(k));
    }
}
 
Example 7
Source File: ElementHelper.java    From org.openntf.domino with Apache License 2.0 5 votes vote down vote up
/**
 * Typecasts a property value. This only works for casting to a class that has a constructor of the for new X(String).
 * If no such constructor exists, a RuntimeException is thrown and the original element property is left unchanged.
 *
 * @param key       the key for the property value to typecast
 * @param classCast the class to typecast to
 * @param elements  the elements to have their property typecasted
 */
public static void typecastProperty(final String key, final Class classCast, final Iterable<Element> elements) {
    for (final Element element : elements) {
        final Object value = element.removeProperty(key);
        if (null != value) {
            try {
                element.setProperty(key, classCast.getConstructor(String.class).newInstance(value.toString()));
            } catch (Exception e) {
                element.setProperty(key, value);
                throw new RuntimeException(e.getMessage(), e);
            }
        }
    }
}
 
Example 8
Source File: ElementHelper.java    From org.openntf.domino with Apache License 2.0 5 votes vote down vote up
/**
 * Set the properties of the provided element using the provided key value pairs.
 * The var args of Objects must be divisible by 2. All odd elements in the array must be a String key.
 *
 * @param element    the element to set the properties of
 * @param keysValues the key value pairs of the properties
 */
public static void setProperties(final Element element, final Object... keysValues) {
    if (keysValues.length % 2 != 0)
        throw new IllegalArgumentException("The object var args must be divisible by 2");
    for (int i = 0; i < keysValues.length; i = i + 2) {
        element.setProperty((String) keysValues[i], keysValues[i + 1]);
    }
}
 
Example 9
Source File: TypeManager.java    From org.openntf.domino with Apache License 2.0 5 votes vote down vote up
@Override
public void initElement(Class<?> kind, FramedGraph<?> framedGraph, Element element) {
	Class<?> typeHoldingTypeField = typeRegistry.getTypeHoldingTypeField(kind);
	if (typeHoldingTypeField != null) {
		TypeValue typeValue = kind.getAnnotation(TypeValue.class);
		if (typeValue != null) {
			element.setProperty(typeHoldingTypeField.getAnnotation(TypeField.class).value(), typeValue.value());
		}
	}
}
 
Example 10
Source File: ElementHelper.java    From org.openntf.domino with Apache License 2.0 3 votes vote down vote up
/**
 * Renames a property by removing the old key and adding the stored value to the new key.
 * If property does not exist, nothing occurs.
 *
 * @param oldKey   the key to rename
 * @param newKey   the key to rename to
 * @param elements the elements to rename
 */
public static void renameProperty(final String oldKey, final String newKey, final Iterable<Element> elements) {
    for (final Element element : elements) {
        Object value = element.removeProperty(oldKey);
        if (null != value)
            element.setProperty(newKey, value);
    }
}
 
Example 11
Source File: ElementHelper.java    From org.openntf.domino with Apache License 2.0 2 votes vote down vote up
/**
 * Copy the properties (key and value) from one element to another.
 * The properties are preserved on the from element.
 * ElementPropertiesRule that share the same key on the to element are overwritten.
 *
 * @param from the element to copy properties from
 * @param to   the element to copy properties to
 */
public static void copyProperties(final Element from, final Element to) {
    for (final String key : from.getPropertyKeys()) {
        to.setProperty(key, from.getProperty(key));
    }
}
 
Example 12
Source File: ElementHelper.java    From org.openntf.domino with Apache License 2.0 2 votes vote down vote up
/**
 * Set the properties of the provided element using the provided map.
 *
 * @param element    the element to set the properties of
 * @param properties the properties to set as a Map
 */
public static void setProperties(final Element element, final Map<String, Object> properties) {
    for (Map.Entry<String, Object> property : properties.entrySet()) {
        element.setProperty(property.getKey(), property.getValue());
    }
}