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.conflict;
18:
19: import org.drools.reteoo.ReteTuple;
20: import org.drools.spi.Activation;
21: import org.drools.spi.ConflictResolver;
22:
23: /**
24: * A conflict resolver that compares the total recency of a tuple when
25: * determining firing order.
26: *
27: * @author <a href="mailto:tirelli@post.com">Edson Tirelli</a>
28: *
29: * @version $Id: TotalRecencyConflictResolver.java 13028 2007-07-03 04:01:41Z fmeyer $
30: */
31: public class TotalRecencyConflictResolver extends
32: AbstractConflictResolver {
33: // ----------------------------------------------------------------------
34: // Class members
35: // ----------------------------------------------------------------------
36:
37: /**
38: *
39: */
40: private static final long serialVersionUID = 400L;
41: /** Singleton instance. */
42: private static final TotalRecencyConflictResolver INSTANCE = new TotalRecencyConflictResolver();
43:
44: // ----------------------------------------------------------------------
45: // Class methods
46: // ----------------------------------------------------------------------
47:
48: /**
49: * Retrieve the singleton instance.
50: *
51: * @return The singleton instance.
52: */
53: public static ConflictResolver getInstance() {
54: return TotalRecencyConflictResolver.INSTANCE;
55: }
56:
57: // ----------------------------------------------------------------------
58: // Constructors
59: // ----------------------------------------------------------------------
60:
61: /**
62: * Construct.
63: */
64: public TotalRecencyConflictResolver() {
65: // intentionally left blank
66: }
67:
68: // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
69:
70: /**
71: * @see ConflictResolver
72: */
73: public int compare(final Activation lhs, final Activation rhs) {
74: long leftRecency = 0;
75: long rightRecency = 0;
76: if (lhs.getTuple() instanceof ReteTuple) {
77: leftRecency = (lhs.getTuple()).getRecency();
78: }
79: if (rhs.getTuple() instanceof ReteTuple) {
80: rightRecency = (rhs.getTuple()).getRecency();
81: }
82: return (rightRecency > leftRecency) ? 1
83: : (rightRecency < leftRecency) ? -1 : 0;
84: }
85:
86: }
|