openlca-python-tutorial icon indicating copy to clipboard operation
openlca-python-tutorial copied to clipboard

Can not find ContributionResultProvider in OpenLCA 1.8 version

Open equeheil opened this issue 5 years ago • 12 comments

Hello, I updated OpenLCA in version 1.8 and I have now a problem with ContributionResultProvider import : from org.openlca.core.results import ContributionResultProvider Is it really because of the OpenLCA new version ? Where is ContributionResultProvider now ? Thanks for your help.

equeheil avatar Jun 03 '19 15:06 equeheil

Yes, the API changed a bit: the ContributionResultProvider was indeed removed. You should be able to get everything directly from the ContributionResult now (e.g. the method calculateContributions in the SystemCalculator returns a ContributionResult).

msrocka avatar Jun 03 '19 16:06 msrocka

I thank you for your answer. Can I ask for another point ? A new problem occured after I updated ContributionResult. I wish to create a product system like in However the line system.setRefId(UUID.randomUUID().toString()) does not work anymore. The error is AttributeError: 'org.openlca.core.model.ProductSystem' object has no attribute 'setRefId' Is it again because of the new OpenLCA version ? Thanks.

equeheil avatar Jun 04 '19 11:06 equeheil

The code link did not work. I wish to use the code in : https://github.com/GreenDelta/openlca-python-tutorial/blob/master/product_systems.md

equeheil avatar Jun 04 '19 11:06 equeheil

yes, we removed the getters and setters from the classes in the core model: so instead of a = model.getSomething() and model.setSomething(something) you have to write now a = model.something and model.something = something.

msrocka avatar Jun 04 '19 12:06 msrocka

Thanks for the answer. I am sorry for bothering you again, but now my problem is with ProductSystemBuilder in builder = ProductSystemBuilder(MatrixCache.createEager(db)) It is expecting now 2 arguments. I tried to resolve this with the example https://github.com/GreenDelta/olca-modules/blob/54a5ca64d19edcc04a01a94a02034b4406214590/olca-core/src/test/java/examples/ProductSystemBuilderExample.java However the linkingConfig.DefaultProviders import does not work. Do you have a documentation for all changes from OpenLCA 1.7 to 1.8 ? Thanks again for your help.

equeheil avatar Jun 05 '19 15:06 equeheil

I quickly converted the syntax of the example and it works for me:

import java.io.File as File

import org.openlca.core.database.IDatabase as IDatabase
import org.openlca.core.database.ProcessDao as ProcessDao
import org.openlca.core.database.ProductSystemDao as ProductSystemDao 
import org.openlca.core.database.derby.DerbyDatabase as DerbyDatabase
import org.openlca.core.matrix.LinkingConfig as LinkingConfig
import org.openlca.core.matrix.ProductSystemBuilder as ProductSystemBuilder
import org.openlca.core.matrix.LinkingConfig.DefaultProviders as DefaultProviders
import org.openlca.core.matrix.cache.MatrixCache as MatrixCache
import org.openlca.core.model.Process as Process
import org.openlca.core.model.ProcessType as ProcessType
import org.openlca.core.model.ProductSystem as ProductSystem

p = ProcessDao(db).getForRefId(
  "proc_74A400500-1018")

# create and auto-complete the product system
config = LinkingConfig()
config.providerLinking = DefaultProviders.PREFER
config.preferredType = ProcessType.UNIT_PROCESS
builder = ProductSystemBuilder(
  MatrixCache.createLazy(db), config)
system = builder.build(p)
system.name = "Created from Jython"

# save the product system
ProductSystemDao(db).insert(system)

Note that this does not refresh the navigation (but it appears in the search etc. and you can of course refresh it via the API or open/close the database to the the update).

No, we do not document the changes but you can follow them in the commit logs of the olca-app and olca-modules repositories.

msrocka avatar Jun 05 '19 15:06 msrocka

This is strange. For me, the LinkingConfig.DefaultProviders does not work with import org.openlca.core.matrix.LinkingConfig.DefaultProviders as DefaultProviders I could not find any changes in the commit logs.

equeheil avatar Jun 06 '19 08:06 equeheil

What is the error message you get and which openLCA version do you use? Not sure what you mean with no changes in the commit logs: you can track the commit history of each class, package, module etc. on Github e.g. for the LinkingConfig here

msrocka avatar Jun 06 '19 11:06 msrocka

For the line import org.openlca.core.matrix.LinkingConfig.DefaultProviders as DefaultProviders I have the error message Unresolved import: DefaultProviders. I use OpenLCA version 1.8.

equeheil avatar Jun 06 '19 13:06 equeheil

I have no error when I run the following script in openLCA 1.8 (the currently official version from the web-site):

import org.openlca.core.matrix.LinkingConfig.DefaultProviders as DefaultProviders

for v in DefaultProviders.values():
  log.info("{}", v)

This gives the expected result:

INFO - IGNORE
INFO - PREFER
INFO - ONLY

msrocka avatar Jun 06 '19 15:06 msrocka

I must have had a problem with the API, but it works now. I wish now to run a calculation with the system.

# calculation
    system = createProductSystem(process)
    productSystem = buildProductSystem(system)
    setup = CalculationSetup(CalculationType.SIMPLE_CALCULATION, productSystem)
    setup.withCosts = True
    impactMethods = methodDao.getForName('ILCD 2011 Midpoint+')
    impactMethod = Descriptors.toDescriptor(impactMethods[0])
    setup.impactMethod = impactMethod
    calculator = SystemCalculator(m_cache, solver)
    result = calculator.calculateContributions(setup)
    provider = ContributionResult(result, e_cache)

I have got an error in result = calculator.calculateContributions(setup).

result = calculator.calculateContributions(setup)
	at org.openlca.core.math.DataStructures.createProductIndex(DataStructures.java:46)
	at org.openlca.core.math.DataStructures.createInventory(DataStructures.java:99)
	at org.openlca.core.math.DataStructures.matrixData(DataStructures.java:111)
	at org.openlca.core.math.SystemCalculator.calculateContributions(SystemCalculator.java:35)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
java.lang.NullPointerException: java.lang.NullPointerException

The error appears in Eclipse, but OpenLCA itself can run the system that was created before. I am still in OpenLCA 1.8. Is there a change in calculator.calculateContributions ? Thanks again for your help.

equeheil avatar Jun 07 '19 14:06 equeheil

I saw more information from the OpenLCA log. Maybe this can help (I am a beginner in IT development). The org.openlca.updates.script.Python failed to evaluate script then appears the errors messages that Eclipse gave me (my previous message) : at org.openlca.core.math.DataStructures.createProductIndex(DataStructures.java:46) etc. And long before, another error appeared : org.openlca.app.App Failed to load libraries from folder <openLCA>/julia Could you know why these errors happen ? Thanks in advance.

equeheil avatar Jun 12 '19 10:06 equeheil