Search in sources :

Example 21 with FlowDao

use of org.openlca.core.database.FlowDao in project olca-modules by GreenDelta.

the class DbLibrarySwap method techFlowsOf.

private List<TechFlow> techFlowsOf(IDatabase db) {
    var processes = new ProcessDao(db).getDescriptors().stream().collect(map());
    var flows = new FlowDao(db).getDescriptors().stream().filter(d -> d.flowType != FlowType.ELEMENTARY_FLOW).collect(map());
    var protoIndex = library.getProductIndex();
    var list = new ArrayList<TechFlow>();
    for (int i = 0; i < protoIndex.getProductCount(); i++) {
        var protoEntry = protoIndex.getProduct(i);
        var process = processes.get(protoEntry.getProcess().getId());
        var flow = flows.get(protoEntry.getProduct().getId());
        if (process != null && flow != null) {
            list.add(TechFlow.of(process, flow));
        }
    }
    return list;
}
Also used : FlowType(org.openlca.core.model.FlowType) JsonImport(org.openlca.jsonld.input.JsonImport) UpdateMode(org.openlca.jsonld.input.UpdateMode) RootEntity(org.openlca.core.model.RootEntity) ProcessDao(org.openlca.core.database.ProcessDao) ZipStore(org.openlca.jsonld.ZipStore) NativeSql(org.openlca.core.database.NativeSql) Collectors(java.util.stream.Collectors) File(java.io.File) RootDescriptor(org.openlca.core.model.descriptors.RootDescriptor) ArrayList(java.util.ArrayList) TLongObjectHashMap(gnu.trove.map.hash.TLongObjectHashMap) TechFlow(org.openlca.core.matrix.index.TechFlow) List(java.util.List) IDatabase(org.openlca.core.database.IDatabase) Map(java.util.Map) TLongHashSet(gnu.trove.set.hash.TLongHashSet) FlowDao(org.openlca.core.database.FlowDao) Collector(java.util.stream.Collector) FlowDao(org.openlca.core.database.FlowDao) ProcessDao(org.openlca.core.database.ProcessDao) ArrayList(java.util.ArrayList)

Example 22 with FlowDao

use of org.openlca.core.database.FlowDao in project olca-modules by GreenDelta.

the class TechIndex method eachProviderOf.

private static void eachProviderOf(IDatabase db, Consumer<TechFlow> fn) {
    var processes = new ProcessDao(db).descriptorMap();
    var flows = new FlowDao(db).descriptorMap();
    String sql = "select f_owner, f_flow, is_input from tbl_exchanges";
    NativeSql.on(db).query(sql, r -> {
        long flowID = r.getLong(2);
        var flow = flows.get(flowID);
        if (flow == null || flow.flowType == null || flow.flowType == FlowType.ELEMENTARY_FLOW)
            return true;
        var type = flow.flowType;
        boolean isInput = r.getBoolean(3);
        if (isInput && type == FlowType.PRODUCT_FLOW)
            return true;
        if (!isInput && type == FlowType.WASTE_FLOW)
            return true;
        long procID = r.getLong(1);
        var process = processes.get(procID);
        if (process == null) {
            // case the process would be null.
            return true;
        }
        fn.accept(TechFlow.of(process, flow));
        return true;
    });
}
Also used : FlowDao(org.openlca.core.database.FlowDao) ProcessDao(org.openlca.core.database.ProcessDao)

Example 23 with FlowDao

use of org.openlca.core.database.FlowDao in project olca-modules by GreenDelta.

the class ProcessTest method createCyclicModel.

private Process[] createCyclicModel(IDatabase db) {
    UnitGroup ug = createUnitGroup(new UnitGroupDao(db));
    FlowProperty fp = createFlowProperty(ug, new FlowPropertyDao(db));
    Flow product1 = createProduct(fp, new FlowDao(db));
    Flow product2 = createProduct(fp, new FlowDao(db));
    ProcessDao dao = new ProcessDao(db);
    Process p1 = createProcess(product1, dao);
    Process p2 = createProcess(product2, dao);
    p1 = addProvider(p1, p2, dao);
    p2 = addProvider(p2, p1, dao);
    return new Process[] { p1, p2 };
}
Also used : UnitGroup(org.openlca.core.model.UnitGroup) FlowDao(org.openlca.core.database.FlowDao) FlowPropertyDao(org.openlca.core.database.FlowPropertyDao) ProcessDao(org.openlca.core.database.ProcessDao) Process(org.openlca.core.model.Process) UnitGroupDao(org.openlca.core.database.UnitGroupDao) FlowProperty(org.openlca.core.model.FlowProperty) Flow(org.openlca.core.model.Flow)

Example 24 with FlowDao

use of org.openlca.core.database.FlowDao in project olca-modules by GreenDelta.

the class RegionalizedCalculationTest method flow.

private Flow flow(String name, String unit, FlowType type) {
    FlowDao dao = new FlowDao(db);
    List<Flow> flows = dao.getForName(name);
    if (!flows.isEmpty())
        return flows.get(0);
    var property = property(unit);
    Flow flow = Flow.of(name, type, property);
    return dao.insert(flow);
}
Also used : FlowDao(org.openlca.core.database.FlowDao) Flow(org.openlca.core.model.Flow) EnviFlow(org.openlca.core.matrix.index.EnviFlow) TechFlow(org.openlca.core.matrix.index.TechFlow)

Example 25 with FlowDao

use of org.openlca.core.database.FlowDao in project olca-modules by GreenDelta.

the class ParameterUsageTreeTest method testImpactFactor.

@Test
public void testImpactFactor() {
    var global = db.insert(Parameter.global("param", 42));
    var flow = new Flow();
    flow.name = "CH4";
    new FlowDao(db).insert(flow);
    var impact = new ImpactCategory();
    impact.name = "GWP";
    var factor = new ImpactFactor();
    factor.flow = flow;
    factor.value = 24.0;
    factor.formula = "2 * param";
    impact.impactFactors.add(factor);
    new ImpactCategoryDao(db).insert(impact);
    // find the factor node
    var tree = ParameterUsageTree.of("param", db);
    var node = find(tree, "GWP", "CH4");
    assertNotNull(node);
    assertEquals(Descriptor.of(flow), node.model);
    assertEquals(UsageType.FORMULA, node.usageType);
    assertEquals("2 * param", node.usage);
    // find the global parameter definition
    node = find(tree, "param");
    assertNotNull(node);
    assertEquals(Descriptor.of(global), node.model);
    assertEquals(UsageType.DEFINITION, node.usageType);
}
Also used : ImpactFactor(org.openlca.core.model.ImpactFactor) FlowDao(org.openlca.core.database.FlowDao) ImpactCategory(org.openlca.core.model.ImpactCategory) ImpactCategoryDao(org.openlca.core.database.ImpactCategoryDao) Flow(org.openlca.core.model.Flow) Test(org.junit.Test)

Aggregations

FlowDao (org.openlca.core.database.FlowDao)38 Flow (org.openlca.core.model.Flow)21 ProcessDao (org.openlca.core.database.ProcessDao)13 FlowPropertyFactor (org.openlca.core.model.FlowPropertyFactor)10 ArrayList (java.util.ArrayList)7 Collectors (java.util.stream.Collectors)6 FlowPropertyDao (org.openlca.core.database.FlowPropertyDao)6 IDatabase (org.openlca.core.database.IDatabase)6 FlowProperty (org.openlca.core.model.FlowProperty)6 FlowDescriptor (org.openlca.core.model.descriptors.FlowDescriptor)5 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 Test (org.junit.Test)4 LocationDao (org.openlca.core.database.LocationDao)4 Process (org.openlca.core.model.Process)4 Unit (org.openlca.core.model.Unit)4 TLongHashSet (gnu.trove.set.hash.TLongHashSet)3 HashSet (java.util.HashSet)3 Objects (java.util.Objects)3