aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/hermit/RuleHelper.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/uk/ac/ox/cs/pagoda/hermit/RuleHelper.java')
-rw-r--r--src/uk/ac/ox/cs/pagoda/hermit/RuleHelper.java108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/uk/ac/ox/cs/pagoda/hermit/RuleHelper.java b/src/uk/ac/ox/cs/pagoda/hermit/RuleHelper.java
new file mode 100644
index 0000000..476fbec
--- /dev/null
+++ b/src/uk/ac/ox/cs/pagoda/hermit/RuleHelper.java
@@ -0,0 +1,108 @@
1package uk.ac.ox.cs.pagoda.hermit;
2
3import org.semanticweb.HermiT.model.AnnotatedEquality;
4import org.semanticweb.HermiT.model.Atom;
5import org.semanticweb.HermiT.model.AtomicRole;
6import org.semanticweb.HermiT.model.DLClause;
7import org.semanticweb.HermiT.model.DLPredicate;
8import org.semanticweb.HermiT.model.Equality;
9import org.semanticweb.HermiT.model.Inequality;
10import org.semanticweb.HermiT.model.NodeIDLessEqualThan;
11import org.semanticweb.HermiT.model.NodeIDsAscendingOrEqual;
12import org.semanticweb.HermiT.model.Term;
13import org.semanticweb.HermiT.model.Variable;
14import uk.ac.ox.cs.pagoda.MyPrefixes;
15import uk.ac.ox.cs.pagoda.util.Namespace;
16
17public class RuleHelper {
18
19// public static String abbreviateIRI(String text) {
20// String prefixName, prefixIRI;
21// int start = -1, ends = -1;
22// while (true) {
23// start = text.indexOf('<', ends + 1);
24// if (start == -1) return text;
25// ends = text.indexOf('>', start + 1);
26// if (ends == -1) return text;
27// String sub = text.substring(start, ends + 1), newSub = text.substring(start + 1, ends);
28//
29// int index = splitPoint(newSub);
30// if (index >= 0) {
31// prefixIRI = newSub.substring(0, index + 1);
32// if ((prefixName = MyPrefixes.PAGOdAPrefixes.getPrefixName(prefixIRI)) == null) {
33// prefixName = getNewPrefixName();
34// MyPrefixes.PAGOdAPrefixes.declarePrefix(prefixName, prefixIRI);
35// }
36// newSub = newSub.replace(prefixIRI, prefixName);
37// text = text.replaceAll(sub, newSub);
38// ends -= sub.length() - newSub.length();
39// }
40// }
41// }
42
43 public static String getText(DLClause clause) {
44 StringBuffer buf = new StringBuffer();
45 String atomText;
46
47 boolean lastSpace = true;
48 for (Atom headAtom: clause.getHeadAtoms()) {
49 if ((atomText = getText(headAtom)) == null) continue;
50 if (!lastSpace) buf.append(" v ");
51 buf.append(atomText);
52 lastSpace = false;
53 }
54 buf.append(" :- ");
55 lastSpace = true;
56 for (Atom bodyAtom: clause.getBodyAtoms()) {
57// for (String str: strs[1].split(", ")) {
58 if ((atomText = getText(bodyAtom)) == null) continue;
59 if (!lastSpace) buf.append(", ");
60 buf.append(atomText);
61 lastSpace = false;
62 }
63 buf.append('.');
64 return buf.toString();
65 }
66
67
68 private static String getText(Atom atom) {
69 if (atom.getDLPredicate() instanceof NodeIDsAscendingOrEqual ||
70 atom.getDLPredicate() instanceof NodeIDLessEqualThan)
71 return null;
72
73 StringBuilder builder = new StringBuilder();
74 if (atom.getArity() == 1) {
75 builder.append(getText(atom.getDLPredicate()));
76 builder.append("(");
77 builder.append(getText(atom.getArgument(0)));
78 builder.append(")");
79 }
80 else {
81 DLPredicate p = atom.getDLPredicate();
82 if (p instanceof Equality || p instanceof AnnotatedEquality) builder.append(Namespace.EQUALITY_ABBR);
83 else if (p instanceof Inequality) builder.append(Namespace.INEQUALITY_ABBR);
84 else builder.append(getText(p));
85 builder.append("(");
86 builder.append(getText(atom.getArgument(0)));
87 builder.append(",");
88 builder.append(getText(atom.getArgument(1)));
89 builder.append(")");
90 }
91 return builder.toString();
92 }
93
94 public static String getText(DLPredicate p) {
95 if (p instanceof Equality || p instanceof AnnotatedEquality) return Namespace.EQUALITY_ABBR;
96 if (p instanceof Inequality) return Namespace.INEQUALITY_ABBR;
97 if (p instanceof AtomicRole && ((AtomicRole) p).getIRI().startsWith("?"))
98 return ((AtomicRole) p).getIRI();
99 return MyPrefixes.PAGOdAPrefixes.abbreviateIRI(p.toString());
100 }
101
102 public static String getText(Term t) {
103 if (t instanceof Variable)
104 return "?" + ((Variable) t).getName();
105 return MyPrefixes.PAGOdAPrefixes.abbreviateIRI(t.toString());
106 }
107
108}