source: fedd/abac-src/ttg/process/Strategy.java @ 181cf9c

axis_examplecompt_changesinfo-opsversion-2.00version-3.01version-3.02
Last change on this file since 181cf9c was 8780cbec, checked in by Jay Jacobs <Jay.Jacobs@…>, 15 years ago

ABAC sources from Cobham

  • Property mode set to 100644
File size: 5.9 KB
Line 
1package com.nailabs.abac.process;
2
3import com.nailabs.abac.trust.*;
4import com.nailabs.abac.test.*;
5import java.util.*;
6/**
7 * The strategy handles processing a working list of nodes in the trust target
8 * graph to be processed.
9 */
10public class Strategy extends Observable {
11    /** The context for which this strategy is negotiating */
12    protected NegotiationContext context;
13
14    /** The worklist for nodes to be processed */
15    private LinkedList workList = new LinkedList();
16
17    private SatisfactionState state = 
18        new SatisfactionState(SatisfactionState.UNKNOWN);
19
20    /** The last message sent out */
21    protected Message lastMessage = null;
22
23    /** The current message just received */
24    protected Message currentMessage = null;
25
26    /** creates a new strategy to process a trust negotiation */
27    public Strategy(NegotiationContext context) {
28        this.context = context;
29        //context.setStrategy(this);
30    }
31
32    /** Adds a node to the work list of nodes to be processed */
33    public void addToWorklist(TTGNode node) {
34        if(node != null && workList.add(node)) {
35            debug("worklist", node.getGoal() + " added to worklist");
36            debug("strategy", "worklist = " + workList);
37        }
38    }
39
40    /** Adds a set of node to the work list */
41    public void addToWorklist(TTGNodeSet list) {
42        if(list == null)return;
43        Iterator i = list.values().iterator();
44        while(i.hasNext()) {
45            try {
46                TTGNode node = (TTGNode)i.next();
47                addToWorklist(node);
48            }
49            catch(Exception ex) {
50                ex.printStackTrace(System.err);
51            }
52        }
53    }
54
55    /** determine the current state of negotiation */
56    public int getSatisfactionState() {
57        int state = SatisfactionState.UNKNOWN;
58        try {
59            state = context.getGraph().getRoot().getSatisfactionValue();
60        }
61        catch(Exception ex) {
62            debug("strategy", "graph = " + context.getGraph());
63            debug("strategy", "root = " + context.getGraph().getRoot());
64            ex.printStackTrace(System.err);
65        }
66        if(isStuck() && (state != SatisfactionState.SATISFIED)) {
67            debug("strategy", "Strategy: is stuck!");
68            return SatisfactionState.FAILED;
69        }
70        return state;
71    }
72    /** process nodes on the work list until the list is empty */
73    public SatisfactionState process(Message msg) {
74        TTG graph = context.getGraph();
75        int state = getSatisfactionState();
76
77        addMessage(msg);
78
79        if(state == SatisfactionState.SATISFIED || 
80           state == SatisfactionState.FAILED) {
81        debug("strategy", "negotiation is complete status: " + state);
82            return new SatisfactionState(state);
83        }
84        if(isStuck()) {
85        debug("strategy", "negotiation is stuck!");
86            return new SatisfactionState(SatisfactionState.FAILED);
87        }
88        iterate();
89    addMessage(graph.getChanges());
90        if(isStuck()) {
91        debug("strategy", "negotiation is stuck!");
92            return new SatisfactionState(SatisfactionState.FAILED);
93        }
94        return new SatisfactionState(state); 
95    }
96
97    /** iterate through the work list until it is empty */
98    public void iterate() {
99        debug("worklist", "worklist = " + workList);
100        setChanged();
101        notifyObservers("location = local");
102        //while worklist is not empty
103        while(!workList.isEmpty()) {
104            //check for success or failure
105            if(getSatisfactionState() != SatisfactionState.UNKNOWN) {
106                return;
107            }
108            //process node at head of list
109            //add list of nodes returned from process to the worklist
110            TTGNode current = ((TTGNode)workList.removeFirst());
111            setCurrentParent(current);
112            current.process(context);
113            scheduleOperationsForNode(current);
114            debug("strategy", "processing node " + current.getGoal());
115            debug("worklist", "worklist = " + workList);
116        }
117    }
118
119    /**
120     * Notify the strategy that a new edge needs to be added to the graph.
121     * The strategy is responsible for performing the operation on the graph,
122     * whether the strategy chooses to  defer the operation or not.
123     */
124    public void scheduleOperation(Operation op) {
125        if(op instanceof EdgeOperation) {
126            context.getGraph().performEdge((EdgeOperation)op);
127        } else {
128            context.getGraph().performNode((NodeOperation)op);
129        }
130       
131    }
132
133    /** Place-holder for stingy strategy. */
134    public void addSatisfiedControlChild(TTGNode node) {
135
136    }
137
138
139    /** Place-holder for stingy strategy */
140    public void addNewNode(TTGNode node) {
141       
142    }
143
144    /**
145     * convenience method for determining whether this negotiator has
146     * a deadlock in processing the negotiation. This method can be
147     * overridden is subsequent versions.
148     */
149    protected boolean isStuck() {
150        // if there's no history, we cannot be stuck yet
151        if(currentMessage == null)
152            return false;
153        // if both messages contain no changes, then a deadlock has occurred
154        return (currentMessage.getOperationsCount() == 0 );
155   }
156
157    /**
158     * add the current message to the queue. Note: the initial message will
159     * also be null.
160     */
161    protected void addMessage(Message msg) {
162        lastMessage = currentMessage;
163        currentMessage = msg;
164    }
165
166
167    /** Note the beginning of a new parent node being processed */
168    protected void setCurrentParent(TTGNode parent) {
169       
170    }
171
172    /** Note the conclusion of a new parent node being processed */
173    protected void scheduleOperationsForNode(TTGNode parent) {
174
175    }
176
177    /** */
178    protected void debug(String key, String message) {
179        StringBuffer buff = new StringBuffer("Strategy[");
180        buff.append(context.getSelf()).append("]: ").append(message);
181        Debug.debug(key, buff.toString());
182    }
183
184    /** */
185    public void addObserver(Observer o) {
186        super.addObserver(o);
187        setChanged();
188        notifyObservers("location = init");
189    }
190
191}
Note: See TracBrowser for help on using the repository browser.