aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/hermit/RuleHelper.java
blob: 81c99a4831680bd2dd3528223eb489f51b3c9758 (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
package uk.ac.ox.cs.pagoda.hermit;

import org.semanticweb.HermiT.model.*;
import uk.ac.ox.cs.pagoda.MyPrefixes;
import uk.ac.ox.cs.pagoda.util.Namespace;

public class RuleHelper {

//	public static String abbreviateIRI(String text) {
//		String prefixName, prefixIRI; 
//		int start = -1, ends = -1;
//		while (true) {
//			start = text.indexOf('<', ends + 1);
//			if (start == -1) return text;
//			ends = text.indexOf('>', start + 1);
//			if (ends == -1)	return text;
//			String sub = text.substring(start, ends + 1), newSub = text.substring(start + 1, ends);
//			
//			int index = splitPoint(newSub); 
//			if (index >= 0) {
//				prefixIRI = newSub.substring(0, index + 1);
//				if ((prefixName = MyPrefixes.PAGOdAPrefixes.getPrefixName(prefixIRI)) == null) {
//					prefixName = getNewPrefixName();
//					MyPrefixes.PAGOdAPrefixes.declarePrefix(prefixName, prefixIRI);
//				}
//				newSub = newSub.replace(prefixIRI, prefixName); 
//				text = text.replaceAll(sub, newSub);
//				ends -= sub.length() - newSub.length();
//			}
//		}
//	}
	
	public static String getText(DLClause clause) {
		StringBuffer buf = new StringBuffer();
		String atomText; 
		
		boolean lastSpace = true;
		for (Atom headAtom: clause.getHeadAtoms()) {
			if ((atomText = getText(headAtom)) == null) continue; 
			if (!lastSpace)	buf.append(" v ");
			buf.append(atomText);
			lastSpace = false;
		}
		buf.append(" :- ");
		lastSpace = true;
		for (Atom bodyAtom: clause.getBodyAtoms()) {
//		for (String str: strs[1].split(", ")) {
			if ((atomText = getText(bodyAtom)) == null) continue; 
			if (!lastSpace) buf.append(", ");
			buf.append(atomText);
			lastSpace = false;
		}
		buf.append('.');
		return buf.toString();
	}

	
	private static String getText(Atom atom) {
		if (atom.getDLPredicate() instanceof NodeIDsAscendingOrEqual ||
				atom.getDLPredicate() instanceof NodeIDLessEqualThan) 
			return null;
		
		StringBuilder builder = new StringBuilder(); 
		if (atom.getArity() == 1) {
			builder.append(getText(atom.getDLPredicate())); 
			builder.append("("); 
			builder.append(getText(atom.getArgument(0)));
			builder.append(")"); 
		}
		else {
			DLPredicate p = atom.getDLPredicate();
			if (p instanceof Equality || p instanceof AnnotatedEquality) builder.append(Namespace.EQUALITY_ABBR); 
			else if (p instanceof Inequality) builder.append(Namespace.INEQUALITY_ABBR); 
			else builder.append(getText(p));
			builder.append("("); 
			builder.append(getText(atom.getArgument(0))); 
			builder.append(","); 
			builder.append(getText(atom.getArgument(1))); 
			builder.append(")"); 
		}
		return builder.toString(); 
	}

	public static String getText(DLPredicate p) {
		if (p instanceof Equality || p instanceof AnnotatedEquality) return Namespace.EQUALITY_ABBR; 
		if (p instanceof Inequality) return Namespace.INEQUALITY_ABBR;
		if (p instanceof AtomicRole && ((AtomicRole) p).getIRI().startsWith("?"))
			return ((AtomicRole) p).getIRI(); 
		return MyPrefixes.PAGOdAPrefixes.abbreviateIRI(p.toString());
	}

	public static String getText(Term t) {
		if (t instanceof Variable)
			return "?" + ((Variable) t).getName(); 
		return MyPrefixes.PAGOdAPrefixes.abbreviateIRI(t.toString());
	}

}