comparison src/main/gov/nasa/jpf/search/heuristic/SimplePriorityHeuristic.java @ 0:61d41facf527

initial v8 import (history reset)
author Peter Mehlitz <Peter.C.Mehlitz@nasa.gov>
date Fri, 23 Jan 2015 10:14:01 -0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:61d41facf527
1 /*
2 * Copyright (C) 2014, United States Government, as represented by the
3 * Administrator of the National Aeronautics and Space Administration.
4 * All rights reserved.
5 *
6 * The Java Pathfinder core (jpf-core) platform is licensed under the
7 * Apache License, Version 2.0 (the "License"); you may not use this file except
8 * in compliance with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0.
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19 package gov.nasa.jpf.search.heuristic;
20
21 import gov.nasa.jpf.Config;
22 import gov.nasa.jpf.util.Predicate;
23 import gov.nasa.jpf.vm.ThreadInfo;
24 import gov.nasa.jpf.vm.VM;
25
26 /**
27 * a heuristic that is based on static priorities that are determined
28 * at state storage time
29 */
30 public abstract class SimplePriorityHeuristic extends HeuristicSearch {
31
32 StaticPriorityQueue queue;
33
34 protected Predicate<ThreadInfo> aliveThread;
35
36 public SimplePriorityHeuristic (Config config, VM vm) {
37 super(config,vm);
38
39 queue = new StaticPriorityQueue(config);
40
41 aliveThread = new Predicate<ThreadInfo>() {
42 @Override
43 public boolean isTrue (ThreadInfo ti) {
44 return (ti.isAlive());
45 }
46 };
47
48 }
49
50 protected abstract int computeHeuristicValue ();
51
52 protected int computeAstarPathCost (VM vm) {
53 return vm.getPathLength();
54 }
55
56 @Override
57 protected HeuristicState queueCurrentState () {
58 int heuristicValue;
59
60 if (vm.isInterestingState()) {
61 heuristicValue = 0;
62 } else if (vm.isBoringState()) {
63 heuristicValue = Integer.MAX_VALUE;
64
65 } else {
66 heuristicValue = computeHeuristicValue();
67
68 if (useAstar) {
69 // <2do> we probably don't want this for isInteresting/isBoring?
70 heuristicValue += computeAstarPathCost(vm);
71 }
72 }
73
74 PrioritizedState hState = new PrioritizedState(vm,heuristicValue);
75
76 queue.add(hState);
77
78 return hState;
79 }
80
81 @Override
82 protected HeuristicState getNextQueuedState () {
83
84 //HeuristicState hState = queue.pollFirst(); // only Java 1.6
85 //if (isBeanSearch) { queue.clear(); }
86 //return hState;
87
88 if (queue.size() == 0) { // the dreaded Java 1.5 version
89 return null;
90 }
91 HeuristicState hState = queue.first();
92
93 if (isBeamSearch) {
94 queue.clear();
95 } else {
96 queue.remove(hState);
97 }
98
99 return hState;
100 }
101
102 @Override
103 public int getQueueSize() {
104 return queue.size();
105 }
106
107 @Override
108 public boolean isQueueLimitReached() {
109 return queue.isQueueLimitReached();
110 }
111 }