| 842 | } |
| 843 | |
| 844 | bool MainWindow::getModelParameters(const SolverConfiguration& sc, const QString& model, QStringList& data, const QStringList& extraArgs, QStringList& inlineData) |
| 845 | { |
| 846 | if (!requireMiniZinc()) { |
| 847 | return false; |
| 848 | } |
| 849 | |
| 850 | // Get model interface to obtain parameters |
| 851 | QStringList args; |
| 852 | args << "-c" << "--model-interface-only" << data << model << extraArgs; |
| 853 | |
| 854 | MznProcess p; |
| 855 | // Passing all flags can break --model-inteface-only, so just pass the necessary ones |
| 856 | SolverConfiguration checkConfig(sc.solverDefinition); |
| 857 | checkConfig.additionalData = sc.additionalData; |
| 858 | checkConfig.extraOptions = sc.extraOptions; |
| 859 | |
| 860 | try { |
| 861 | auto result = p.run(checkConfig, args, QFileInfo(model).absolutePath()); |
| 862 | |
| 863 | QStringList additionalDataFiles = data; |
| 864 | if (result.exitCode == 0) { |
| 865 | auto jdoc = QJsonDocument::fromJson(result.stdOut.toUtf8()); |
| 866 | if (jdoc.isObject() && jdoc.object()["input"].isObject() && jdoc.object()["method"].isString()) { |
| 867 | QJsonObject inputArgs = jdoc.object()["input"].toObject(); |
| 868 | QStringList undefinedArgs = inputArgs.keys(); |
| 869 | if (undefinedArgs.size() > 0) { |
| 870 | QStringList params; |
| 871 | paramDialog->getParams(undefinedArgs, getProject().dataFiles(), params, additionalDataFiles); |
| 872 | if (additionalDataFiles.isEmpty()) { |
| 873 | if (params.size()==0) { |
| 874 | return false; |
| 875 | } |
| 876 | for (int i=0; i<undefinedArgs.size(); i++) { |
| 877 | if (params[i].isEmpty()) { |
| 878 | if (! (inputArgs[undefinedArgs[i]].isObject() && inputArgs[undefinedArgs[i]].toObject().contains("optional")) ) { |
| 879 | QMessageBox::critical(this, "Undefined parameter", "The parameter '" + undefinedArgs[i] + "' is undefined."); |
| 880 | return false; |
| 881 | } |
| 882 | } else { |
| 883 | inlineData << undefinedArgs[i] + " = " + params[i]; |
| 884 | } |
| 885 | } |
| 886 | } |
| 887 | } |
| 888 | } else { |
| 889 | ui->outputWidget->addText("Error when checking model parameters:\n", ui->outputWidget->errorCharFormat()); |
| 890 | ui->outputWidget->addText(result.stdErr); |
| 891 | QMessageBox::critical(this, "Internal error", "Could not determine model parameters"); |
| 892 | return false; |
| 893 | } |
| 894 | } |
| 895 | for (const auto& d : additionalDataFiles) { |
| 896 | if (!data.contains(d)) { |
| 897 | // Ensure we don't add the same data file twice |
| 898 | data << d; |
| 899 | } |
| 900 | } |
| 901 | return true; |