Search in sources :

Example 1 with TranslatorOptions

use of com.sri.ai.praise.core.representation.translation.ciaranframework.api.TranslatorOptions in project aic-praise by aic-sri-international.

the class Translate method main.

/**
 * Translate Probabilistic Models based on given command line arguments.
 *
 * @param args
 *        pass '--help' to see description of expected program arguments.
 */
public static void main(String[] args) {
    TranslationArgs translationArgs = getArgs(args);
    for (Translator translator : translationArgs.translators) {
        File sourceDirectory = new File(translationArgs.rootModelOutputDirectory, translator.getSource().getCode());
        if (!sourceDirectory.isDirectory()) {
            throw new IllegalArgumentException("Source Directory " + sourceDirectory + " does not exist");
        }
        File targetDirectory = new File(translationArgs.rootModelOutputDirectory, translator.getTarget().getCode());
        if (!targetDirectory.isDirectory()) {
            targetDirectory.mkdir();
        }
        String sourceModelFileExtension = translator.getInputFileExtensions()[0];
        for (File sourceModelFile : sourceDirectory.listFiles((dir, name) -> name.endsWith(sourceModelFileExtension))) {
            System.out.println("Translating " + sourceModelFile.getName() + " from " + translator.getSource().getCode() + " to " + translator.getTarget().getCode());
            String sourceModelFileNameWithNoExtension = translator.getInputModelFileNameWithNoExtension(sourceModelFile);
            Stopwatch sw = Stopwatch.createStarted();
            try (InputModelReaders inputModelReaders = new InputModelReaders(translator, sourceModelFile, sourceModelFileExtension);
                OutputModelWriters translatedOutputs = new OutputModelWriters(translator, sourceModelFile, sourceModelFileExtension)) {
                translator.translate(sourceModelFileNameWithNoExtension, inputModelReaders.readers, translatedOutputs.writers, new TranslatorOptions());
            } catch (Exception ex) {
                System.err.println("Error during translation");
                ex.printStackTrace();
                System.exit(-1);
            }
            sw.stop();
            System.out.println("Took " + sw.toString());
        // TODO - append the time it took to translate the file to a .csv file
        }
    }
}
Also used : Translator(com.sri.ai.praise.core.representation.translation.ciaranframework.api.Translator) Stopwatch(com.google.common.base.Stopwatch) TranslatorOptions(com.sri.ai.praise.core.representation.translation.ciaranframework.api.TranslatorOptions) File(java.io.File)

Example 2 with TranslatorOptions

use of com.sri.ai.praise.core.representation.translation.ciaranframework.api.TranslatorOptions in project aic-praise by aic-sri-international.

the class PRAiSEController method importUAIModel.

private void importUAIModel(ActionEvent ae) {
    File uaiModelFile = uaiFileChooser.showOpenDialog(mainStage);
    if (uaiModelFile != null) {
        File uaiEvidenceFile = new File(uaiModelFile.getParent(), uaiModelFile.getName() + ".evid");
        StringWriter hogmWriter = new StringWriter();
        try (BufferedReader uaiModelReader = new BufferedReader(new FileReader(uaiModelFile));
            BufferedReader uaiEvidenceReader = new BufferedReader(new FileReader(uaiEvidenceFile));
            PrintWriter hogmPrintWriter = new PrintWriter(hogmWriter)) {
            UAI_to_HOGMv1_Using_Equalities_Translator translator = new UAI_to_HOGMv1_Using_Equalities_Translator();
            translator.translate(uaiModelFile.getName(), new Reader[] { uaiModelReader, uaiEvidenceReader }, new PrintWriter[] { hogmPrintWriter }, new TranslatorOptions());
            String hogmModel = hogmWriter.toString();
            // For convenience, pull out all possible queries
            HOGMParserWrapper parser = new HOGMParserWrapper();
            HOGModel parsedModel = parser.parseModel(hogmModel);
            ExpressionBasedModel factorsAndTypes = new HOGMExpressionBasedModel(parsedModel);
            List<String> queries = new ArrayList<>(factorsAndTypes.getMapFromRandomVariableNameToTypeName().keySet());
            newModel(hogmModel, queries);
        } catch (Throwable th) {
            FXUtil.exception(th);
        }
    }
}
Also used : HOGMExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel) ExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel) ArrayList(java.util.ArrayList) TranslatorOptions(com.sri.ai.praise.core.representation.translation.ciaranframework.api.TranslatorOptions) HOGMParserWrapper(com.sri.ai.praise.core.representation.classbased.hogm.parsing.HOGMParserWrapper) HOGModel(com.sri.ai.praise.core.representation.classbased.hogm.HOGModel) StringWriter(java.io.StringWriter) BufferedReader(java.io.BufferedReader) HOGMExpressionBasedModel(com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel) FileReader(java.io.FileReader) File(java.io.File) PrintWriter(java.io.PrintWriter) UAI_to_HOGMv1_Using_Equalities_Translator(com.sri.ai.praise.core.representation.translation.ciaranframework.core.uai.UAI_to_HOGMv1_Using_Equalities_Translator)

Example 3 with TranslatorOptions

use of com.sri.ai.praise.core.representation.translation.ciaranframework.api.TranslatorOptions in project aic-praise by aic-sri-international.

the class PRAiSEController method exportUAIModel.

private void exportUAIModel(ActionEvent ae) {
    try {
        callCurrentModelPageEditor(modelPage -> {
            File uaiModelFile = uaiFileChooser.showSaveDialog(mainStage);
            if (uaiModelFile != null) {
                if (!uaiModelFile.getName().endsWith(ModelLanguage.UAI.getDefaultFileExtension())) {
                    uaiModelFile = new File(uaiModelFile.getParent(), uaiModelFile.getName() + ModelLanguage.UAI.getDefaultFileExtension());
                }
                File uaiEvidenceFile = new File(uaiModelFile.getParent(), uaiModelFile.getName() + ModelLanguage.UAI.getDefaultFileExtension() + ".evid");
                try (PrintWriter uaiModelWriter = new PrintWriter(uaiModelFile);
                    PrintWriter uaiEvidenceWriter = new PrintWriter(uaiEvidenceFile)) {
                    HOGMv1_to_UAI_Translator translator = new HOGMv1_to_UAI_Translator();
                    translator.translate(uaiModelFile.getName(), new Reader[] { new StringReader(modelPage.getCurrentPageContents()) }, new PrintWriter[] { uaiModelWriter, uaiEvidenceWriter }, new TranslatorOptions());
                } catch (Throwable th) {
                    FXUtil.exception(th);
                }
            }
        });
    } catch (Throwable th) {
        FXUtil.exception(th);
    }
}
Also used : HOGMv1_to_UAI_Translator(com.sri.ai.praise.core.representation.translation.ciaranframework.core.uai.HOGMv1_to_UAI_Translator) StringReader(java.io.StringReader) TranslatorOptions(com.sri.ai.praise.core.representation.translation.ciaranframework.api.TranslatorOptions) File(java.io.File) PrintWriter(java.io.PrintWriter)

Aggregations

TranslatorOptions (com.sri.ai.praise.core.representation.translation.ciaranframework.api.TranslatorOptions)3 File (java.io.File)3 PrintWriter (java.io.PrintWriter)2 Stopwatch (com.google.common.base.Stopwatch)1 ExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.expressionbased.api.ExpressionBasedModel)1 HOGModel (com.sri.ai.praise.core.representation.classbased.hogm.HOGModel)1 HOGMExpressionBasedModel (com.sri.ai.praise.core.representation.classbased.hogm.components.HOGMExpressionBasedModel)1 HOGMParserWrapper (com.sri.ai.praise.core.representation.classbased.hogm.parsing.HOGMParserWrapper)1 Translator (com.sri.ai.praise.core.representation.translation.ciaranframework.api.Translator)1 HOGMv1_to_UAI_Translator (com.sri.ai.praise.core.representation.translation.ciaranframework.core.uai.HOGMv1_to_UAI_Translator)1 UAI_to_HOGMv1_Using_Equalities_Translator (com.sri.ai.praise.core.representation.translation.ciaranframework.core.uai.UAI_to_HOGMv1_Using_Equalities_Translator)1 BufferedReader (java.io.BufferedReader)1 FileReader (java.io.FileReader)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 ArrayList (java.util.ArrayList)1