|
| 1 | +/* |
| 2 | + * Copyright 2013 Christoph Böhme |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 the "License"; |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package net.b3e.mf.extra.pipe; |
| 18 | + |
| 19 | +import org.culturegraph.mf.framework.DefaultObjectPipe; |
| 20 | +import org.culturegraph.mf.framework.ObjectReceiver; |
| 21 | +import org.culturegraph.mf.framework.annotations.Description; |
| 22 | +import org.culturegraph.mf.framework.annotations.In; |
| 23 | +import org.culturegraph.mf.framework.annotations.Out; |
| 24 | +import org.culturegraph.mf.types.Triple; |
| 25 | + |
| 26 | +/** |
| 27 | + * Shifts subjectTo predicateTo and object around. |
| 28 | + * |
| 29 | + * @author Christoph Böhme |
| 30 | + * |
| 31 | + */ |
| 32 | +@Description("Shifts subjectTo predicateTo and object around") |
| 33 | +@In(Triple.class) |
| 34 | +@Out(Triple.class) |
| 35 | +public final class TripleReorder extends |
| 36 | + DefaultObjectPipe<Triple, ObjectReceiver<Triple>> { |
| 37 | + |
| 38 | + /** |
| 39 | + * Names of the elements in the triple |
| 40 | + */ |
| 41 | + public enum TripleElement { SUBJECT, PREDICATE, OBJECT }; |
| 42 | + // Do not change the item order because the process method |
| 43 | + // uses ordinal(). |
| 44 | + |
| 45 | + private TripleElement subjectFrom = TripleElement.SUBJECT; |
| 46 | + private TripleElement predicateFrom = TripleElement.PREDICATE; |
| 47 | + private TripleElement objectFrom = TripleElement.OBJECT; |
| 48 | + |
| 49 | + public TripleElement getSubjectFrom() { |
| 50 | + return subjectFrom; |
| 51 | + } |
| 52 | + |
| 53 | + public TripleElement getPredicateFrom() { |
| 54 | + return predicateFrom; |
| 55 | + } |
| 56 | + |
| 57 | + public TripleElement getObjectFrom() { |
| 58 | + return objectFrom; |
| 59 | + } |
| 60 | + |
| 61 | + public void setSubjectFrom(final TripleElement subjectFrom) { |
| 62 | + this.subjectFrom = subjectFrom; |
| 63 | + } |
| 64 | + |
| 65 | + public void setPredicateFrom(final TripleElement predicateFrom) { |
| 66 | + this.predicateFrom = predicateFrom; |
| 67 | + } |
| 68 | + |
| 69 | + public void setObjectFrom(final TripleElement objectFrom) { |
| 70 | + this.objectFrom = objectFrom; |
| 71 | + } |
| 72 | + |
| 73 | + @Override |
| 74 | + public void process(final Triple triple) { |
| 75 | + final String[] elements = { |
| 76 | + triple.getSubject(), |
| 77 | + triple.getPredicate(), |
| 78 | + triple.getObject(), |
| 79 | + }; |
| 80 | + |
| 81 | + getReceiver().process(new Triple( |
| 82 | + elements[subjectFrom.ordinal()], |
| 83 | + elements[predicateFrom.ordinal()], |
| 84 | + elements[objectFrom.ordinal()] |
| 85 | + )); |
| 86 | + } |
| 87 | + |
| 88 | +} |
0 commit comments