use of suite.util.String_ in project suite by stupidsing.
the class Assembler method assemble.
public Bytes assemble(String in0) {
Set<Character> whitespaces = Collections.singleton('\n');
Fun<String, List<Run>> gct = CommentPreprocessor.groupCommentPreprocessor(whitespaces);
Fun<String, List<Run>> lct = CommentPreprocessor.lineCommentPreprocessor(whitespaces);
String in1 = Preprocess.transform(List.of(gct, lct), in0).t0;
Generalizer generalizer = new Generalizer();
List<String> lines = List.of(in1.split("\n"));
Pair<String, String> pe;
int start = 0;
while (!(pe = String_.split2(lines.get(start), "=")).t1.isEmpty()) {
generalizer.getVariable(Atom.of(pe.t0)).bound(Suite.parse(pe.t1));
start++;
}
List<Pair<Reference, Node>> lnis = //
Read.from(//
List_.right(lines, start)).map(line -> {
Pair<String, String> pt = String_.split2(line, "\t");
String label = pt.t0;
String command = pt.t1;
Reference reference = String_.isNotBlank(label) ? generalizer.getVariable(Atom.of(label)) : null;
Node instruction = generalizer.generalize(Suite.parse(command));
return Pair.of(reference, instruction);
}).toList();
return assemble(generalizer, lnis);
}
use of suite.util.String_ in project suite by stupidsing.
the class DailyMain method alloc.
private Result alloc(String tag, float fund, BackAllocator backAllocator, Streamlet<Asset> assets) {
TimeRange period = TimeRange.daysBefore(64);
Simulate sim = BackAllocTester.of(cfg, period, assets, backAllocator, log).simulate(fund);
Account account0 = Account.ofPortfolio(cfg.queryHistory().filter(r -> String_.equals(r.strategy, tag)));
Account account1 = sim.account;
Map<String, Integer> assets0 = account0.assets();
Map<String, Integer> assets1 = account1.assets();
Set<String> symbols = Set_.union(assets0.keySet(), assets1.keySet());
Map<String, Float> priceBySymbol = cfg.quote(symbols);
List<Trade> trades = Trade_.diff(Trade.NA, assets0, assets1, priceBySymbol::get).toList();
sb.append("\nstrategy = " + tag + ", " + sim.conclusion());
return new Result(tag, trades);
}
use of suite.util.String_ in project suite by stupidsing.
the class SewingProverImpl method compileCps.
private Cps compileCps(BinderFactory bf, Node node, Cps cpsx) {
List<Node> list;
Tree tree;
Node[] m;
Cps cps;
if (1 < (list = TreeUtil.breakdown(TermOp.AND___, node)).size()) {
cps = cpsx;
for (Node n : List_.reverse(list)) cps = compileCps(bf, n, cps);
} else if (1 < (list = TreeUtil.breakdown(TermOp.OR____, node)).size())
cps = orCps(Read.from(list).map(n -> compileCps(bf, n, cpsx)));
else if ((m = Suite.pattern(".0 = .1").match(node)) != null) {
boolean b = complexity(m[0]) <= complexity(m[1]);
Node n0 = b ? m[0] : m[1];
Node n1 = b ? m[1] : m[0];
Bind_ p = bf.binder(n1);
Clone_ f = bf.cloner(n0);
cps = rt -> p.test(rt, f.apply(rt.env)) ? cpsx : null;
} else if ((m = Suite.pattern(".0 .1").match(node)) != null && m[0] instanceof Atom)
cps = compileCpsCallPredicate(bf, ((Atom) m[0]).name, m[1], node, cpsx);
else if (node instanceof Atom) {
String name = ((Atom) node).name;
if (String_.equals(name, ""))
cps = cpsx;
else if (String_.equals(name, "fail"))
cps = rt -> null;
else
cps = compileCpsCallPredicate(bf, name, Atom.NIL, node, cpsx);
} else if (node instanceof Reference) {
Clone_ f = bf.cloner(node);
cps = rt -> compileCps(passThru, f.apply(rt.env), cpsx);
} else if ((tree = Tree.decompose(node)) != null)
cps = compileCpsCallPredicate(bf, tree.getOperator().getName(), node, node, cpsx);
else if (node instanceof Tuple)
cps = compileCpsCallPredicate(bf, node, cpsx);
else
cps = Fail.t("cannot understand " + node);
return cps;
}
use of suite.util.String_ in project suite by stupidsing.
the class Yahoo method getStockHistory.
private StockHistory getStockHistory(String symbol) {
Path path = HomeDir.dir("yahoo").resolve(symbol + ".txt");
StockHistory stockHistory0;
if (Files.exists(path))
try {
List<String> lines = Rethrow.ex(() -> Files.readAllLines(path));
stockHistory0 = StockHistory.of(Read.from(lines).outlet());
} catch (Exception ex) {
stockHistory0 = StockHistory.new_();
}
else
stockHistory0 = StockHistory.new_();
Time time = HkexUtil.getCloseTimeBefore(Time.now());
StockHistory stockHistory1;
if (stockHistory0.isActive && Time.compare(stockHistory0.time, time) < 0) {
JsonNode json = queryL1(symbol, TimeRange.of(stockHistory0.time.addDays(-14), Time.now()));
Streamlet<JsonNode> jsons = //
Read.each(json).flatMap(json_ -> json_.path("chart").path("result"));
String exchange = //
jsons.map(//
json_ -> json_.path("meta").path("exchangeName").textValue()).uniqueResult();
long[] ts = //
jsons.flatMap(//
json_ -> json_.path("timestamp")).collect(//
Obj_Lng.lift(t -> getOpenTimeBefore(exchange, t.longValue()))).toArray();
int length = ts.length;
Streamlet2<String, Streamlet<JsonNode>> dataJsons0 = //
Read.<String>empty().map2(tag -> //
jsons.flatMap(json_ -> {
JsonNode json0 = json_.path("indicators");
JsonNode json1;
if (//
false || //
!(json1 = json0.path("unadjclose")).isMissingNode() || !(json1 = json0.path("unadjquote")).isMissingNode())
return json1;
else
return List.of();
}).flatMap(json_ -> json_.path("unadj" + tag)));
Streamlet2<String, Streamlet<JsonNode>> dataJsons1 = //
Read.each("open", "close", "high", "low", //
"volume").map2(tag -> //
jsons.flatMap(//
json_ -> json_.path("indicators").path("quote")).flatMap(json_ -> json_.path(tag)));
Map<String, LngFltPair[]> data = //
Streamlet2.concat(dataJsons0, //
dataJsons1).mapValue(//
json_ -> json_.collect(Obj_Flt.lift(JsonNode::floatValue)).toArray()).filterValue(//
fs -> length <= fs.length).mapValue(//
fs -> To.array(length, LngFltPair.class, i -> LngFltPair.of(ts[i], fs[i]))).toMap();
LngFltPair[] dividends = //
jsons.flatMap(//
json_ -> json_.path("events").path("dividends")).map(//
json_ -> LngFltPair.of(json_.path("date").longValue(), json_.path("amount").floatValue())).sort(//
LngFltPair.comparatorByFirst()).toArray(LngFltPair.class);
LngFltPair[] splits = //
jsons.flatMap(//
json_ -> json_.path("events").path("splits")).map(json_ -> LngFltPair.of(json_.path("date").longValue(), //
json_.path("numerator").floatValue() / json_.path("denominator").floatValue())).sort(//
LngFltPair.comparatorByFirst()).toArray(LngFltPair.class);
if (data.containsKey("close"))
stockHistory1 = //
StockHistory.of(exchange, time, true, data, dividends, //
splits).merge(//
stockHistory0).alignToDate();
else
stockHistory1 = Fail.t();
FileUtil.write(path, stockHistory1.write());
} else
stockHistory1 = stockHistory0;
Predicate<LngFltPair> splitFilter;
LngFltPair[] splits2;
if (String_.equals(symbol, "0700.HK"))
splitFilter = pair -> pair.t0 != Time.of(2014, 5, 15, 9, 30).epochSec();
else if (String_.equals(symbol, "2318.HK"))
splitFilter = pair -> pair.t0 != Time.of(2015, 7, 27, 9, 30).epochSec();
else
splitFilter = null;
splits2 = //
splitFilter != null ? //
Read.from(stockHistory1.splits).filter(splitFilter).toArray(LngFltPair.class) : stockHistory1.splits;
StockHistory stockHistory2 = stockHistory1.create(stockHistory1.data, stockHistory1.dividends, splits2);
StockHistory stockHistory3 = LogUtil.prefix("for " + symbol + ": ", () -> stockHistory2.cleanse());
return stockHistory3;
}
use of suite.util.String_ in project suite by stupidsing.
the class Profiler method recordStats.
private void recordStats() {
long currentThreadId = Thread.currentThread().getId();
ThreadMXBean mx = ManagementFactory.getThreadMXBean();
long[] threadIds = mx.getAllThreadIds();
ThreadInfo[] threadInfos = mx.getThreadInfo(threadIds, stackTraceDepth);
count.getAndIncrement();
for (ThreadInfo threadInfo : threadInfos) if (//
threadInfo != null && //
threadInfo.getThreadId() != currentThreadId && //
threadInfo.getThreadState() == State.RUNNABLE && !String_.equals(threadInfo.getThreadName(), "ReaderThread")) {
StackTraceElement[] stackTrace = threadInfo.getStackTrace();
Set<String> elements = new HashSet<>();
int i = stackTrace.length;
Call call = callRoot;
// anonymous classes
while (0 < i) {
StackTraceElement elem = stackTrace[--i];
String fileName = elem.getFileName();
int lineNumber = elem.getLineNumber();
String mn = elem.getClassName() + "." + elem.getMethodName();
String fn = fileName != null ? " " + fileName + (1 < lineNumber ? ":" + lineNumber : "") : "<unknown>";
String name = mn + fn;
(call = call.callees.computeIfAbsent(mn, any -> new Call())).count++;
if (elements.add(name))
records.computeIfAbsent(name, any -> new Record()).count++;
}
}
}
Aggregations