use of org.openlca.core.database.IDatabase in project olca-modules by GreenDelta.
the class DbLibrarySwap method techFlowsOf.
private List<TechFlow> techFlowsOf(IDatabase db) {
var processes = new ProcessDao(db).getDescriptors().stream().collect(map());
var flows = new FlowDao(db).getDescriptors().stream().filter(d -> d.flowType != FlowType.ELEMENTARY_FLOW).collect(map());
var protoIndex = library.getProductIndex();
var list = new ArrayList<TechFlow>();
for (int i = 0; i < protoIndex.getProductCount(); i++) {
var protoEntry = protoIndex.getProduct(i);
var process = processes.get(protoEntry.getProcess().getId());
var flow = flows.get(protoEntry.getProduct().getId());
if (process != null && flow != null) {
list.add(TechFlow.of(process, flow));
}
}
return list;
}
use of org.openlca.core.database.IDatabase in project olca-modules by GreenDelta.
the class Graph method build.
/**
* Creates a graph that synchronizes the given eILCD model with the given
* database.
*/
static Graph build(Model model, IDatabase db) {
Graph g = new Graph();
Logger log = LoggerFactory.getLogger(Graph.class);
if (model == null || model.info == null || db == null) {
log.warn("Invalid constraints; return empty index");
return g;
}
Technology tech = model.info.technology;
if (tech == null) {
log.warn("No processes in model; return empty index");
return g;
}
Map<Integer, Group> groups = tech.groups.stream().collect(Collectors.toMap(group -> group.id, group -> group));
ProcessDao dao = new ProcessDao(db);
for (ProcessInstance pi : tech.processes) {
if (pi.process == null || pi.process.uuid == null) {
log.warn("Invalid process reference node={}", pi.id);
continue;
}
String refID = pi.process.uuid;
Process process = dao.getForRefId(refID);
if (process == null) {
log.warn("Could not find process {}; skip node {}", refID, pi.id);
continue;
}
Node n = Node.init(pi, process);
if (!pi.groupRefs.isEmpty()) {
GroupRef gr = pi.groupRefs.get(0);
n.group = groups.get(gr.groupID);
}
g.putNode(n);
}
buildLinks(g, model);
QuantitativeReference qRef = model.info.quantitativeReference;
if (qRef != null && qRef.refProcess != null) {
g.root = g.getNode(qRef.refProcess);
}
return g;
}
use of org.openlca.core.database.IDatabase in project olca-app by GreenDelta.
the class ImpactPage method loadFactors.
private List<Factor> loadFactors() {
IDatabase db = Database.get();
String sql = "select f_impact_category, f_unit, f_location, " + "value from tbl_impact_factors where f_flow = " + getModel().id;
EntityCache ecache = Cache.getEntityCache();
List<Factor> factors = new ArrayList<>();
try {
NativeSql.on(db).query(sql, r -> {
Factor f = new Factor();
f.impact = ecache.get(ImpactDescriptor.class, r.getLong(1));
if (f.impact == null)
return true;
Unit unit = ecache.get(Unit.class, r.getLong(2));
if (unit != null) {
f.unit = unit.name;
}
long locID = r.getLong(3);
if (!r.wasNull()) {
f.location = ecache.get(LocationDescriptor.class, locID);
}
f.value = r.getDouble(4);
factors.add(f);
return true;
});
} catch (Exception e) {
Logger log = LoggerFactory.getLogger(getClass());
log.error("Failed to load LCIA factors", e);
}
// sort and set display flags
factors.sort((f1, f2) -> {
int c = Strings.compare(Labels.name(f1.impact), Labels.name(f2.impact));
if (c != 0)
return c;
if (f1.location == null)
return -1;
if (f2.location == null)
return 1;
return Strings.compare(f1.location.code, f2.location.code);
});
return factors;
}
use of org.openlca.core.database.IDatabase in project olca-app by GreenDelta.
the class GeoFactorCalculator method run.
@Override
public void run() {
// check the input
if (setup == null || impact == null) {
log.error("no setup or LCIA category");
return;
}
IDatabase db = Database.get();
if (db == null) {
log.error("no connected database");
return;
}
if (setup.bindings.isEmpty()) {
log.warn("no flow bindings; nothing to do");
return;
}
// and finally generate the LCIA factors
if (setup.features.isEmpty()) {
log.error("no features available for the " + "intersection calculation");
return;
}
var params = calcParamVals(setup.features);
createFactors(params);
}
use of org.openlca.core.database.IDatabase in project olca-app by GreenDelta.
the class GeoJsonImportWizard method performFinish.
@Override
public boolean performFinish() {
File[] files = filePage.getFiles();
if (files == null || files.length == 0)
return false;
IDatabase db = Database.get();
if (db == null) {
MsgBox.error(M.NoDatabaseOpened, M.NeedOpenDatabase);
return true;
}
try {
getContainer().run(true, false, m -> {
m.beginTask("Import geometries from GeoJSON...", IProgressMonitor.UNKNOWN);
for (File f : files) {
new GeoJsonImport(f, db).run();
}
m.done();
});
Navigator.refresh(Navigator.findElement(ModelType.LOCATION));
return true;
} catch (Exception e) {
ErrorReporter.on("Failed to import GeoJSON file", e);
return true;
}
}
Aggregations