aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/reasoner/light/RDFoxQueryEngine.java
blob: 30771abbab44862ad5f14c0f7c502f8875cf8461 (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
package uk.ac.ox.cs.pagoda.reasoner.light;

import java.io.File;
import java.util.Collection;

import uk.ac.ox.cs.pagoda.MyPrefixes;
import uk.ac.ox.cs.pagoda.query.AnswerTuples;
import uk.ac.ox.cs.pagoda.reasoner.QueryEngine;
import uk.ac.ox.cs.pagoda.reasoner.QueryReasoner;
import uk.ac.ox.cs.pagoda.tracking.AnswerTuplesWriter;
import uk.ac.ox.cs.pagoda.util.Timer;
import uk.ac.ox.cs.pagoda.util.Utility;
import uk.ac.ox.cs.JRDFox.JRDFStoreException;
import uk.ac.ox.cs.JRDFox.Prefixes;
import uk.ac.ox.cs.JRDFox.store.DataStore;
import uk.ac.ox.cs.JRDFox.store.DataStore.StoreType;

public abstract class RDFoxQueryEngine implements QueryEngine {
	
	public static final int matNoOfThreads = Runtime.getRuntime().availableProcessors() * 2;
	
	protected String name; 
	protected Prefixes prefixes = MyPrefixes.PAGOdAPrefixes.getRDFoxPrefixes();

	public RDFoxQueryEngine(String name) {
		this.name = name; 
	} 
	
	public abstract DataStore getDataStore();
	
	public abstract void dispose(); 
	
	public void importRDFData(String fileName, String importedFile) {
		if (importedFile == null || importedFile.isEmpty()) return ; 
		Timer t = new Timer(); 
		DataStore store = getDataStore(); 
		try {
			long oldTripleCount = store.getTriplesCount(), tripleCount;
			for (String file: importedFile.split(QueryReasoner.ImportDataFileSeparator))
				store.importTurtleFile(new File(file), prefixes);
			tripleCount = store.getTriplesCount(); 
			Utility.logDebug(name + " store after importing " + fileName + ": " + tripleCount + " (" + (tripleCount - oldTripleCount) + " new)");
			store.clearRulesAndMakeFactsExplicit();
		} catch (JRDFStoreException e) {
			e.printStackTrace();
		}
		Utility.logDebug(name + " store finished importing " + fileName + " in " + t.duration() + " seconds.");
	}
	
	public void materialise(String programName, String programText) {
		if (programText == null) return ; 
		Timer t = new Timer();
		DataStore store = getDataStore(); 
		try {
			long oldTripleCount = store.getTriplesCount(), tripleCount;
//			store.addRules(new String[] {programText});
			store.importRules(programText);
			store.applyReasoning();
			tripleCount = store.getTriplesCount(); 
			Utility.logDebug(name + " store after materialising " + programName + ": " + tripleCount + " (" + (tripleCount - oldTripleCount) + " new)");
			store.clearRulesAndMakeFactsExplicit();
		} catch (JRDFStoreException e) {
			e.printStackTrace();
		}
		Utility.logDebug(name + " store finished the materialisation of " + programName + " in " + t.duration() + " seconds.");
	}

	@Override
	public void evaluate(Collection<String> queryTexts, String answerFile) {
		if (queryTexts == null)
			return ;
		
		int queryID = 0; 
		AnswerTuplesWriter answerWriter = new AnswerTuplesWriter(answerFile); 
		AnswerTuples answerTuples;
		Timer t = new Timer(); 
		try {
			for (String query: queryTexts) {
				t.reset();
				answerTuples = null; 
				try {
					answerTuples = evaluate(query); 
					Utility.logDebug("time to answer Query " + ++queryID + ": " + t.duration());
					answerWriter.write(answerTuples.getAnswerVariables(), answerTuples);
				} finally {
					if (answerTuples != null) answerTuples.dispose();
				}
			}
		} finally {
			answerWriter.close();
		}
		
		Utility.logDebug("done computing query answers by RDFox.");
		
	}
	
	public static DataStore createDataStore() {
		DataStore instance = null; 
		try {
//			instance = new DataStore("par-head-n");
			instance = new DataStore(StoreType.NarrowParallelHead);
			instance.setNumberOfThreads(matNoOfThreads);
			instance.initialize();
		} catch (JRDFStoreException e) {
			e.printStackTrace();
		}
		return instance;
	}

}