0 votes
645 views
I have a number of processes that I want to turn into product systems. Is there a short cut for this or do I have to turn each process into a product system one by one?
in openLCA by (240 points)

1 Answer

0 votes
by (5.2k points)

I thought I had posted this previously, but the post isn't jumping out. Anyways here's a python script that you can paste into the built-in python developer tool. You should get a dialog box asking you to select all the processes you want to turn into product systems. Note that this is currently only setup to link to default providers only. If you want to change that to one of the other options you'll need this line: linkingConfig.providerLinking = DefaultProviders.ONLY to DefaultProviders.PREFER_DEFAULTS or something. Good luck!

#Most of these imports are probably unnecessary now - leftover from a previous script
import csv
from org.openlca.app import App
from org.openlca.app.components import FileChooser, ModelSelectionDialog
from org.openlca.app.db import Cache
from org.openlca.core.math import CalculationSetup, CalculationType, SystemCalculator
from org.openlca.core.matrix import ProductSystemBuilder
from org.openlca.core.model import ModelType, ProductSystem
from org.openlca.core.model.descriptors import Descriptors
from org.openlca.core.database import ImpactMethodDao, ProcessDao, ProductSystemDao, BaseDao, Daos, ExchangeDao
from org.openlca.core.matrix  import LinkingConfig
from org.openlca.core.database.derby import DerbyDatabase
from org.eclipse.swt.widgets import Display
from java.lang import Long
from java.util import UUID
from java.io import File
from org.openlca.app.util import UI, Dialog
from org.openlca.app import App
from org.openlca.core.matrix.cache import MatrixCache
from org.openlca.core.matrix.LinkingConfig import DefaultProviders
from org.openlca.core.model import ProcessType

#Apparently db is global variable - maybe it comes in with import App above.
processDao = ProcessDao(db)
systemDao = ProductSystemDao(db)
methodDao = ImpactMethodDao(db)

def run(process):
    log.trace("You have entered run")
    log.trace(str(process.name))
    log.trace("Building system for " + process.name)
    linkingConfig = LinkingConfig()
    log.trace("Made a new linking config!")
    #I set this up to use default providers only. Options are
    #IGNORE, PREFER, and ONLY.
    #If you select PREFER or IGNORE you probably need the 
    #next line (options are LCI_RESULT or UNIT_PROCESS):
    #linkingConfig.preferredType = ProcessType.UNIT_PROCESS
    linkingConfig.providerLinking = DefaultProviders.ONLY
    log.trace("Made it default providers only!")
    dasCache=MatrixCache.createLazy(db)
    log.trace("Got MatrixCache")
    builder = ProductSystemBuilder(dasCache,linkingConfig)
    log.trace("Created Builder")
    system = builder.build(process)
    log.trace("Completed auto-building product system")
    ProductSystemDao(db).insert(system)
    log.trace("Inserted system into database!")
    
def main():
    log.trace("Here we go")
    processes = list(ModelSelectionDialog.multiSelect(ModelType.PROCESS))
    log.trace("You selected some processes, maybe " + str(len(processes)) + "?")
    if processes is None or len(processes) is 0:
        log.trace("No processes selected")
        return
    for descriptor in processes:
      log.trace("You have entered the for loop")
      log.trace(str(type(descriptor)))
      log.trace(str(descriptor.id))
      try: 
          process = processDao.getForId(descriptor.id)
          log.trace("process was assigned")
          run(process)
      except:
          log.trace("Well something went wrong")
Display.getDefault().asyncExec(main)
...