Search in sources :

Example 1 with ModelLanguage

use of com.sri.ai.praise.lang.ModelLanguage in project aic-praise by aic-sri-international.

the class PagedModelContainer method getModelPagesFromURI.

public static List<ModelPage> getModelPagesFromURI(URI uri) throws IOException {
    List<ModelPage> result = new ArrayList<>();
    AtomicReference<ModelLanguage> containerModelLanguage = new AtomicReference<>();
    List<String> modelSpecifications = new ArrayList<>();
    Map<String, List<String>> fragments = new HashMap<>();
    try (BufferedReader in = new BufferedReader(new InputStreamReader(uri.toURL().openStream(), FILE_CHARSET))) {
        getContent(containerModelLanguage, in.lines(), modelSpecifications, fragments);
    }
    for (String modelSpecification : modelSpecifications) {
        String name = extractField(MODEL_FIELD_NAME, modelSpecification);
        String model = extractModel(MODEL_FIELD_PARTS, modelSpecification, fragments);
        List<String> queries = extractQueries(MODEL_FIELD_QUERIES, modelSpecification, fragments);
        result.add(new ModelPage(containerModelLanguage.get(), name, model, queries));
    }
    return result;
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) ModelLanguage(com.sri.ai.praise.lang.ModelLanguage) BufferedReader(java.io.BufferedReader) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with ModelLanguage

use of com.sri.ai.praise.lang.ModelLanguage 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 : HOGMv1_to_UAI_Translator(com.sri.ai.praise.lang.translate.impl.HOGMv1_to_UAI_Translator) UAI_to_HOGMv1_Using_Inequalities_Translator(com.sri.ai.praise.lang.translate.impl.UAI_to_HOGMv1_Using_Inequalities_Translator) UAI_to_HuginDotNet_Translator(com.sri.ai.praise.lang.translate.impl.UAI_to_HuginDotNet_Translator) ModelLanguage(com.sri.ai.praise.lang.ModelLanguage)

Example 3 with ModelLanguage

use of com.sri.ai.praise.lang.ModelLanguage in project aic-praise by aic-sri-international.

the class PRAiSE method guessLanguageModel.

private static ModelLanguage guessLanguageModel(List<File> inputFiles) {
    ModelLanguage result = Arrays.stream(ModelLanguage.values()).filter(ml -> inputFiles.stream().anyMatch(inputFile -> inputFile.getName().toLowerCase().endsWith(ml.getDefaultFileExtension().toLowerCase()))).findFirst().orElse(null);
    if (result == null) {
        // Check if the input is a container file and if so get the language from it
        result = inputFiles.stream().filter(inputFile -> inputFile.getName().toLowerCase().endsWith(PagedModelContainer.DEFAULT_CONTAINER_FILE_EXTENSION.toLowerCase())).map(containerInputFile -> {
            ModelLanguage containedLanguage = null;
            try {
                List<ModelPage> models = PagedModelContainer.getModelPagesFromURI(containerInputFile.toURI());
                if (models.size() > 0) {
                    containedLanguage = models.get(0).getLanguage();
                }
            } catch (IOException ioe) {
                System.err.println(ioe.getMessage());
                ioe.printStackTrace();
            }
            return containedLanguage;
        }).findFirst().orElse(null);
    }
    if (result == null) {
        // For simplicity, defaults to HOGMv1 if nothing specified
        result = ModelLanguage.HOGMv1;
    }
    return result;
}
Also used : Arrays(java.util.Arrays) Supplier(java.util.function.Supplier) PagedModelContainer(com.sri.ai.praise.model.common.io.PagedModelContainer) ArrayList(java.util.ArrayList) Charset(java.nio.charset.Charset) OptionException(joptsimple.OptionException) OptionParser(joptsimple.OptionParser) HOGMQueryResult(com.sri.ai.praise.sgsolver.solver.HOGMQueryResult) ModelLanguage(com.sri.ai.praise.lang.ModelLanguage) OptionSet(joptsimple.OptionSet) PrintStream(java.io.PrintStream) OptionSpec(joptsimple.OptionSpec) Charsets(com.google.common.base.Charsets) Files(java.nio.file.Files) HOGMQueryRunner(com.sri.ai.praise.sgsolver.solver.HOGMQueryRunner) IOException(java.io.IOException) ModelPage(com.sri.ai.praise.model.common.io.ModelPage) Collectors(java.util.stream.Collectors) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) Beta(com.google.common.annotations.Beta) List(java.util.List) StringJoiner(java.util.StringJoiner) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ArrayList(java.util.ArrayList) List(java.util.List) ModelLanguage(com.sri.ai.praise.lang.ModelLanguage) IOException(java.io.IOException)

Aggregations

ModelLanguage (com.sri.ai.praise.lang.ModelLanguage)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Beta (com.google.common.annotations.Beta)1 Charsets (com.google.common.base.Charsets)1 Theory (com.sri.ai.grinder.sgdpllt.api.Theory)1 HOGMv1_to_UAI_Translator (com.sri.ai.praise.lang.translate.impl.HOGMv1_to_UAI_Translator)1 UAI_to_HOGMv1_Using_Inequalities_Translator (com.sri.ai.praise.lang.translate.impl.UAI_to_HOGMv1_Using_Inequalities_Translator)1 UAI_to_HuginDotNet_Translator (com.sri.ai.praise.lang.translate.impl.UAI_to_HuginDotNet_Translator)1 ModelPage (com.sri.ai.praise.model.common.io.ModelPage)1 PagedModelContainer (com.sri.ai.praise.model.common.io.PagedModelContainer)1 HOGMQueryResult (com.sri.ai.praise.sgsolver.solver.HOGMQueryResult)1 HOGMQueryRunner (com.sri.ai.praise.sgsolver.solver.HOGMQueryRunner)1 BufferedReader (java.io.BufferedReader)1 File (java.io.File)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1 PrintStream (java.io.PrintStream)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1