Search in sources :

Example 6 with Rpc

use of org.openlca.ipc.Rpc in project olca-modules by GreenDelta.

the class CostHandler method getTotalRequirements.

@Rpc("get/costs/total_requirements")
public RpcResponse getTotalRequirements(RpcRequest req) {
    return utils.contribution(req, (result, cache) -> {
        JsonArray items = new JsonArray();
        var techIdx = result.techIndex();
        for (int i = 0; i < techIdx.size(); i++) {
            var tr = result.totalRequirements()[i];
            if (tr == 0)
                continue;
            var product = techIdx.at(i);
            var obj = new JsonObject();
            obj.add("process", JsonRef.of(product.provider(), cache));
            obj.add("product", JsonRef.of(product.flow(), cache));
            obj.addProperty("amount", tr);
            obj.addProperty("costs", result.getDirectCostResult(product));
            items.add(obj);
        }
        return items;
    });
}
Also used : JsonArray(com.google.gson.JsonArray) JsonObject(com.google.gson.JsonObject) Rpc(org.openlca.ipc.Rpc)

Example 7 with Rpc

use of org.openlca.ipc.Rpc in project olca-modules by GreenDelta.

the class ExportHandler method jsonLd.

@Rpc("export/json-ld")
public RpcResponse jsonLd(RpcRequest req) {
    if (req == null || req.params == null || !req.params.isJsonObject())
        return Responses.badRequest("No @id given", req);
    JsonObject obj = req.params.getAsJsonObject();
    String path = Json.getString(obj, "path");
    if (path == null)
        return Responses.badRequest("No `path` given", req);
    Map<ModelType, Set<String>> toExport = getModels(obj);
    if (toExport == null)
        return Responses.badRequest("No `models` given", req);
    try {
        var store = ZipStore.open(new File(path));
        var export = new JsonExport(context.db, store);
        for (ModelType type : toExport.keySet()) {
            for (String refId : toExport.get(type)) {
                export.write(Daos.root(context.db, type).getForRefId(refId));
            }
        }
        store.close();
        return Responses.ok("Exported to " + path, req);
    } catch (IOException e) {
        return Responses.serverError(e, req);
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) JsonObject(com.google.gson.JsonObject) ModelType(org.openlca.core.model.ModelType) JsonExport(org.openlca.jsonld.output.JsonExport) IOException(java.io.IOException) File(java.io.File) Rpc(org.openlca.ipc.Rpc)

Example 8 with Rpc

use of org.openlca.ipc.Rpc in project olca-modules by GreenDelta.

the class ImpactHandler method getProcessResultsImpacts.

@Rpc("get/impacts/process_results")
public RpcResponse getProcessResultsImpacts(RpcRequest req) {
    return utils.fullProcess(req, (result, process, cache) -> {
        JsonArray contributions = new JsonArray();
        result.getImpacts().forEach(impact -> {
            double total = result.getTotalImpactResult(impact);
            if (total == 0)
                return;
            Contribution<ImpactDescriptor> c = new Contribution<>();
            c.item = impact;
            c.amount = result.getDirectImpactResult(process, impact);
            c.share = c.amount / total;
            if (c.amount == 0)
                return;
            contributions.add(JsonRpc.encode(c, cache, json -> {
                json.addProperty("unit", impact.referenceUnit);
                json.addProperty("upstream", result.getUpstreamImpactResult(process, impact));
            }));
        });
        return contributions;
    });
}
Also used : JsonArray(com.google.gson.JsonArray) UpstreamNode(org.openlca.core.results.UpstreamNode) RpcRequest(org.openlca.ipc.RpcRequest) ImpactValue(org.openlca.core.results.ImpactValue) UpstreamTree(org.openlca.core.results.UpstreamTree) LocationDescriptor(org.openlca.core.model.descriptors.LocationDescriptor) Rpc(org.openlca.ipc.Rpc) HashMap(java.util.HashMap) LocationResult(org.openlca.core.results.LocationResult) RpcResponse(org.openlca.ipc.RpcResponse) RootDescriptor(org.openlca.core.model.descriptors.RootDescriptor) ArrayList(java.util.ArrayList) StringPair(org.openlca.ipc.handlers.Upstream.StringPair) List(java.util.List) JsonArray(com.google.gson.JsonArray) EnviFlow(org.openlca.core.matrix.index.EnviFlow) ContributionResult(org.openlca.core.results.ContributionResult) Contribution(org.openlca.core.results.Contribution) Map(java.util.Map) FlowDescriptor(org.openlca.core.model.descriptors.FlowDescriptor) ImpactDescriptor(org.openlca.core.model.descriptors.ImpactDescriptor) ProcessDescriptor(org.openlca.core.model.descriptors.ProcessDescriptor) ImpactDescriptor(org.openlca.core.model.descriptors.ImpactDescriptor) Contribution(org.openlca.core.results.Contribution) Rpc(org.openlca.ipc.Rpc)

Example 9 with Rpc

use of org.openlca.ipc.Rpc in project olca-modules by GreenDelta.

the class InventoryHandler method getLocationContributions.

@Rpc("get/inventory/contributions/locations")
public RpcResponse getLocationContributions(RpcRequest req) {
    return utils.contributionFlow(req, (result, flow, cache) -> {
        LocationResult r = new LocationResult(result, cache.db);
        List<Contribution<LocationDescriptor>> cons = utils.toDescriptors(r.getContributions(flow.flow()));
        cons = utils.filter(cons, c -> c.amount != 0);
        String unit = utils.getUnit(flow, cache);
        return JsonRpc.encode(cons, cache, json -> json.addProperty("unit", unit));
    });
}
Also used : UpstreamNode(org.openlca.core.results.UpstreamNode) RpcRequest(org.openlca.ipc.RpcRequest) UpstreamTree(org.openlca.core.results.UpstreamTree) LocationDescriptor(org.openlca.core.model.descriptors.LocationDescriptor) FlowValue(org.openlca.core.results.FlowValue) Rpc(org.openlca.ipc.Rpc) LocationResult(org.openlca.core.results.LocationResult) RpcResponse(org.openlca.ipc.RpcResponse) RootDescriptor(org.openlca.core.model.descriptors.RootDescriptor) ArrayList(java.util.ArrayList) StringPair(org.openlca.ipc.handlers.Upstream.StringPair) List(java.util.List) JsonArray(com.google.gson.JsonArray) Contribution(org.openlca.core.results.Contribution) FlowDescriptor(org.openlca.core.model.descriptors.FlowDescriptor) ProcessDescriptor(org.openlca.core.model.descriptors.ProcessDescriptor) Contribution(org.openlca.core.results.Contribution) LocationResult(org.openlca.core.results.LocationResult) Rpc(org.openlca.ipc.Rpc)

Example 10 with Rpc

use of org.openlca.ipc.Rpc in project olca-modules by GreenDelta.

the class ModelHandler method createProductSystem.

@Rpc("create/product_system")
public RpcResponse createProductSystem(RpcRequest req) {
    if (req.params == null || !req.params.isJsonObject())
        return Responses.invalidParams("params must be an object with valid processId", req);
    var obj = req.params.getAsJsonObject();
    if (!obj.has("processId") || !obj.get("processId").isJsonPrimitive())
        return Responses.invalidParams("params must be an object with valid processId", req);
    var processId = obj.get("processId").getAsString();
    if (Strings.nullOrEmpty(processId))
        return Responses.invalidParams("params must be an object with valid processId", req);
    var refProcess = new ProcessDao(db).getForRefId(processId);
    if (refProcess == null)
        return Responses.invalidParams("No process found for ref id " + processId, req);
    var system = ProductSystem.of(refProcess);
    system = new ProductSystemDao(db).insert(system);
    var config = new LinkingConfig().preferredType(ProcessType.UNIT_PROCESS);
    if (obj.has("preferredType") && obj.get("preferredType").getAsString().equalsIgnoreCase("lci_result")) {
        config.preferredType(ProcessType.LCI_RESULT);
    }
    config.providerLinking(ProviderLinking.PREFER_DEFAULTS);
    if (obj.has("providerLinking")) {
        if (obj.get("providerLinking").getAsString().equalsIgnoreCase("ignore")) {
            config.providerLinking(ProviderLinking.IGNORE_DEFAULTS);
        } else if (obj.get("providerLinking").getAsString().equalsIgnoreCase("only")) {
            config.providerLinking(ProviderLinking.ONLY_DEFAULTS);
        }
    }
    var builder = new ProductSystemBuilder(MatrixCache.createLazy(db), config);
    builder.autoComplete(system);
    system = ProductSystemBuilder.update(db, system);
    var res = new JsonObject();
    res.addProperty("@id", system.refId);
    return Responses.ok(res, req);
}
Also used : ProcessDao(org.openlca.core.database.ProcessDao) LinkingConfig(org.openlca.core.matrix.linking.LinkingConfig) ProductSystemBuilder(org.openlca.core.matrix.ProductSystemBuilder) JsonObject(com.google.gson.JsonObject) ProductSystemDao(org.openlca.core.database.ProductSystemDao) Rpc(org.openlca.ipc.Rpc)

Aggregations

Rpc (org.openlca.ipc.Rpc)13 JsonArray (com.google.gson.JsonArray)6 JsonObject (com.google.gson.JsonObject)6 UpstreamNode (org.openlca.core.results.UpstreamNode)5 UpstreamTree (org.openlca.core.results.UpstreamTree)5 StringPair (org.openlca.ipc.handlers.Upstream.StringPair)5 ArrayList (java.util.ArrayList)3 List (java.util.List)3 FlowDescriptor (org.openlca.core.model.descriptors.FlowDescriptor)3 LocationDescriptor (org.openlca.core.model.descriptors.LocationDescriptor)3 ProcessDescriptor (org.openlca.core.model.descriptors.ProcessDescriptor)3 RootDescriptor (org.openlca.core.model.descriptors.RootDescriptor)3 Contribution (org.openlca.core.results.Contribution)3 LocationResult (org.openlca.core.results.LocationResult)3 RpcRequest (org.openlca.ipc.RpcRequest)3 RpcResponse (org.openlca.ipc.RpcResponse)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 EnviFlow (org.openlca.core.matrix.index.EnviFlow)2 ImpactDescriptor (org.openlca.core.model.descriptors.ImpactDescriptor)2