001: package org.drools.ruleflow.core.impl;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.Collections;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.Map.Entry;
024:
025: import org.drools.ruleflow.core.Connection;
026: import org.drools.ruleflow.core.Constraint;
027: import org.drools.ruleflow.core.Split;
028:
029: /**
030: * Default implementation of a split node.
031: *
032: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
033: */
034: public class SplitImpl extends NodeImpl implements Split {
035:
036: private static final long serialVersionUID = 400L;
037:
038: private int type;
039: private Map constraints;
040:
041: public SplitImpl() {
042: this .type = TYPE_UNDEFINED;
043: this .constraints = new HashMap();
044: }
045:
046: public SplitImpl(final int type) {
047: this .type = type;
048: this .constraints = new HashMap();
049: }
050:
051: public void setType(final int type) {
052: this .type = type;
053: }
054:
055: public int getType() {
056: return this .type;
057: }
058:
059: public Constraint getConstraint(final Connection connection) {
060: if (connection == null) {
061: throw new IllegalArgumentException("connection is null");
062: }
063:
064: // dirty hack because keys were entered wrong
065: // probably caused by xstreams
066: // TODO xstream 1.3.0 should fix this by default; in 1.2.2 it's fixable: http://jira.codehaus.org/browse/XSTR-363
067: final HashMap newMap = new HashMap();
068: for (final Iterator it = this .constraints.entrySet().iterator(); it
069: .hasNext();) {
070: final Entry entry = (Entry) it.next();
071: newMap.put(entry.getKey(), entry.getValue());
072: }
073: this .constraints = newMap;
074:
075: if (this .type == TYPE_OR || this .type == TYPE_XOR) {
076: return (Constraint) this .constraints.get(connection);
077: }
078: throw new UnsupportedOperationException(
079: "Constraints are "
080: + "only supported with XOR or OR split types, not with: "
081: + getType());
082: }
083:
084: public void setConstraint(final Connection connection,
085: final Constraint constraint) {
086: if (this .type == TYPE_OR || this .type == TYPE_XOR) {
087: if (connection == null) {
088: throw new IllegalArgumentException("connection is null");
089: }
090: if (!getOutgoingConnections().contains(connection)) {
091: throw new IllegalArgumentException(
092: "connection is unknown:" + connection);
093: }
094: this .constraints.put(connection, constraint);
095: } else {
096: throw new UnsupportedOperationException(
097: "Constraints are "
098: + "only supported with XOR or OR split types, not with type:"
099: + getType());
100: }
101: }
102:
103: public Map getConstraints() {
104: if (this .type == TYPE_OR || this .type == TYPE_XOR) {
105: return Collections.unmodifiableMap(this .constraints);
106: }
107: throw new UnsupportedOperationException(
108: "Constraints are "
109: + "only supported with XOR or OR split types, not with: "
110: + getType());
111: }
112:
113: public Connection getFrom() {
114: if (getIncomingConnections().size() > 0) {
115: return (Connection) getIncomingConnections().get(0);
116: }
117: return null;
118: }
119:
120: protected void validateAddIncomingConnection(
121: final Connection connection) {
122: super .validateAddIncomingConnection(connection);
123: if (getIncomingConnections().size() > 0) {
124: throw new IllegalArgumentException(
125: "A split cannot have more than one incoming connection");
126: }
127: }
128:
129: protected void validateAddOutgoingConnection(
130: final Connection connection) {
131: super .validateAddOutgoingConnection(connection);
132: if (connection.getType() != Connection.TYPE_NORMAL) {
133: throw new IllegalArgumentException(
134: "Unknown connection type :"
135: + connection.getType()
136: + ", only NORMAL is allowed as outgoing connection.");
137: }
138: }
139:
140: public void removeOutgoingConnection(final Connection connection) {
141: super.removeOutgoingConnection(connection);
142: this.constraints.remove(connection);
143: }
144: }
|