use of org.openlca.core.results.SimpleResult in project olca-modules by GreenDelta.
the class SimulatorTest method testImpactParam.
@Test
public void testImpactParam() {
// create a simple model with an uncertain
// parameter in a LCIA category
Process p = TestProcess.refProduct("p", 1.0, "kg").elemOut("CH4", 1.0, "kg").get();
ProductSystem s = TestSystem.of(p).get();
ImpactMethod m = TestData.method("method", TestData.impact("GWP").factor("CH4", "1 * param", "kg").parameter("param", Uncertainty.uniform(22, 26)).get());
// create the simulator
var setup = CalculationSetup.monteCarlo(s, 100).withImpactMethod(m);
var simulator = Simulator.create(setup, db).withSolver(new JavaSolver());
// check the simulation results
for (int i = 0; i < 100; i++) {
SimpleResult r = simulator.nextRun();
double[] impacts = r.totalImpactResults();
Assert.assertEquals(1, impacts.length);
double val = impacts[0];
Assert.assertTrue(val >= 22 && val <= 26);
}
Arrays.asList(s, m, p).forEach(db::delete);
}
use of org.openlca.core.results.SimpleResult in project olca-modules by GreenDelta.
the class JsonRpc method encode.
static JsonObject encode(SimpleResult r, String id, EntityCache cache) {
JsonObject obj = new JsonObject();
obj.addProperty("@id", id);
if (r == null)
return obj;
obj.addProperty("@type", r.getClass().getSimpleName());
obj.add("flows", encode(r.getFlows().stream().map(EnviFlow::flow).collect(Collectors.toSet()), cache));
obj.add("processes", encode(r.getProcesses(), cache));
obj.add("flowResults", encode(r.getTotalFlowResults(), result -> encode(result, cache)));
if (!r.hasImpacts())
return obj;
obj.add("impacts", encode(r.getImpacts(), cache));
obj.add("impactResults", encode(r.getTotalImpactResults(), result -> encode(result, cache)));
return obj;
}
use of org.openlca.core.results.SimpleResult in project olca-modules by GreenDelta.
the class Utils method simple.
RpcResponse simple(RpcRequest req, Simple handler) {
if (req == null || req.params == null || !req.params.isJsonObject())
return Responses.invalidParams("No parameter given", req);
JsonObject json = req.params.getAsJsonObject();
SimpleResult result = getResult(json);
EntityCache cache = EntityCache.create(ctx.db);
return Responses.ok(handler.handle(result, cache), req);
}
use of org.openlca.core.results.SimpleResult in project olca-app by GreenDelta.
the class Combo method on.
public static Builder on(IResult r) {
Combo c = new Combo();
c.flows = new ArrayList<>();
TLongHashSet flowIDs = new TLongHashSet();
for (var f : r.getFlows()) {
if (f.flow() == null || flowIDs.contains(f.flow().id))
continue;
flowIDs.add(f.flow().id);
c.flows.add(f.flow());
}
// add LCIA categories
if (r.hasImpacts()) {
c.impacts = r.getImpacts();
}
// add cost / added value selection
if (r.hasCosts() && (r instanceof SimpleResult)) {
SimpleResult sr = (SimpleResult) r;
CostResultDescriptor d1 = new CostResultDescriptor();
d1.forAddedValue = false;
d1.name = M.Netcosts;
CostResultDescriptor d2 = new CostResultDescriptor();
d2.forAddedValue = true;
d2.name = M.AddedValue;
c.costs = sr.totalCosts() >= 0 ? Arrays.asList(d1, d2) : Arrays.asList(d2, d1);
}
return new Builder(c);
}
use of org.openlca.core.results.SimpleResult in project olca-modules by GreenDelta.
the class ExportHandler method excel.
@Rpc("export/excel")
public RpcResponse excel(RpcRequest req) {
if (req == null || req.params == null || !req.params.isJsonObject())
return Responses.badRequest("No @id given", req);
JsonObject obj = req.params.getAsJsonObject();
String id = Json.getString(obj, "@id");
if (id == null)
return Responses.badRequest("No `@id` given", req);
String path = Json.getString(obj, "path");
if (path == null)
return Responses.badRequest("No `path` given", req);
Object val = context.cache.get(id);
if (!(val instanceof CachedResult))
return Responses.notImplemented("The Excel export is currently" + " only implemented for calculation results", req);
CachedResult<?> r = (CachedResult<?>) val;
if (r.result instanceof SimpleResult)
return exportSimpleResult(req, path, r);
if (r.result instanceof Simulator)
return exportSimulationResult(req, path, r);
return Responses.notImplemented("The Excel export is currently" + " only implemented for calculation results", req);
}
Aggregations