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;
}
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;
}
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;
}
Aggregations