跳到主要内容

基本模式

InMemoryDataset

Les datasets InMemoryDataset permettent de représenter les tables statistiques dans une application Java.

Exemple

ScriptEngine engine = new ScriptEngineManager().getEngineByName("vtl");

Bindings bindings = new SimpleBindings();

InMemoryDataset dataset = new InMemoryDataset(
List.of(
Map.of("var1", "x", "var2", "y", "var3", 5),
Map.of("var1", "xx", "var2", "yy", "var3", 10)
),
Map.of("var1", String.class, "var2", String.class, "var3", Long.class),
Map.of("var1", Role.IDENTIFIER, "var2", Role.ATTRIBUTE, "var3", Role.MEASURE)
);
bindings.put("myDataset", dataset);
engine.getContext().setBindings(bindings, ScriptContext.ENGINE_SCOPE);

String script = "res := myDataset[filter var3 > 6];";

try {
engine.eval(script);
} catch (ScriptException e) {
e.printStackTrace();
}

Bindings outputBindings = engine.getContext().getBindings(ScriptContext.ENGINE_SCOPE);
InMemoryDataset result = (InMemoryDataset) outputBindings.get("res");

System.out.println(result.getDataPoints().size());
// 1