diff options
| author | RncLsn <rnc.lsn@gmail.com> | 2015-05-13 16:04:41 +0100 |
|---|---|---|
| committer | RncLsn <rnc.lsn@gmail.com> | 2015-05-13 16:04:41 +0100 |
| commit | d0c209780ac209ba20de1ef2ba68551dd3321b3c (patch) | |
| tree | e3ed5f293fc7889d7d66e2bbce0fd03cc2d07a4a /src/uk/ac/ox/cs/pagoda/rules/approximators | |
| parent | 7e0ecc07285209e65f9d4d022065d06a4997fc86 (diff) | |
| download | ACQuA-d0c209780ac209ba20de1ef2ba68551dd3321b3c.tar.gz ACQuA-d0c209780ac209ba20de1ef2ba68551dd3321b3c.zip | |
Implemented SkolemTermsManager.
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/rules/approximators')
5 files changed, 135 insertions, 129 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/rules/approximators/LimitedSkolemisationApproximator.java b/src/uk/ac/ox/cs/pagoda/rules/approximators/LimitedSkolemisationApproximator.java index 8fc6b77..e53ae49 100644 --- a/src/uk/ac/ox/cs/pagoda/rules/approximators/LimitedSkolemisationApproximator.java +++ b/src/uk/ac/ox/cs/pagoda/rules/approximators/LimitedSkolemisationApproximator.java | |||
| @@ -1,8 +1,10 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.rules.approximators; | 1 | package uk.ac.ox.cs.pagoda.rules.approximators; |
| 2 | 2 | ||
| 3 | import org.semanticweb.HermiT.model.DLClause; | 3 | import org.semanticweb.HermiT.model.DLClause; |
| 4 | import org.semanticweb.HermiT.model.Individual; | ||
| 4 | import uk.ac.ox.cs.pagoda.multistage.AnswerTupleID; | 5 | import uk.ac.ox.cs.pagoda.multistage.AnswerTupleID; |
| 5 | import uk.ac.ox.cs.pagoda.rules.ExistConstantApproximator; | 6 | import uk.ac.ox.cs.pagoda.rules.ExistConstantApproximator; |
| 7 | import uk.ac.ox.cs.pagoda.util.tuples.Tuple; | ||
| 6 | 8 | ||
| 7 | import java.util.*; | 9 | import java.util.*; |
| 8 | 10 | ||
| @@ -33,7 +35,7 @@ public class LimitedSkolemisationApproximator implements TupleDependentApproxima | |||
| 33 | } | 35 | } |
| 34 | 36 | ||
| 35 | @Override | 37 | @Override |
| 36 | public Collection<DLClause> convert(DLClause clause, DLClause originalClause, Collection<AnswerTupleID> violationTuples) { | 38 | public Collection<DLClause> convert(DLClause clause, DLClause originalClause, Collection<Tuple<Individual>> violationTuples) { |
| 37 | switch (clause.getHeadLength()) { | 39 | switch (clause.getHeadLength()) { |
| 38 | case 1: | 40 | case 1: |
| 39 | return overApprox(clause, originalClause, violationTuples); | 41 | return overApprox(clause, originalClause, violationTuples); |
| @@ -48,26 +50,26 @@ public class LimitedSkolemisationApproximator implements TupleDependentApproxima | |||
| 48 | 50 | ||
| 49 | } | 51 | } |
| 50 | 52 | ||
| 51 | private Collection<DLClause> overApprox(DLClause clause, DLClause originalClause, Collection<AnswerTupleID> violationTuples) { | 53 | private Collection<DLClause> overApprox(DLClause clause, DLClause originalClause, Collection<Tuple<Individual>> violationTuples) { |
| 52 | ArrayList<DLClause> result = new ArrayList<>(); | 54 | ArrayList<DLClause> result = new ArrayList<>(); |
| 53 | 55 | ||
| 54 | for (AnswerTupleID violationTuple : violationTuples) | 56 | for (Tuple<Individual> violationTuple : violationTuples) |
| 55 | if (getDepth(violationTuple) > maxTermDepth) | 57 | if (getDepth(violationTuple) > maxTermDepth) |
| 56 | result.addAll(alternativeApproximator.convert(clause, originalClause, null)); | 58 | result.addAll(alternativeApproximator.convert(clause, originalClause, null)); |
| 57 | else | 59 | else |
| 58 | result.add(getInstantiatedSkolemisation(clause, originalClause, violationTuple)); | 60 | result.add(getGroundSkolemisation(clause, originalClause, violationTuple)); |
| 59 | 61 | ||
| 60 | return result; | 62 | return result; |
| 61 | } | 63 | } |
| 62 | 64 | ||
| 63 | 65 | ||
| 64 | private DLClause getInstantiatedSkolemisation(DLClause clause, DLClause originalClause, AnswerTupleID violationTuple) { | 66 | private DLClause getGroundSkolemisation(DLClause clause, DLClause originalClause, Tuple<Individual> violationTuple) { |
| 65 | // TODO implement | 67 | // TODO implement |
| 66 | // filter the violation tuples appearing on both the sides of the rule | 68 | // filter the violation tuples appearing on both the sides of the rule |
| 67 | return null; | 69 | return null; |
| 68 | } | 70 | } |
| 69 | 71 | ||
| 70 | private int getDepth(AnswerTupleID violationTuple) { | 72 | private int getDepth(Tuple<Individual> violationTuple) { |
| 71 | if (!mapIndividualsToDepth.containsKey(violationTuple)) return 0; | 73 | if (!mapIndividualsToDepth.containsKey(violationTuple)) return 0; |
| 72 | return mapIndividualsToDepth.get(violationTuple); | 74 | return mapIndividualsToDepth.get(violationTuple); |
| 73 | } | 75 | } |
diff --git a/src/uk/ac/ox/cs/pagoda/rules/approximators/OverApproxExist.java b/src/uk/ac/ox/cs/pagoda/rules/approximators/OverApproxExist.java index e0bafe0..d71c472 100644 --- a/src/uk/ac/ox/cs/pagoda/rules/approximators/OverApproxExist.java +++ b/src/uk/ac/ox/cs/pagoda/rules/approximators/OverApproxExist.java | |||
| @@ -2,27 +2,15 @@ package uk.ac.ox.cs.pagoda.rules.approximators; | |||
| 2 | 2 | ||
| 3 | import org.semanticweb.HermiT.model.*; | 3 | import org.semanticweb.HermiT.model.*; |
| 4 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; | 4 | import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper; |
| 5 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 6 | 5 | ||
| 7 | import java.util.*; | 6 | import java.util.Collection; |
| 7 | import java.util.Iterator; | ||
| 8 | import java.util.LinkedList; | ||
| 8 | 9 | ||
| 9 | public class OverApproxExist implements Approximator { | 10 | public class OverApproxExist implements Approximator { |
| 10 | 11 | ||
| 11 | public static final String negativeSuffix = "_neg"; | 12 | public static final String negativeSuffix = "_neg"; |
| 12 | public static final String skolemisedIndividualPrefix = Namespace.PAGODA_ANONY + "individual"; | ||
| 13 | private static final Variable X = Variable.create("X"); | 13 | private static final Variable X = Variable.create("X"); |
| 14 | //DEBUG | ||
| 15 | public static Collection<String> createdIndividualIRIs = new HashSet<>(); | ||
| 16 | private static int individualCounter = 0; | ||
| 17 | private static Map<DLClause, Integer> individualNumber = new HashMap<DLClause, Integer>(); | ||
| 18 | |||
| 19 | private static int noOfExistential(DLClause originalClause) { | ||
| 20 | int no = 0; | ||
| 21 | for (Atom atom: originalClause.getHeadAtoms()) | ||
| 22 | if (atom.getDLPredicate() instanceof AtLeast) | ||
| 23 | no += ((AtLeast) atom.getDLPredicate()).getNumber(); | ||
| 24 | return no; | ||
| 25 | } | ||
| 26 | 14 | ||
| 27 | private static int indexOfExistential(Atom headAtom, DLClause originalClause) { | 15 | private static int indexOfExistential(Atom headAtom, DLClause originalClause) { |
| 28 | if (!(headAtom.getDLPredicate() instanceof AtLeast)) return -1; | 16 | if (!(headAtom.getDLPredicate() instanceof AtLeast)) return -1; |
| @@ -65,36 +53,6 @@ public class OverApproxExist implements Approximator { | |||
| 65 | return null; | 53 | return null; |
| 66 | } | 54 | } |
| 67 | 55 | ||
| 68 | public static int getNumberOfSkolemisedIndividual() { | ||
| 69 | return individualCounter; | ||
| 70 | } | ||
| 71 | //DEBUG | ||
| 72 | |||
| 73 | public static Individual getNewIndividual(DLClause originalClause, int offset) { | ||
| 74 | Individual ret; | ||
| 75 | int individualID; | ||
| 76 | if (individualNumber.containsKey(originalClause)) { | ||
| 77 | individualID = individualNumber.get(originalClause) + offset; | ||
| 78 | ret = Individual.create(skolemisedIndividualPrefix + individualID); | ||
| 79 | } | ||
| 80 | else { | ||
| 81 | individualNumber.put(originalClause, individualCounter); | ||
| 82 | individualID = individualCounter + offset; | ||
| 83 | ret = Individual.create(skolemisedIndividualPrefix + individualID); | ||
| 84 | individualCounter += noOfExistential(originalClause); | ||
| 85 | } | ||
| 86 | return ret; | ||
| 87 | } | ||
| 88 | |||
| 89 | public static int indexOfSkolemisedIndividual(Atom atom) { | ||
| 90 | Term t; | ||
| 91 | for (int index = 0; index < atom.getArity(); ++index) { | ||
| 92 | t = atom.getArgument(index); | ||
| 93 | if (t instanceof Individual && ((Individual) t).getIRI().contains(skolemisedIndividualPrefix)) return index; | ||
| 94 | } | ||
| 95 | return -1; | ||
| 96 | } | ||
| 97 | |||
| 98 | @Override | 56 | @Override |
| 99 | public Collection<DLClause> convert(DLClause clause, DLClause originalClause) { | 57 | public Collection<DLClause> convert(DLClause clause, DLClause originalClause) { |
| 100 | Collection<DLClause> ret; | 58 | Collection<DLClause> ret; |
| @@ -140,7 +98,8 @@ public class OverApproxExist implements Approximator { | |||
| 140 | 98 | ||
| 141 | int card = atLeastConcept.getNumber(); | 99 | int card = atLeastConcept.getNumber(); |
| 142 | Individual[] individuals = new Individual[card]; | 100 | Individual[] individuals = new Individual[card]; |
| 143 | for (int i = 0; i < card; ++i) individuals[i] = getNewIndividual(originalClause, offset + i); | 101 | SkolemTermsManager termsManager = SkolemTermsManager.getInstance(); |
| 102 | for (int i = 0; i < card; ++i) individuals[i] = termsManager.getFreshIndividual(originalClause, offset + i); | ||
| 144 | 103 | ||
| 145 | for (int i = 0; i < card; ++i) { | 104 | for (int i = 0; i < card; ++i) { |
| 146 | if (atomicConcept != null) | 105 | if (atomicConcept != null) |
diff --git a/src/uk/ac/ox/cs/pagoda/rules/approximators/SkolemTermsDispenser.java b/src/uk/ac/ox/cs/pagoda/rules/approximators/SkolemTermsDispenser.java deleted file mode 100644 index a920ec5..0000000 --- a/src/uk/ac/ox/cs/pagoda/rules/approximators/SkolemTermsDispenser.java +++ /dev/null | |||
| @@ -1,74 +0,0 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.rules.approximators; | ||
| 2 | |||
| 3 | import org.semanticweb.HermiT.model.AtLeast; | ||
| 4 | import org.semanticweb.HermiT.model.Atom; | ||
| 5 | import org.semanticweb.HermiT.model.DLClause; | ||
| 6 | import org.semanticweb.HermiT.model.Individual; | ||
| 7 | import uk.ac.ox.cs.pagoda.multistage.AnswerTupleID; | ||
| 8 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 9 | |||
| 10 | import java.util.HashMap; | ||
| 11 | import java.util.Map; | ||
| 12 | |||
| 13 | /** | ||
| 14 | * If you need a Skolem term (i.e. fresh individual), ask this class. | ||
| 15 | */ | ||
| 16 | public class SkolemTermsDispenser { | ||
| 17 | |||
| 18 | public static final String skolemisedIndividualPrefix = Namespace.PAGODA_ANONY + "individual"; | ||
| 19 | private static SkolemTermsDispenser skolemTermsDispenser; | ||
| 20 | private int individualCounter = 0; | ||
| 21 | private Map<DLClause, Integer> termNumber = new HashMap<>(); | ||
| 22 | private Map<SkolemTermId, Integer> mapTermToDepth = new HashMap<>(); | ||
| 23 | private int dependenciesCounter = 0; | ||
| 24 | private Map<AnswerTupleID, Integer> mapDependencyToId = new HashMap<>(); | ||
| 25 | |||
| 26 | private SkolemTermsDispenser() { | ||
| 27 | } | ||
| 28 | |||
| 29 | public static SkolemTermsDispenser getInstance() { | ||
| 30 | if (skolemTermsDispenser == null) skolemTermsDispenser = new SkolemTermsDispenser(); | ||
| 31 | return skolemTermsDispenser; | ||
| 32 | } | ||
| 33 | |||
| 34 | private int getDependencyId(AnswerTupleID answerTupleID) { | ||
| 35 | if (mapDependencyToId.containsKey(answerTupleID)) return mapDependencyToId.get(answerTupleID); | ||
| 36 | else return mapDependencyToId.put(answerTupleID, dependenciesCounter++); | ||
| 37 | } | ||
| 38 | |||
| 39 | public Individual getNewIndividual(DLClause originalClause, int offset, AnswerTupleID dependency) { | ||
| 40 | if (!termNumber.containsKey(originalClause)) { | ||
| 41 | termNumber.put(originalClause, individualCounter); | ||
| 42 | individualCounter += noOfExistential(originalClause); | ||
| 43 | } | ||
| 44 | if (!mapDependencyToId.containsKey(dependency)) { | ||
| 45 | mapDependencyToId.put(dependency, dependenciesCounter++); | ||
| 46 | } | ||
| 47 | |||
| 48 | SkolemTermId termId = new SkolemTermId(termNumber.get(originalClause) + offset, getDependencyId(dependency)); | ||
| 49 | return Individual.create(skolemisedIndividualPrefix + termId); | ||
| 50 | } | ||
| 51 | |||
| 52 | private int noOfExistential(DLClause originalClause) { | ||
| 53 | int no = 0; | ||
| 54 | for (Atom atom : originalClause.getHeadAtoms()) | ||
| 55 | if (atom.getDLPredicate() instanceof AtLeast) | ||
| 56 | no += ((AtLeast) atom.getDLPredicate()).getNumber(); | ||
| 57 | return no; | ||
| 58 | } | ||
| 59 | |||
| 60 | private class SkolemTermId { | ||
| 61 | private final int idBase; | ||
| 62 | private final int idOffset; | ||
| 63 | |||
| 64 | private SkolemTermId(int idBase, int idOffset) { | ||
| 65 | this.idBase = idBase; | ||
| 66 | this.idOffset = idOffset; | ||
| 67 | } | ||
| 68 | |||
| 69 | @Override | ||
| 70 | public String toString() { | ||
| 71 | return idBase + "_" + idOffset; | ||
| 72 | } | ||
| 73 | } | ||
| 74 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/rules/approximators/SkolemTermsManager.java b/src/uk/ac/ox/cs/pagoda/rules/approximators/SkolemTermsManager.java new file mode 100644 index 0000000..0413e65 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/rules/approximators/SkolemTermsManager.java | |||
| @@ -0,0 +1,116 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.rules.approximators; | ||
| 2 | |||
| 3 | import org.semanticweb.HermiT.model.*; | ||
| 4 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 5 | import uk.ac.ox.cs.pagoda.util.tuples.Tuple; | ||
| 6 | |||
| 7 | import java.util.HashMap; | ||
| 8 | import java.util.Map; | ||
| 9 | |||
| 10 | /** | ||
| 11 | * If you need a Skolem term (i.e. fresh individual), ask this class. | ||
| 12 | */ | ||
| 13 | public class SkolemTermsManager { | ||
| 14 | |||
| 15 | public static final String skolemisedIndividualPrefix = Namespace.PAGODA_ANONY + "individual"; | ||
| 16 | |||
| 17 | private static SkolemTermsManager skolemTermsManager; | ||
| 18 | |||
| 19 | private int individualCounter = 0; | ||
| 20 | private Map<DLClause, Integer> termNumber = new HashMap<>(); | ||
| 21 | private Map<Individual, Integer> mapIndividualToDepth = new HashMap<>(); | ||
| 22 | private int dependenciesCounter = 0; | ||
| 23 | |||
| 24 | // replace with hashcode. in case of collision you get only a different upper bound model. | ||
| 25 | private Map<Tuple<Individual>, Integer> mapDependencyToId = new HashMap<>(); | ||
| 26 | |||
| 27 | /** | ||
| 28 | * Get a fresh Individual, unique for the clause, the offset and the dependency. | ||
| 29 | * */ | ||
| 30 | public Individual getFreshIndividual(DLClause originalClause, int offset, Tuple<Individual> dependency) { | ||
| 31 | if (!termNumber.containsKey(originalClause)) { | ||
| 32 | termNumber.put(originalClause, individualCounter); | ||
| 33 | individualCounter += noOfExistential(originalClause); | ||
| 34 | } | ||
| 35 | if (!mapDependencyToId.containsKey(dependency)) { | ||
| 36 | mapDependencyToId.put(dependency, dependenciesCounter++); | ||
| 37 | } | ||
| 38 | |||
| 39 | String termId = termNumber.get(originalClause) + offset + "_" + mapDependencyToId(dependency); | ||
| 40 | Individual newIndividual = Individual.create(skolemisedIndividualPrefix + termId); | ||
| 41 | |||
| 42 | int depth = 0; | ||
| 43 | for (Individual individual : dependency) | ||
| 44 | depth = Integer.max(depth, mapIndividualToDepth(individual)); | ||
| 45 | mapIndividualToDepth.put(newIndividual, depth); | ||
| 46 | |||
| 47 | return newIndividual; | ||
| 48 | } | ||
| 49 | |||
| 50 | /** | ||
| 51 | * Get a fresh Individual, unique for the clause and the offset. | ||
| 52 | * */ | ||
| 53 | public Individual getFreshIndividual(DLClause originalClause, int offset) { | ||
| 54 | if (!termNumber.containsKey(originalClause)) { | ||
| 55 | termNumber.put(originalClause, individualCounter); | ||
| 56 | individualCounter += noOfExistential(originalClause); | ||
| 57 | } | ||
| 58 | |||
| 59 | String termId = "" + termNumber.get(originalClause) + offset; | ||
| 60 | Individual newIndividual = Individual.create(skolemisedIndividualPrefix + termId); | ||
| 61 | mapIndividualToDepth.put(newIndividual, 0); | ||
| 62 | |||
| 63 | return newIndividual; | ||
| 64 | } | ||
| 65 | |||
| 66 | /** | ||
| 67 | * Get the depth of a term. | ||
| 68 | * <p> | ||
| 69 | * The term must have been generated by this manager. | ||
| 70 | * */ | ||
| 71 | public int getDepth(Individual individual) { | ||
| 72 | return mapIndividualToDepth(individual); | ||
| 73 | } | ||
| 74 | |||
| 75 | /** | ||
| 76 | * Get the number of individuals generated by this manager. | ||
| 77 | * */ | ||
| 78 | public int getNumberOfSkolemisedIndividual() { | ||
| 79 | return mapIndividualToDepth.keySet().size(); | ||
| 80 | } | ||
| 81 | |||
| 82 | public static int indexOfSkolemisedIndividual(Atom atom) { | ||
| 83 | Term t; | ||
| 84 | for (int index = 0; index < atom.getArity(); ++index) { | ||
| 85 | t = atom.getArgument(index); | ||
| 86 | if (t instanceof Individual && ((Individual) t).getIRI().contains(skolemisedIndividualPrefix)) return index; | ||
| 87 | } | ||
| 88 | return -1; | ||
| 89 | } | ||
| 90 | |||
| 91 | private SkolemTermsManager() { | ||
| 92 | } | ||
| 93 | |||
| 94 | public static SkolemTermsManager getInstance() { | ||
| 95 | if (skolemTermsManager == null) skolemTermsManager = new SkolemTermsManager(); | ||
| 96 | return skolemTermsManager; | ||
| 97 | } | ||
| 98 | |||
| 99 | private int mapDependencyToId(Tuple<Individual> dependency) { | ||
| 100 | if (mapDependencyToId.containsKey(dependency)) return mapDependencyToId.get(dependency); | ||
| 101 | else return mapDependencyToId.put(dependency, dependenciesCounter++); | ||
| 102 | } | ||
| 103 | |||
| 104 | private int mapIndividualToDepth(Individual dependency) { | ||
| 105 | if (mapIndividualToDepth.containsKey(dependency)) return mapIndividualToDepth.get(dependency); | ||
| 106 | else return 0; | ||
| 107 | } | ||
| 108 | |||
| 109 | private int noOfExistential(DLClause originalClause) { | ||
| 110 | int no = 0; | ||
| 111 | for (Atom atom : originalClause.getHeadAtoms()) | ||
| 112 | if (atom.getDLPredicate() instanceof AtLeast) | ||
| 113 | no += ((AtLeast) atom.getDLPredicate()).getNumber(); | ||
| 114 | return no; | ||
| 115 | } | ||
| 116 | } | ||
diff --git a/src/uk/ac/ox/cs/pagoda/rules/approximators/TupleDependentApproximator.java b/src/uk/ac/ox/cs/pagoda/rules/approximators/TupleDependentApproximator.java index 1a729e5..c99a1ad 100644 --- a/src/uk/ac/ox/cs/pagoda/rules/approximators/TupleDependentApproximator.java +++ b/src/uk/ac/ox/cs/pagoda/rules/approximators/TupleDependentApproximator.java | |||
| @@ -1,16 +1,19 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.rules.approximators; | 1 | package uk.ac.ox.cs.pagoda.rules.approximators; |
| 2 | 2 | ||
| 3 | import org.semanticweb.HermiT.model.DLClause; | 3 | import org.semanticweb.HermiT.model.DLClause; |
| 4 | import uk.ac.ox.cs.pagoda.multistage.AnswerTupleID; | 4 | import org.semanticweb.HermiT.model.Individual; |
| 5 | import uk.ac.ox.cs.pagoda.util.tuples.Tuple; | ||
| 5 | 6 | ||
| 6 | import java.util.Collection; | 7 | import java.util.Collection; |
| 7 | 8 | ||
| 8 | /** | 9 | /** |
| 9 | * It approximates rules according to a specific instantiation of the body. | 10 | * It can approximate clauses according to a collection of tuples of individuals. |
| 11 | * <p> | ||
| 12 | * In particular it can be used to approximate rules given some body instantiations. | ||
| 10 | */ | 13 | */ |
| 11 | public interface TupleDependentApproximator { | 14 | public interface TupleDependentApproximator { |
| 12 | 15 | ||
| 13 | Collection<DLClause> convert(DLClause clause, | 16 | Collection<DLClause> convert(DLClause clause, |
| 14 | DLClause originalClause, | 17 | DLClause originalClause, |
| 15 | Collection<AnswerTupleID> violationTuples); | 18 | Collection<Tuple<Individual>> individualsTuples); |
| 16 | } | 19 | } |
