How to create edge label in Arangodb? - arangodb

java-driver 4.1.10 I've created database named mydatabase1 I've created a graph using Java so my question is that how to set edge label using Java code?

If you mean with "edge label" a field in an edge-document, you can set it when you call insertEdge like in the code below.
ArangoDB arangoDB = new ArangoDB.Builder().build();
// create database
arangoDB.createDatabase("myDatabase");
// create named graph
Set<EdgeDefinition> edgeDefinitions = Collections.singleton(
new EdgeDefinition().collection("myEdgeCollection").from("myVertexCollection").to("myVertexCollection"));
arangoDB.db("myDatabase").createGraph("myGraph", edgeDefinitions);
// create from vertex
BaseDocument from = new BaseDocument("myFromKey");
arangoDB.db("myDatabase").graph("myGraph").vertexCollection("myVertexCollection").insertVertex(from);
// create to vertex
BaseDocument to = new BaseDocument("myToKey");
arangoDB.db("myDatabase").graph("myGraph").vertexCollection("myVertexCollection").insertVertex(to);
// create edge
BaseEdgeDocument edge = new BaseEdgeDocument("myVertexCollection/myFromKey",
"myVertexCollection/myToKey");
edge.addAttribute("label", "value");
edge.addAttribute("whatever", 42);
arangoDB.db("myDatabase").graph("myGraph").edgeCollection("myEdgeCollection").insertEdge(edge);
Instead of using BaseEdgeDocument you can also use Map<String, Object>
Map<String, Object> edge = new HashMap<String, Object>();
edge.put("_from", "myVertexCollection/myFromKey");
edge.put("_to", "myVertexCollection/myToKey");
edge.put("label", "value");
edge.put("whatever", 42);
arangoDB.db("myDatabase").graph("myGraph").edgeCollection("myEdgeCollection").in sertEdge(edge);
or create your own POJO representing your edge. The edge needs at least the fields _from and _to. If you don't want to name the fields in your POJO _from and _to, you can use the annotation DocumentField with the values Type.FROM and Type.TO on two String fields of your choice.
public class MyEdge {
#DocumentField(Type.FROM)
private String from;
#DocumentField(Type.TO)
private String to;
public MyEdge() {}
..setter & getter
}

Related

How to get objects from list by data from map with condition and save to another map using Groovy

Looking for solution how to match objects from list with data from map with condition if object field starts with map values and save to another map with Groovy
i have map with some data
Map<String, String> dataMap = new HashMap()
dataMap.put("d1", "DATA1")
dataMap.put("d2", "DATA2")
dataMap.put("d3", "DATA3")
and list of DataElement objects
List<DataElement> elements = new ArrayList()
elements.add(new DataElement("TEXT1"))
elements.add(new DataElement("TEXT2"))
elements.add(new DataElement("DATA1_text1"))
elements.add(new DataElement("DATA2_text2"))
class DataElement {
public field;
public DataElement(String text){
this.field = text
}
public getField(){
return this.field
}
And i'am trying to get new Map where keys are values from first map and values are objects(field) from List with condition if object field starts with map value: Result should be:
[d1=DATA1_text1, d2=DATA2_text2]
My code is working but may be there is more elegant variant with using collectEntries:
list = new HashMap()
mapping = dataMap.each { key, v ->
elements.each { el ->
if (el.getField().startsWith(v)) {
list.put(key, el)
}
}
}
dataMap.collectEntries{k,v->
[k,elements.find{e-> e.getField().startsWith(v)} ]
}.findAll{k,v-> v} //to keep only non empty values

Random string in cucumber scenarios

I am testing a GUI using cucumber. I need to test CRUD operations of the GUI.
When I write a scenario to create a new entity in GUI, I am unable to run multiple times, since the second time scenario fails because the ID I specified for the entity already exists (created in the first run) in the system the second time I run the test.
The system I am testing doesn't allow deleting entities. System needs to be started in a special mode to delete entities, so deleting the entity created after the test is not an option.
It would be great if I could use a random number for the entity id. For an example:
when user creates a new Branch with following values:
|Branch ID|<random_string_1>|
|Address|1, abc, def.|
|Telephone|01111111111|
And user searches for a branch by "Branch ID" = "<random_string_1>"
Then branch details should be as following
|Branch ID|<random_string_1>|
|Address|1, abc, def.|
|Telephone|01111111111|
Is there an option in cucumber to do something like this? Or, is there any other way I can achieve this?
In the end, I've added RandomStringTransformer class to test suite:
public class RandomStringTransformer extends Transformer<String> {
private static final Map<String, String> RANDOM_STRINGS = new HashMap<>(); //Key -> random string
public static final RandomStringTransformer INSTANCE = new RandomStringTransformer();
#Override
public String transform(String input) {
return transformString(input);
}
public DataTable transform(DataTable dataTable) {
dataTable.getGherkinRows().forEach(dataTableRow -> dataTableRow.getCells().replaceAll(this::transformString));
return dataTable;
}
private String transformString(String input) {
final String[] inputCopy = {input};
Map<String, String> replacements = new HashMap<>();
Matcher matcher = Pattern.compile("(<random_string_[^>]*>)").matcher(input);
while (matcher.find()) {
String group = matcher.group(0);
replacements.put(group, RANDOM_STRINGS.computeIfAbsent(group, key -> Utilities.getNextUniqueString()));
}
replacements.forEach((key, value) -> inputCopy[0] = inputCopy[0].replace(key, value));
return inputCopy[0];
}
}
And used the transformer in step definition:
#When("^user creates a branch of name "([^"]*)" with following values$")
public void branchIsCreatedWithDetails(#Transform(RandomStringTransformer.class) String branchName, DataTable fieldValues) {
fieldValues = RandomStringTransformer.INSTANCE.transform(fieldValues);
//Now, fieldValues table values and branchName are replaced with random values if they were in format <random_string_SOMETHING>
}
The #Transform annotation is not supported in Cucumber 3 anymore.
You have to transform data manually in the method body.
#When("^user creates a branch of name "([^"]*)" with following values$")
public void branchIsCreatedWithDetails(String branchName, DataTable fieldValues) {
fieldValues = RandomStringTransformer.INSTANCE.transform(fieldValues);
//Now, fieldValues table values and branchName are replaced with random values if they were in format <random_string_SOMETHING>
}
Read this for more information to migrate: http://grasshopper.tech/98/

Passing Interfaces into Dictionary key's

I have been following following coreclr for a little while and I am new to programming. My question is why do they pass interfaces into Dictionary's especially the key value?
//
// Allocate a new Dictionary containing a copy of the old values, plus the new value. We have to do this manually to
// minimize allocations of IEnumerators, etc.
//
Dictionary newValues = new Dictionary(current.m_localValues.Count + (hadPreviousValue ? 0 : 1));
My understanding is that interface is to implemented by a class. Once implemented it can call/use functions or store data in the classes properties/ variables. I am missing some understanding of interfaces and their use cases but I do not know what that it.
Why do you instantiate a variable to an interface or pass an interface into a parameter? My understanding is you will then have an instance of that variable which still can't hold values nor change state through methods.
Let me explain.
Interface is contract. It just contains method without implementation. Now it may possible that that interface is being implemented by any number of class.
public interface IEntity { int Id {get;set;} }
public class Student : IEntity { public int Id {get;set;} // Interface Property }
public class Teacher : IEntity { public int Id {get;set;} // Interface Property }
Dictionary<IEntity,object> obj = new Dictionary<IEntity,object>(); Student s = new Student(); Teacher t = new Teacher(); obj.Add(s,any object); obj.Add(t,any object);
This is because of interface that your dictionary can hold reference of both type ( Student and Teacher).
In .NET when any object is created it is uniquely identify by GetHashCode() method. // You can find more detail on this on MSDN.
Also Dictionary not means that keys must be only primitive type. This is the reason it is good if you have more than one key ( Like composite key in Database) so it allow you to identify uniquely based on your custom implementation.
Now second Generic.
public class PersonInfo<T> where T : IEntity
{
public string Name {get;set;}
public T Entity {get;set;}
}
PersonInfo<Student> student = new PersonInfo<Student>();
student.T = new Student();
student.Name = "";
PersonInfo<Teacher> Teacher = new PersonInfo<Teacher>();
teacher.T= new Teacher();
teacher.Name = "";
When you have interface. It not actually interface. You always have a reference to object with that interface. And that object is the one responsible for comparison in dictionary
The benefit is not difference from using class as a key. Dictionary can be used as list to iterate KeyValuePair to take key to do some operation. But using interface means you can store various type of class with same interface instead of just one type. Which is decoupled and more flexible

Static classes for variables instead of methods

I want to store a number of unique variables (objects) in a class like so:
//Notice that each object has unique attributes passed to it
public static Entity SomeEntity01, SomeEntity02, SomeEntity03;
SomeEntity01 = new Entity(some, values);
SomeEntity02 = new Entity(some, new, values);
SomeEntity03 = new Entity(some, other, values);
I wish to access these variables in the class like this:
MyClass.SomeEntity01
I can do this with instantiation, but it would obfuscate the code with useless instances. I can't do this ordinarily because classes don't allow object instantiation outside of methods (from what I can tell).
If possible, how can I store and access variables (specifically objects) in a class without using instantiation or static methods?
You can initialize static fields in a static constructor:
public static class MyClass
{
public static Entity SomeEntity01, SomeEntity02, SomeEntity03;
static MyClass() // static constructor
{
SomeEntity01 = new Entity(some, values);
SomeEntity02 = new Entity(some, new, values);
SomeEntity03 = new Entity(some, other, values);
}
}

ordering of Hashtable in J2ME

I have some data and I have added them to Hashtable in some orders what I want to do now is to get the data in the same order that I have entered
What is the data type that I can use?
Assuming your key is a String you could add some ordering to it and have a getter method for the sorted data. See example below:
static int order;
Hashtable map = new Hashtable();
void put (String key, Object value) {
map.put(order + key, value);
order++;
}
Enumeration getSorted() {
Enumeration keys = map.keys();
Vector sortedKeys = new Vector();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
insertionSort(key, sortedKeys);
}
Vector sortedData = new Vector();
keys = sortedKeys.elements();
while (keys.hasMoreElements()) {
String key = (String) keys.nextElement();
sortedData.addElement(map.get(key));
}
return sortedData.elements();
}
You can find insertionSort algorithms at http://en.wikipedia.org/wiki/Insertion_sort
A Hashtable does not retain any ordering.
If you need insertion order access see if Linked Hash Map is offered in JavaME
You can download a source code of Java SE and make work LinkedHashMap in J2ME easily by removing generics (but also you might need to perform this on it's parent classes and interfaces).
You can find LinkedHashMap for Java ME here

Resources