Search in sources :

Example 1 with String_

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);
}
Also used : Read(suite.streamlet.Read) SewingProverBuilder2(suite.lp.search.SewingProverBuilder2) Fun(suite.util.FunUtil.Fun) ArrayList(java.util.ArrayList) Node(suite.node.Node) CommentPreprocessor(suite.parser.CommentPreprocessor) String_(suite.util.String_) Preprocess(suite.text.Preprocess) Run(suite.text.Preprocess.Run) Binder(suite.lp.doer.Binder) RuleSet(suite.lp.kb.RuleSet) Generalizer(suite.lp.doer.Generalizer) Reference(suite.node.Reference) Suite(suite.Suite) Trail(suite.lp.Trail) Finder(suite.lp.search.ProverBuilder.Finder) Set(java.util.Set) TermOp(suite.node.io.TermOp) Bytes(suite.primitive.Bytes) BytesBuilder(suite.primitive.Bytes.BytesBuilder) List_(suite.util.List_) To(suite.util.To) Tree(suite.node.Tree) Pair(suite.adt.pair.Pair) List(java.util.List) Atom(suite.node.Atom) Int(suite.node.Int) Collections(java.util.Collections) Fail(suite.util.Fail) Reference(suite.node.Reference) Node(suite.node.Node) Generalizer(suite.lp.doer.Generalizer) ArrayList(java.util.ArrayList) List(java.util.List) Pair(suite.adt.pair.Pair)

Example 2 with String_

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);
}
Also used : BackAllocator(suite.trade.backalloc.BackAllocator) Read(suite.streamlet.Read) LogUtil(suite.os.LogUtil) SerializedStoreCache(suite.os.SerializedStoreCache) Trade_(suite.trade.Trade_) RunUtil(suite.util.RunUtil) ArrayList(java.util.ArrayList) Bacs(suite.trade.backalloc.BackAllocConfigurations.Bacs) ConfigurationImpl(suite.trade.data.ConfigurationImpl) String_(suite.util.String_) BackAllocatorOld(suite.trade.backalloc.strategy.BackAllocatorOld) Map(java.util.Map) BackAllocTester(suite.trade.backalloc.BackAllocTester) Simulate(suite.trade.backalloc.BackAllocTester.Simulate) Set_(suite.util.Set_) BuySellStrategy(suite.trade.singlealloc.BuySellStrategy) Streamlet2(suite.streamlet.Streamlet2) ExecutableProgram(suite.util.RunUtil.ExecutableProgram) Summarize(suite.trade.analysis.Summarize) Set(java.util.Set) To(suite.util.To) Obj_Dbl(suite.primitive.DblPrimitives.Obj_Dbl) Serialize(suite.util.Serialize) Strategos(suite.trade.singlealloc.Strategos) Pair(suite.adt.pair.Pair) List(java.util.List) Trade(suite.trade.Trade) BackAllocConfiguration(suite.trade.backalloc.BackAllocConfiguration) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) BackAllocConfigurations(suite.trade.backalloc.BackAllocConfigurations) Configuration(suite.trade.data.Configuration) MathUtil(suite.math.MathUtil) Account(suite.trade.Account) DataSource(suite.trade.data.DataSource) Sink(suite.util.FunUtil.Sink) As(suite.streamlet.As) SingleAllocBackTest(suite.trade.singlealloc.SingleAllocBackTest) DblStreamlet(suite.primitive.streamlet.DblStreamlet) Asset(suite.trade.Asset) SmtpSslGmail(suite.smtp.SmtpSslGmail) TimeRange(suite.trade.TimeRange) SummarizeByStrategy(suite.trade.analysis.Summarize.SummarizeByStrategy) Account(suite.trade.Account) TimeRange(suite.trade.TimeRange) Trade(suite.trade.Trade) Simulate(suite.trade.backalloc.BackAllocTester.Simulate)

Example 3 with String_

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;
}
Also used : Prover(suite.lp.doer.Prover) BindEnv(suite.lp.doer.BinderFactory.BindEnv) BuiltinPredicate(suite.lp.predicate.PredicateUtil.BuiltinPredicate) LogUtil(suite.os.LogUtil) Mutable(suite.adt.Mutable) BinderFactory(suite.lp.doer.BinderFactory) Node(suite.node.Node) ProverConstant(suite.lp.doer.ProverConstant) VariableMapper(suite.lp.sewing.VariableMapper) Suspend(suite.node.Suspend) Map(java.util.Map) Prototype(suite.lp.kb.Prototype) RuleSet(suite.lp.kb.RuleSet) Generalizer(suite.lp.doer.Generalizer) Bind_(suite.lp.doer.BinderFactory.Bind_) Cloner(suite.lp.doer.Cloner) Object_(suite.util.Object_) SystemPredicates(suite.lp.predicate.SystemPredicates) Tree(suite.node.Tree) Pair(suite.adt.pair.Pair) Friends.max(suite.util.Friends.max) List(java.util.List) Clone_(suite.lp.doer.ClonerFactory.Clone_) ListMultimap(suite.adt.map.ListMultimap) As(suite.streamlet.As) Int(suite.node.Int) TreeUtil(suite.node.util.TreeUtil) ProverConfig(suite.lp.Configuration.ProverConfig) Read(suite.streamlet.Read) Rule(suite.lp.kb.Rule) HashMap(java.util.HashMap) IList(suite.immutable.IList) ArrayList(java.util.ArrayList) Data(suite.node.Data) Formatter(suite.node.io.Formatter) String_(suite.util.String_) Rethrow(suite.util.Rethrow) Binder(suite.lp.doer.Binder) Tuple(suite.node.Tuple) Reference(suite.node.Reference) Suite(suite.Suite) SuiteException(suite.node.util.SuiteException) Iterator(java.util.Iterator) CompileExpressionImpl(suite.lp.compile.impl.CompileExpressionImpl) Source(suite.util.FunUtil.Source) TermOp(suite.node.io.TermOp) List_(suite.util.List_) Evaluate_(suite.lp.doer.EvaluatorFactory.Evaluate_) Rewrite(suite.node.util.Rewrite) Streamlet(suite.streamlet.Streamlet) Atom(suite.node.Atom) Sink(suite.util.FunUtil.Sink) ProverFactory(suite.lp.doer.ProverFactory) Env(suite.lp.sewing.Env) Fail(suite.util.Fail) Reference(suite.node.Reference) Node(suite.node.Node) Atom(suite.node.Atom) Bind_(suite.lp.doer.BinderFactory.Bind_) Clone_(suite.lp.doer.ClonerFactory.Clone_) Tree(suite.node.Tree) Tuple(suite.node.Tuple)

Example 4 with String_

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;
}
Also used : Path(java.nio.file.Path) Read(suite.streamlet.Read) Singleton(suite.node.util.Singleton) Obj_Flt(suite.primitive.FltPrimitives.Obj_Flt) LogUtil(suite.os.LogUtil) URL(java.net.URL) HashMap(java.util.HashMap) Obj_Lng(suite.primitive.LngPrimitives.Obj_Lng) HomeDir(suite.util.HomeDir) String_(suite.util.String_) Rethrow(suite.util.Rethrow) Map(java.util.Map) FileUtil(suite.os.FileUtil) JsonNode(com.fasterxml.jackson.databind.JsonNode) Path(java.nio.file.Path) LngFltPair(suite.primitive.adt.pair.LngFltPair) HttpUtil(suite.http.HttpUtil) Streamlet2(suite.streamlet.Streamlet2) Object_(suite.util.Object_) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) FoldOp(suite.util.FunUtil2.FoldOp) Constants(suite.Constants) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Set(java.util.Set) To(suite.util.To) URLEncoder(java.net.URLEncoder) List(java.util.List) Streamlet(suite.streamlet.Streamlet) Time(suite.trade.Time) As(suite.streamlet.As) TimeRange(suite.trade.TimeRange) Fail(suite.util.Fail) InputStream(java.io.InputStream) Time(suite.trade.Time) JsonNode(com.fasterxml.jackson.databind.JsonNode) LngFltPair(suite.primitive.adt.pair.LngFltPair) Streamlet(suite.streamlet.Streamlet) List(java.util.List)

Example 5 with String_

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++;
        }
    }
}
Also used : Read(suite.streamlet.Read) Set(java.util.Set) Service(suite.net.Service) HashMap(java.util.HashMap) Timer(java.util.Timer) ThreadMXBean(java.lang.management.ThreadMXBean) HashSet(java.util.HashSet) ThreadInfo(java.lang.management.ThreadInfo) String_(suite.util.String_) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) As(suite.streamlet.As) ManagementFactory(java.lang.management.ManagementFactory) TimerTask(java.util.TimerTask) State(java.lang.Thread.State) ThreadMXBean(java.lang.management.ThreadMXBean) ThreadInfo(java.lang.management.ThreadInfo) Set(java.util.Set) HashSet(java.util.HashSet)

Aggregations

String_ (suite.util.String_)14 Map (java.util.Map)12 Read (suite.streamlet.Read)12 List (java.util.List)11 As (suite.streamlet.As)10 Pair (suite.adt.pair.Pair)9 Streamlet (suite.streamlet.Streamlet)9 ArrayList (java.util.ArrayList)8 Set (java.util.Set)8 To (suite.util.To)7 HashMap (java.util.HashMap)6 LogUtil (suite.os.LogUtil)6 Streamlet2 (suite.streamlet.Streamlet2)6 Fail (suite.util.Fail)6 Sink (suite.util.FunUtil.Sink)6 Suite (suite.Suite)5 Node (suite.node.Node)5 Time (suite.trade.Time)5 Rethrow (suite.util.Rethrow)5 MathUtil (suite.math.MathUtil)4