// GenerateOptFlowOptimizeCode generates optimize codes for execution
// The returned value is (runnerProgramCode, submitProgramCode, error)
GenerateOptFlowOptimizeCode generates optimize codes for execution
The returned value is (runnerProgramCode, submitProgramCode, error)
// GenerateOptFlowOptimizeCode generates optimize codes for execution // The returned value is ( runnerProgramCode submitProgramCode error )
func GenerateOptFlowOptimizeCode(optimStmt *ir.OptimizeStmt, session *pb.Session, dbName, tableName, runnerModuleName string) (string, string, error) {
	const (
		dataAttrPrefix   = "data."
		solverAttrPrefix = "solver."
		workerAttrPrefix = "worker."
	)
	resultTable := optimStmt.ResultTable
	if !strings.Contains(resultTable, ".") {
		resultTable = fmt.Sprintf("%s.%s", dbName, resultTable)
	}
	attrs := make(map[string]map[string]interface{})
	for k, v := range optimStmt.Attributes {
		prefix := ""
		if strings.HasPrefix(k, dataAttrPrefix) {
			prefix = dataAttrPrefix
		} else if strings.HasPrefix(k, solverAttrPrefix) {
			prefix = solverAttrPrefix
		} else if strings.HasPrefix(k, workerAttrPrefix) {
			prefix = workerAttrPrefix
		} else {
			return "", "", fmt.Errorf("unrecognized attribute %s", k)
		}
		k = k[len(prefix):]
		prefixKey := prefix[0 : len(prefix)-1]
		if _, ok := attrs[prefixKey]; !ok {
			attrs[prefixKey] = make(map[string]interface{})
		}
		attrs[prefixKey][k] = v
	}
	attrJSON, err := json.Marshal(attrs)
	if err != nil {
		return "", "", err
	}
	filler := optimizeFiller{
		UserID:          session.UserId,
		Variables:       optimStmt.Variables,
		ResultValueName: optimStmt.ResultValueName,
		VariableType:    optimStmt.VariableType,
		Objective:       optimStmt.Objective,
		Direction:       optimStmt.Direction,
		Constraints:     optimStmt.Constraints,
		Solver:          optimStmt.Solver,
		AttributeJSON:   string(attrJSON),
		TrainTable:      fmt.Sprintf("%s.%s", dbName, tableName),
		ResultTable:     resultTable,
		RunnerModule:    runnerModuleName,
	}
	var runnerProgram bytes.Buffer
	runnerTpl := template.Must(template.New(runnerModuleName).Parse(optFlowRunnerText))
	if err := runnerTpl.Execute(&runnerProgram, filler); err != nil {
		return "", "", err
	}
	tpl := template.Must(template.New("optimize").Parse(optFlowSubmitText))
	var submitProgram bytes.Buffer
	if err := tpl.Execute(&submitProgram, filler); err != nil {
		return "", "", err
	}
	return runnerProgram.String(), submitProgram.String(), nil
}
func GenerateOptFlowOptimizeCode ( optimStmt * ir . OptimizeStmt , session * pb . Session , dbName , tableName , runnerModuleName string ) ( string , string , error ) { const ( dataAttrPrefix = " " solverAttrPrefix = " " workerAttrPrefix = " " ) resultTable := optimStmt . ResultTable if ! strings . Contains ( resultTable , " " ) { resultTable = fmt . Sprintf ( " " , dbName , resultTable ) } attrs := make ( map [ string ] map [ string ] interface { } ) for k , v := range optimStmt . Attributes { prefix := " " if strings . HasPrefix ( k , dataAttrPrefix ) { prefix = dataAttrPrefix } else if strings . HasPrefix ( k , solverAttrPrefix ) { prefix = solverAttrPrefix } else if strings . HasPrefix ( k , workerAttrPrefix ) { prefix = workerAttrPrefix } else { return " " , " " , fmt . Errorf ( " " , k ) } k = k [ len ( prefix ) : ] prefixKey := prefix [ 0 : len ( prefix ) - 1 ] if _ , ok := attrs [ prefixKey ] ; ! ok { attrs [ prefixKey ] = make ( map [ string ] interface { } ) } attrs [ prefixKey ] [ k ] = v } attrJSON , err := json . Marshal ( attrs ) if err != nil { return " " , " " , err } filler := optimizeFiller { UserID : session . UserId , Variables : optimStmt . Variables , ResultValueName : optimStmt . ResultValueName , VariableType : optimStmt . VariableType , Objective : optimStmt . Objective , Direction : optimStmt . Direction , Constraints : optimStmt . Constraints , Solver : optimStmt . Solver , AttributeJSON : string ( attrJSON ) , TrainTable : fmt . Sprintf ( " " , dbName , tableName ) , ResultTable : resultTable , RunnerModule : runnerModuleName , } var runnerProgram bytes . Buffer runnerTpl := template . Must ( template . New ( runnerModuleName ) . Parse ( optFlowRunnerText ) ) if err := runnerTpl . Execute ( & runnerProgram , filler ) ; err != nil { return " " , " " , err } tpl := template . Must ( template . New ( " " ) . Parse ( optFlowSubmitText ) ) var submitProgram bytes . Buffer if err := tpl . Execute ( & submitProgram , filler ) ; err != nil { return " " , " " , err } return runnerProgram . String ( ) , submitProgram . String ( ) , nil }
GenerateOptFlowOptimizeCode generates optimize codes for execution
The returned value is (runnerProgramCode, submitProgramCode, error)
GenerateOptFlowOptimizeCode generates optimize codes for execution The returned value is ( runnerProgramCode submitProgramCode error )