All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xyz.ottr.lutra.bottr.parser.BInstanceMapParser Maven / Gradle / Ivy

Go to download

This Lutra submodule implements bOTTR (https://spec.ottr.xyz/bOTTR/0.1/). bOTTR is a mapping language for transforming data from relational databases, triplestores, CSV-files and other sources, into instances of templates. Lutra is the reference implementation of the OTTR framework. For more information about OTTR, see http://ottr.xyz.

There is a newer version: 0.6.19
Show newest version
package xyz.ottr.lutra.bottr.parser;

/*-
 * #%L
 * lutra-wottr
 * %%
 * Copyright (C) 2018 - 2019 University of Oslo
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 2.1 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.util.List;
import java.util.function.Function;
import org.apache.jena.rdf.model.Literal;
import org.apache.jena.rdf.model.Model;
import org.apache.jena.rdf.model.Resource;
import xyz.ottr.lutra.bottr.BOTTR;
import xyz.ottr.lutra.bottr.model.ArgumentMaps;
import xyz.ottr.lutra.bottr.model.InstanceMap;
import xyz.ottr.lutra.bottr.model.Source;
import xyz.ottr.lutra.system.Result;
import xyz.ottr.lutra.system.ResultStream;
import xyz.ottr.lutra.wottr.parser.ModelSelector;

public class BInstanceMapParser implements Function> {

    private final String filePath; // @Nullable. file location of the InstanceMap, used for making correct absolute paths to sources

    public BInstanceMapParser(String filePath) {
        this.filePath = filePath;
    }

    @Override
    public ResultStream apply(Model model) {
        List instanceMaps = ModelSelector.getInstancesOfClass(model, BOTTR.InstanceMap);
        return ResultStream.innerOf(instanceMaps)
            .mapFlatMap(i -> parseInstanceMap(model, i));
    }

    private Result parseInstanceMap(Model model, Resource instanceMap) {

        Result builder = Result.of(InstanceMap.builder());
        builder.addResult(getTemplate(model, instanceMap), InstanceMap.Builder::templateIRI);
        builder.addResult(getQuery(model, instanceMap), InstanceMap.Builder::query);

        Result> source = getSource(model, instanceMap);
        builder.addResult(source, InstanceMap.Builder::source);

        Result tupleMap = source.flatMap(s -> getArgumentMaps(model, instanceMap, s));
        builder.addResult(tupleMap, InstanceMap.Builder::argumentMaps);
        return builder.map(InstanceMap.Builder::build);
    }

    private Result getTemplate(Model model, Resource instanceMap) {
        return ModelSelector.getRequiredURIResourceObject(model, instanceMap, BOTTR.template)
            .map(Resource::getURI);
    }

    private Result getQuery(Model model, Resource instanceMap) {
        return ModelSelector.getRequiredLiteralObject(model, instanceMap, BOTTR.query)
            .map(Literal::getLexicalForm);
    }

    private Result> getSource(Model model, Resource instanceMap) {
        return ModelSelector.getRequiredResourceObject(model, instanceMap, BOTTR.source)
            .flatMap(new BSourceParser(model, this.filePath));
    }

    private Result getArgumentMaps(Model model, Resource instanceMap, Source source) {
        return ModelSelector.getOptionalListObject(model, instanceMap, BOTTR.argumentMaps)
            .flatMapOrElse(
                new BArgumentMapsParser(model, source),
                Result.of(new ArgumentMaps(model, source))
            );
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy