01: /*
02: * Copyright 2005 JBoss Inc
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License");
05: * you may not use this file except in compliance with the License.
06: * You may obtain a copy of the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS,
12: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13: * See the License for the specific language governing permissions and
14: * limitations under the License.
15: */
16:
17: package org.drools.rule;
18:
19: import java.util.Collection;
20: import java.util.Collections;
21: import java.util.Map;
22:
23: import org.drools.RuntimeDroolsException;
24: import org.drools.base.ClassObjectType;
25:
26: /**
27: * @author etirelli
28: *
29: */
30: public class Collect extends ConditionalElement implements
31: PatternSource {
32:
33: private static final long serialVersionUID = 400L;
34:
35: private Pattern sourcePattern;
36: private Pattern resultPattern;
37:
38: public Collect(final Pattern sourcePattern,
39: final Pattern resultPattern) {
40:
41: this .sourcePattern = sourcePattern;
42: this .resultPattern = resultPattern;
43: }
44:
45: public Object clone() {
46: return new Collect(this .sourcePattern, this .resultPattern);
47: }
48:
49: public Pattern getResultPattern() {
50: return this .resultPattern;
51: }
52:
53: public Pattern getSourcePattern() {
54: return this .sourcePattern;
55: }
56:
57: public Collection instantiateResultObject()
58: throws RuntimeDroolsException {
59: try {
60: // Collect can only be used with a Collection implementation, so
61: // FactTemplateObject type is not allowed
62: return (Collection) ((ClassObjectType) this .resultPattern
63: .getObjectType()).getClassType().newInstance();
64: } catch (final ClassCastException cce) {
65: throw new RuntimeDroolsException(
66: "Collect CE requires a Collection implementation as return type",
67: cce);
68: } catch (final InstantiationException e) {
69: throw new RuntimeDroolsException(
70: "Collect CE requires a non-argument constructor for the return type",
71: e);
72: } catch (final IllegalAccessException e) {
73: throw new RuntimeDroolsException(
74: "Collect CE requires an accessible constructor for the return type",
75: e);
76: }
77: }
78:
79: public Map getInnerDeclarations() {
80: return this .sourcePattern.getInnerDeclarations();
81: }
82:
83: public Map getOuterDeclarations() {
84: return Collections.EMPTY_MAP;
85: }
86:
87: /**
88: * @inheritDoc
89: */
90: public Declaration resolveDeclaration(final String identifier) {
91: return (Declaration) this.sourcePattern.getInnerDeclarations()
92: .get(identifier);
93: }
94: }
|