@SuppressWarnings("unchecked") public ResidentConverter(List<ObjectConverter.ColumnInfo> allColumns) throws java.io.IOException { Optional<ObjectConverter.ColumnInfo> column; final java.util.List<ObjectConverter.ColumnInfo> columns = allColumns .stream() .filter( it -> "mixinReference".equals(it.typeSchema) && "Resident_entity".equals(it.typeName)) .collect(Collectors.toList()); columnCount = columns.size(); readers = new ObjectConverter.Reader[columnCount]; for (int i = 0; i < readers.length; i++) { readers[i] = (instance, rdr, ctx) -> StringConverter.skip(rdr, ctx); } final java.util.List<ObjectConverter.ColumnInfo> columnsExtended = allColumns .stream() .filter( it -> "mixinReference".equals(it.typeSchema) && "-ngs_Resident_type-".equals(it.typeName)) .collect(Collectors.toList()); columnCountExtended = columnsExtended.size(); readersExtended = new ObjectConverter.Reader[columnCountExtended]; for (int i = 0; i < readersExtended.length; i++) { readersExtended[i] = (instance, rdr, ctx) -> StringConverter.skip(rdr, ctx); } column = columns.stream().filter(it -> "id".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'id' column in mixinReference Resident_entity. Check if DB is in sync"); __index___id = (int) column.get().order - 1; column = columnsExtended.stream().filter(it -> "id".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'id' column in mixinReference Resident. Check if DB is in sync"); __index__extended_id = (int) column.get().order - 1; column = columns.stream().filter(it -> "birth".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'birth' column in mixinReference Resident_entity. Check if DB is in sync"); __index___birth = (int) column.get().order - 1; column = columnsExtended.stream().filter(it -> "birth".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'birth' column in mixinReference Resident. Check if DB is in sync"); __index__extended_birth = (int) column.get().order - 1; }
/** Unit tests */ public static void main(String[] args) { final Tree targetTree = tree( 1, tree( 2, tree(3, tree(4), tree(3, tree(4), tree(5))), tree(5, tree(6, tree(7), tree(8, tree(9), tree())), tree(10, tree(11), tree()))), tree(12)); System.out.println(" Test Pattern"); checkPattern(targetTree, tree(), Optional.of(targetTree)); checkPattern(targetTree, tree(9), Optional.of(tree(9))); checkPattern(targetTree, tree(12), Optional.of(tree(12))); checkPattern(targetTree, tree(13), Optional.empty()); checkPattern(targetTree, tree(1), Optional.of(targetTree)); checkPattern(targetTree, tree(2, tree(3), tree(5)), Optional.of(targetTree.left())); checkPattern( targetTree, tree(3, tree(4), tree(5)), Optional.of(targetTree.left().left().right())); checkPattern( targetTree, tree(6, tree(), tree(8)), Optional.of(targetTree.left().right().left())); checkPattern(targetTree, tree(6, tree(), tree(7)), Optional.empty()); checkPattern( targetTree, tree(5, tree(6, tree(7), tree(8, tree(9), tree())), tree(10, tree(11), tree())), Optional.of(targetTree.left().right())); }
public void exit() { Alert alert = new Alert(Alert.AlertType.CONFIRMATION); alert.setTitle("Quit?"); alert.setHeaderText("You've selected to quit this program."); alert.setContentText("Are you sure you want to quit?"); Optional<ButtonType> result = alert.showAndWait(); if (result.get() == ButtonType.OK) System.exit(0); }
/** * Search the tree for the first instance of `pattern`, giving preference to the left child. * * <p>`pattern` is a tree, representing a pattern of nodes. `NilNode`s should be considered * wildcards. * * <p>Example patterns ---------------- `Tree()` Matches any tree. * * <p>`Tree(3)` Matches any tree where the root node has a value of `3`. * * <p>`Tree(3, Tree(2), Tree())` Matches any tree where the root node has a value of `3`, and a * left sub-tree with a root value of `2`, and any (or no) right sub-tree. */ static Optional<Tree> find(Tree target, Tree pattern) { if (pattern.isEmpty()) return Optional.of(target); if (target.isEmpty()) return Optional.empty(); if (prefixMatches(target, pattern)) return Optional.of(target); Optional<Tree> leftMatch = find(target.left(), pattern); if (leftMatch.isPresent()) return leftMatch; return find(target.right(), pattern); }
public Collection<String> getArguments() { Collection<String> args = new ArrayList<>(); Optional.ofNullable(username).ifPresent(s -> args.addAll(Arrays.asList("-u", s))); Optional.ofNullable(password).ifPresent(s -> args.addAll(Arrays.asList("-p", s))); Optional.ofNullable(from) .ifPresent(d -> args.addAll(Arrays.asList("-f", d.format(DateTimeFormatter.ISO_DATE)))); return args; }
@Override public Object apply(List<Object> args, Context context) throws ParseException { File outFile = null; String editor = getEditor(); try { outFile = File.createTempFile("stellar_shell", "out"); if (args.size() > 0) { String arg = (String) args.get(0); try (PrintWriter pw = new PrintWriter(outFile)) { IOUtils.write(arg, pw); } } } catch (IOException e) { String message = "Unable to create temp file: " + e.getMessage(); LOG.error(message, e); throw new IllegalStateException(message, e); } Optional<Object> console = context.getCapability(CONSOLE, false); try { PausableInput.INSTANCE.pause(); // shut down the IO for the console ProcessBuilder processBuilder = new ProcessBuilder(editor, outFile.getAbsolutePath()); processBuilder.redirectInput(ProcessBuilder.Redirect.INHERIT); processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT); processBuilder.redirectError(ProcessBuilder.Redirect.INHERIT); try { Process p = processBuilder.start(); // wait for termination. p.waitFor(); try (BufferedReader br = new BufferedReader(new FileReader(outFile))) { String ret = IOUtils.toString(br).trim(); return ret; } } catch (Exception e) { String message = "Unable to read output: " + e.getMessage(); LOG.error(message, e); return null; } } finally { try { PausableInput.INSTANCE.unpause(); if (console.isPresent()) { ((Console) console.get()).pushToInputStream("\b\n"); } } catch (IOException e) { LOG.error("Unable to unpause: " + e.getMessage(), e); } if (outFile.exists()) { outFile.delete(); } } }
public void save() { final Properties properties = new Properties(); Optional.ofNullable(username).ifPresent(s -> properties.setProperty("username", s)); Optional.ofNullable(password).ifPresent(s -> properties.setProperty("password", s)); Optional.ofNullable(from) .ifPresent(d -> properties.setProperty("from", d.format(DateTimeFormatter.ISO_DATE))); try { properties.store(getConfigurationWriter(), null); } catch (IOException e) { e.printStackTrace(); // TODO logging } }
/** * @param directoryWithUnpackedFiles Directory with unpacked files (should include BBIs and DBF) * @return Map of BBI files names to their corresponding verifications * @throws SQLException * @throws ClassNotFoundException * @throws FileNotFoundException * @implNote Uses sqlite to open DBF */ private Map<String, Map<String, String>> getVerificationMapFromUnpackedFiles( File directoryWithUnpackedFiles) throws SQLException, ClassNotFoundException, FileNotFoundException { Map<String, Map<String, String>> bbiFilesToVerification = new LinkedHashMap<>(); Map<String, String> verificationMap; Optional<File> foundDBFile = FileUtils.listFiles(directoryWithUnpackedFiles, dbfExtensions, true).stream().findFirst(); File dbFile = foundDBFile.orElseThrow(() -> new FileNotFoundException("DBF not found")); Class.forName("org.sqlite.JDBC"); try (Connection connection = DriverManager.getConnection("jdbc:sqlite:" + dbFile)) { Statement statement = connection.createStatement(); ResultSet rs = statement.executeQuery("SELECT * FROM Results"); while (rs.next()) { verificationMap = new LinkedHashMap<>(); verificationMap.put(Constants.VERIFICATION_ID, rs.getString("Id_pc")); verificationMap.put(Constants.PROVIDER, rs.getString("Customer")); verificationMap.put(Constants.DATE, rs.getString("Date")); verificationMap.put(Constants.COUNTER_NUMBER, rs.getString("CounterNumber")); verificationMap.put(Constants.COUNTER_SIZE_AND_SYMBOL, rs.getString("Type")); verificationMap.put(Constants.YEAR, rs.getString("Year")); verificationMap.put(Constants.STAMP, rs.getString("Account")); verificationMap.put(Constants.LAST_NAME, rs.getString("Surname")); verificationMap.put(Constants.FIRST_NAME, rs.getString("Name")); verificationMap.put(Constants.MIDDLE_NAME, rs.getString("Middlename")); verificationMap.put(Constants.PHONE_NUMBER, rs.getString("TelNumber")); verificationMap.put(Constants.REGION, rs.getString("District")); verificationMap.put(Constants.CITY, rs.getString("City")); verificationMap.put(Constants.STREET, rs.getString("Street")); verificationMap.put(Constants.BUILDING, rs.getString("Building")); verificationMap.put(Constants.FLAT, rs.getString("Apartment")); bbiFilesToVerification.put(rs.getString("FileNumber"), verificationMap); } } return bbiFilesToVerification; }
public Optional<String> getName() { return Optional.of(source.getName()); }
/** * @param payObjects покупки, суммарную стоимость которых нужно посчитать * @return суммарное колличество стоимости покупок */ public Integer getSumPrice(List<PayObject> payObjects) { Optional<Integer> optional = payObjects.stream().map(PayObject::getPrice).reduce((s1, s2) -> s1 + s2); return optional.orElse(0); }
/** * @param payObjects покупки, текст о которых нужно составить * @return сгрупированный текст о всех переданных покупок */ public String getTextPayObjects(List<PayObject> payObjects) { Optional<String> optional = payObjects.stream().map(PayObject::toString).reduce((s1, s2) -> s1 + "\n\n" + s2); return optional.orElse(""); }
@SuppressWarnings("unchecked") public Detail2Converter(List<ObjectConverter.ColumnInfo> allColumns) throws java.io.IOException { Optional<ObjectConverter.ColumnInfo> column; final java.util.List<ObjectConverter.ColumnInfo> columns = allColumns .stream() .filter(it -> "test".equals(it.typeSchema) && "Detail2_entity".equals(it.typeName)) .collect(Collectors.toList()); columnCount = columns.size(); readers = new ObjectConverter.Reader[columnCount]; for (int i = 0; i < readers.length; i++) { readers[i] = (instance, rdr, ctx) -> { StringConverter.skip(rdr, ctx); return instance; }; } final java.util.List<ObjectConverter.ColumnInfo> columnsExtended = allColumns .stream() .filter(it -> "test".equals(it.typeSchema) && "-ngs_Detail2_type-".equals(it.typeName)) .collect(Collectors.toList()); columnCountExtended = columnsExtended.size(); readersExtended = new ObjectConverter.Reader[columnCountExtended]; for (int i = 0; i < readersExtended.length; i++) { readersExtended[i] = (instance, rdr, ctx) -> { StringConverter.skip(rdr, ctx); return instance; }; } column = columns.stream().filter(it -> "u".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'u' column in test Detail2_entity. Check if DB is in sync"); __index___u = (int) column.get().order - 1; column = columnsExtended.stream().filter(it -> "u".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'u' column in test Detail2. Check if DB is in sync"); __index__extended_u = (int) column.get().order - 1; column = columns.stream().filter(it -> "dd".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'dd' column in test Detail2_entity. Check if DB is in sync"); __index___dd = (int) column.get().order - 1; column = columnsExtended.stream().filter(it -> "dd".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'dd' column in test Detail2. Check if DB is in sync"); __index__extended_dd = (int) column.get().order - 1; column = columns.stream().filter(it -> "EntityCompositeid".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'EntityCompositeid' column in test Detail2_entity. Check if DB is in sync"); __index___EntityCompositeid = (int) column.get().order - 1; column = columnsExtended.stream().filter(it -> "EntityCompositeid".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'EntityCompositeid' column in test Detail2. Check if DB is in sync"); __index__extended_EntityCompositeid = (int) column.get().order - 1; column = columns.stream().filter(it -> "EntityIndex".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'EntityIndex' column in test Detail2_entity. Check if DB is in sync"); __index___EntityIndex = (int) column.get().order - 1; column = columnsExtended.stream().filter(it -> "EntityIndex".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'EntityIndex' column in test Detail2. Check if DB is in sync"); __index__extended_EntityIndex = (int) column.get().order - 1; column = columns.stream().filter(it -> "Index".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'Index' column in test Detail2_entity. Check if DB is in sync"); __index___Index = (int) column.get().order - 1; column = columnsExtended.stream().filter(it -> "Index".equals(it.columnName)).findAny(); if (!column.isPresent()) throw new java.io.IOException( "Unable to find 'Index' column in test Detail2. Check if DB is in sync"); __index__extended_Index = (int) column.get().order - 1; }
/** * Create a clause searcher which searches naively through every possible subtree as a clause. For * an end-user, this is almost certainly not what you want. However, it is very useful for * training time. * * @param tree The dependency tree to search over. * @param assumedTruth The truth of the premise. Almost always True. */ public ClauseSplitterSearchProblem(SemanticGraph tree, boolean assumedTruth) { this(tree, assumedTruth, Optional.empty(), Optional.empty()); }
static void checkPattern(Tree target, Tree pattern, Optional<Tree> expected) { Optional<Tree> result = find(target, pattern); String mark = result.equals(expected) ? "[✓]" : "[ ]"; System.out.println(mark + " " + pattern.toString()); assert result.equals(expected) : "Expected" + expected + ", but got " + result; }
public ByteArrayWrapper(byte[] data) { this(Optional.empty(), data); }
public ByteArrayWrapper(String name, byte[] data) { this(Optional.of(name), data); }
/** * The core implementation of the search. * * @param root The root word to search from. Traditionally, this is the root of the sentence. * @param candidateFragments The callback for the resulting sentence fragments. This is a * predicate of a triple of values. The return value of the predicate determines whether we * should continue searching. The triple is a triple of * <ol> * <li>The log probability of the sentence fragment, according to the featurizer and the * weights * <li>The features along the path to this fragment. The last element of this is the * features from the most recent step. * <li>The sentence fragment. Because it is relatively expensive to compute the resulting * tree, this is returned as a lazy {@link Supplier}. * </ol> * * @param classifier The classifier for whether an arc should be on the path to a clause split, a * clause split itself, or neither. * @param featurizer The featurizer to use. Make sure this matches the weights! * @param actionSpace The action space we are allowed to take. Each action defines a means of * splitting a clause on a dependency boundary. */ protected void search( // The root to search from IndexedWord root, // The output specs final Predicate<Triple<Double, List<Counter<String>>, Supplier<SentenceFragment>>> candidateFragments, // The learning specs final Classifier<ClauseSplitter.ClauseClassifierLabel, String> classifier, Map<String, ? extends List<String>> hardCodedSplits, final Function<Triple<State, Action, State>, Counter<String>> featurizer, final Collection<Action> actionSpace, final int maxTicks) { // (the fringe) PriorityQueue<Pair<State, List<Counter<String>>>> fringe = new FixedPrioritiesPriorityQueue<>(); // (avoid duplicate work) Set<IndexedWord> seenWords = new HashSet<>(); State firstState = new State(null, null, -9000, null, x -> {}, true); // First state is implicitly "done" fringe.add(Pair.makePair(firstState, new ArrayList<>(0)), -0.0); int ticks = 0; while (!fringe.isEmpty()) { if (++ticks > maxTicks) { // System.err.println("WARNING! Timed out on search with " + ticks + " ticks"); return; } // Useful variables double logProbSoFar = fringe.getPriority(); assert logProbSoFar <= 0.0; Pair<State, List<Counter<String>>> lastStatePair = fringe.removeFirst(); State lastState = lastStatePair.first; List<Counter<String>> featuresSoFar = lastStatePair.second; IndexedWord rootWord = lastState.edge == null ? root : lastState.edge.getDependent(); // Register thunk if (lastState.isDone) { if (!candidateFragments.test( Triple.makeTriple( logProbSoFar, featuresSoFar, () -> { SemanticGraph copy = new SemanticGraph(tree); lastState .thunk .andThen( x -> { // Add the extra edges back in, if they don't break the tree-ness of the // extraction for (IndexedWord newTreeRoot : x.getRoots()) { if (newTreeRoot != null) { // what a strange thing to have happen... for (SemanticGraphEdge extraEdge : extraEdgesByGovernor.get(newTreeRoot)) { assert Util.isTree(x); //noinspection unchecked addSubtree( x, newTreeRoot, extraEdge.getRelation().toString(), tree, extraEdge.getDependent(), tree.getIncomingEdgesSorted(newTreeRoot)); assert Util.isTree(x); } } } }) .accept(copy); return new SentenceFragment(copy, assumedTruth, false); }))) { break; } } // Find relevant auxilliary terms SemanticGraphEdge subjOrNull = null; SemanticGraphEdge objOrNull = null; for (SemanticGraphEdge auxEdge : tree.outgoingEdgeIterable(rootWord)) { String relString = auxEdge.getRelation().toString(); if (relString.contains("obj")) { objOrNull = auxEdge; } else if (relString.contains("subj")) { subjOrNull = auxEdge; } } // Iterate over children // For each outgoing edge... for (SemanticGraphEdge outgoingEdge : tree.outgoingEdgeIterable(rootWord)) { // Prohibit indirect speech verbs from splitting off clauses // (e.g., 'said', 'think') // This fires if the governor is an indirect speech verb, and the outgoing edge is a ccomp if (outgoingEdge.getRelation().toString().equals("ccomp") && ((outgoingEdge.getGovernor().lemma() != null && INDIRECT_SPEECH_LEMMAS.contains(outgoingEdge.getGovernor().lemma())) || INDIRECT_SPEECH_LEMMAS.contains(outgoingEdge.getGovernor().word()))) { continue; } // Get some variables String outgoingEdgeRelation = outgoingEdge.getRelation().toString(); List<String> forcedArcOrder = hardCodedSplits.get(outgoingEdgeRelation); if (forcedArcOrder == null && outgoingEdgeRelation.contains(":")) { forcedArcOrder = hardCodedSplits.get( outgoingEdgeRelation.substring(0, outgoingEdgeRelation.indexOf(":")) + ":*"); } boolean doneForcedArc = false; // For each action... for (Action action : (forcedArcOrder == null ? actionSpace : orderActions(actionSpace, forcedArcOrder))) { // Check the prerequisite if (!action.prerequisitesMet(tree, outgoingEdge)) { continue; } if (forcedArcOrder != null && doneForcedArc) { break; } // 1. Compute the child state Optional<State> candidate = action.applyTo(tree, lastState, outgoingEdge, subjOrNull, objOrNull); if (candidate.isPresent()) { double logProbability; ClauseClassifierLabel bestLabel; Counter<String> features = featurizer.apply(Triple.makeTriple(lastState, action, candidate.get())); if (forcedArcOrder != null && !doneForcedArc) { logProbability = 0.0; bestLabel = ClauseClassifierLabel.CLAUSE_SPLIT; doneForcedArc = true; } else if (features.containsKey("__undocumented_junit_no_classifier")) { logProbability = Double.NEGATIVE_INFINITY; bestLabel = ClauseClassifierLabel.CLAUSE_INTERM; } else { Counter<ClauseClassifierLabel> scores = classifier.scoresOf(new RVFDatum<>(features)); if (scores.size() > 0) { Counters.logNormalizeInPlace(scores); } String rel = outgoingEdge.getRelation().toString(); if ("nsubj".equals(rel) || "dobj".equals(rel)) { scores.remove( ClauseClassifierLabel.NOT_A_CLAUSE); // Always at least yield on nsubj and dobj } logProbability = Counters.max(scores, Double.NEGATIVE_INFINITY); bestLabel = Counters.argmax(scores, (x, y) -> 0, ClauseClassifierLabel.CLAUSE_SPLIT); } if (bestLabel != ClauseClassifierLabel.NOT_A_CLAUSE) { Pair<State, List<Counter<String>>> childState = Pair.makePair( candidate.get().withIsDone(bestLabel), new ArrayList<Counter<String>>(featuresSoFar) { { add(features); } }); // 2. Register the child state if (!seenWords.contains(childState.first.edge.getDependent())) { // System.err.println(" pushing " + action.signature() + " with " + // argmax.first.edge); fringe.add(childState, logProbability); } } } } } seenWords.add(rootWord); } // System.err.println("Search finished in " + ticks + " ticks and " + classifierEvals + " // classifier evaluations."); }