001: /**
002: * Copyright 2007 Jens Dietrich Licensed under the Apache License, Version 2.0 (the "License");
003: * you may not use this file except in compliance with the License.
004: * You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
005: * Unless required by applicable law or agreed to in writing, software distributed under the
006: * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
007: * either express or implied. See the License for the specific language governing permissions
008: * and limitations under the License.
009: */package nz.org.take;
010:
011: import java.util.*;
012:
013: import org.apache.commons.collections.CollectionUtils;
014: import org.apache.commons.collections.ListUtils;
015:
016: /**
017: * Default implementation of knowledge base.
018: * Access to instance variables is synchronized, i.e. this class is thread safe.
019: * @author <a href="http://www-ist.massey.ac.nz/JBDietrich/">Jens Dietrich</a>
020: */
021:
022: public class DefaultKnowledgeBase extends AbstractAnnotatable implements
023: KnowledgeBase {
024: private List<KnowledgeElement> elements = new ArrayList<KnowledgeElement>();
025: private List<Query> queries = new ArrayList<Query>();
026: private Map<String, KnowledgeElement> elementsById = new HashMap<String, KnowledgeElement>();
027:
028: /**
029: * Retrieve knowledge by id.
030: * @param id an id
031: * @return a knowledge element
032: */
033: public synchronized KnowledgeElement getElement(String id) {
034: return this .elementsById.get(id);
035: }
036:
037: /**
038: * Retrieve knowledge by predicate.
039: * @param p the predicate
040: * @return knowledge elements
041: */
042: public synchronized List<KnowledgeElement> getElements(
043: final Predicate p) {
044: org.apache.commons.collections.Predicate filter = new org.apache.commons.collections.Predicate() {
045: public boolean evaluate(Object arg) {
046: KnowledgeElement e = (KnowledgeElement) arg;
047: return p.equals(e.getPredicate());
048: }
049: };
050: return (List<KnowledgeElement>) CollectionUtils.select(
051: this .elements, filter);
052: }
053:
054: /**
055: * Get all elements.
056: * @return knowledge elements
057: */
058: public synchronized List<KnowledgeElement> getElements() {
059: return (List<KnowledgeElement>) ListUtils
060: .unmodifiableList(this .elements);
061: }
062:
063: /**
064: * Get all predicates for which knowledge elements are available.
065: * @return predicates
066: */
067: public synchronized Collection<Predicate> getSupportedPredicates() {
068: Set<Predicate> predicates = new HashSet<Predicate>();
069: for (KnowledgeElement e : this .elements) {
070: predicates.add(e.getPredicate());
071: }
072: return predicates;
073: }
074:
075: /**
076: * Add an element.
077: * @param e an element
078: */
079: public synchronized void add(KnowledgeElement e) {
080: this .elements.add(e);
081: this .elementsById.put(e.getId(), e);
082: }
083:
084: /**
085: * Remove an element, return true if this succeeded.
086: * @param e an element
087: * @return a boolean
088: */
089: public synchronized boolean remove(KnowledgeElement e) {
090: boolean result = this .elements.remove(e);
091: result = result
092: && (null != this .elementsById.remove(e.getId()));
093: return result;
094: }
095:
096: /**
097: * Add a query.
098: * @param q a query
099: */
100: public synchronized void add(Query q) {
101: this .queries.add(q);
102: }
103:
104: /**
105: * Remove a query, return true if this succeeded.
106: * @param q a query
107: * @return a boolean
108: */
109: public synchronized boolean remove(Query q) {
110: return this .queries.remove(q);
111: }
112:
113: /**
114: * Get all queries.
115: * @return queries
116: */
117: public List<Query> getQueries() {
118: return (List<Query>) ListUtils.unmodifiableList(this .queries);
119: }
120:
121: public void accept(KnowledgeBaseVisitor visitor) {
122: if (visitor.visit(this )) {
123: for (Query q : queries)
124: q.accept(visitor);
125: for (KnowledgeElement e : elements)
126: e.accept(visitor);
127: }
128: visitor.endVisit(this );
129: }
130:
131: /**
132: * Get a knowledge base.
133: * @return a knowledge base
134: */
135: public KnowledgeBase getKnowledgeBase() throws TakeException {
136: return this;
137: }
138: }
|