# Valuation system

## Valuate Mascopt objects

It is useful to be able to valuate the nodes or the edges of a graph, like putting a weight or a size. The mascopt library contains a simple system of valuation. You can store:

• String
• Integer
• Double

What is interesting in using this valuation system is that all this values are stored in the files representing the graph, for different file formats (.mgl, .mgx). Besides, as the library provides a way of sharing objects (like sharing nodes between two graphs), we must recognize which graph have stored a value on a node. We add the notion of context for a value: it means that a value is valid on an object relative to a context. For example, imagine a graph G1 and G2 using the same node n1. You can store the value named "weight" = 12 on node n1, relative to the graph G1 and also store a value named "weight" = 56 on node n2. Then, for getting the value "weight" on node n1 require to precise for with graph G1 or G2 you want it.

## Set and Get a value without context

Getting and Setting value is coded in the MascoptObject or MascoptSet classes which contains different methods:

• For storing a String on nodes, arcs, node sets, edge sets, and graphs:
```getValue(String name)
setValue(String name, String value)
```
• For storing an Integer on nodes, arcs and graphs:
```getIntegerValue(String name)
setIntegerValue(String name, Integer value)
```
• For storing an int on nodes, arcs and graphs:
```getIntValue(String name)
setIntValue(String name, int value)
```
• For storing a Double on nodes, arcs and graphs:
```getDoubleValue(String name)
setDoubleValue(String name, Double value)
```
• For storing a double on nodes, arcs and graphs:
```getDouValue(String name)
setDouValue(String name, double value)
```

For example:

```n1.setValue("poids","12");
n1.getValue("poids");
n1.setIntegerValue("poids", new Integer(9));
n1.setDouValue("poids", 18.34);
```

## Set and Get a value with context

• For storing a String on nodes, arcs, node sets, edge sets, and graphs:
```getValue(String name, Object context)
setValue(String name, Object context, String value)
```
• For storing an Integer on nodes, arcs and graphs:
```getIntegerValue(String name, Object context)
setIntegerValue(String name, Object context, Integer value)
```
• For storing an int on nodes, arcs and graphs:
```getIntValue(String name, Object context)
setIntValue(String name, Object context, int value)
```
• For storing a Double on nodes, arcs and graphs:
```getDoubleValue(String name, Object context)
setDoubleValue(String name, Object context, Double value)
```
• For storing a double on nodes, arcs and graphs:
```getDouValue(String name, Object context)
setDouValue(String name, Object context, double value)
```

For example:

```Graph g1 = ...;
Graph g2 = ...;
n1.setValue("poids", g1, "12");
n1.setValue("poids", g2, "89");
n1.getValue("poids", g2);
n1.setIntegerValue("poids", g2. new Integer(9));
n1.setDouValue("poids", g1, 18.34);
```

## Mixing values with and without context:

If you store a value without context for the value named "weight", then you can store a contexted value for example, relative to Graph g1. The non contexted value is not lost. But imagine that you try to get a contexted value named "weight" relative to Graph g2. If you didn't set a value for this context, then the library returns the non contexted value, by default. For example

```Graph g1 = ...;
Graph g2 = ...;
n1.setValue("poids", "12");
n1.setValue("poids", g1, "65");
String value = n1.getValue("poids",g2);
```

In this example, value=12.

## How to get values, when reading a file ?

When reading a file, the mascopt library can't know what type to use for a value. All the values are stored like Strings. So when reading the values, you can't access the values with the methods getIntegerValue() or getDoubleValue(). All the values are stored into String objects. Use the only the getValue() method. Then, when you set Integer or Double on elements of the graph, you can read it as Integer or Double.

NEW This is no longer true with mgl version 1.1 and higher, the mascopt library now knows from the file the type of the object and creates the right object when restoring a file.

## Convert String to numeric values

The previous section leads us to explain how to convert a String into a numeric object. It's quite simple. To convert an Integer or a Double into a String just do:

```int a = 45;
Integer b = new Integer(120);
double c = 23.4;
Double d = 234.234;
String s_a = "" + a;
String s_b = "" + b;
String s_c = "" + c;
String s_d = "" + d;
```

To convert a String into a Double or an Integer, just do:

```String s_a = "12";
String s_b = "13.213";
String s_c = "32.32";
String s_d = "14";
Integer a = new Integer(Integer.parseInt(s_a));
double b = Double.parseFloat(s_b);
Double c = new Double(Double.parseFloat(s_c));
int d = Integer.parseInt(s_d);
```

## Convert numeric classes to numeric simple types:

To convert Double and double:

```Double myDouble = new Double(6.0);
double b = myDouble.doubleValue();
Double myDouble2 = new Double(b);
```