01: /*
02: * Copyright 2007 Bastian Schenke Licensed under the Apache License, Version 2.0 (the "License");
03: * you may not use this file except in compliance with the License.
04: * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
05: * Unless required by applicable law or agreed to in writing, software distributed under the
06: * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
07: * either express or implied. See the License for the specific language governing permissions
08: * and limitations under the License.
09: */
10: package nz.org.take.r2ml.util;
11:
12: import java.util.ArrayList;
13: import java.util.Arrays;
14: import java.util.Collection;
15:
16: import javax.xml.namespace.QName;
17:
18: import nz.org.take.KnowledgeBase;
19: import nz.org.take.Predicate;
20: import nz.org.take.PropertyPredicate;
21: import nz.org.take.Query;
22: import nz.org.take.r2ml.MappingContext;
23:
24: /**
25: * A generator that builds querries for all predicates in a given knowledge
26: * base. For each predicate all possible in out signatures are generated.
27: *
28: * @author schenke
29: *
30: */
31: public abstract class AbstractQueryGenerator implements QueryGenerator {
32:
33: /**
34: * @param predicates
35: */
36: protected Collection<Query> buildAllQueries(
37: Collection<Predicate> predicates) {
38: Collection<Query> queries = new ArrayList<Query>();
39: if (predicates == null || predicates.size() == 0)
40: System.out.println("No predicates in knowledgebase!");
41: Collection<String> propertyPredicateNames = MappingContext
42: .get().getPropertyPredicateNames();
43: for (Predicate predicate : predicates) {
44: boolean[][] signatures = generateInOutSignatures(predicate);
45: for (boolean[] signature : signatures) {
46: if (propertyPredicateNames
47: .contains(predicate.getName())
48: && !signature[0]) {
49: // System.out.println("ommit query " + predicate.getName() + " " + Arrays.toString(signature));
50: continue;
51: }
52: Query q = new Query();
53: q.setPredicate(predicate);
54: q.setInputParams(signature);
55: q.setPublic(true);
56: queries.add(q);
57:
58: } // all signatures
59:
60: } // all predicates
61: return queries;
62: }
63:
64: private static boolean[][] generateInOutSignatures(
65: Predicate predicate) {
66:
67: // number of possible signatures
68: int sigSize = predicate.getSlotTypes().length;
69: int sigCount = (int) Math.pow(2, sigSize);
70:
71: boolean[][] signatures = new boolean[sigCount][sigSize];
72:
73: for (int i = 0; i < sigSize; i++) {
74: for (int j = 0; j < sigCount; j++) {
75: signatures[j][i] = (int) (j / Math.pow(2, i) % 2) == 0;
76: }
77: }
78:
79: return signatures;
80: }
81:
82: }
|