001/**
002 * Copyright (C) 2007 - 2016, Jens Lehmann
003 *
004 * This file is part of DL-Learner.
005 *
006 * DL-Learner is free software; you can redistribute it and/or modify
007 * it under the terms of the GNU General Public License as published by
008 * the Free Software Foundation; either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * DL-Learner is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU General Public License for more details.
015 *
016 * You should have received a copy of the GNU General Public License
017 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
018 */
019/**
020 *
021 */
022package org.dllearner.kb.sparql.simple;
023
024import java.util.Set;
025
026import com.jamonapi.Monitor;
027import com.jamonapi.MonitorFactory;
028
029/**
030 * @author didierc
031 */
032@SuppressWarnings("ALL")
033public class ABoxQueryGenerator {
034
035    public String createQuery(Set<String> individuals, String aboxfilter) {
036        Monitor monABoxQueryGeneration = MonitorFactory.getTimeMonitor("ABox query generator").start();
037        StringBuilder builder = new StringBuilder();
038        builder.append("PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>\n");
039        builder.append("CONSTRUCT {?s ?p ?o  } ");
040        builder.append("{ ?s ?p ?o . " );
041        builder.append(makeInFilter("?s", individuals));
042        if (aboxfilter != null) {
043            builder.append(aboxfilter);
044        }
045        builder.append("FILTER ( (?p!=rdf:type))");
046        builder.append("}");
047        monABoxQueryGeneration.stop();
048        return builder.toString();
049    }
050
051    public static StringBuilder makeInFilter(String var, Set<String> uris) {
052        StringBuilder builder = new StringBuilder();
053        builder.append(" FILTER (").append(var).append(" IN( ");
054        for (String uri : uris) {
055            if (!uri.startsWith("<")) builder.append("<");
056            builder.append(uri);
057            if (!uri.endsWith(">")) builder.append(">");
058            builder.append(", ");
059        }
060        builder.deleteCharAt(builder.length() - 2);
061        builder.append(")) . ");
062        return builder;
063    }
064
065    /**
066     *
067     *
068     * @param oldIndividuals
069     * @param model
070     * @return
071     */
072
073    /*public String createLastQuery(List<String> individuals, OntModel model, List<String> filters) {
074    Monitor monABoxQueryGeneration = MonitorFactory.getTimeMonitor("ABox query generator")
075            .start();
076    StringBuilder builder = new StringBuilder();
077    if (false) {
078        builder.append("CONSTRUCT {?s ?p ?o . ?o a ?class} ");
079        builder.append("{ ?s ?p ?o .");
080        builder.append("?o a ?class");
081    } else {
082        builder.append("CONSTRUCT {?s ?p ?o } ");
083        builder.append("{ ?s ?p ?o . ");
084    }
085
086    Set<String> curIndividuals;
087    if (model.isEmpty()) {
088        curIndividuals = new HashSet<String>(individuals);
089    } else {
090        curIndividuals = this.difference2(individuals, model);
091    }
092    builder.append(" FILTER ( ?s IN( ");
093    for (String individual : curIndividuals) {
094        builder.append("<");
095        builder.append(individual);
096        builder.append(">");
097        builder.append(", ");
098    }
099    builder.deleteCharAt(builder.length() - 2);
100    builder.append(")). ");
101    if (filters != null) {
102        for (String filter : filters) {
103            builder.append(filter);
104            builder.append(". ");
105        }
106    }
107    builder.append("}");
108    monABoxQueryGeneration.stop();
109    return builder.toString();
110}    */
111
112    /*private List<String> getIndividualsFromModel
113           (OntModel
114                    model) {
115       ExtendedIterator<OWLIndividual> iterator = model.listIndividuals();
116       LinkedList<String> result = new LinkedList<String>();
117       while (iterator.hasNext()) {
118           result.add(iterator.next().getURI());
119       }
120       return result;
121   }
122
123   public List<String> difference
124           (List<String> a, List<String> b) {
125       ArrayList<String> result = new ArrayList<String>(b);
126       result.removeAll(a);
127       return result;
128   } */
129}