//todo: check for the correctness // In my opinion, this is correct public BitSet getBitSetObject() { BitSet bs = new BitSet(); for (int i = 0; i < 32; i++) { int mask = 1 << i; if (mask <= bitset) { if ((bitset & mask) != 0) { bs.Add(true); } else { bs.Add(false); } } else { break; } } return bs; }
public NBA(APSet apset) { //_state_count = 0; _apset = apset; _start_state = null; //ly: added to the source _index = new List<NBA_State>(); _final_states = new BitSet(); }
/** * Constructor * @param id the name of the node */ public SafraTreeNode(int id) { this._id = id; //_final_flag(false), // _parent(0), //_olderBrother(0), //_youngerBrother(0), //_oldestChild(0), //_youngestChild(0), //_childCount(0) _labeling = new BitSet(); }
/** Get the signature for this state */ public RabinSignature getSignature() { BitSet Larray = _acceptance.getAcceptance_L_forState(_state_index); BitSet Uarray = _acceptance.getAcceptance_U_forState(_state_index); BitSet llist = new BitSet(Larray); BitSet ulist = new BitSet(Uarray); //foreach (bool bit in Larray.bitset) //{ // llist.Add(bit); //} //foreach (bool bit in Uarray.bitset) //{ // ulist.Add(bit); //} return new RabinSignature(llist, ulist, _acceptance.size()); }
/** Node visitor */ public void visit(SafraTree tree, SafraTreeNode node) { if (node.getChildCount() <= 1) { return; } BitSet already_seen = new BitSet(); bool first = true; //for (SafraTreeNode::child_iterator it=node->children_begin();it!=node->children_end();++it) SafraTreeNode it = node.children_begin(); while (it != node.children_end()) { SafraTreeNode cur_child = it; if (first) { already_seen = new BitSet(cur_child.getLabeling());////////////get the NBA states in child first = false; it = it.increment();////////note added } else { BitSet current = new BitSet(cur_child.getLabeling()); BitSet intersection = new BitSet(already_seen); // make copy if (intersection.intersects(current)) { // There are some labels, which occur in older brothers, // remove them from current node and its children STVisitor_subtract_labeling stv_sub = new STVisitor_subtract_labeling(intersection); tree.walkSubTreePostOrder(stv_sub, cur_child); } already_seen.Union(current); it = it.increment(); } } }
/** Calculate the BitSet for a state from the acceptance pairs, store * result in result. * @param state_index the state * @param acc the BitSetVector (either _L or _U) * @param result the Bitset where the results are stored, has to be clear * at the beginning! */ private void getBitSetForState(int state_index, List<BitSet> acc, BitSet result) { for (int i = 0; i < acc.Count; i++) { if (acc[i] != null) { if (acc[i].get(state_index)) { //result[i] = true; result.set(i); } } } }
// ---- Rabin/Streett acceptance specific /** * Creates a new acceptance pair. * @return the index of the new acceptance pair. */ public int newAcceptancePair() { BitSet l = new BitSet(); BitSet u = new BitSet(); _acceptance_L.Add(l); _acceptance_U.Add(u); _acceptance_count++; return _acceptance_L.Count - 1; }
/** * Get the U part of the acceptance signature for a state (changes to the * BitSet do not affect the automaton). */ public BitSet getAcceptance_U_forState(int state_index) { BitSet result = new BitSet(_acceptance_U.Count, false); getBitSetForState(state_index, _acceptance_U, result); return result; }
/** * Constructor * @param nba_states_with_all_succ_final A BitSet with the indizes of the * NBA states that only have accepting (final) * successors. * @param tree_template SafraTreeTemplate to keep track of removed nodes */ public STVisitor_check_for_succ_final(BitSet nba_states_with_all_succ_final, SafraTreeTemplate tree_template) { _success = false; _nba_states_with_all_succ_final = nba_states_with_all_succ_final; _tree_template = tree_template; }
/** */ public void visit(SafraTree tree, SafraTreeNode node) { if (_final_states.intersects(node.getLabeling()))//////////if this node has the state in accepting states of NBA { BitSet q_and_f = new BitSet(_final_states); q_and_f.Intersect(node.getLabeling());////////////////get the intersect of the label and the accepting states SafraTreeNode new_child = tree.newNode(); node.addAsYoungestChild(new_child); _tree_template.setRenameable(new_child.getID()); new_child.getLabeling().Assign(q_and_f); } }
/** Calculate the stutter closure for the NBA, for all symbols. * @param nba the NBA */ public static NBA stutter_closure(NBA nba) { APSet apset = nba.getAPSet_cp(); NBA nba_result_ptr = new NBA(apset); NBA result = nba_result_ptr; int element_count = apset.powersetSize(); Debug.Assert(nba.getStartState() != null); int start_state = nba.getStartState().getName(); for (int i = 0; i < nba.size(); i++) { int st = result.nba_i_newState(); Debug.Assert(st == i); if (st == start_state) { result.setStartState(result[st]); } if (nba[st].isFinal()) { result[st].setFinal(true); } } for (int i = 0; i < nba.size(); i++) { for (int j = 0; j < element_count; j++) { int st = result.nba_i_newState(); Debug.Assert(st == nba.size() + (i*element_count) + j); result[st].addEdge(new APElement(j), result[i]); result[st].addEdge(new APElement(j), result[st]); } } List<List<BitSet>> reachable = new List<List<BitSet>>(); //reachable.resize(element_count); Ultility.resize(reachable, element_count); for (int j = 0; j < element_count; j++) { //NBAEdgeSuccessors edge_successor = new NBAEdgeSuccessors(new APElement(j)); SCCs scc = new SCCs(); GraphAlgorithms.calculateSCCs(nba, scc, true, new APElement(j)); //,edge_successor reachable[j] = scc.getReachabilityForAllStates(); #if VERBOSE std::cerr << "SCCs for " << APElement(j).toString(*apset) << std::endl; std::cerr << scc << std::endl; std::cerr << " Reachability: "<< std::endl; std::vector<BitSet>& reach=*reachable[j]; for (unsigned int t=0; t < reach.size(); t++) { std::cerr << t << " -> " << reach[t] << std::endl; } std::cerr << " ---\n"; #endif } for (int i = 0; i < nba.size(); i++) { NBA_State from = result[i]; for (int j = 0; j < element_count; j++) { BitSet result_to = new BitSet(); BitSet to = nba[i].getEdge(new APElement(j)); //for (BitSetIterator it=BitSetIterator(*to);it!=BitSetIterator::end(*to);++it) for (int it = BitSetIterator.start(to); it != BitSetIterator.end(to); it = BitSetIterator.increment(to, it)) { int to_state = it; // We can go directly to the original state result_to.set(to_state); // We can also go to the corresponding stutter state instead int stutter_state = nba.size() + (to_state*element_count) + j; result_to.set(stutter_state); // ... and then we can go directly to all the states that are j-reachable from to result_to.Union(reachable[j][to_state]); } from.getEdge(new APElement(j)).Assign(result_to); } } //for (int i=0; i<reachable.size(); ++i) { // delete reachable[i]; // } return nba_result_ptr; }
/** Constructor * @param other another RabinSignature */ public RabinSignature(RabinSignature other) { //_L(other._L), _U(other._U), _size(other._size) this._L = new BitSet(other._L); this._U = new BitSet(other._U); this._size = other._size; }
/** Constructor for getting the acceptance signature for a Tree. * @param tree the Tree, get acceptance signature from * tree.generateAcceptance(*this). */ //template <typename Tree> public RabinSignature(StateInterface tree) { //added by ly this._L = new BitSet(); this._U = new BitSet(); tree.generateAcceptance(this); _size = 0; }
/** * Constructor * nba_reachability A vector of BitSets (state index -> BitSet) of states * in the NBA that are reachable from a state. * N the maximum number of nodes in the Safra tree */ public STVisitor_reorder_children(List<BitSet> nba_reachability, int N) { _nba_reachability = nba_reachability; _n = N; _node_order = new int[N]; _node_reachability = new BitSet[N]; //added by ly for (int i = 0; i < N; i++) { _node_reachability[i] = new BitSet(); } }
private static LEG CMP(BitSet a, BitSet b) { if (a < b) { return LEG.LESS; } else { if (a == b) { return LEG.EQUAL; } else { return LEG.GREATER; } } }
/** Node visitor */ public void visit(SafraTree tree, SafraTreeNode node) { if (node.getChildCount() == 0) { return; } BitSet labeling_union = new BitSet(); //for (SafraTreeNode::child_iterator it=node->children_begin();it!=node->children_end();++it) SafraTreeNode it = node.children_begin(); while (it != node.children_end()) { labeling_union.Union(it.getLabeling()); it = it.increment(); } if (labeling_union == node.getLabeling()) { // The union of the labelings of the children is exactly the // same as the labeling of the parent -> // remove children STVisitor_remove_subtree stv_remove = new STVisitor_remove_subtree(_tree_template); tree.walkChildrenPostOrder(stv_remove, node); node.setFinalFlag(true);///////////should be "+ i", means in Li } }
public STVisitor_subtract_labeling(BitSet bitset) { _bitset = bitset; }
/** Node visitor */ public void visit(SafraTree tree, SafraTreeNode node) { BitSet new_labeling = new BitSet(); BitSet old_labeling = node.getLabeling(); for (int i = old_labeling.nextSetBit(0); i != -1; i = old_labeling.nextSetBit(i + 1)) { new_labeling.Union(_nba[i].getEdge(_elem));//////////////generate new label. } node.getLabeling().Assign(new_labeling); }
/** * Move the bits set in acc to the places specified by mapping. */ private void move_acceptance_bits(BitSet acc, List<int> mapping) { int i = 0; while (i != -1) { int j = mapping[i]; // :: j is always <= i if (j > i) { throw new Exception("Wrong mapping in move_acceptance_bits"); } if (i == j) { // do nothing } else { // move bit from i->j //acc[j] = true; //acc[i] = false; acc.set(j); acc.clear(i); } //i=acc.nextSetBit(i+1); //i++; i = acc.nextSetBit(i + 1); } }
/** Set the number of acceptance pairs */ public void setSize(int size) { _size=size; //added by ly this._L = new BitSet(size, false); this._U = new BitSet(size, false); }
/** Constructor * @param size the number of acceptance pairs */ public RabinSignature(int size) { _size = size; //added by ly this._L = new BitSet(size, false); this._U = new BitSet(size, false); }
/** * Constructor. * @param final_states the states that are accepting (final) in the NBA * @param tree_template the tree template to keep track of new nodes */ public STVisitor_check_finalset(BitSet final_states, SafraTreeTemplate tree_template) { _final_states = final_states; _tree_template = tree_template; }
/** Constructor * @param L the L part of the acceptance signature. * @param U the U part of the acceptance signature. * @param size the number of acceptance pairs */ public RabinSignature(BitSet L, BitSet U, int size) { // : _L(L), _U(U), _size(size) this._L = new BitSet(L); this._U = new BitSet(U); this._size = size; }
public static NBA product_automaton(NBA nba_1, NBA nba_2) { Debug.Assert(nba_1.getAPSet() == nba_2.getAPSet()); NBA product_nba = new NBA(nba_1.getAPSet_cp()); APSet apset = nba_1.getAPSet(); Debug.Assert(apset == nba_2.getAPSet()); for (int s_1 = 0; s_1 < nba_1.size(); s_1++) { for (int s_2 = 0; s_2 < nba_2.size(); s_2++) { for (int copy = 0; copy < 2; copy++) { int s_r = product_nba.nba_i_newState(); Debug.Assert(s_r == (s_1 * nba_2.size() + s_2) * 2 + copy); int to_copy = copy; if (copy == 0 && nba_1[s_1].isFinal()) { to_copy = 1; } if (copy == 1 && nba_2[s_2].isFinal()) { product_nba[s_r].setFinal(true); to_copy = 0; } //for (typename APSet::element_iterator it=apset.all_elements_begin();it!=apset.all_elements_end();++it) for (int it = apset.all_elements_begin(); it != apset.all_elements_end(); it++) { APElement label = new APElement(it); BitSet to_s1 = nba_1[s_1].getEdge(label); BitSet to_s2 = nba_2[s_2].getEdge(label); BitSet to_set = new BitSet(); //for (BitSetIterator it_e_1 = BitSetIterator(*to_s1); it_e_1 != BitSetIterator::end(*to_s1); ++it_e_1) //for (int it_e_1 = 0; it_e_1 != to_s1.bitset.Count; ++it_e_1) for (int it_e_1 = BitSetIterator.start(to_s1); it_e_1 != BitSetIterator.end(to_s1); it_e_1 = BitSetIterator.increment(to_s1, it_e_1)) { //for (BitSetIterator it_e_2 = BitSetIterator(*to_s2); it_e_2 != BitSetIterator::end(*to_s2); ++it_e_2) //for (int it_e_2 = 0; it_e_2 != to_s2.bitset.Count; ++it_e_2) for (int it_e_2 = BitSetIterator.start(to_s2); it_e_2 != BitSetIterator.end(to_s2); it_e_2 = BitSetIterator.increment(to_s2, it_e_2)) { int to = it_e_1 * nba_2.size() + it_e_2 * 2 + to_copy; to_set.set(to); } } product_nba[s_r].getEdge(label).Assign(to_set); } } } } int start_1 = nba_1.getStartState().getName(); int start_2 = nba_2.getStartState().getName(); product_nba.setStartState(product_nba[start_1 * nba_2.size() + start_2]); return product_nba; }
/** Visit a state (perform DFS) */ public void visit(int v) { SCC_DFS_Data sdd = new SCC_DFS_Data(); sdd.dfs_nr = current_dfs_nr++; sdd.root_index = v; sdd.inComponent = false; _stack.Push(v); //todo: be careful of the following swap //_dfs_data[v].reset(sdd); //Ultility.swap(_dfs_data[v], sdd); _dfs_data[v] = sdd; //for (typename SuccessorAccess::successor_iterator succ_it=_successor_access.begin(_graph, v);succ_it!=_successor_access.end(_graph, v); ++succ_it) if(_labelMark == null) { for (KeyValuePair<APElement, BitSet> it_set = _graph[v].edges_begin(); !_graph[v].edges_end(); it_set = _graph[v].increment()) { for (int succ_it = BitSetIterator.start(it_set.Value); succ_it != BitSetIterator.end(it_set.Value); succ_it = BitSetIterator.increment(it_set.Value, succ_it)) { int w = succ_it; if (_dfs_data[w] == null) //.get() { // not yet visited visit(w); } SCC_DFS_Data sdd_w = _dfs_data[w]; //.get(); if (sdd_w.inComponent == false) { int dfs_nr_root_v = _dfs_data[sdd.root_index].dfs_nr; int dfs_nr_root_w = _dfs_data[sdd_w.root_index].dfs_nr; if (dfs_nr_root_v > dfs_nr_root_w) { sdd.root_index = sdd_w.root_index; } } } } } else { BitSet it_set = _graph[v].getEdge(_labelMark); for (int succ_it = BitSetIterator.start(it_set); succ_it != BitSetIterator.end(it_set); succ_it = BitSetIterator.increment(it_set, succ_it)) { int w = succ_it; if (_dfs_data[w] == null) //.get() { // not yet visited visit(w); } SCC_DFS_Data sdd_w = _dfs_data[w]; //.get(); if (sdd_w.inComponent == false) { int dfs_nr_root_v = _dfs_data[sdd.root_index].dfs_nr; int dfs_nr_root_w = _dfs_data[sdd_w.root_index].dfs_nr; if (dfs_nr_root_v > dfs_nr_root_w) { sdd.root_index = sdd_w.root_index; } } } } if (sdd.root_index == v) { BitSet set = new BitSet(); int w; do { //w=_stack.Peek(); //.top(); w = _stack.Pop(); set.set(w); _result.setState2SCC(w, scc_nr); SCC_DFS_Data sdd_w = _dfs_data[w];//.get(); sdd_w.inComponent = true; } while (w != v); scc_nr = _result.addSCC(set) + 1; } }
/** Add a new SCC */ public int addSCC(BitSet scc) { _sccs.Add(scc); return _sccs.Count - 1; }
/** Calculate the stutter closure for the NBA, for a certain symbol. * @param nba the NBA * @param label the symbol for which to perform the stutter closure */ public static NBA stutter_closure(NBA nba, APElement label) { APSet apset = nba.getAPSet_cp(); NBA nba_result_ptr = new NBA(apset); NBA result = nba_result_ptr; int element_count = apset.powersetSize(); Debug.Assert(nba.getStartState() != null); int start_state = nba.getStartState().getName(); for (int i = 0; i < nba.size(); i++) { int st = result.nba_i_newState(); Debug.Assert(st == i); if (st == start_state) { result.setStartState(result[st]); } if (nba[st].isFinal()) { result[st].setFinal(true); } } for (int i = 0; i < nba.size(); i++) { int st = result.nba_i_newState(); Debug.Assert(st == nba.size() + i); result[st].addEdge(label, result[i]); result[st].addEdge(label, result[st]); } //List<BitSet> reachable = null; //NBAEdgeSuccessors edge_successor = new NBAEdgeSuccessors(label); SCCs scc = new SCCs(); GraphAlgorithms.calculateSCCs(nba, scc, true, label); //,edge_successor List<BitSet> reachable = scc.getReachabilityForAllStates(); // std::cerr << "SCCs for " << label.toString(*apset) << std::endl; // std::cerr << scc << std::endl; // std::cerr << " Reachability: "<< std::endl; // for (unsigned int t=0; t < reachable->size(); t++) { // std::cerr << t << " -> " << (*reachable)[t] << std::endl; // } // std::cerr << " ---\n"; for (int i = 0; i < nba.size(); i++) { NBA_State from = result[i]; for (int j = 0; j < element_count; j++) { BitSet result_to = new BitSet(); BitSet to = nba[i].getEdge(new APElement(j)); if (j != label.bitset) { result_to = to; } else { //for (BitSetIterator it=BitSetIterator(*to);it!=BitSetIterator::end(*to);++it) for (int it = BitSetIterator.start(to); it != BitSetIterator.end(to); it = BitSetIterator.increment(to, it)) { int to_state = it; // We can go directly to the original state result_to.set(to_state); // We can also go to the corresponding stutter state instead int stutter_state = nba.size() + to_state; result_to.set(stutter_state); // ... and then we can go directly to all the states that are j-reachable from to result_to.Union(reachable[to_state]); } } from.getEdge(new APElement(j)).Assign(result_to); } } //delete reachable; return nba_result_ptr; }
public List<BitSet> getReachabilityForAllStates() { //std::vector<BitSet>* v=new std::vector<BitSet>; //v->resize(_state_to_scc.size()); List<BitSet> v = new List<BitSet>(); Ultility.resizeExact(v, _state_to_scc.Count); for (int i = 0; i < _state_to_scc.Count; ++i) { int scc = state2scc(i); BitSet reachable_sccs = _reachability[scc]; BitSet reachable_states = new BitSet(); //for (BitSetIterator it(reachable_sccs); it != BitSetIterator::end(reachable_sccs); ++it) for (int it = BitSetIterator.start(reachable_sccs); it != BitSetIterator.end(reachable_sccs); it = BitSetIterator.increment(reachable_sccs, it)) { // union with all states from the reachable scc reachable_states.Union(_sccs[it]); } v[i] = reachable_states; //std::cerr << "from "<<i<<": "<<reachable_states<<std::endl; } return v; }
/** Type of an acceptance signature */ // public KeyValuePair<BitSet, BitSet> acceptance_signature_t; /** * Constructor, fills the container with the acceptance signatures of the states. * @param dra the DRA */ public AcceptanceSignatureContainer(DRA dra) { //_acceptancesig_vector.resize(dra.size()); _acceptancesig_vector = new List<KeyValuePair<BitSet, BitSet>>(); //Ultility.resize(_acceptancesig_vector, dra.size()); _bitsets = new List<BitSet>(); for (int i = 0; i < dra.size(); i++) { BitSet b = dra.acceptance().getAcceptance_L_forState(i); BitSet bp = new BitSet(b); _bitsets.Add(bp); //push_back // _acceptancesig_vector[i].Key = bp; b = dra.acceptance().getAcceptance_U_forState(i); BitSet bp1 = new BitSet(b); _bitsets.Add(bp1); // //_acceptancesig_vector[i].Value = bp1; _acceptancesig_vector.Add(new KeyValuePair<BitSet, BitSet>(bp, bp1)); } }
/** Perform set operation Union */ public void Union(BitSet other) { this.Or(other); }