diff options
| author | yzhou <yujiao.zhou@gmail.com> | 2015-04-21 10:34:27 +0100 |
|---|---|---|
| committer | yzhou <yujiao.zhou@gmail.com> | 2015-04-21 10:34:27 +0100 |
| commit | 9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8 (patch) | |
| tree | 47511c0fb89dccff0db4b5990522e04f294d795b /src/uk/ac/ox/cs/pagoda/summary/Summary.java | |
| parent | b1ac207612ee8b045244253fb94b866104bc34f2 (diff) | |
| download | ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.tar.gz ACQuA-9ce65c5a963b03ee97fe9cb6c5aa65a3c04a80a8.zip | |
initial version
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/summary/Summary.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/summary/Summary.java | 215 |
1 files changed, 215 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/summary/Summary.java b/src/uk/ac/ox/cs/pagoda/summary/Summary.java new file mode 100644 index 0000000..264ff76 --- /dev/null +++ b/src/uk/ac/ox/cs/pagoda/summary/Summary.java | |||
| @@ -0,0 +1,215 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.summary; | ||
| 2 | |||
| 3 | import java.io.File; | ||
| 4 | import java.util.Collection; | ||
| 5 | import java.util.HashMap; | ||
| 6 | import java.util.LinkedList; | ||
| 7 | import java.util.Map; | ||
| 8 | |||
| 9 | import org.semanticweb.HermiT.model.Atom; | ||
| 10 | import org.semanticweb.HermiT.model.DLClause; | ||
| 11 | import org.semanticweb.HermiT.model.Individual; | ||
| 12 | import org.semanticweb.HermiT.model.Term; | ||
| 13 | import org.semanticweb.owlapi.model.IRI; | ||
| 14 | import org.semanticweb.owlapi.model.OWLAxiom; | ||
| 15 | import org.semanticweb.owlapi.model.OWLClassAssertionAxiom; | ||
| 16 | import org.semanticweb.owlapi.model.OWLDataFactory; | ||
| 17 | import org.semanticweb.owlapi.model.OWLDataPropertyAssertionAxiom; | ||
| 18 | import org.semanticweb.owlapi.model.OWLLiteral; | ||
| 19 | import org.semanticweb.owlapi.model.OWLNamedIndividual; | ||
| 20 | import org.semanticweb.owlapi.model.OWLObjectProperty; | ||
| 21 | import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; | ||
| 22 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 23 | import org.semanticweb.owlapi.model.OWLOntologyCreationException; | ||
| 24 | import org.semanticweb.owlapi.model.OWLOntologyManager; | ||
| 25 | import org.semanticweb.owlapi.model.OWLOntologyStorageException; | ||
| 26 | |||
| 27 | import uk.ac.ox.cs.JRDFox.model.GroundTerm; | ||
| 28 | import uk.ac.ox.cs.JRDFox.model.Literal; | ||
| 29 | import uk.ac.ox.cs.pagoda.owl.OWLHelper; | ||
| 30 | import uk.ac.ox.cs.pagoda.query.AnswerTuple; | ||
| 31 | import uk.ac.ox.cs.pagoda.query.QueryRecord; | ||
| 32 | import uk.ac.ox.cs.pagoda.util.Namespace; | ||
| 33 | import uk.ac.ox.cs.pagoda.util.SparqlHelper; | ||
| 34 | import uk.ac.ox.cs.pagoda.util.Utility; | ||
| 35 | |||
| 36 | public class Summary { | ||
| 37 | |||
| 38 | OWLOntologyManager manager; | ||
| 39 | OWLDataFactory factory; | ||
| 40 | OWLOntology ontology, summarisedOntology; | ||
| 41 | Graph graph; | ||
| 42 | |||
| 43 | public Summary(OWLOntology ontology) { | ||
| 44 | OWLHelper.identifyAndChangeAnnotationAssertions(ontology); | ||
| 45 | this.ontology = ontology; | ||
| 46 | graph = new Graph(ontology); | ||
| 47 | factory = (manager = ontology.getOWLOntologyManager()).getOWLDataFactory(); | ||
| 48 | } | ||
| 49 | |||
| 50 | public Summary(OWLOntology ontology, Graph graph) { | ||
| 51 | this.ontology = ontology; | ||
| 52 | this.graph = graph; | ||
| 53 | factory = (manager = ontology.getOWLOntologyManager()).getOWLDataFactory(); | ||
| 54 | } | ||
| 55 | |||
| 56 | Map<String, String> label2representative = new HashMap<String, String>(); | ||
| 57 | Map<String, String> representatives = new HashMap<String, String>(); | ||
| 58 | |||
| 59 | public Collection<String> getRepresentatives() { | ||
| 60 | return representatives.values(); | ||
| 61 | } | ||
| 62 | |||
| 63 | Map<String, LinkedList<String>> groups = null; | ||
| 64 | |||
| 65 | public Collection<String> getGroup(String representative) { | ||
| 66 | if (groups == null) { | ||
| 67 | groups = new HashMap<String, LinkedList<String>>(); | ||
| 68 | LinkedList<String> group; | ||
| 69 | for (Map.Entry<String, String> entry: representatives.entrySet()) { | ||
| 70 | if ((group = groups.get(entry.getValue())) == null) | ||
| 71 | group = new LinkedList<String>(); | ||
| 72 | group.add(entry.getKey()); | ||
| 73 | } | ||
| 74 | } | ||
| 75 | return groups.get(representative); | ||
| 76 | } | ||
| 77 | |||
| 78 | private void process(OWLOntology ontology, OWLOntology abstractOntology) { | ||
| 79 | OWLOntologyManager manager = ontology.getOWLOntologyManager(); | ||
| 80 | groupIndividualsByConcepts(); | ||
| 81 | |||
| 82 | manager.addAxioms(abstractOntology, ontology.getRBoxAxioms(true)); | ||
| 83 | manager.addAxioms(abstractOntology, ontology.getTBoxAxioms(true)); | ||
| 84 | |||
| 85 | OWLAxiom newAxiom; | ||
| 86 | for (OWLAxiom axiom: ontology.getABoxAxioms(true)) { | ||
| 87 | newAxiom = summeriseAxiom(axiom); | ||
| 88 | manager.addAxiom(abstractOntology, newAxiom); | ||
| 89 | } | ||
| 90 | |||
| 91 | OWLObjectProperty sameAs = factory.getOWLObjectProperty(IRI.create(Namespace.EQUALITY)); | ||
| 92 | for (Map.Entry<String, String> entry: representatives.entrySet()) | ||
| 93 | if (!entry.getKey().equals(entry.getValue())) | ||
| 94 | manager.addAxiom(abstractOntology, | ||
| 95 | factory.getOWLObjectPropertyAssertionAxiom( | ||
| 96 | sameAs, | ||
| 97 | factory.getOWLNamedIndividual(IRI.create(entry.getKey())), | ||
| 98 | factory.getOWLNamedIndividual(IRI.create(entry.getValue())))); | ||
| 99 | } | ||
| 100 | |||
| 101 | private void groupIndividualsByConcepts() { | ||
| 102 | String name, label, representative; | ||
| 103 | Utility.logDebug("grouping individuals by its concepts"); | ||
| 104 | |||
| 105 | for (Node node: graph.getNodes()) { | ||
| 106 | name = node.getName(); | ||
| 107 | label = node.getLabel(); | ||
| 108 | |||
| 109 | if ((representative = label2representative.get(label)) == null) { | ||
| 110 | representative = name; | ||
| 111 | label2representative.put(label, name); | ||
| 112 | } | ||
| 113 | |||
| 114 | representatives.put(name, representative); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | private OWLAxiom summeriseAxiom(OWLAxiom axiom) { | ||
| 119 | if (axiom instanceof OWLClassAssertionAxiom) { | ||
| 120 | OWLClassAssertionAxiom assertion = (OWLClassAssertionAxiom) axiom; | ||
| 121 | OWLNamedIndividual a = getRepresentativeIndividual(assertion.getIndividual().toStringID()); | ||
| 122 | return factory.getOWLClassAssertionAxiom(assertion.getClassExpression(), a); | ||
| 123 | } | ||
| 124 | else if (axiom instanceof OWLObjectPropertyAssertionAxiom) { | ||
| 125 | OWLObjectPropertyAssertionAxiom assertion = (OWLObjectPropertyAssertionAxiom) axiom; | ||
| 126 | OWLNamedIndividual a = getRepresentativeIndividual(assertion.getSubject().toStringID()); | ||
| 127 | OWLNamedIndividual b = getRepresentativeIndividual(assertion.getObject().toStringID()); | ||
| 128 | return factory.getOWLObjectPropertyAssertionAxiom(assertion.getProperty(), a, b); | ||
| 129 | } | ||
| 130 | else if (axiom instanceof OWLDataPropertyAssertionAxiom) { | ||
| 131 | OWLDataPropertyAssertionAxiom assertion = (OWLDataPropertyAssertionAxiom) axiom; | ||
| 132 | OWLNamedIndividual a = getRepresentativeIndividual(assertion.getSubject().toStringID()); | ||
| 133 | OWLLiteral b = assertion.getObject(); | ||
| 134 | return factory.getOWLDataPropertyAssertionAxiom(assertion.getProperty(), a, b); | ||
| 135 | |||
| 136 | } | ||
| 137 | else { | ||
| 138 | Utility.logError("Unknown axiom: " + axiom); | ||
| 139 | return null; | ||
| 140 | } | ||
| 141 | } | ||
| 142 | |||
| 143 | public OWLNamedIndividual getRepresentativeIndividual(String name) { | ||
| 144 | return factory.getOWLNamedIndividual(IRI.create(getRepresentativeName(name))); | ||
| 145 | } | ||
| 146 | |||
| 147 | public String getRepresentativeName(String name) { | ||
| 148 | String rep = representatives.get(name); | ||
| 149 | if (rep == null) return name; | ||
| 150 | return rep; | ||
| 151 | } | ||
| 152 | |||
| 153 | public OWLOntology getSummary() { | ||
| 154 | if (summarisedOntology == null) { | ||
| 155 | try { | ||
| 156 | summarisedOntology = ontology.getOWLOntologyManager().createOntology(); | ||
| 157 | } catch (OWLOntologyCreationException e) { | ||
| 158 | summarisedOntology = null; | ||
| 159 | e.printStackTrace(); | ||
| 160 | } | ||
| 161 | process(ontology, summarisedOntology); | ||
| 162 | } | ||
| 163 | return summarisedOntology; | ||
| 164 | } | ||
| 165 | |||
| 166 | public void save(String fileName) { | ||
| 167 | try { | ||
| 168 | manager.saveOntology(summarisedOntology, IRI.create(new File(fileName))); | ||
| 169 | } catch (OWLOntologyStorageException e) { | ||
| 170 | e.printStackTrace(); | ||
| 171 | } | ||
| 172 | } | ||
| 173 | |||
| 174 | private GroundTerm getSummary(GroundTerm t) { | ||
| 175 | if (t instanceof Literal) return t; | ||
| 176 | return uk.ac.ox.cs.JRDFox.model.Individual.create(getSummary(((uk.ac.ox.cs.JRDFox.model.Individual) t).getIRI())); | ||
| 177 | } | ||
| 178 | |||
| 179 | public String getSummary(QueryRecord record) { | ||
| 180 | DLClause queryClause = getSummary(record.getClause()); | ||
| 181 | return SparqlHelper.getSPARQLQuery(queryClause.getBodyAtoms(), record.getAnswerVariables()); | ||
| 182 | } | ||
| 183 | |||
| 184 | public DLClause getSummary(DLClause clause) { | ||
| 185 | Atom[] newHeadAtoms = new Atom[clause.getHeadLength()], newBodyAtoms = new Atom[clause.getBodyLength()]; | ||
| 186 | int index = 0; | ||
| 187 | for (Atom atom: clause.getHeadAtoms()) | ||
| 188 | newHeadAtoms[index++] = getSummary(atom); | ||
| 189 | for (Atom atom: clause.getBodyAtoms()) | ||
| 190 | newBodyAtoms[index++] = getSummary(atom); | ||
| 191 | |||
| 192 | return DLClause.create(newHeadAtoms, newBodyAtoms); | ||
| 193 | } | ||
| 194 | |||
| 195 | public Atom getSummary(Atom atom) { | ||
| 196 | Term[] args = new Term [atom.getArity()]; | ||
| 197 | for (int i = 0; i < atom.getArity(); ++i) | ||
| 198 | if ((args[i] = atom.getArgument(i)) instanceof Individual) | ||
| 199 | args[i] = Individual.create(getSummary(atom.getArgument(i).toString())); | ||
| 200 | return Atom.create(atom.getDLPredicate(), args); | ||
| 201 | } | ||
| 202 | |||
| 203 | public String getSummary(String name) { | ||
| 204 | return getRepresentativeName(OWLHelper.removeAngles(name)); | ||
| 205 | } | ||
| 206 | |||
| 207 | public AnswerTuple getSummary(AnswerTuple answer) { | ||
| 208 | int arity = answer.getArity(); | ||
| 209 | GroundTerm[] t = new GroundTerm[arity]; | ||
| 210 | for (int i = 0; i < arity; ++i) | ||
| 211 | t[i] = getSummary(answer.getGroundTerm(i)); | ||
| 212 | return new AnswerTuple(t); | ||
| 213 | } | ||
| 214 | |||
| 215 | } | ||
