/** * Prepare the rootNode and it's child nodes so that we can get matchedString by index */ private void buildRootNode() { if (isTransactionNodeBuilt) return; isTransactionNodeBuilt = true; rootNode = new Node(); rootNode.setNbrChar(1); List<Node> nextNodes = prepareTransactionNodes(automaton.getInitialState()); rootNode.setNextNodes(nextNodes); rootNode.updateNbrMatchedString(); }
private String buildStringFromNode(Node node, int indexOrder) { String result = ""; long passedStringNbr = 0; long step = node.getNbrMatchedString() / node.getNbrChar(); for (char usedChar = node.getMinChar(); usedChar <= node.getMaxChar(); ++usedChar) { passedStringNbr += step; if (passedStringNbr >= indexOrder) { passedStringNbr -= step; indexOrder -= passedStringNbr; result = result.concat("" + usedChar); break; } } long passedStringNbrInChildNode = 0; if (result.length() == 0) passedStringNbrInChildNode = passedStringNbr; for (Node childN : node.getNextNodes()) { passedStringNbrInChildNode += childN.getNbrMatchedString(); if (passedStringNbrInChildNode >= indexOrder) { passedStringNbrInChildNode -= childN.getNbrMatchedString(); indexOrder -= passedStringNbrInChildNode; result = result.concat(buildStringFromNode(childN, indexOrder)); break; } } return result; }
/** * Calculate the number of string that will be generated until the * transaction presented by this node, and set the result in * <code>nbrMatchedString</code>. */ public void updateNbrMatchedString() { if (isNbrMatchedStringUpdated) return; if (nextNodes.size() == 0) nbrMatchedString = nbrChar; else for (Node childNode : nextNodes) { childNode.updateNbrMatchedString(); long childNbrChar = childNode.getNbrMatchedString(); nbrMatchedString += nbrChar * childNbrChar; } isNbrMatchedStringUpdated = true; }
/** * Build list of nodes that present possible transactions from the <code>state</code>. * * @param state * @return */ private List<Node> prepareTransactionNodes(State state) { List<Node> transactionNodes = new ArrayList<Node>(); if (preparedTransactionNode == Integer.MAX_VALUE / 2) return transactionNodes; ++ preparedTransactionNode; if (state.isAccept()) { Node acceptedNode = new Node(); acceptedNode.setNbrChar(1); transactionNodes.add(acceptedNode); } List<Transition> transitions = state.getSortedTransitions(true); for (Transition transition : transitions) { Node trsNode = new Node(); int nbrChar = transition.getMax() - transition.getMin() + 1; trsNode.setNbrChar(nbrChar); trsNode.setMaxChar(transition.getMax()); trsNode.setMinChar(transition.getMin()); List<Node> nextNodes = prepareTransactionNodes(transition.getDest()); trsNode.setNextNodes(nextNodes); transactionNodes.add(trsNode); } return transactionNodes; }
/** * @return first string in lexicographical order that is matched by the given pattern. */ public String getFirstMatch() { buildRootNode(); Node node = rootNode; String result = ""; while (node.getNextNodes().size() > 0) { result = result.concat("" + node.getMinChar()); node = node.getNextNodes().get(0); } result = result.substring(1); return result; }
/** * @return the number of strings that are matched by the given pattern. * @throws StackOverflowError if the given pattern generates a large, possibly infinite, number of strings. */ public long matchedStringsSize() { buildRootNode(); return rootNode.getNbrMatchedString(); }
/** * Build list of nodes that present possible transactions from the <code>state</code>. * * @param state * @return */ private List<Node> prepareTransactionNodes(State state) { List<Node> transactionNodes = new ArrayList<Node>(); if (preparedTransactionNode == Integer.MAX_VALUE / 2) return transactionNodes; ++preparedTransactionNode; if (state.isAccept()) { Node acceptedNode = new Node(); acceptedNode.setNbrChar(1); transactionNodes.add(acceptedNode); } List<Transition> transitions = state.getSortedTransitions(true); for (Transition transition : transitions) { Node trsNode = new Node(); int nbrChar = transition.getMax() - transition.getMin() + 1; trsNode.setNbrChar(nbrChar); trsNode.setMaxChar(transition.getMax()); trsNode.setMinChar(transition.getMin()); List<Node> nextNodes = prepareTransactionNodes(transition.getDest()); trsNode.setNextNodes(nextNodes); transactionNodes.add(trsNode); } return transactionNodes; }
/** * @return first string in lexicographical order that is matched by the given pattern. */ public String getFirstMatch() { buildRootNode(); Node node = rootNode; String result = ""; while (node.getNextNodes().size() > 0) { result = result.concat("" + node.getMinChar()); node = node.getNextNodes().get(0); } result = result.substring(1); return result; }
/** * Calculate the number of string that will be generated until the * transaction presented by this node, and set the result in * <code>nbrMatchedString</code>. */ public void updateNbrMatchedString() { if (isNbrMatchedStringUpdated) return; if (nextNodes.size() == 0) nbrMatchedString = nbrChar; else for (Node childNode : nextNodes) { childNode.updateNbrMatchedString(); long childNbrChar = childNode.getNbrMatchedString(); nbrMatchedString += nbrChar * childNbrChar; } isNbrMatchedStringUpdated = true; }
/** * @return the number of strings that are matched by the given pattern. * @throws StackOverflowError * if the given pattern generates a large, possibly infinite, number of strings. */ public long matchedStringsSize() { buildRootNode(); return rootNode.getNbrMatchedString(); }
private String buildStringFromNode(Node node, int indexOrder) { String result = ""; long passedStringNbr = 0; long step = node.getNbrMatchedString() / node.getNbrChar(); for (char usedChar = node.getMinChar(); usedChar <= node.getMaxChar(); ++ usedChar) { passedStringNbr += step; if (passedStringNbr >= indexOrder) { passedStringNbr -= step; indexOrder -= passedStringNbr; result = result.concat("" + usedChar); break; } } long passedStringNbrInChildNode = 0; if (result.length() == 0) passedStringNbrInChildNode = passedStringNbr; for (Node childN : node.getNextNodes()) { passedStringNbrInChildNode += childN.getNbrMatchedString(); if (passedStringNbrInChildNode >= indexOrder) { passedStringNbrInChildNode -= childN.getNbrMatchedString(); indexOrder -= passedStringNbrInChildNode; result = result.concat(buildStringFromNode(childN, indexOrder)); break; } } return result; }
/** * Prepare the rootNode and it's child nodes so that we can get matchedString by index */ private void buildRootNode() { if (isTransactionNodeBuilt) return; isTransactionNodeBuilt = true; rootNode = new Node(); rootNode.setNbrChar(1); List<Node> nextNodes = prepareTransactionNodes(automaton.getInitialState()); rootNode.setNextNodes(nextNodes); rootNode.updateNbrMatchedString(); }