From 4b7253559c290b6fdd1c4122830f153fda85dd62 Mon Sep 17 00:00:00 2001 From: RncLsn Date: Fri, 29 May 2015 18:35:51 +0100 Subject: Disposable. --- src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java | 2 +- src/uk/ac/ox/cs/pagoda/query/AnswerTuples.java | 37 +- src/uk/ac/ox/cs/pagoda/query/AnswerTuplesImp.java | 5 +- src/uk/ac/ox/cs/pagoda/query/QueryRecord.java | 1327 +++++++++++---------- 4 files changed, 738 insertions(+), 633 deletions(-) (limited to 'src/uk/ac/ox/cs/pagoda/query') diff --git a/src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java b/src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java index cb2b0a3..78aced1 100644 --- a/src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java +++ b/src/uk/ac/ox/cs/pagoda/query/AnswerTuple.java @@ -172,7 +172,7 @@ public class AnswerTuple { String lexicalForm = matcher.group("lexicalForm"); String dataTypeIRI = matcher.group("dataType"); Datatype dataType; - if (dataTypeIRI.isEmpty()) dataType = Datatype.RDF_PLAIN_LITERAL; + if(dataTypeIRI == null || dataTypeIRI.isEmpty()) dataType = Datatype.RDF_PLAIN_LITERAL; else dataType = uk.ac.ox.cs.JRDFox.model.Datatype.value(dataTypeIRI); terms[i] = uk.ac.ox.cs.JRDFox.model.Literal.create(lexicalForm, dataType); } diff --git a/src/uk/ac/ox/cs/pagoda/query/AnswerTuples.java b/src/uk/ac/ox/cs/pagoda/query/AnswerTuples.java index e1e5302..81efed0 100644 --- a/src/uk/ac/ox/cs/pagoda/query/AnswerTuples.java +++ b/src/uk/ac/ox/cs/pagoda/query/AnswerTuples.java @@ -1,23 +1,22 @@ package uk.ac.ox.cs.pagoda.query; -public interface AnswerTuples { - - public void reset(); - - public boolean isValid(); - - public int getArity(); - - public String[] getAnswerVariables(); - - public void moveNext(); - - public void dispose(); - - public AnswerTuple getTuple(); - - public boolean contains(AnswerTuple t); - - public void remove(); +import uk.ac.ox.cs.pagoda.util.disposable.Disposable; +public abstract class AnswerTuples extends Disposable { + + public abstract void reset(); + + public abstract boolean isValid(); + + public abstract int getArity(); + + public abstract String[] getAnswerVariables(); + + public abstract void moveNext(); + + public abstract AnswerTuple getTuple(); + + public abstract boolean contains(AnswerTuple t); + + public abstract void remove(); } diff --git a/src/uk/ac/ox/cs/pagoda/query/AnswerTuplesImp.java b/src/uk/ac/ox/cs/pagoda/query/AnswerTuplesImp.java index 3e0f320..fd3fb1f 100644 --- a/src/uk/ac/ox/cs/pagoda/query/AnswerTuplesImp.java +++ b/src/uk/ac/ox/cs/pagoda/query/AnswerTuplesImp.java @@ -3,7 +3,7 @@ package uk.ac.ox.cs.pagoda.query; import java.util.Iterator; import java.util.Set; -public class AnswerTuplesImp implements AnswerTuples { +public class AnswerTuplesImp extends AnswerTuples { int m_index; Iterator m_iter; @@ -53,9 +53,6 @@ public class AnswerTuplesImp implements AnswerTuples { m_tuple = null; } - @Override - public void dispose() {} - @Override public void reset() { if (m_answers1 == null || m_answers1.isEmpty()) { diff --git a/src/uk/ac/ox/cs/pagoda/query/QueryRecord.java b/src/uk/ac/ox/cs/pagoda/query/QueryRecord.java index 1f81a78..742b7da 100644 --- a/src/uk/ac/ox/cs/pagoda/query/QueryRecord.java +++ b/src/uk/ac/ox/cs/pagoda/query/QueryRecord.java @@ -11,6 +11,8 @@ import uk.ac.ox.cs.pagoda.util.ConjunctiveQueryHelper; import uk.ac.ox.cs.pagoda.util.Namespace; import uk.ac.ox.cs.pagoda.util.PagodaProperties; import uk.ac.ox.cs.pagoda.util.Utility; +import uk.ac.ox.cs.pagoda.util.disposable.Disposable; +import uk.ac.ox.cs.pagoda.util.disposable.DisposedException; import uk.ac.ox.cs.pagoda.util.tuples.Tuple; import uk.ac.ox.cs.pagoda.util.tuples.TupleBuilder; @@ -18,162 +20,180 @@ import java.io.*; import java.lang.reflect.Type; import java.util.*; -public class QueryRecord { - - public static final String botQueryText = "SELECT ?X WHERE { ?X }"; - public static final String SEPARATOR = "----------------------------------------"; - private static final String RDF_TYPE = "a"; //"rdf:type"; //RDF.type.toString(); - boolean processed = false; - String stringQueryID = null; - OWLOntology relevantOntology = null; - Set relevantClauses = new HashSet(); - double[] timer; - int subID; - DLClause queryClause = null; - int queryID = -1; - Set soundAnswerTuples = new HashSet(); - private Step difficulty; - private String queryText; - private String[][] answerVariables = null; - private Set gapAnswerTuples = null; - private QueryManager m_manager; - - private QueryRecord() { - } +public class QueryRecord extends Disposable { + + public static final String botQueryText = + "SELECT ?X WHERE { ?X }"; + public static final String SEPARATOR = "----------------------------------------"; + private static final String RDF_TYPE = "a"; //"rdf:type"; //RDF.type.toString(); + boolean processed = false; + String stringQueryID = null; + OWLOntology relevantOntology = null; + Set relevantClauses = new HashSet(); + double[] timer; + int subID; + DLClause queryClause = null; + int queryID = -1; + Set soundAnswerTuples = new HashSet(); + private Step difficulty; + private String queryText; + private String[][] answerVariables = null; + private Set gapAnswerTuples = null; + private QueryManager m_manager; + + private QueryRecord() { + } // private boolean containsAuxPredicate(String str) { // return str.contains(Namespace.PAGODA_AUX) || str.contains("_AUX") || str.contains("owl#Nothing") || // str.contains("internal:def"); // } - public QueryRecord(QueryManager manager, String text, int id, int subID) { - m_manager = manager; - resetInfo(text, id, subID); - resetTimer(); - } - - public static Collection collectQueryTexts(Collection queryRecords) { - Collection texts = new LinkedList(); - for(QueryRecord record : queryRecords) - texts.add(record.queryText); - return texts; - } - - public void resetInfo(String text, int id, int subid) { - queryID = id; - subID = subid; - stringQueryID = id + (subID == 0 ? "" : "_" + subID); - m_manager.remove(queryText); - m_manager.put(text, this); - queryClause = null; - answerVariables = ConjunctiveQueryHelper.getAnswerVariables(text); - queryText = text; // .replace("_:", "?"); - } - - public void resetTimer() { - int length = Step.values().length; - timer = new double[length]; - for(int i = 0; i < length; ++i) timer[i] = 0; - } - - public AnswerTuples getAnswers() { - if(isProcessed()) - return getLowerBoundAnswers(); - - return getUpperBoundAnswers(); - } - - public AnswerTuples getLowerBoundAnswers() { - return new AnswerTuplesImp(answerVariables[0], soundAnswerTuples); - } - - public AnswerTuples getUpperBoundAnswers() { - return new AnswerTuplesImp(answerVariables[0], soundAnswerTuples, gapAnswerTuples); - } - - public boolean updateLowerBoundAnswers(AnswerTuples answerTuples) { - if(answerTuples == null) return false; - boolean update = false; - for(AnswerTuple tuple; answerTuples.isValid(); answerTuples.moveNext()) { - tuple = answerTuples.getTuple(); - if(!soundAnswerTuples.contains(tuple) && (gapAnswerTuples == null || gapAnswerTuples.contains(tuple))) { - soundAnswerTuples.add(tuple); - if(gapAnswerTuples != null) - gapAnswerTuples.remove(tuple); - update = true; - } - } - - if(soundAnswerTuples.isEmpty()) - Utility.logInfo("Lower bound answers empty"); - else if(update) - Utility.logInfo("Lower bound answers updated: " + soundAnswerTuples.size()); - else - Utility.logInfo("Lower bound answers unchanged"); - - return update; - } - - public boolean updateUpperBoundAnswers(AnswerTuples answerTuples) { - return updateUpperBoundAnswers(answerTuples, false); - } - - public boolean updateUpperBoundAnswers(AnswerTuples answerTuples, boolean toCheckAux) { - if(!(answerTuples instanceof RDFoxAnswerTuples)) { - String msg = "The upper bound must be computed by RDFox!"; - Utility.logError(msg); - throw new IllegalArgumentException(msg); - } - - RDFoxAnswerTuples rdfoxAnswerTuples = (RDFoxAnswerTuples) answerTuples; - - Set candidateGapAnswerTuples = new HashSet(); - AnswerTuple tuple; - for(; rdfoxAnswerTuples.isValid(); rdfoxAnswerTuples.moveNext()) { - tuple = rdfoxAnswerTuples.getTuple(); - if(isBottom() || !tuple.hasAnonymousIndividual()) - if((!toCheckAux || !tuple.hasAuxPredicate()) && !soundAnswerTuples.contains(tuple)) - candidateGapAnswerTuples.add(tuple); - } - - /*** START: debugging ***/ - if(PagodaProperties.isDebuggingMode()) { - if(rdfoxAnswerTuples.getArity() != getAnswerVariables().length) - throw new IllegalArgumentException( - "The arity of answers (" + rdfoxAnswerTuples.getArity() + ") " + - "is different from the number of answer variables (" + - getAnswerVariables().length + ")"); - - Set namedAnswerTuples = new HashSet<>(); - rdfoxAnswerTuples.reset(); - for(; rdfoxAnswerTuples.isValid(); rdfoxAnswerTuples.moveNext()) { - tuple = rdfoxAnswerTuples.getTuple(); + public QueryRecord(QueryManager manager, String text, int id, int subID) { + m_manager = manager; + resetInfo(text, id, subID); + resetTimer(); + } + + public static Collection collectQueryTexts(Collection queryRecords) { + Collection texts = new LinkedList(); + for(QueryRecord record : queryRecords) + texts.add(record.queryText); + return texts; + } + + public void resetInfo(String text, int id, int subid) { + if(isDisposed()) throw new DisposedException(); + + queryID = id; + subID = subid; + stringQueryID = id + (subID == 0 ? "" : "_" + subID); + m_manager.remove(queryText); + m_manager.put(text, this); + queryClause = null; + answerVariables = ConjunctiveQueryHelper.getAnswerVariables(text); + queryText = text; // .replace("_:", "?"); + } + + public void resetTimer() { + if(isDisposed()) throw new DisposedException(); + + int length = Step.values().length; + timer = new double[length]; + for(int i = 0; i < length; ++i) timer[i] = 0; + } + + public AnswerTuples getAnswers() { + if(isDisposed()) throw new DisposedException(); + + if(isProcessed()) + return getLowerBoundAnswers(); + + return getUpperBoundAnswers(); + } + + public AnswerTuples getLowerBoundAnswers() { + if(isDisposed()) throw new DisposedException(); + + return new AnswerTuplesImp(answerVariables[0], soundAnswerTuples); + } + + public AnswerTuples getUpperBoundAnswers() { + if(isDisposed()) throw new DisposedException(); + + return new AnswerTuplesImp(answerVariables[0], soundAnswerTuples, gapAnswerTuples); + } + + public boolean updateLowerBoundAnswers(AnswerTuples answerTuples) { + if(isDisposed()) throw new DisposedException(); + + if(answerTuples == null) return false; + boolean update = false; + for(AnswerTuple tuple; answerTuples.isValid(); answerTuples.moveNext()) { + tuple = answerTuples.getTuple(); + if(!soundAnswerTuples.contains(tuple) && (gapAnswerTuples == null || gapAnswerTuples.contains(tuple))) { + soundAnswerTuples.add(tuple); + if(gapAnswerTuples != null) + gapAnswerTuples.remove(tuple); + update = true; + } + } + + if(soundAnswerTuples.isEmpty()) + Utility.logInfo("Lower bound answers empty"); + else if(update) + Utility.logInfo("Lower bound answers updated: " + soundAnswerTuples.size()); + else + Utility.logInfo("Lower bound answers unchanged"); + + return update; + } + + public boolean updateUpperBoundAnswers(AnswerTuples answerTuples) { + if(isDisposed()) throw new DisposedException(); + + return updateUpperBoundAnswers(answerTuples, false); + } + + public boolean updateUpperBoundAnswers(AnswerTuples answerTuples, boolean toCheckAux) { + if(isDisposed()) throw new DisposedException(); + + if(!(answerTuples instanceof RDFoxAnswerTuples)) { + String msg = "The upper bound must be computed by RDFox!"; + Utility.logError(msg); + throw new IllegalArgumentException(msg); + } + + RDFoxAnswerTuples rdfoxAnswerTuples = (RDFoxAnswerTuples) answerTuples; + + Set candidateGapAnswerTuples = new HashSet(); + AnswerTuple tuple; + for(; rdfoxAnswerTuples.isValid(); rdfoxAnswerTuples.moveNext()) { + tuple = rdfoxAnswerTuples.getTuple(); + if(isBottom() || !tuple.hasAnonymousIndividual()) + if((!toCheckAux || !tuple.hasAuxPredicate()) && !soundAnswerTuples.contains(tuple)) + candidateGapAnswerTuples.add(tuple); + } + + /*** START: debugging ***/ + if(PagodaProperties.isDebuggingMode()) { + if(rdfoxAnswerTuples.getArity() != getAnswerVariables().length) + throw new IllegalArgumentException( + "The arity of answers (" + rdfoxAnswerTuples.getArity() + ") " + + "is different from the number of answer variables (" + + getAnswerVariables().length + ")"); + + Set namedAnswerTuples = new HashSet<>(); + rdfoxAnswerTuples.reset(); + for(; rdfoxAnswerTuples.isValid(); rdfoxAnswerTuples.moveNext()) { + tuple = rdfoxAnswerTuples.getTuple(); // if(isBottom() || !tuple.hasAnonymousIndividual()) { - namedAnswerTuples.add(tuple); + namedAnswerTuples.add(tuple); // } - } - HashSet difference = new HashSet<>(soundAnswerTuples); - difference.removeAll(namedAnswerTuples); - if(!difference.isEmpty()) - throw new IllegalArgumentException("The upper bound does not contain the lower bound! Missing answers: " + difference - .size()); - } - /*** END: debugging ***/ - - boolean update; - if(gapAnswerTuples == null) { - gapAnswerTuples = candidateGapAnswerTuples; - update = true; - } else { - update = gapAnswerTuples.retainAll(candidateGapAnswerTuples); - } - - if(update) - Utility.logInfo("Upper bound answers updated: " + getNumberOfAnswers()); - else - Utility.logInfo("Upper bound answers unchanged"); - - return update; + } + HashSet difference = new HashSet<>(soundAnswerTuples); + difference.removeAll(namedAnswerTuples); + if(!difference.isEmpty()) + throw new IllegalArgumentException("The upper bound does not contain the lower bound! Missing answers: " + difference + .size()); + } + /*** END: debugging ***/ + + boolean update; + if(gapAnswerTuples == null) { + gapAnswerTuples = candidateGapAnswerTuples; + update = true; + } + else { + update = gapAnswerTuples.retainAll(candidateGapAnswerTuples); + } + + if(update) + Utility.logInfo("Upper bound answers updated: " + getNumberOfAnswers()); + else + Utility.logInfo("Upper bound answers unchanged"); + + return update; // boolean update = false; // for(Iterator iter = gapAnswerTuples.iterator(); iter.hasNext(); ) { @@ -183,96 +203,116 @@ public class QueryRecord { // update = true; // } // } - } - - public int getNumberOfAnswers() { - return soundAnswerTuples.size() + gapAnswerTuples.size(); - } - - public void markAsProcessed() { - processed = true; - } - - public boolean isProcessed() { - if(gapAnswerTuples != null && gapAnswerTuples.isEmpty()) processed = true; - return processed; - } - - public String[] getDistinguishedVariables() { - return answerVariables[1]; - } - - public String[] getAnswerVariables() { - return answerVariables[0]; - } - - public String[][] getVariables() { - return answerVariables; - } - - public String getQueryText() { - return queryText; - } - - public String getQueryID() { - return stringQueryID; - } - - public AnswerTuples getGapAnswers() { - return new AnswerTuplesImp(answerVariables[0], gapAnswerTuples); - } - - public String toString() { - return queryText; - } - - public void outputAnswers(BufferedWriter writer) throws IOException { - - int answerCounter = soundAnswerTuples.size(); - if(!isProcessed()) answerCounter += gapAnswerTuples.size(); - - Utility.logInfo("The number of answer tuples: " + answerCounter); - - if (writer != null) { - writer.write("-------------- Query " + queryID + " ---------------------"); - writer.newLine(); - writer.write(queryText); - writer.newLine(); - StringBuilder space = new StringBuilder(); - int arity = getArity(), varSpace = 0; - for (int i = 0; i < arity; ++i) - varSpace += answerVariables[0][i].length(); - for (int i = 0; i < (SEPARATOR.length() - varSpace) / (arity + 1); ++i) - space.append(" "); - for (int i = 0; i < getArity(); ++i) { - writer.write(space.toString()); - writer.write(answerVariables[0][i]); - } - writer.newLine(); - writer.write(SEPARATOR); - writer.newLine(); - for (AnswerTuple tuple: soundAnswerTuples) { - writer.write(tuple.toString()); - writer.newLine(); - } - if(!isProcessed()) - for (AnswerTuple tuple: gapAnswerTuples) { - writer.write("*"); - writer.write(tuple.toString()); - writer.newLine(); - } + } + + public int getNumberOfAnswers() { + if(isDisposed()) throw new DisposedException(); + + return soundAnswerTuples.size() + gapAnswerTuples.size(); + } + + public void markAsProcessed() { + processed = true; + } + + public boolean isProcessed() { + if(isDisposed()) throw new DisposedException(); + + if(gapAnswerTuples != null && gapAnswerTuples.isEmpty()) processed = true; + return processed; + } + + public String[] getDistinguishedVariables() { + if(isDisposed()) throw new DisposedException(); + + return answerVariables[1]; + } + + public String[] getAnswerVariables() { + if(isDisposed()) throw new DisposedException(); + + return answerVariables[0]; + } + + public String[][] getVariables() { + if(isDisposed()) throw new DisposedException(); + + return answerVariables; + } + + public String getQueryText() { + if(isDisposed()) throw new DisposedException(); + + return queryText; + } + + public String getQueryID() { + if(isDisposed()) throw new DisposedException(); + + return stringQueryID; + } + + public AnswerTuples getGapAnswers() { + if(isDisposed()) throw new DisposedException(); + + return new AnswerTuplesImp(answerVariables[0], gapAnswerTuples); + } + + public String toString() { + if(isDisposed()) throw new DisposedException(); + + return queryText; + } + + public void outputAnswers(BufferedWriter writer) throws IOException { + if(isDisposed()) throw new DisposedException(); + + int answerCounter = soundAnswerTuples.size(); + if(!isProcessed()) answerCounter += gapAnswerTuples.size(); + + Utility.logInfo("The number of answer tuples: " + answerCounter); + + if(writer != null) { + writer.write("-------------- Query " + queryID + " ---------------------"); + writer.newLine(); + writer.write(queryText); + writer.newLine(); + StringBuilder space = new StringBuilder(); + int arity = getArity(), varSpace = 0; + for(int i = 0; i < arity; ++i) + varSpace += answerVariables[0][i].length(); + for(int i = 0; i < (SEPARATOR.length() - varSpace) / (arity + 1); ++i) + space.append(" "); + for(int i = 0; i < getArity(); ++i) { + writer.write(space.toString()); + writer.write(answerVariables[0][i]); + } + writer.newLine(); + writer.write(SEPARATOR); + writer.newLine(); + for(AnswerTuple tuple : soundAnswerTuples) { + writer.write(tuple.toString()); + writer.newLine(); + } + if(!isProcessed()) + for(AnswerTuple tuple : gapAnswerTuples) { + writer.write("*"); + writer.write(tuple.toString()); + writer.newLine(); + } // writer.write(SEPARATOR); - writer.newLine(); - } + writer.newLine(); + } - } + } - public void outputAnswerStatistics() { + public void outputAnswerStatistics() { + if(isDisposed()) throw new DisposedException(); - int answerCounter = soundAnswerTuples.size(); - if(!isProcessed()) answerCounter += gapAnswerTuples.size(); + int answerCounter = soundAnswerTuples.size(); + if(!isProcessed()) answerCounter += gapAnswerTuples.size(); - Utility.logInfo("The number of answer tuples: " + answerCounter); + Utility.logInfo("The number of answer tuples: " + answerCounter); // if (jsonAnswers != null) { // JSONObject jsonAnswer = new JSONObject(); // @@ -297,407 +337,476 @@ public class QueryRecord { // // jsonAnswers.put(Integer.toString(queryID), jsonAnswer); // } - } - - public void outputTimes() { - for (Step step: Step.values()) { - Utility.logDebug("time for " + step + ": " + timer[step.ordinal()]); - } - } - - public String outputSoundAnswerTuple() { - StringBuilder builder = new StringBuilder(); - for (AnswerTuple tuple: soundAnswerTuples) - builder.append(tuple.toString()).append(Utility.LINE_SEPARATOR); - return builder.toString(); - } - - public String outputGapAnswerTuple() { - StringBuilder builder = new StringBuilder(); - for(AnswerTuple tuple : gapAnswerTuples) - builder.append(tuple.toString()).append(Utility.LINE_SEPARATOR); - return builder.toString(); - } - - public Step getDifficulty() { - return difficulty; - } - - public void setDifficulty(Step step) { - this.difficulty = step; - } - - public OWLOntology getRelevantOntology() { - return relevantOntology; - } - - public void setRelevantOntology(OWLOntology knowledgebase) { - relevantOntology = knowledgebase; - } - - public void saveRelevantOntology(String filename) { - if(relevantOntology == null) return; - OWLOntologyManager manager = relevantOntology.getOWLOntologyManager(); - try { - FileOutputStream outputStream = new FileOutputStream(filename); - manager.saveOntology(relevantOntology, outputStream); - outputStream.close(); - } catch (OWLOntologyStorageException e) { - e.printStackTrace(); - } catch (FileNotFoundException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - } - - public void saveRelevantClause() { - if(relevantClauses == null) return; - GeneralProgram p = new GeneralProgram(relevantClauses, relevantOntology); - p.save(); - } - - public void removeUpperBoundAnswers(Collection answers) { - for (AnswerTuple answer: answers) { + } + + public void outputTimes() { + if(isDisposed()) throw new DisposedException(); + + for(Step step : Step.values()) { + Utility.logDebug("time for " + step + ": " + timer[step.ordinal()]); + } + } + + public String outputSoundAnswerTuple() { + if(isDisposed()) throw new DisposedException(); + + StringBuilder builder = new StringBuilder(); + for(AnswerTuple tuple : soundAnswerTuples) + builder.append(tuple.toString()).append(Utility.LINE_SEPARATOR); + return builder.toString(); + } + + public String outputGapAnswerTuple() { + if(isDisposed()) throw new DisposedException(); + + StringBuilder builder = new StringBuilder(); + for(AnswerTuple tuple : gapAnswerTuples) + builder.append(tuple.toString()).append(Utility.LINE_SEPARATOR); + return builder.toString(); + } + + public Step getDifficulty() { + if(isDisposed()) throw new DisposedException(); + + return difficulty; + } + + public void setDifficulty(Step step) { + if(isDisposed()) throw new DisposedException(); + + this.difficulty = step; + } + + public OWLOntology getRelevantOntology() { + if(isDisposed()) throw new DisposedException(); + + return relevantOntology; + } + + public void setRelevantOntology(OWLOntology knowledgebase) { + if(isDisposed()) throw new DisposedException(); + + relevantOntology = knowledgebase; + } + + public void saveRelevantOntology(String filename) { + if(isDisposed()) throw new DisposedException(); + + if(relevantOntology == null) return; + OWLOntologyManager manager = relevantOntology.getOWLOntologyManager(); + try { + FileOutputStream outputStream = new FileOutputStream(filename); + manager.saveOntology(relevantOntology, outputStream); + outputStream.close(); + } catch(OWLOntologyStorageException e) { + e.printStackTrace(); + } catch(FileNotFoundException e) { + e.printStackTrace(); + } catch(IOException e) { + e.printStackTrace(); + } + } + + public void saveRelevantClause() { + if(isDisposed()) throw new DisposedException(); + + if(relevantClauses == null) return; + GeneralProgram p = new GeneralProgram(relevantClauses, relevantOntology); + p.save(); + } + + public void removeUpperBoundAnswers(Collection answers) { + if(isDisposed()) throw new DisposedException(); + + for(AnswerTuple answer : answers) { // if (soundAnswerTuples.contains(answer)) // Utility.logError("The answer (" + answer + ") cannot be removed, because it is in the lower bound."); - if (!gapAnswerTuples.contains(answer)) - Utility.logError("The answer (" + answer + ") cannot be removed, because it is not in the upper bound."); - gapAnswerTuples.remove(answer); - } - } - - public void addLowerBoundAnswers(Collection answers) { - for (AnswerTuple answer: answers) { - if (!gapAnswerTuples.contains(answer)) - Utility.logError("The answer (" + answer + ") cannot be added, because it is not in the upper bound."); - gapAnswerTuples.remove(answer); - - answer = AnswerTuple.getInstance(answer, answerVariables[0].length); + if(!gapAnswerTuples.contains(answer)) + Utility.logError("The answer (" + answer + ") cannot be removed, because it is not in the upper bound."); + gapAnswerTuples.remove(answer); + } + } + + public void addLowerBoundAnswers(Collection answers) { + if(isDisposed()) throw new DisposedException(); + + for(AnswerTuple answer : answers) { + if(!gapAnswerTuples.contains(answer)) + Utility.logError("The answer (" + answer + ") cannot be added, because it is not in the upper bound."); + gapAnswerTuples.remove(answer); + + answer = AnswerTuple.getInstance(answer, answerVariables[0].length); // if (soundAnswerTuples.contains(answer)) // Utility.logError("The answer (" + answer + ") cannot be added, because it is in the lower bound."); - soundAnswerTuples.add(answer); - } - } - - public int getNoOfSoundAnswers() { - return soundAnswerTuples.size(); - } - - public void addProcessingTime(Step step, double time) { - timer[step.ordinal()] += time; - } - - public int getArity() { - return answerVariables[0].length; - } - - public void addRelevantClauses(DLClause clause) { - relevantClauses.add(clause); - } - - public Set getRelevantClauses() { - return relevantClauses; - } - - public void clearClauses() { - relevantClauses.clear(); - } - - public boolean isHorn() { - for (DLClause clause: relevantClauses) - if (clause.getHeadLength() > 1) - return false; - return true; - } - - public void saveABoxInTurtle(String filename) { - try { - BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename))); - OWLIndividual a, b; - StringBuilder builder = new StringBuilder(); - for (OWLAxiom axiom: relevantOntology.getABoxAxioms(true)) { - if (axiom instanceof OWLClassAssertionAxiom) { - OWLClassAssertionAxiom classAssertion = (OWLClassAssertionAxiom) axiom; - OWLClass c = (OWLClass) classAssertion.getClassExpression(); - a = classAssertion.getIndividual(); - builder.append(a.toString()) - .append(" <") - .append(Namespace.RDF_TYPE) - .append("> ") - .append(c.toString()); - } - else if (axiom instanceof OWLObjectPropertyAssertionAxiom) { - OWLObjectPropertyAssertionAxiom propertyAssertion = (OWLObjectPropertyAssertionAxiom) axiom; - OWLObjectProperty p = (OWLObjectProperty) propertyAssertion.getProperty(); - a = propertyAssertion.getSubject(); - b = propertyAssertion.getObject(); - builder.append(a.toString()).append(" ").append(p.toString()).append(" ").append(b.toString()); - } - else if (axiom instanceof OWLDataPropertyAssertionAxiom) { - OWLDataPropertyAssertionAxiom propertyAssertion = (OWLDataPropertyAssertionAxiom) axiom; - OWLDataProperty p = (OWLDataProperty) propertyAssertion.getProperty(); - a = propertyAssertion.getSubject(); - OWLLiteral l = propertyAssertion.getObject(); - builder.append(a.toString()).append(" ").append(p.toString()).append(" ").append(l.toString()); - } - - writer.write(builder.toString()); - writer.write(" ."); - writer.newLine(); - builder.setLength(0); - } - writer.close(); - } catch (IOException e) { - e.printStackTrace(); - } finally { - - } - } - - public void updateSubID() { - ++subID; - stringQueryID = String.valueOf(queryID) + "_" + subID; - } - - public DLClause getClause() { - if (queryClause != null) - return queryClause; - return queryClause = DLClauseHelper.getQuery(queryText, null); - } - - public boolean isBottom() { - return queryID == 0; - } - - public int getNoOfCompleteAnswers() { - return soundAnswerTuples.size() + gapAnswerTuples.size(); - } - - public int getSubID() { - return subID; - } - - public boolean hasSameGapAnswers(QueryRecord that) { - return gapAnswerTuples.containsAll(that.gapAnswerTuples) && that.gapAnswerTuples.containsAll(gapAnswerTuples); - } - - public void dispose() { - m_manager.remove(queryText); - if(gapAnswerTuples != null) gapAnswerTuples = null; - if(soundAnswerTuples != null) soundAnswerTuples = null; - if (relevantClauses != null) relevantClauses.clear(); - if (relevantOntology != null) - relevantOntology.getOWLOntologyManager().removeOntology(relevantOntology); - answerVariables = null; - } - - public boolean canBeEncodedIntoAtom() { - // FIXME - return true; + soundAnswerTuples.add(answer); + } + } + + public int getNoOfSoundAnswers() { + if(isDisposed()) throw new DisposedException(); + + return soundAnswerTuples.size(); + } + + public void addProcessingTime(Step step, double time) { + if(isDisposed()) throw new DisposedException(); + + timer[step.ordinal()] += time; + } + + public int getArity() { + if(isDisposed()) throw new DisposedException(); + + return answerVariables[0].length; + } + + public void addRelevantClauses(DLClause clause) { + if(isDisposed()) throw new DisposedException(); + + relevantClauses.add(clause); + } + + public Set getRelevantClauses() { + if(isDisposed()) throw new DisposedException(); + + return relevantClauses; + } + + public void clearClauses() { + if(isDisposed()) throw new DisposedException(); + + relevantClauses.clear(); + } + + public boolean isHorn() { + if(isDisposed()) throw new DisposedException(); + + for(DLClause clause : relevantClauses) + if(clause.getHeadLength() > 1) + return false; + return true; + } + + public void saveABoxInTurtle(String filename) { + if(isDisposed()) throw new DisposedException(); + + try { + BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(filename))); + OWLIndividual a, b; + StringBuilder builder = new StringBuilder(); + for(OWLAxiom axiom : relevantOntology.getABoxAxioms(true)) { + if(axiom instanceof OWLClassAssertionAxiom) { + OWLClassAssertionAxiom classAssertion = (OWLClassAssertionAxiom) axiom; + OWLClass c = (OWLClass) classAssertion.getClassExpression(); + a = classAssertion.getIndividual(); + builder.append(a.toString()) + .append(" <") + .append(Namespace.RDF_TYPE) + .append("> ") + .append(c.toString()); + } + else if(axiom instanceof OWLObjectPropertyAssertionAxiom) { + OWLObjectPropertyAssertionAxiom propertyAssertion = (OWLObjectPropertyAssertionAxiom) axiom; + OWLObjectProperty p = (OWLObjectProperty) propertyAssertion.getProperty(); + a = propertyAssertion.getSubject(); + b = propertyAssertion.getObject(); + builder.append(a.toString()).append(" ").append(p.toString()).append(" ").append(b.toString()); + } + else if(axiom instanceof OWLDataPropertyAssertionAxiom) { + OWLDataPropertyAssertionAxiom propertyAssertion = (OWLDataPropertyAssertionAxiom) axiom; + OWLDataProperty p = (OWLDataProperty) propertyAssertion.getProperty(); + a = propertyAssertion.getSubject(); + OWLLiteral l = propertyAssertion.getObject(); + builder.append(a.toString()).append(" ").append(p.toString()).append(" ").append(l.toString()); + } + + writer.write(builder.toString()); + writer.write(" ."); + writer.newLine(); + builder.setLength(0); + } + writer.close(); + } catch(IOException e) { + e.printStackTrace(); + } finally { + + } + } + + public void updateSubID() { + if(isDisposed()) throw new DisposedException(); + + ++subID; + stringQueryID = String.valueOf(queryID) + "_" + subID; + } + + public DLClause getClause() { + if(isDisposed()) throw new DisposedException(); + + if(queryClause != null) + return queryClause; + return queryClause = DLClauseHelper.getQuery(queryText, null); + } + + public boolean isBottom() { + if(isDisposed()) throw new DisposedException(); + + return queryID == 0; + } + + public int getNoOfCompleteAnswers() { + if(isDisposed()) throw new DisposedException(); + + return soundAnswerTuples.size() + gapAnswerTuples.size(); + } + + public int getSubID() { + if(isDisposed()) throw new DisposedException(); + + return subID; + } + + public boolean hasSameGapAnswers(QueryRecord that) { + if(isDisposed()) throw new DisposedException(); + + return gapAnswerTuples.containsAll(that.gapAnswerTuples) && that.gapAnswerTuples.containsAll(gapAnswerTuples); + } + + @Override + public void dispose() { + super.dispose(); + m_manager.remove(queryText); + if(gapAnswerTuples != null) gapAnswerTuples = null; + if(soundAnswerTuples != null) soundAnswerTuples = null; + if(relevantClauses != null) relevantClauses.clear(); + if(relevantOntology != null) + relevantOntology.getOWLOntologyManager().removeOntology(relevantOntology); + answerVariables = null; + } + + public boolean canBeEncodedIntoAtom() { + if(isDisposed()) throw new DisposedException(); + + // FIXME + return true; // return false; - } - - public boolean isPredicate(AnswerTuple a, int i) { - Atom[] atoms = getClause().getBodyAtoms(); - Variable v = Variable.create(answerVariables[1][i]); - String iri; - for(Atom atom : atoms) { - DLPredicate p = atom.getDLPredicate(); - if (p instanceof AtomicConcept) { - if(((AtomicConcept) p).getIRI().equals(v.toString())) return true; - } - else if (p instanceof AtomicRole) { - iri = ((AtomicRole) p).getIRI(); - if (iri.equals(v.toString())) return true; - if(iri.startsWith("?")) - iri = a.getGroundTerm(i).toString(); - if(iri.equals(Namespace.RDF_TYPE) && atom.getArgument(1).equals(v)) return true; - } - } - return false; - } - - // TODO remove fully extended query - public Tuple getExtendedQueryText() { + } + + public boolean isPredicate(AnswerTuple a, int i) { + if(isDisposed()) throw new DisposedException(); + + Atom[] atoms = getClause().getBodyAtoms(); + Variable v = Variable.create(answerVariables[1][i]); + String iri; + for(Atom atom : atoms) { + DLPredicate p = atom.getDLPredicate(); + if(p instanceof AtomicConcept) { + if(((AtomicConcept) p).getIRI().equals(v.toString())) return true; + } + else if(p instanceof AtomicRole) { + iri = ((AtomicRole) p).getIRI(); + if(iri.equals(v.toString())) return true; + if(iri.startsWith("?")) + iri = a.getGroundTerm(i).toString(); + if(iri.equals(Namespace.RDF_TYPE) && atom.getArgument(1).equals(v)) return true; + } + } + return false; + } + + // TODO remove fully extended query + public Tuple getExtendedQueryText() { + if(isDisposed()) throw new DisposedException(); + // String[] ret = new String[2];s - int index = queryText.toUpperCase().indexOf(" WHERE"); - String extendedSelect = queryText.substring(0, index); - String extendedWhere= queryText.substring(index + 1), fullyExtendedWhere = queryText.substring(index + 1); - - String sub, obj; - Map> links = new HashMap>(); - Set list; - for (Atom atom: getClause().getBodyAtoms()) - if (atom.getDLPredicate() instanceof AtomicRole && atom.getArgument(0) instanceof Variable && atom.getArgument(1) instanceof Variable) { - sub = atom.getArgumentVariable(0).getName(); - obj = atom.getArgumentVariable(1).getName(); - if((list = links.get(sub)) == null) - links.put(sub, list = new HashSet()); - list.add(obj); - if((list = links.get(obj)) == null) - links.put(obj, list = new HashSet()); - list.add(sub); - } - - StringBuilder extra = new StringBuilder(), fullyExtra = new StringBuilder(); + int index = queryText.toUpperCase().indexOf(" WHERE"); + String extendedSelect = queryText.substring(0, index); + String extendedWhere = queryText.substring(index + 1), fullyExtendedWhere = queryText.substring(index + 1); + + String sub, obj; + Map> links = new HashMap>(); + Set list; + for(Atom atom : getClause().getBodyAtoms()) + if(atom.getDLPredicate() instanceof AtomicRole && atom.getArgument(0) instanceof Variable && atom.getArgument(1) instanceof Variable) { + sub = atom.getArgumentVariable(0).getName(); + obj = atom.getArgumentVariable(1).getName(); + if((list = links.get(sub)) == null) + links.put(sub, list = new HashSet()); + list.add(obj); + if((list = links.get(obj)) == null) + links.put(obj, list = new HashSet()); + list.add(sub); + } + + StringBuilder extra = new StringBuilder(), fullyExtra = new StringBuilder(); // if (answerVariables[0] != answerVariables[1]) { - for (int i = answerVariables[0].length; i < answerVariables[1].length; ++i) { + for(int i = answerVariables[0].length; i < answerVariables[1].length; ++i) { // for (int i = 0; i < answerVariables[1].length; ++i) { - fullyExtra.append(" . ?") - .append(answerVariables[1][i]) - .append(" " + RDF_TYPE + " <") - .append(Namespace.PAGODA_ORIGINAL) - .append(">"); - if ((list = links.get(answerVariables[1][i])) == null || list.size() < 2) ; - else { - extra.append(" . ?") - .append(answerVariables[1][i]) - .append(" " + RDF_TYPE + " <") - .append(Namespace.PAGODA_ORIGINAL) - .append(">"); - } - } - - if(extra.length() > 0) { - extra.append(" }"); - extendedWhere = extendedWhere.replace(" }", extendedWhere.contains(". }") ? extra.substring(2) : extra.toString()); - } - - if(fullyExtra.length() > 0) { - fullyExtra.append(" }"); - fullyExtendedWhere = fullyExtendedWhere.replace(" }", fullyExtendedWhere.contains(". }") ? fullyExtra.substring(2) : fullyExtra.toString()); - } + fullyExtra.append(" . ?") + .append(answerVariables[1][i]) + .append(" " + RDF_TYPE + " <") + .append(Namespace.PAGODA_ORIGINAL) + .append(">"); + if((list = links.get(answerVariables[1][i])) == null || list.size() < 2) ; + else { + extra.append(" . ?") + .append(answerVariables[1][i]) + .append(" " + RDF_TYPE + " <") + .append(Namespace.PAGODA_ORIGINAL) + .append(">"); + } + } + + if(extra.length() > 0) { + extra.append(" }"); + extendedWhere = + extendedWhere.replace(" }", extendedWhere.contains(". }") ? extra.substring(2) : extra.toString()); + } + + if(fullyExtra.length() > 0) { + fullyExtra.append(" }"); + fullyExtendedWhere = + fullyExtendedWhere.replace(" }", fullyExtendedWhere.contains(". }") ? fullyExtra.substring(2) : fullyExtra + .toString()); + } // } - TupleBuilder result = new TupleBuilder(); - result.append(extendedSelect + " " + fullyExtendedWhere); - - extra.setLength(0); - if (answerVariables[0] != answerVariables[1]) { - for (int i = answerVariables[0].length; i < answerVariables[1].length; ++i) - extra.append(" ?").append(answerVariables[1][i]); - extendedSelect = extendedSelect + extra.toString(); - } - result.append(extendedSelect + " " + extendedWhere); - - return result.build(); - } - - public boolean hasNonAnsDistinguishedVariables() { - return answerVariables[1].length > answerVariables[0].length; - } - - /** - * Two QueryRecords are equal iff - * they have the same queryText, - * soundAnswerTuples. - */ - @Override - public boolean equals(Object o) { - if(!o.getClass().equals(getClass())) return false; - QueryRecord that = (QueryRecord) o; - return this.queryText.equals(that.queryText) - && soundAnswerTuples.equals(that.soundAnswerTuples); - } - - @Override - public int hashCode() { - return Objects.hash(queryText, soundAnswerTuples); - } - - public enum Step { - LOWER_BOUND, - UPPER_BOUND, - SIMPLE_UPPER_BOUND, - LAZY_UPPER_BOUND, - L_SKOLEM_UPPER_BOUND, - EL_LOWER_BOUND, - FRAGMENT, - FRAGMENT_REFINEMENT, - SUMMARISATION, - DEPENDENCY, - FULL_REASONING - } - - /** - * A Json serializer, which considers the main attributes. - */ - public static class QueryRecordSerializer implements JsonSerializer { - public JsonElement serialize(QueryRecord src, Type typeOfSrc, JsonSerializationContext context) { - Gson gson = new GsonBuilder().setPrettyPrinting().create(); - JsonObject object = new JsonObject(); - object.addProperty("queryID", src.queryID); - object.addProperty("queryText", src.queryText); + TupleBuilder result = new TupleBuilder(); + result.append(extendedSelect + " " + fullyExtendedWhere); + + extra.setLength(0); + if(answerVariables[0] != answerVariables[1]) { + for(int i = answerVariables[0].length; i < answerVariables[1].length; ++i) + extra.append(" ?").append(answerVariables[1][i]); + extendedSelect = extendedSelect + extra.toString(); + } + result.append(extendedSelect + " " + extendedWhere); + + return result.build(); + } + + public boolean hasNonAnsDistinguishedVariables() { + if(isDisposed()) throw new DisposedException(); + + return answerVariables[1].length > answerVariables[0].length; + } + + /** + * Two QueryRecords are equal iff + * they have the same queryText, + * soundAnswerTuples. + */ + @Override + public boolean equals(Object o) { + if(isDisposed()) throw new DisposedException(); + + if(!o.getClass().equals(getClass())) return false; + QueryRecord that = (QueryRecord) o; + return this.queryText.equals(that.queryText) + && soundAnswerTuples.equals(that.soundAnswerTuples); + } + + @Override + public int hashCode() { + if(isDisposed()) throw new DisposedException(); + + return Objects.hash(queryText, soundAnswerTuples); + } + + public enum Step { + LOWER_BOUND, + UPPER_BOUND, + SIMPLE_UPPER_BOUND, + LAZY_UPPER_BOUND, + L_SKOLEM_UPPER_BOUND, + EL_LOWER_BOUND, + FRAGMENT, + FRAGMENT_REFINEMENT, + SUMMARISATION, + DEPENDENCY, + FULL_REASONING + } + + /** + * A Json serializer, which considers the main attributes. + */ + public static class QueryRecordSerializer implements JsonSerializer { + public JsonElement serialize(QueryRecord src, Type typeOfSrc, JsonSerializationContext context) { + Gson gson = new GsonBuilder().setPrettyPrinting().create(); + JsonObject object = new JsonObject(); + object.addProperty("queryID", src.queryID); + object.addProperty("queryText", src.queryText); // object.addProperty("difficulty", src.difficulty != null ? src.difficulty.toString() : ""); - object.add("answerVariables", context.serialize(src.getAnswerVariables())); - object.add("answers", context.serialize(src.soundAnswerTuples)); + object.add("answerVariables", context.serialize(src.getAnswerVariables())); + object.add("answers", context.serialize(src.soundAnswerTuples)); // object.add("gapAnswers", context.serialize(src.gapAnswerTuples)); - return object; - } - } - - /** - * A Json deserializer, compliant to the output of the serializer defined above. - */ - public static class QueryRecordDeserializer implements JsonDeserializer { - public QueryRecord deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) - throws JsonParseException { - - QueryRecord record = new QueryRecord(); - JsonObject object = json.getAsJsonObject(); - record.queryID = object.getAsJsonPrimitive("queryID").getAsInt(); - record.queryText = object.getAsJsonPrimitive("queryText").getAsString(); + return object; + } + } + + /** + * A Json deserializer, compliant to the output of the serializer defined above. + */ + public static class QueryRecordDeserializer implements JsonDeserializer { + public QueryRecord deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) + throws JsonParseException { + + QueryRecord record = new QueryRecord(); + JsonObject object = json.getAsJsonObject(); + record.queryID = object.getAsJsonPrimitive("queryID").getAsInt(); + record.queryText = object.getAsJsonPrimitive("queryText").getAsString(); // record.difficulty = Step.valueOf(object.getAsJsonPrimitive("difficulty").getAsString()); - JsonArray answerVariablesJson = object.getAsJsonArray("answerVariables"); - record.answerVariables = new String[2][]; - record.answerVariables[0] = new String[answerVariablesJson.size()]; - for(int i = 0; i < answerVariablesJson.size(); i++) - record.answerVariables[0][i] = answerVariablesJson.get(i).getAsString(); + JsonArray answerVariablesJson = object.getAsJsonArray("answerVariables"); + record.answerVariables = new String[2][]; + record.answerVariables[0] = new String[answerVariablesJson.size()]; + for(int i = 0; i < answerVariablesJson.size(); i++) + record.answerVariables[0][i] = answerVariablesJson.get(i).getAsString(); - record.soundAnswerTuples = new HashSet<>(); + record.soundAnswerTuples = new HashSet<>(); // record.gapAnswerTuples = new HashSet<>(); - Type type = new TypeToken() { }.getType(); - for (JsonElement answer : object.getAsJsonArray("answers")) { - record.soundAnswerTuples.add(context.deserialize(answer, type)); - } + Type type = new TypeToken() { + }.getType(); + for(JsonElement answer : object.getAsJsonArray("answers")) { + record.soundAnswerTuples.add(context.deserialize(answer, type)); + } // for (JsonElement answer : object.getAsJsonArray("gapAnswers")) { // record.soundAnswerTuples.add(context.deserialize(answer, type)); // } - return record; - } - } - - /** - * Provides an instance (singleton) of Gson, having a specific configuration. - * */ - public static class GsonCreator { - - private static Gson gson; - - private GsonCreator() {} - - public static Gson getInstance() { - if(gson == null) { - gson = new GsonBuilder() - .registerTypeAdapter(AnswerTuple.class, new AnswerTuple.AnswerTupleSerializer()) - .registerTypeAdapter(QueryRecord.class, new QueryRecord.QueryRecordSerializer()) - .registerTypeAdapter(QueryRecord.class, new QueryRecord.QueryRecordDeserializer()) - .registerTypeAdapter(AnswerTuple.class, new AnswerTuple.AnswerTupleDeserializer()) - .disableHtmlEscaping() - .setPrettyPrinting() - .create(); - } - return gson; - } + return record; + } + } + + /** + * Provides an instance (singleton) of Gson, having a specific configuration. + */ + public static class GsonCreator { + + private static Gson gson; + + private GsonCreator() { + } + + public static Gson getInstance() { + if(gson == null) { + gson = new GsonBuilder() + .registerTypeAdapter(AnswerTuple.class, new AnswerTuple.AnswerTupleSerializer()) + .registerTypeAdapter(QueryRecord.class, new QueryRecord.QueryRecordSerializer()) + .registerTypeAdapter(QueryRecord.class, new QueryRecord.QueryRecordDeserializer()) + .registerTypeAdapter(AnswerTuple.class, new AnswerTuple.AnswerTupleDeserializer()) + .disableHtmlEscaping() + .setPrettyPrinting() + .create(); + } + return gson; + } // public static void dispose() { // gson = null; // } - } + } } -- cgit v1.2.3