GreenDelta/openlca-python-tutorial

Deleteing a process in a product system seems to break the product system

ezrakahn opened this issue · 1 comments

Hi

I have some larger product systems of a few hundred processes. I have a use case where some of the processes need to be deleted, while preserving the rest of the product system, and even keeping the provider processes for the deleted process.

If I delete an upstream process using process.delete(), the product system appears to be corrupted, with only a fraction of the provider processes showing up in the graph, and the product system file not being included with the json export.

Is this to be expected, or am I not approaching this correctly? Also, is this an appropriate place for these types of usability questions?

Thanks in advance!

Yes, if you want to delete a process that is used in a product system, you first have to remove it from the system; something like this should work:

import org.openlca.core.database.ProductSystemDao as SystemDao
import org.openlca.core.database.ProcessDao as ProcessDao
import java.lang.Long as Long

sysDao = SystemDao(db)
system = sysDao.getForName('A')[0]

procDao = ProcessDao(db)
process = procDao.getForName('B')[0]

link_removals = []
for link in system.processLinks:
  if link.processId == process.id or link.providerId == process.id:
    log.info('delete link {} -> {}', link.providerId, link.processId)
    link_removals.append(link)

system.processLinks.removeAll(link_removals)    
system.processes.remove(Long(process.id))  # ->Long()! otherwise we get a key error here

sysDao.update(system)
procDao.delete(process)

Note that this does not work if the process is the reference process of the system. Also, the product system may not is a connected graph anymore. You could modify the links to connect them to a different process (with the same product flow). There is also a class ProcessUseSearch with which you can find where a prosess is used.

I think a good place for such questions could be Stack Overflow. We could create an openlca tag there so that it is easy to find these questions and get notified automatically. What do yot think?