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 */
019package org.dllearner.algorithms.qtl.util;
020
021import org.apache.jena.sparql.core.Var;
022
023/**
024 * A SPARQL variable generator, e.g. <code>?x1, ?x2, ?x3, ...</code>
025 *
026 * @author Lorenz Buehmann
027 *
028 */
029public class VarGenerator {
030        
031        private final String base;
032        private int cnt = 0;
033
034        /**
035         * @param base the base variable name, which will be used as prefix, e.g. <code>x</code> will result in variables
036         *               <code>?x1, ?x2, ?x3, ...</code>
037         */
038        public VarGenerator(String base) {
039                this.base = base;
040        }
041
042        /**
043         * A variable generator with the default prefix <code>s</code>, i.e. <code>?s1, ?s2, ?s3, ...</code>
044         */
045        public VarGenerator() {
046                this("s");
047        }
048
049        /**
050         * Generate a new variable of form <code>${base}${cnt}</code> and increment the counter.
051         * @return a new variable
052         */
053        public Var newVar(){
054                return Var.alloc(base + cnt++);
055        }
056
057        /**
058         * Reset the counter.
059         */
060        public void reset(){
061                cnt = 0;
062        }
063}