use of org.openlca.core.database.FlowDao in project olca-modules by GreenDelta.
the class ConversionTableTest method testFlowPropertyFactor.
@Test
public void testFlowPropertyFactor() throws Exception {
FlowDao dao = new FlowDao(database);
Flow flow = new Flow();
flow.name = "test-flow";
FlowPropertyFactor factor1 = new FlowPropertyFactor();
factor1.conversionFactor = 1d;
flow.flowPropertyFactors.add(factor1);
FlowPropertyFactor factor2 = new FlowPropertyFactor();
factor2.conversionFactor = 0.42;
flow.flowPropertyFactors.add(factor2);
dao.insert(flow);
ConversionTable table = ConversionTable.create(database);
Assert.assertEquals(1d, table.getPropertyFactor(factor1.id), 1e-16);
Assert.assertEquals(0.42, table.getPropertyFactor(factor2.id), 1e-16);
dao.delete(flow);
}
use of org.openlca.core.database.FlowDao in project olca-modules by GreenDelta.
the class ProductSystemWriter method mapLinks.
private void mapLinks(ProductSystem system, ProtoProductSystem.Builder proto, Map<Long, RootDescriptor> processes) {
// collect the used flows
var flowIDs = new HashSet<Long>();
var usedExchanges = new TLongHashSet();
for (var link : system.processLinks) {
flowIDs.add(link.flowId);
usedExchanges.add(link.exchangeId);
}
var flows = new FlowDao(config.db).getDescriptors(flowIDs).stream().collect(Collectors.toMap(d -> d.id, d -> d));
// collect the used exchanges
var exchangeIDs = new TLongIntHashMap();
String sql = "select id, internal_id from tbl_exchanges";
NativeSql.on(config.db).query(sql, r -> {
long id = r.getLong(1);
if (usedExchanges.contains(id)) {
exchangeIDs.put(id, r.getInt(2));
}
return true;
});
// add the links
for (var link : system.processLinks) {
var protoLink = ProtoProcessLink.newBuilder();
// provider
var provider = processes.get(link.providerId);
if (provider != null) {
protoLink.setProvider(Refs.tinyRefOf(provider));
Out.dep(config, provider);
}
// process
var process = processes.get(link.processId);
if (process != null) {
protoLink.setProcess(Refs.tinyRefOf(process));
Out.dep(config, process);
}
// flow
var flow = flows.get(link.flowId);
if (flow != null) {
protoLink.setFlow(Refs.tinyRefOf(flow));
}
// linked exchange
var eid = exchangeIDs.get(link.exchangeId);
if (eid != 0) {
protoLink.setExchange(ProtoExchangeRef.newBuilder().setInternalId(eid).build());
}
// add the link
proto.addProcessLinks(protoLink);
}
}
use of org.openlca.core.database.FlowDao 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;
}
use of org.openlca.core.database.FlowDao in project olca-app by GreenDelta.
the class FlowUseSection method render.
void render(Composite body, FormToolkit toolkit) {
log.trace("render flow-use-section for flow {}", flow);
FlowDao dao = new FlowDao(database);
Set<Long> recipients = dao.getWhereInput(flow.id);
Set<Long> providers = dao.getWhereOutput(flow.id);
if (recipients.isEmpty() && providers.isEmpty())
return;
Section section = UI.section(body, toolkit, M.UsedInProcesses);
section.setExpanded(false);
parent = UI.sectionClient(section, toolkit);
this.toolkit = toolkit;
App.runInUI("Render usage links", () -> {
renderLinks(M.ConsumedBy, recipients, Icon.INPUT.get());
renderLinks(M.ProducedBy, providers, Icon.OUTPUT.get());
});
}
use of org.openlca.core.database.FlowDao in project olca-app by GreenDelta.
the class Matcher method findProvider.
private ProcessDescriptor findProvider(IDatabase db, FlowRef s, FlowRef t) {
long tid = t.flow.id;
var processIDs = t.flow.flowType == FlowType.WASTE_FLOW ? new FlowDao(db).getWhereInput(tid) : new FlowDao(db).getWhereOutput(tid);
if (processIDs.isEmpty())
return null;
var candidates = new ProcessDao(db).getDescriptors(processIDs);
if (candidates.isEmpty())
return null;
if (candidates.size() == 1)
return candidates.get(0);
ProcessDescriptor cand = null;
double score = 0.0;
parser.parseInto(phrase1, s.flow.name);
for (var d : candidates) {
// include possible location codes; location codes are
// often added to process names
var processName = Labels.name(d);
parser.parseInto(phrase2, processName);
double sim = similarity.get(phrase1, phrase2);
if (cand == null || sim > score) {
cand = d;
score = sim;
}
}
return cand;
}
Aggregations