diff options
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/summary/Graph.java')
| -rw-r--r-- | src/uk/ac/ox/cs/pagoda/summary/Graph.java | 154 |
1 files changed, 0 insertions, 154 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/summary/Graph.java b/src/uk/ac/ox/cs/pagoda/summary/Graph.java deleted file mode 100644 index cfa94a4..0000000 --- a/src/uk/ac/ox/cs/pagoda/summary/Graph.java +++ /dev/null | |||
| @@ -1,154 +0,0 @@ | |||
| 1 | package uk.ac.ox.cs.pagoda.summary; | ||
| 2 | |||
| 3 | import java.util.Arrays; | ||
| 4 | import java.util.Collection; | ||
| 5 | import java.util.Comparator; | ||
| 6 | import java.util.HashMap; | ||
| 7 | import java.util.HashSet; | ||
| 8 | import java.util.LinkedList; | ||
| 9 | import java.util.Map; | ||
| 10 | import java.util.Set; | ||
| 11 | |||
| 12 | import org.semanticweb.HermiT.model.Constant; | ||
| 13 | import org.semanticweb.HermiT.model.Individual; | ||
| 14 | import org.semanticweb.HermiT.model.Term; | ||
| 15 | import org.semanticweb.owlapi.model.OWLAxiom; | ||
| 16 | import org.semanticweb.owlapi.model.OWLClass; | ||
| 17 | import org.semanticweb.owlapi.model.OWLClassAssertionAxiom; | ||
| 18 | import org.semanticweb.owlapi.model.OWLObjectProperty; | ||
| 19 | import org.semanticweb.owlapi.model.OWLObjectPropertyAssertionAxiom; | ||
| 20 | import org.semanticweb.owlapi.model.OWLOntology; | ||
| 21 | |||
| 22 | import uk.ac.ox.cs.JRDFox.model.GroundTerm; | ||
| 23 | import uk.ac.ox.cs.JRDFox.model.Literal; | ||
| 24 | import uk.ac.ox.cs.pagoda.query.AnswerTuple; | ||
| 25 | |||
| 26 | public class Graph { | ||
| 27 | |||
| 28 | Set<Node> nodes = new HashSet<Node>(); | ||
| 29 | Map<Node, Edge[]> sortedOutGoingEdges = new HashMap<Node, Edge[]>(); | ||
| 30 | Map<Node, Edge[]> sortedInComingEdges = new HashMap<Node, Edge[]>(); | ||
| 31 | |||
| 32 | public Graph(OWLOntology ontology) { | ||
| 33 | Map<Node, Collection<Edge>> outGoingEdges = new HashMap<Node, Collection<Edge>>(); | ||
| 34 | Map<Node, Collection<Edge>> inComingEdges = new HashMap<Node, Collection<Edge>>(); | ||
| 35 | |||
| 36 | for (OWLAxiom axiom: ontology.getABoxAxioms(true)) | ||
| 37 | if (axiom instanceof OWLClassAssertionAxiom) | ||
| 38 | addClassAssertion((OWLClassAssertionAxiom) axiom); | ||
| 39 | else if (axiom instanceof OWLObjectPropertyAssertionAxiom) | ||
| 40 | addPropertyAssertion((OWLObjectPropertyAssertionAxiom) axiom, inComingEdges, outGoingEdges); | ||
| 41 | |||
| 42 | for (Node node: nodes) { | ||
| 43 | sortedOutGoingEdges.put(node, sort(outGoingEdges.get(node))); | ||
| 44 | sortedInComingEdges.put(node, sort(inComingEdges.get(node))); | ||
| 45 | } | ||
| 46 | |||
| 47 | outGoingEdges.clear(); | ||
| 48 | inComingEdges.clear(); | ||
| 49 | } | ||
| 50 | |||
| 51 | public Collection<Node> getNodes() { return nodes; } | ||
| 52 | |||
| 53 | private void addPropertyAssertion(OWLObjectPropertyAssertionAxiom axiom, | ||
| 54 | Map<Node, Collection<Edge>> inComingEdges, Map<Node, Collection<Edge>> outGoingEdges) { | ||
| 55 | |||
| 56 | Node u = getNode(axiom.getSubject().toStringID()), v = getNode(axiom.getObject().toStringID()); | ||
| 57 | |||
| 58 | nodes.add(u); | ||
| 59 | nodes.add(v); | ||
| 60 | |||
| 61 | Edge e = new Edge(u, v, ((OWLObjectProperty) axiom.getProperty()).toStringID()); | ||
| 62 | |||
| 63 | Collection<Edge> edges = outGoingEdges.get(u); | ||
| 64 | if (edges == null) { | ||
| 65 | edges = new LinkedList<Edge>(); | ||
| 66 | outGoingEdges.put(u, edges); | ||
| 67 | } | ||
| 68 | edges.add(e); | ||
| 69 | |||
| 70 | edges = inComingEdges.get(v); | ||
| 71 | if (edges == null) { | ||
| 72 | edges = new LinkedList<Edge>(); | ||
| 73 | inComingEdges.put(v, edges); | ||
| 74 | } | ||
| 75 | edges.add(e); | ||
| 76 | } | ||
| 77 | |||
| 78 | private void addClassAssertion(OWLClassAssertionAxiom axiom) { | ||
| 79 | OWLClass cls = (OWLClass) axiom.getClassExpression(); | ||
| 80 | Node u; | ||
| 81 | // if (cls.getIRI().toString().startsWith(HermitSummaryFilter.QueryAnswerTermPrefix)) | ||
| 82 | // u = getNode(axiom.getIndividual().toStringID(), false); | ||
| 83 | // else | ||
| 84 | u = getNode(axiom.getIndividual().toStringID()); | ||
| 85 | |||
| 86 | if (u == null) return ; | ||
| 87 | u.addConcept(cls.toStringID()); | ||
| 88 | nodes.add(u); | ||
| 89 | } | ||
| 90 | |||
| 91 | public Edge[] getOutGoingEdges(Node u) { | ||
| 92 | return sortedOutGoingEdges.get(u); | ||
| 93 | } | ||
| 94 | |||
| 95 | public Edge[] getInComingEdges(Node u) { | ||
| 96 | return sortedInComingEdges.get(u); | ||
| 97 | } | ||
| 98 | |||
| 99 | Comparator<Edge> edgeComp = new EdgeComparatorByNodeLabel(); | ||
| 100 | |||
| 101 | public Edge[] sort(Collection<Edge> edges) { | ||
| 102 | if (edges == null) return new Edge[0]; | ||
| 103 | Edge[] sortedEdges = new Edge[edges.size()]; | ||
| 104 | edges.toArray(sortedEdges); | ||
| 105 | Arrays.sort(sortedEdges, edgeComp); | ||
| 106 | return sortedEdges; | ||
| 107 | } | ||
| 108 | |||
| 109 | |||
| 110 | private Comparator<Node> coarseNodeComparator = null; | ||
| 111 | |||
| 112 | public Comparator<Node> getCoarseNodeComparator() { | ||
| 113 | if (coarseNodeComparator == null) | ||
| 114 | coarseNodeComparator = new EstimatedFeatureComparator(this); | ||
| 115 | return coarseNodeComparator; | ||
| 116 | } | ||
| 117 | |||
| 118 | Map<String, Node> allNodes = new HashMap<String, Node>(); | ||
| 119 | |||
| 120 | private Node getNode(String nodeName) { | ||
| 121 | Node node = null; | ||
| 122 | if ((node = allNodes.get(nodeName)) == null) { | ||
| 123 | node = new Node(nodeName); | ||
| 124 | allNodes.put(nodeName, node); | ||
| 125 | } | ||
| 126 | return node; | ||
| 127 | } | ||
| 128 | |||
| 129 | private Node getNode(GroundTerm t) { | ||
| 130 | if (t instanceof uk.ac.ox.cs.JRDFox.model.Individual) | ||
| 131 | return getNode(((uk.ac.ox.cs.JRDFox.model.Individual) t).getIRI()); | ||
| 132 | else { | ||
| 133 | Literal l = (Literal) t; | ||
| 134 | return getNode(l.getLexicalForm() + "^^" + l.getDatatype().getIRI()); | ||
| 135 | } | ||
| 136 | } | ||
| 137 | |||
| 138 | public Node getNode(Term t) { | ||
| 139 | if (t instanceof Individual) | ||
| 140 | return getNode(((Individual) t).getIRI()); | ||
| 141 | else if (t instanceof Constant) | ||
| 142 | return getNode(((Constant) t).getLexicalForm() + "^^" + ((Constant) t).getDatatypeURI()); | ||
| 143 | return null; | ||
| 144 | } | ||
| 145 | |||
| 146 | public NodeTuple getNodeTuple(AnswerTuple tuple) { | ||
| 147 | NodeTuple ret = new NodeTuple(tuple); | ||
| 148 | for (int i = 0; i < tuple.getArity(); ++i) | ||
| 149 | ret.addNode(getNode(tuple.getGroundTerm(i))); | ||
| 150 | return ret; | ||
| 151 | } | ||
| 152 | |||
| 153 | } | ||
| 154 | |||
