use of org.openlca.core.model.Process in project olca-modules by GreenDelta.
the class AvoidedFlowsTest method testWaste.
@Test
public void testWaste() {
Process p = TestProcess.refProduct("p", 1, "kg").wasteOut("w", 0.5, "kg").elemOut("CO2", 2.0, "kg").get();
TestProcess.findExchange(p, "w").isAvoided = true;
p = db.update(p);
Process w = TestProcess.refWaste("w", 1.0, "kg").elemOut("CO2", 2.0, "kg").get();
check(p, w);
}
use of org.openlca.core.model.Process 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.model.Process in project olca-app by GreenDelta.
the class DBProvider method sync.
/**
* Sync the flow references with the respective flow in the database (when
* it exists). It tests that the definition of the flow reference can be
* fulfilled with a database flow (i.e. the ref-IDs match and the flow
* property and unit is defined for that flow). If this is not the case it
* returns it sets an error state to the given reference. Otherwise, it will
* mutate the flow reference to have the respective database IDs of the
* corresponding flow and sets the property and unit to the respective
* defaults if they are missing. Also, it will return the matching flow in
* case there was no error (otherwise null).
*/
public Flow sync(FlowRef ref) {
if (Sync.isInvalidFlowRef(ref))
return null;
// we update the status in the following sync. steps
ref.status = null;
// check the flow
Flow flow = new FlowDao(db).getForRefId(ref.flow.refId);
if (flow == null) {
ref.status = MappingStatus.error("there is no flow with id=" + ref.flow.refId + " in the database");
return null;
}
// check the flow property
FlowProperty prop = null;
if (ref.property == null) {
prop = flow.referenceFlowProperty;
} else {
for (FlowPropertyFactor f : flow.flowPropertyFactors) {
if (f.flowProperty == null)
continue;
if (Objects.equals(ref.property.refId, f.flowProperty.refId)) {
prop = f.flowProperty;
break;
}
}
}
if (prop == null || prop.unitGroup == null) {
ref.status = MappingStatus.error("the flow in the database has" + " no corresponding flow property");
return null;
}
// check the unit
Unit u = null;
if (ref.unit == null) {
u = prop.unitGroup.referenceUnit;
} else {
for (Unit ui : prop.unitGroup.units) {
if (Objects.equals(ref.unit.refId, ui.refId)) {
u = ui;
break;
}
}
}
if (u == null) {
ref.status = MappingStatus.error("the flow in the database has" + " no corresponding unit");
return null;
}
// check a possible provider
Process provider = null;
if (ref.provider != null) {
provider = new ProcessDao(db).getForRefId(ref.provider.refId);
if (provider == null) {
ref.status = MappingStatus.error("the provider does not exist in the database");
return null;
}
boolean exists = provider.exchanges.stream().anyMatch(e -> !e.isAvoided && Objects.equals(e.flow, flow) && ((e.isInput && flow.flowType == FlowType.WASTE_FLOW) || (!e.isInput && flow.flowType == FlowType.PRODUCT_FLOW)));
if (!exists) {
ref.status = MappingStatus.error("the given provider does not deliver that flow");
return null;
}
}
// sync the reference data
if (ref.property == null) {
ref.property = Descriptor.of(prop);
}
if (ref.unit == null) {
ref.unit = Descriptor.of(u);
}
ref.flow.id = flow.id;
ref.property.id = prop.id;
ref.unit.id = u.id;
if (provider != null) {
ref.provider = Descriptor.of(provider);
}
Sync.checkFlowName(ref, flow.name);
Sync.checkFlowCategory(ref, String.join("/", Categories.path(flow.category)));
Sync.checkFlowType(ref, flow.flowType);
Sync.checkFlowLocation(ref, flow.location == null ? null : flow.location.code);
if (provider != null) {
Sync.checkProviderLocation(ref, provider.location == null ? null : provider.location.code);
}
if (ref.status == null) {
ref.status = MappingStatus.ok("flow in sync. with database");
}
return flow;
}
use of org.openlca.core.model.Process in project olca-app by GreenDelta.
the class InfoPage method createDqEntryRow.
private Hyperlink createDqEntryRow(Composite parent, FormToolkit tk) {
UI.formLabel(parent, tk, M.DataQualityEntry);
Supplier<String> dqLabel = () -> {
Process p = getModel();
return p.dqSystem == null || Strings.nullOrEmpty(p.dqEntry) ? "(not specified)" : p.dqSystem.applyScoreLabels(p.dqEntry);
};
Hyperlink link = UI.formLink(parent, tk, dqLabel.get());
Controls.onClick(link, e -> {
if (getModel().dqSystem == null) {
MsgBox.info("No data quality system is selected");
return;
}
String oldVal = getModel().dqEntry;
DQSystem system = getModel().dqSystem;
String entry = getModel().dqEntry;
DataQualityShell shell = DataQualityShell.withoutUncertainty(parent.getShell(), system, entry);
shell.onOk = (_shell) -> {
getModel().dqEntry = _shell.getSelection();
};
shell.onDelete = (_shell) -> {
getModel().dqEntry = null;
};
shell.addDisposeListener(_e -> {
if (Objects.equals(oldVal, getModel().dqEntry))
return;
link.setText(dqLabel.get());
link.pack();
getEditor().setDirty(true);
});
shell.open();
});
link.setEnabled(isEditable());
new CommentControl(parent, getToolkit(), "dqEntry", getComments());
return link;
}
use of org.openlca.core.model.Process in project olca-app by GreenDelta.
the class ProcessEditor method evalFormulas.
private void evalFormulas() {
Process p = getModel();
var errors = Formulas.eval(Database.get(), p);
if (!errors.isEmpty()) {
String message = errors.get(0);
if (errors.size() > 1)
message += " (" + (errors.size() - 1) + " more)";
MsgBox.error(M.FormulaEvaluationFailed, message);
}
}
Aggregations