Search in sources :

Example 1 with Translator

use of com.sri.ai.praise.core.representation.translation.ciaranframework.api.Translator 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 Translator

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

the class TranslatorFactory method newTranslator.

/**
 * Instantiate a new Translator based on the given source->target mapping.
 * @param source
 *        the source modeling language to translate from.
 * @param target
 *        the target modeling language to translate to.
 * @return a new Translator instance capable of translating source->target
 * @throws RuntimeException if unable to instantiate a translator for the given source and target.
 */
public static Translator newTranslator(ModelLanguage source, ModelLanguage target) {
    Translator result = null;
    Class<?> translatorClass = _translators.get(new Pair<ModelLanguage, ModelLanguage>(source, target));
    try {
        result = (Translator) translatorClass.newInstance();
    } catch (Exception ex) {
        throw new RuntimeException("Unable to create a new translator for " + source + "->" + target, ex);
    }
    return result;
}
Also used : UAI_to_HuginDotNet_Translator(com.sri.ai.praise.core.representation.translation.ciaranframework.core.hugin.UAI_to_HuginDotNet_Translator) HOGMv1_to_UAI_Translator(com.sri.ai.praise.core.representation.translation.ciaranframework.core.uai.HOGMv1_to_UAI_Translator) UAI_to_HOGMv1_Using_Inequalities_Translator(com.sri.ai.praise.core.representation.translation.ciaranframework.core.uai.UAI_to_HOGMv1_Using_Inequalities_Translator) Translator(com.sri.ai.praise.core.representation.translation.ciaranframework.api.Translator) ModelLanguage(com.sri.ai.praise.core.representation.interfacebased.factor.api.ModelLanguage)

Example 3 with Translator

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

the class VECSolver method solve.

@Override
public ExternalProcessSolverResult solve(String solveRequestId, ModelLanguage modelLanguage, String model, String evidenceQuery) throws Exception {
    if (modelLanguage != ModelLanguage.HOGMv1) {
        throw new UnsupportedOperationException(modelLanguage.name() + " is currently not supported by this solver.");
    }
    Translator inputToUAITranslator = TranslatorFactory.newTranslator(modelLanguage, ModelLanguage.UAI);
    // NOTE: This trick is dependent on the input model being HOGMv1
    String hogmv1Model = model + "\nrandom UAIQuery : Boolean;\nif " + evidenceQuery + " then UAIQuery else not UAIQuery;\n";
    VECCallResult partitionResult = callVECPR("Partition Function " + solveRequestId, inputToUAITranslator, hogmv1Model, new Reader[] { new StringReader(hogmv1Model) });
    VECCallResult evidenceResult = callVECPR("Evidence " + solveRequestId, inputToUAITranslator, hogmv1Model + "\nUAIQuery;", new Reader[] { new StringReader(hogmv1Model), new StringReader("UAIQuery") });
    Double probabilityResult = Math.pow(10, evidenceResult.resultLog10) / Math.pow(10, partitionResult.resultLog10);
    ExternalProcessSolverResult result = new ExternalProcessSolverResult(Math.max(partitionResult.translationTookMS, evidenceResult.translationTookMS), Math.max(partitionResult.vecProcessTookMS, evidenceResult.vecProcessTookMS), probabilityResult.isNaN() ? null : Expressions.makeSymbol(probabilityResult));
    return result;
}
Also used : Translator(com.sri.ai.praise.core.representation.translation.ciaranframework.api.Translator) StringReader(java.io.StringReader) ExternalProcessSolverResult(com.sri.ai.praise.core.inference.externalprocesssolver.core.ExternalProcessSolverResult)

Aggregations

Translator (com.sri.ai.praise.core.representation.translation.ciaranframework.api.Translator)3 Stopwatch (com.google.common.base.Stopwatch)1 ExternalProcessSolverResult (com.sri.ai.praise.core.inference.externalprocesssolver.core.ExternalProcessSolverResult)1 ModelLanguage (com.sri.ai.praise.core.representation.interfacebased.factor.api.ModelLanguage)1 TranslatorOptions (com.sri.ai.praise.core.representation.translation.ciaranframework.api.TranslatorOptions)1 UAI_to_HuginDotNet_Translator (com.sri.ai.praise.core.representation.translation.ciaranframework.core.hugin.UAI_to_HuginDotNet_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_Inequalities_Translator (com.sri.ai.praise.core.representation.translation.ciaranframework.core.uai.UAI_to_HOGMv1_Using_Inequalities_Translator)1 File (java.io.File)1 StringReader (java.io.StringReader)1