001: package org.ofbiz.rules.engine;
002:
003: import java.util.*;
004:
005: /**
006: * <p><b>Title:</b> Program
007: * <p><b>Description:</b> None
008: * <p>Copyright (c) 1999 Steven J. Metsker.
009: * <p>Copyright (c) 2001 The Open For Business Project - www.ofbiz.org
010: *
011: * <p>Permission is hereby granted, free of charge, to any person obtaining a
012: * copy of this software and associated documentation files (the "Software"),
013: * to deal in the Software without restriction, including without limitation
014: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
015: * and/or sell copies of the Software, and to permit persons to whom the
016: * Software is furnished to do so, subject to the following conditions:
017: *
018: * <p>The above copyright notice and this permission notice shall be included
019: * in all copies or substantial portions of the Software.
020: *
021: * <p>THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
022: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
023: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
024: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
025: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
026: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
027: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
028: *
029: * <br>
030: * <p>A Program is a collection of rules and facts that together
031: * form a logical model.
032: *
033: * @author Steven J. Metsker
034: * @version 1.0
035: */
036:
037: public class Program implements AxiomSource {
038: protected List axioms = new ArrayList();
039:
040: /**
041: * Create a new program with no axioms.
042: */
043: public Program() {
044: }
045:
046: /**
047: * Create a new program with the given axioms.
048: */
049: public Program(Axiom[] axioms) {
050: for (int i = 0; i < axioms.length; i++) {
051: addAxiom(axioms[i]);
052: }
053: }
054:
055: /**
056: * Adds an axiom to this program.
057: *
058: * @param Axiom the axiom to add.
059: */
060: public void addAxiom(Axiom a) {
061: axioms.add(a);
062: }
063:
064: /**
065: * Appends all the axioms of another source to this one.
066: *
067: * @param program the source of the new axioms
068: */
069: public void append(AxiomSource as) {
070: AxiomEnumeration e = as.axioms();
071:
072: while (e.hasMoreAxioms()) {
073: addAxiom(e.nextAxiom());
074: }
075: }
076:
077: /**
078: * Returns an enumeration of the axioms in this program.
079: *
080: * @return an enumeration of the axioms in this program.
081: */
082: public AxiomEnumeration axioms() {
083: return new ProgramEnumerator(this );
084: }
085:
086: /**
087: * Returns an enumeration of the axioms in this program.
088: *
089: * @return an enumeration of the axioms in this program.
090: */
091: public AxiomEnumeration axioms(Structure ignored) {
092: return axioms();
093: }
094:
095: /**
096: * Returns a string representation of this program.
097: *
098: * @return a string representation of this program.
099: */
100: public String toString() {
101: StringBuffer buf = new StringBuffer();
102: boolean haveShownALine = false;
103: Enumeration e = Collections.enumeration(axioms);
104:
105: while (e.hasMoreElements()) {
106: if (haveShownALine) {
107: buf.append("\n");
108: }
109: buf.append(e.nextElement().toString());
110: buf.append(";");
111: haveShownALine = true;
112: }
113: return buf.toString();
114: }
115: }
|