01: /**
02: * Copyright 2007 Jens Dietrich 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: */package nz.org.take;
10:
11: import java.util.ArrayList;
12: import java.util.List;
13:
14: /**
15: * Derivation rules, i.e., if .. then .. constructs.
16: * @author <a href="http://www-ist.massey.ac.nz/JBDietrich/">Jens Dietrich</a>
17: */
18:
19: public class DerivationRule extends AbstractAnnotatable implements
20: Clause {
21: private Fact head = null;
22: private String id = null;
23: private List<Prerequisite> body = new ArrayList<Prerequisite>();
24:
25: public Predicate getPredicate() {
26: return head == null ? null : head.getPredicate();
27: }
28:
29: public Fact getHead() {
30: return head;
31: }
32:
33: public void setHead(Fact head) {
34: this .head = head;
35: }
36:
37: public List<Prerequisite> getBody() {
38: return body;
39: }
40:
41: public void setBody(List<Prerequisite> body) {
42: this .body = body;
43: }
44:
45: public String getId() {
46: return id;
47: }
48:
49: public void setId(String id) {
50: this .id = id;
51: }
52:
53: public String toString() {
54: StringBuffer b = new StringBuffer();
55: boolean f = true;
56: b.append(" IF ");
57: for (Prerequisite p : this .body) {
58: if (f)
59: f = false;
60: else
61: b.append(" AND ");
62: b.append(p);
63: }
64: b.append(" THEN ");
65: b.append(head);
66: return b.toString();
67: }
68:
69: public void accept(KnowledgeBaseVisitor visitor) {
70: if (visitor.visit(this )) {
71: head.accept(visitor);
72: for (Prerequisite p : body)
73: p.accept(visitor);
74: }
75: visitor.endVisit(this);
76: }
77: }
|