Search in sources :

Example 16 with CategoryDao

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

the class IsicTreeTest method testSyncFlowCategories.

@Test
public void testSyncFlowCategories() {
    IDatabase database = Tests.getDb();
    CategoryDao dao = new CategoryDao(database);
    Category cat = new Category();
    cat.name = "0121:Growing of grapes";
    cat.modelType = ModelType.FLOW;
    cat = dao.insert(cat);
    new IsicCategoryTreeSync(database, ModelType.FLOW).run();
    cat = dao.getForId(cat.id);
    Assert.assertNotNull(cat.category);
    Assert.assertEquals("012:Growing of perennial crops", cat.category.name);
}
Also used : IDatabase(org.openlca.core.database.IDatabase) Category(org.openlca.core.model.Category) CategoryDao(org.openlca.core.database.CategoryDao) Test(org.junit.Test)

Example 17 with CategoryDao

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

the class ImpactMethodExport method doIt.

@Override
protected void doIt(CSVPrinter printer, IDatabase db) throws IOException {
    log.trace("write impact methods");
    ImpactMethodDao dao = new ImpactMethodDao(db);
    CategoryDao categoryDao = new CategoryDao(db);
    List<ImpactMethodDescriptor> methods = dao.getDescriptors();
    for (ImpactMethodDescriptor method : methods) {
        Object[] line = createLine(method, categoryDao);
        printer.printRecord(line);
    }
    log.trace("{} impact methods written", methods.size());
}
Also used : CategoryDao(org.openlca.core.database.CategoryDao) ImpactMethodDescriptor(org.openlca.core.model.descriptors.ImpactMethodDescriptor) ImpactMethodDao(org.openlca.core.database.ImpactMethodDao)

Example 18 with CategoryDao

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

the class CategoriesTest method testPaths.

@Test
public void testPaths() {
    var root = Category.of("Elementary flows", ModelType.FLOW);
    var top = Category.childOf(root, "air");
    var sub = Category.childOf(top, "high population density");
    new CategoryDao(db).insert(root);
    var categories = Categories.pathsOf(db);
    // test string paths
    assertNull(categories.pathOf(null));
    assertNull(categories.pathOf(-999L));
    assertEquals("Elementary flows", categories.pathOf(root.id));
    assertEquals("Elementary flows/air", categories.pathOf(top.id));
    assertEquals("Elementary flows/air/high population density", categories.pathOf(sub.id));
    // test lists
    assertTrue(categories.listOf(null).isEmpty());
    assertTrue(categories.listOf(-999L).isEmpty());
    assertEquals(List.of("Elementary flows"), categories.listOf(root.id));
    assertEquals(List.of("Elementary flows", "air"), categories.listOf(top.id));
    assertEquals(List.of("Elementary flows", "air", "high population density"), categories.listOf(sub.id));
    // clean up
    db.delete(sub, top, root);
}
Also used : CategoryDao(org.openlca.core.database.CategoryDao) Test(org.junit.Test)

Example 19 with CategoryDao

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

the class ModelImport method run.

public ProductSystem run(Model model) {
    if (model == null)
        return null;
    var dao = new ProductSystemDao(config.db());
    system = dao.getForRefId(model.getUUID());
    if (system != null)
        return system;
    String origin = Models.getOrigin(model);
    if (Strings.nullOrEqual("openLCA", origin)) {
        system = new ProductSystem();
        IO.mapMetaData(model, system);
        String[] path = Categories.getPath(model);
        system.category = new CategoryDao(config.db()).sync(ModelType.PRODUCT_SYSTEM, path);
        mapModel(model);
        system = dao.insert(system);
        config.log().imported(system);
        return system;
    } else {
        Graph g = Graph.build(model, config.db());
        g = Transformation.on(g);
        return new GraphSync(config).sync(model, g);
    }
}
Also used : CategoryDao(org.openlca.core.database.CategoryDao) ProductSystem(org.openlca.core.model.ProductSystem) ProductSystemDao(org.openlca.core.database.ProductSystemDao)

Example 20 with CategoryDao

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

the class MethodImport method flow.

private FlowRec flow(spold2.ImpactFactor eFactor) {
    FlowRec rec = flowCache.get(eFactor.flowID);
    if (rec != null)
        return rec;
    // try to find an existing flow with a matching unit
    Flow flow = new FlowDao(db).getForRefId(eFactor.flowID);
    if (flow != null) {
        FlowPropertyFactor property = null;
        Unit unit = null;
        for (FlowPropertyFactor f : flow.flowPropertyFactors) {
            FlowProperty prop = f.flowProperty;
            if (prop == null || prop.unitGroup == null) {
                continue;
            }
            Unit u = prop.unitGroup.getUnit(eFactor.flowUnit);
            if (u == null)
                continue;
            property = f;
            unit = u;
            if (Objects.equals(prop, flow.referenceFlowProperty))
                break;
        }
        if (unit == null) {
            log.warn("a flow {} with the ID {} exists but it " + "does not have a unit {}", flow.name, eFactor.flowID, eFactor.flowUnit);
            return null;
        }
        rec = new FlowRec(flow, property, unit);
        flowCache.put(eFactor.flowID, rec);
        return rec;
    }
    // try to create a new flow
    if (unitMap == null) {
        unitMap = UnitMapping.createDefault(db);
    }
    UnitMappingEntry e = unitMap.getEntry(eFactor.flowUnit);
    if (e == null) {
        // TODO: not sure how far we should go with the unit mapping here
        // we could even generate flow properties and unit groups for units
        // that do not exist yet but for the elementary flows this should
        // rarely be the case
        log.warn("Unknown unit {}; could not create flow {} with ID {}", eFactor.flowUnit, eFactor.flowName, eFactor.flowID);
        return null;
    }
    flow = new Flow();
    flow.refId = eFactor.flowID;
    flow.name = eFactor.flowName;
    flow.flowType = FlowType.ELEMENTARY_FLOW;
    if (eFactor.compartment != null) {
        flow.category = new CategoryDao(db).sync(ModelType.FLOW, "Elementary flows", eFactor.compartment.compartment, eFactor.compartment.subCompartment);
    }
    flow.referenceFlowProperty = e.flowProperty;
    FlowPropertyFactor property = new FlowPropertyFactor();
    property.conversionFactor = 1.0;
    property.flowProperty = e.flowProperty;
    flow.flowPropertyFactors.add(property);
    flow.lastChange = new Date().getTime();
    flow = new FlowDao(db).insert(flow);
    rec = new FlowRec(flow, property, e.unit);
    flowCache.put(eFactor.flowID, rec);
    return rec;
}
Also used : CategoryDao(org.openlca.core.database.CategoryDao) FlowDao(org.openlca.core.database.FlowDao) UnitMappingEntry(org.openlca.io.UnitMappingEntry) FlowPropertyFactor(org.openlca.core.model.FlowPropertyFactor) Unit(org.openlca.core.model.Unit) FlowProperty(org.openlca.core.model.FlowProperty) Date(java.util.Date) Flow(org.openlca.core.model.Flow)

Aggregations

CategoryDao (org.openlca.core.database.CategoryDao)20 Category (org.openlca.core.model.Category)11 Test (org.junit.Test)4 CategoryElement (org.openlca.app.navigation.elements.CategoryElement)2 ModelElement (org.openlca.app.navigation.elements.ModelElement)2 FlowDao (org.openlca.core.database.FlowDao)2 IDatabase (org.openlca.core.database.IDatabase)2 ImpactMethodDao (org.openlca.core.database.ImpactMethodDao)2 ProductSystemDao (org.openlca.core.database.ProductSystemDao)2 FlowProperty (org.openlca.core.model.FlowProperty)2 Logger (org.slf4j.Logger)2 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 ActorDao (org.openlca.core.database.ActorDao)1 CurrencyDao (org.openlca.core.database.CurrencyDao)1 DQSystemDao (org.openlca.core.database.DQSystemDao)1 FlowPropertyDao (org.openlca.core.database.FlowPropertyDao)1 ImpactCategoryDao (org.openlca.core.database.ImpactCategoryDao)1 LocationDao (org.openlca.core.database.LocationDao)1 NwSetDao (org.openlca.core.database.NwSetDao)1