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 package org.openrdf.elmo.sesame;
30
31 import java.beans.PropertyDescriptor;
32 import java.lang.reflect.Method;
33
34 import org.openrdf.elmo.annotations.inverseOf;
35 import org.openrdf.elmo.annotations.rdf;
36 import org.openrdf.elmo.sesame.helpers.PropertyChanger;
37 import org.openrdf.model.Resource;
38 import org.openrdf.model.URI;
39 import org.openrdf.model.Value;
40 import org.openrdf.repository.RepositoryException;
41 import org.openrdf.repository.contextaware.ContextAwareConnection;
42
43
44
45
46
47
48
49
50
51
52
53 public class InferencingPropertyFactory<E> extends SesamePropertyFactory<E> {
54
55 private URI[] equivalent;
56
57 private URI[] inverseOf;
58
59 @Override
60 public SesamePropertyFactory<E> setPropertyDescriptor(
61 PropertyDescriptor property) {
62 Method getter = property.getReadMethod();
63 if (getter != null) {
64 inverseOf ann = getter.getAnnotation(inverseOf.class);
65 if (getter.isAnnotationPresent(rdf.class)) {
66 String[] eq = getter.getAnnotation(rdf.class).value();
67 if (eq.length > 1) {
68 equivalent = new URI[eq.length - 1];
69 for (int i = 1; i < eq.length; i++) {
70 equivalent[i - 1] = getValueFactory().createURI(eq[i]);
71 }
72 }
73 if (getter.isAnnotationPresent(inverseOf.class)) {
74 String[] inv = ann.value();
75 inverseOf = new URI[inv.length];
76 for (int i = 0; i < inv.length; i++) {
77 inverseOf[i] = getValueFactory().createURI(inv[i]);
78 }
79 }
80 } else if (getter.isAnnotationPresent(inverseOf.class)) {
81 String[] inv = ann.value();
82 if (inv.length > 1) {
83 inverseOf = new URI[inv.length - 1];
84 for (int i = 1; i < inv.length; i++) {
85 inverseOf[i - 1] = getValueFactory().createURI(inv[i]);
86 }
87 }
88 }
89 }
90 return super.setPropertyDescriptor(property);
91 }
92
93 @Override
94 protected PropertyChanger getPropertyChanger() {
95 final URI pred = getPredicate();
96 final URI[] equivalent = this.equivalent;
97 final URI[] inverseOf = this.inverseOf;
98 if (equivalent == null && inverseOf == null)
99 return super.getPropertyChanger();
100 return new PropertyChanger(pred, getOneOf()) {
101 @Override
102 public void add(ContextAwareConnection conn, Resource subj,
103 Value obj) throws RepositoryException {
104 super.add(conn, subj, obj);
105 if (equivalent != null) {
106 for (URI eq : equivalent) {
107 conn.add(subj, eq, obj);
108 }
109 }
110 if (inverseOf != null) {
111 for (URI inv : inverseOf) {
112 conn.add((Resource) obj, inv, subj);
113 }
114 }
115 }
116
117 @Override
118 public void remove(ContextAwareConnection conn, Resource subj,
119 Value obj) throws RepositoryException {
120 super.remove(conn, subj, obj);
121 if (equivalent != null) {
122 for (URI eq : equivalent) {
123 conn.remove(subj, eq, obj);
124 }
125 }
126 if (inverseOf != null) {
127 for (URI inv : inverseOf) {
128 conn.remove((Resource) obj, inv, subj);
129 }
130 }
131 }
132
133 };
134 }
135
136 }