Search in sources :

Example 11 with IDatabase

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

the class TestSocialAspectImport method testCopySocialAspects.

/**
 * Test that social aspects of processes are copied between databases:
 * https://github.com/GreenDelta/olca-app/issues/88. We ignore this test by
 * default, because creating temporary databases take so long...
 */
@Test
@Ignore
public void testCopySocialAspects() throws Exception {
    IDatabase db1 = Derby.createInMemory();
    SocialIndicator indicator = new SocialIndicator();
    indicator.refId = "si";
    SocialIndicatorDao idao = new SocialIndicatorDao(db1);
    idao.insert(indicator);
    Process process = new Process();
    process.refId = "pr";
    SocialAspect aspect = new SocialAspect();
    aspect.indicator = indicator;
    process.socialAspects.add(aspect);
    ProcessDao pdao = new ProcessDao(db1);
    pdao.insert(process);
    IDatabase db2 = Derby.createInMemory();
    DatabaseImport imp = new DatabaseImport(db1, db2);
    imp.run();
    db1.close();
    idao = new SocialIndicatorDao(db2);
    indicator = idao.getForRefId("si");
    assertNotNull(indicator);
    pdao = new ProcessDao(db2);
    process = pdao.getForRefId("pr");
    assertEquals(1, process.socialAspects.size());
    aspect = process.socialAspects.get(0);
    assertNotNull(aspect.indicator);
    assertEquals("si", aspect.indicator.refId);
    db2.close();
}
Also used : IDatabase(org.openlca.core.database.IDatabase) SocialAspect(org.openlca.core.model.SocialAspect) ProcessDao(org.openlca.core.database.ProcessDao) SocialIndicator(org.openlca.core.model.SocialIndicator) Process(org.openlca.core.model.Process) SocialIndicatorDao(org.openlca.core.database.SocialIndicatorDao) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 12 with IDatabase

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

the class CategoriesTest method testFindOrAdd.

@Test
public void testFindOrAdd() {
    IDatabase db = Tests.getDb();
    String[] path = { "A", "B", "C", "D", "E" };
    Category c = null;
    for (int i = 0; i < 10; i++) {
        Category next = Categories.findOrAdd(db, ModelType.FLOW, path);
        if (c != null) {
            assertEquals(c.id, next.id);
        } else {
            c = next;
        }
    }
    checkCat(path, c);
}
Also used : IDatabase(org.openlca.core.database.IDatabase) Category(org.openlca.core.model.Category) Test(org.junit.Test)

Example 13 with IDatabase

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

the class CategoriesTest method testFindOrAddModel.

@Test
public void testFindOrAddModel() {
    IDatabase db = Tests.getDb();
    String[] path = { "A", "B", "C", "D", "E" };
    Category c = null;
    for (int i = 0; i < 10; i++) {
        Category next = Categories.findOrAdd(db, ModelType.FLOW, path);
        if (c != null) {
            assertEquals(c.id, next.id);
        } else {
            c = next;
        }
        Flow f = new Flow();
        f.category = c;
        FlowDao dao = new FlowDao(db);
        dao.insert(f);
        f = dao.getForId(f.id);
        checkCat(path, f.category);
    }
}
Also used : IDatabase(org.openlca.core.database.IDatabase) Category(org.openlca.core.model.Category) FlowDao(org.openlca.core.database.FlowDao) Flow(org.openlca.core.model.Flow) Test(org.junit.Test)

Example 14 with IDatabase

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

the class ProductSystemBuilderExample method main.

public static void main(String[] args) {
    String dbPath = "C:/Users/Besitzer/openLCA-data-1.4/databases/ecoinvent_2_2_unit";
    IDatabase db = new Derby(new File(dbPath));
    // load the reference process of the new product system
    Process p = new ProcessDao(db).getForRefId("81261285-cc4a-3588-8cce-3aabb786d7aa");
    // create and auto-complete the product system
    var config = new LinkingConfig().providerLinking(ProviderLinking.PREFER_DEFAULTS).preferredType(ProcessType.UNIT_PROCESS);
    var system = new ProductSystemBuilder(MatrixCache.createLazy(db), config).build(p);
    // save the product system
    new ProductSystemDao(db).insert(system);
}
Also used : IDatabase(org.openlca.core.database.IDatabase) Derby(org.openlca.core.database.Derby) ProcessDao(org.openlca.core.database.ProcessDao) LinkingConfig(org.openlca.core.matrix.linking.LinkingConfig) ProductSystemBuilder(org.openlca.core.matrix.ProductSystemBuilder) Process(org.openlca.core.model.Process) File(java.io.File) ProductSystemDao(org.openlca.core.database.ProductSystemDao)

Example 15 with IDatabase

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

the class ProductSystemInMemoryCalculationExample method main.

public static void main(String[] args) throws Exception {
    // load the database and matrix cache
    String workspace = "C:/Users/Besitzer/openLCA-data-1.4";
    String dbPath = workspace + "/databases/ecoinvent_2_2_unit";
    IDatabase db = new Derby(new File(dbPath));
    MatrixCache mcache = MatrixCache.createLazy(db);
    // load the reference process and create
    // the product system with auto-completion
    // the system is not saved in the database
    Process p = new ProcessDao(db).getForRefId("7ff672e3-a296-30e8-b1bb-a3173711a28b");
    var config = new LinkingConfig().providerLinking(ProviderLinking.PREFER_DEFAULTS).preferredType(ProcessType.UNIT_PROCESS);
    var builder = new ProductSystemBuilder(mcache, config);
    var system = builder.build(p);
    var method = db.get(ImpactMethod.class, "207ffac9-aaa8-401d-ac90-874defd3751a");
    // create the calculation setup
    var setup = CalculationSetup.simple(system).withImpactMethod(method);
    // load the native library and calculate the result
    // TODO: load Julia libraries first here
    SystemCalculator calc = new SystemCalculator(db);
    SimpleResult r = calc.calculateSimple(setup);
    // print the LCIA results
    for (ImpactDescriptor impact : r.getImpacts()) {
        System.out.println(impact.name + "\t" + r.getTotalImpactResult(impact) + "\t" + impact.referenceUnit);
    }
    // finally, close the database
    db.close();
}
Also used : IDatabase(org.openlca.core.database.IDatabase) Derby(org.openlca.core.database.Derby) ImpactDescriptor(org.openlca.core.model.descriptors.ImpactDescriptor) Process(org.openlca.core.model.Process) SimpleResult(org.openlca.core.results.SimpleResult) MatrixCache(org.openlca.core.matrix.cache.MatrixCache) ProcessDao(org.openlca.core.database.ProcessDao) LinkingConfig(org.openlca.core.matrix.linking.LinkingConfig) ProductSystemBuilder(org.openlca.core.matrix.ProductSystemBuilder) File(java.io.File) SystemCalculator(org.openlca.core.math.SystemCalculator)

Aggregations

IDatabase (org.openlca.core.database.IDatabase)25 ProcessDao (org.openlca.core.database.ProcessDao)12 File (java.io.File)9 Process (org.openlca.core.model.Process)9 Test (org.junit.Test)8 Category (org.openlca.core.model.Category)4 Logger (org.slf4j.Logger)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Collectors (java.util.stream.Collectors)3 FlowDao (org.openlca.core.database.FlowDao)3 ProcessDescriptor (org.openlca.core.model.descriptors.ProcessDescriptor)3 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CategoryDao (org.openlca.core.database.CategoryDao)2 Derby (org.openlca.core.database.Derby)2 NativeSql (org.openlca.core.database.NativeSql)2 ParameterDao (org.openlca.core.database.ParameterDao)2 ProductSystemDao (org.openlca.core.database.ProductSystemDao)2 ProductSystemBuilder (org.openlca.core.matrix.ProductSystemBuilder)2