aboutsummaryrefslogtreecommitdiff
path: root/test/uk/ac/ox/cs/pagoda/summary/SummaryTester.java
blob: 18b60909060d5aa827b857138c1cc235684c1bf6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package uk.ac.ox.cs.pagoda.summary;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;

import org.semanticweb.HermiT.Reasoner;
import org.semanticweb.owlapi.model.AxiomType;
import org.semanticweb.owlapi.model.IRI;
import org.semanticweb.owlapi.model.OWLClassExpression;
import org.semanticweb.owlapi.model.OWLDataFactory;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyCreationException;
import org.semanticweb.owlapi.model.OWLOntologyStorageException;

import uk.ac.ox.cs.pagoda.hermit.DLClauseHelper;
import uk.ac.ox.cs.pagoda.owl.OWLHelper;
import uk.ac.ox.cs.pagoda.owl.QueryRoller;
import uk.ac.ox.cs.pagoda.query.QueryManager;
import uk.ac.ox.cs.pagoda.query.QueryRecord;
import uk.ac.ox.cs.pagoda.summary.Summary;

public class SummaryTester {

	static String FILE_BREAK = System.getProperty("file.separator");
	static String LINE_BREAK = System.getProperty("line.separator");
	
	public static void main(String[] args) throws Exception {
//		String arg = "ontologies/claros/all-in-one-manually.owl"; 
//		String arg = "ontologies/claros/Claros.owl ontologies/claros/data"; 
		String arg =  "../uobmGenerator/univ-bench-dl.owl " + 
				"../uobmGenerator/uobm1 " + //"a " + 
				"ontologies/uobm/queries/uobm_standard_less.sparql"; 
				
		testSummarisedUpperBound(arg.split("\\ ")); 
	}
	
	/**
	 * args[0] ontology file location
	 * args[1] data directory
	 * args[2] sparql query file location
	 * 
	 * @param args 
	 * @throws OWLOntologyCreationException
	 * @throws FileNotFoundException
	 * @throws OWLOntologyStorageException 
	 */
	public static void testSummarisedUpperBound(String[] args) throws OWLOntologyCreationException, FileNotFoundException, OWLOntologyStorageException {
		OWLOntology onto = OWLHelper.loadOntology(args[0]); 
		try {
			onto = OWLHelper.getImportedOntology(onto, args[1]); 
		} catch (IOException e) {
			e.printStackTrace();
		} 
		 
		Summary sum = testSummary(onto);
		System.out.println("Summarisation Done."); 
		
		System.out.println(args[2]); 
		Scanner scanner = new Scanner(new File(args[2])); 
		OWLOntology summary = sum.getSummary();
		OWLDataFactory factory = summary.getOWLOntologyManager().getOWLDataFactory(); 
		QueryRoller r = new QueryRoller(factory);
		OWLClassExpression summarisedQueryExp;
		Reasoner reasoner = new Reasoner(summary); 
		QueryManager queryManager = new QueryManager(); 
		int upperBoundCounter, queryID = 0;
		StringBuilder queryText = new StringBuilder();
		String[] vars; 
		
		for (String line; ; ) {
			queryText.setLength(0);
			while (scanner.hasNextLine() && (line = scanner.nextLine()) != null && !line.startsWith("^[query"));
			if (!scanner.hasNextLine()) break; 
			
			while (scanner.hasNextLine() && (line = scanner.nextLine()) != null && !line.isEmpty())
				queryText.append(line).append(LINE_BREAK);
			if (!scanner.hasNextLine()) break; 
				
			System.out.println("------------ starting computing for Query " + ++queryID + "------------"); 
			
			System.out.println(queryText); 
			
			QueryRecord record = queryManager.create(queryText.toString(), queryID);
			vars = record.getAnswerVariables(); 
			if (vars.length > 1) {
				System.out.println("The query cannot be processed by HermiT ... More than one answer variable"); 
				continue; 
			}
			
			summarisedQueryExp = r.rollUp(DLClauseHelper.getQuery(sum.getSummary(record), null), vars[0]);
			
			upperBoundCounter = 0; 
			for (String representative: sum.getRepresentatives()) 
				if (reasoner.isEntailed(factory.getOWLClassAssertionAxiom(summarisedQueryExp, factory.getOWLNamedIndividual(IRI.create(representative))))) {
					upperBoundCounter += sum.getGroup(representative).size(); 
				}
			
			System.out.println("There are " + upperBoundCounter + " individual(s) in the upper bound computed by summary."); 
		}
		scanner.close();
	}
	
	public static Summary testSummary(OWLOntology ontology) throws OWLOntologyCreationException, FileNotFoundException {
		Summary sum = new Summary(ontology);
		
		System.out.println("original ontology data: "); 
		outputStatistics(ontology); 
		
		OWLOntology summary = sum.getSummary();
		
		System.out.println("summarised ontology data: "); 
		outputStatistics(summary);
		
		try {
			FileOutputStream out = new FileOutputStream("summary.owl"); 
			summary.getOWLOntologyManager().saveOntology(summary, out);
			out.close();
		} catch (OWLOntologyStorageException e) {
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return sum; 
	}

	private static void outputStatistics(OWLOntology onto) {
		System.out.println("TBox: " + onto.getTBoxAxioms(true).size() +
				"\tRBox: " + onto.getRBoxAxioms(true).size() + 
				"\tABox: " + onto.getABoxAxioms(true).size());
		System.out.println("Class Assertions: " + onto.getAxiomCount(AxiomType.CLASS_ASSERTION, true) + 
				"\tObject Property Assertions: " + onto.getAxiomCount(AxiomType.OBJECT_PROPERTY_ASSERTION, true)); 
	}

}