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);
}
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());
}
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);
}
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);
}
}
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;
}
Aggregations