示例#1
0
文件: import.go 项目: lanior/upc
func (self Format) ImportFile(file *os.File) ([]*model.Problem, error) {
	container := &Container{}
	err := utils.NewXmlDecoder(file).Decode(&container)
	if err != nil {
		return nil, err
	}

	result := make([]*model.Problem, len(container.Problems))
	for i, problem := range container.Problems {
		p, err := convertProblem(&problem)
		if err != nil {
			return nil, err
		}
		result[i] = p
	}
	return result, nil
}
示例#2
0
文件: import.go 项目: lanior/upc
func (self Format) ImportArchive(archive *formats.ProblemArchiveReader) (problems []*model.Problem, err error) {
	defer SuppressPanic(&err)

	reader, err := archive.Open("problem.xml")
	PanicIf(err)
	defer reader.Close()

	problem := new(Problem)
	utils.NewXmlDecoder(reader).Decode(problem)

	testset := &problem.Script.Testset
	p := &model.Problem{
		InputFile:  testset.InputName,
		OutputFile: testset.OutputName,

		TimeLimit:   float32(parseTime(testset.TimeLimit)),
		MemoryLimit: float32(testset.MemoryLimit) / 1024 / 1024,

		Tests: make([]model.Test, testset.TestCount),
	}

	inputPattern := preparePattern(testset.InputPattern)
	outputPattern := preparePattern(testset.AnswerPattern)
	for i := 0; i < testset.TestCount; i++ {
		inputPattern = inputPattern
		outputPattern = outputPattern

		data, err := archive.ReadFile(fmt.Sprintf(inputPattern, i+1))
		PanicIf(err, "Cannot read test %d input: %s", i+1, err)
		p.Tests[i].Input = model.ConstTestData(data)

		data, err = archive.ReadFile(fmt.Sprintf(outputPattern, i+1))
		PanicIf(err, "Cannot read test %d input: %s", i+1, err)
		p.Tests[i].Output = model.ConstSolutionData(data)
	}
	return []*model.Problem{p}, nil
}
示例#3
0
文件: import.go 项目: lanior/upc
func (self Format) ImportArchive(archive *formats.ProblemArchiveReader) (problems []*model.Problem, err error) {
	defer SuppressPanic(&err)

	files := archive.Glob("*.xml")
	PanicIf(len(files) == 0, "Task file not found")
	PanicIf(len(files) > 1, "Several xml files found in the archive")

	reader, err := archive.Open(files[0])
	PanicIf(err)
	defer reader.Close()

	container := new(Container)
	utils.NewXmlDecoder(reader).Decode(container)

	problem := container.Problem
	PanicIf(problem == nil, "Problem node not found")

	p := &model.Problem{
		Title:  problem.Title,
		Author: problem.Author,

		TimeLimit:   float32(problem.TimeLimit),
		MemoryLimit: float32(problem.MemoryLimit),

		Statement:    parseSGML(problem.Statement),
		Constraints:  parseSGML(problem.Constraints),
		InputFormat:  parseSGML(problem.InputFormat),
		OutputFormat: parseSGML(problem.OutputFormat),

		Generators: make(map[string]*model.Generator),
		Solutions:  make(map[string]*model.Solution),
		Images:     make(model.ImagesDict),
	}

	getSample := func(res *SampleResource) string {
		if res.Source != "" {
			data, err := archive.ReadFile(res.Source)
			PanicIf(err)
			return string(data)
		}
		return res.Text
	}

	p.Samples = make([]model.Sample, len(problem.Samples))
	for i, sample := range problem.Samples {
		p.Samples[i].Input = getSample(&sample.SampleIn)
		p.Samples[i].Output = getSample(&sample.SampleOut)
	}

	for _, picture := range problem.Pictures {
		data, err := archive.ReadFile(picture.Source)
		PanicIf(err)
		p.Images[picture.Name] = data
	}

	for _, generator := range problem.Generators {
		addGenerator(archive, p, &generator)
	}

	for _, genRange := range problem.GeneratorRanges {
		for j := genRange.From; j <= genRange.To; j++ {
			generator := genRange.Generator
			generator.Name = applyTestRank(generator.Name, j)
			generator.Source = applyTestRank(generator.Source, j)
			addGenerator(archive, p, &generator)
		}
	}

	for _, solution := range problem.Solutions {
		lang := getLanguage(solution.LanguageCode, solution.Source)
		PanicIf(lang == nil, "Unknown solution language")

		data, err := archive.ReadFile(solution.Source)
		PanicIf(err)

		p.Solutions[solution.Name] = &model.Solution{
			Name:       solution.Name,
			Language:   lang.Name,
			SourceCode: string(data),
		}
	}

	convertTest := func(t *model.Test, test *Test) {
		t.Points = test.Points

		if in := test.InputFile; in != nil {
			if in.Source != "" {
				data, err := archive.ReadFile(in.Source)
				PanicIf(err)

				t.Input = model.ConstTestData(data)
			} else if in.Generator != "" || in.Parameters != "" {
				if t.Input == nil {
					t.Input = &model.GeneratedInput{}
				}

				if in.Generator != "" {
					t.Input.(*model.GeneratedInput).Generator = in.Generator
				}

				if in.Parameters != "" {
					t.Input.(*model.GeneratedInput).Parameters = in.Parameters
				}
			}
		}

		if out := test.OutputFile; out != nil {
			if out.Source != "" {
				data, err := archive.ReadFile(out.Source)
				PanicIf(err)

				t.Output = model.ConstSolutionData(data)
			} else if out.Solution != "" {
				t.Output = &model.SolutionOutput{Solution: out.Solution}
			}
		}
	}

	addTestGroup := func(test *Test) {
		for rank := range stringRange(test.Rank) {
			if rank < 1 {
				continue
			}

			for len(p.Tests) < rank {
				p.Tests = append(p.Tests, model.Test{})
			}

			processedTest := *test
			if in := processedTest.InputFile; in != nil {
				processedTest.InputFile = &InputFile{
					Generator:   applyTestRank(in.Generator, rank),
					Source:      applyTestRank(in.Source, rank),
					Parameters:  applyTestRank(in.Parameters, rank),
					GenerateAll: in.GenerateAll,
				}
			}

			if out := processedTest.OutputFile; out != nil {
				processedTest.OutputFile = &OutputFile{
					Solution: applyTestRank(out.Solution, rank),
					Source:   applyTestRank(out.Source, rank),
				}
			}

			convertTest(&p.Tests[rank-1], &processedTest)
		}
	}

	for _, testRange := range problem.TestRanges {
		test := Test{
			Rank:          fmt.Sprintf("%d-%d", testRange.From, testRange.To),
			TestDataMixin: testRange.TestDataMixin,
		}
		addTestGroup(&test)
	}

	for _, test := range problem.Tests {
		addTestGroup(&test)
	}

	for i, test := range p.Tests {
		if test.Input == nil || test.Output == nil {
			Panic("Test %d: incomplete input or output section", i+1)
		}
	}
	return []*model.Problem{p}, nil
}
示例#4
0
文件: import.go 项目: lanior/upc
func (self Format) ImportArchive(archive *formats.ProblemArchiveReader) (problems []*model.Problem, err error) {
	defer SuppressPanic(&err)

	file, err := archive.Open("conf/serve.cfg")
	PanicIf(err, "Cannot open contest config: %s", err)
	defer file.Close()

	config, err := ReadConfig(file)
	PanicIf(err, "Cannot read contest config: %s", err)

	problemsData := make(map[string]*Section)

	global := &Section{Values: make(ValuesMap)}

	getGlobal := func(key, defaultValue string) string {
		value, ok := global.Values[key]
		if !ok {
			return defaultValue
		}
		return value
	}

	for i, section := range config {
		if section.Name == "problem" {
			problemsData[section.Values["short_name"]] = &config[i]
		} else if section.Name == "" {
			global = &config[i]
		}
	}

	for _, problem := range problemsData {
		if problem.Values["abstract"] == "1" {
			continue
		}

		getValue := func(key, defaultValue string) string {
			value, ok := problem.Values[key]
			if !ok {
				super := problem.Values["super"]
				if parentProblem := problemsData[super]; parentProblem != nil {
					value, ok := parentProblem.Values[key]
					if !ok {
						return defaultValue
					}
					return value
				}
			}
			return value
		}

		p := &model.Problem{
			Title:       getValue("long_name", ""),
			TimeLimit:   float32(getInt(getValue("time_limit", "0"))),
			MemoryLimit: float32(getMemory(getValue("max_vm_size", "0")) / 1024 / 1024),
			Solutions:   make(map[string]*model.Solution),
		}

		if getValue("use_stdin", "false") != "false" {
			p.InputFile = "*STDIN"
		} else {
			p.InputFile = getValue("input_file", "input")
		}

		if getValue("use_stdout", "false") != "false" {
			p.OutputFile = "*STDOUT"
		} else {
			p.OutputFile = getValue("output_file", "output")
		}

		shortName := getValue("short_name", "")

		testSfx := getValue("test_sfx", global.Values["test_sfx"])
		corrSfx := getValue("corr_sfx", global.Values["corr_sfx"])

		var testDir, corrDir, solDir, statementPath string
		if getGlobal("advanced_layout", "0") == "1" {
			testDir = filepath.Join("problems", shortName, "tests")
			corrDir = testDir
			solDir = filepath.Join("problems", shortName, "solutions")
			statementPath = filepath.Join("problems", shortName, "statement.xml")
		} else {
			Panic("Unsupported problems layout")
		}

		testPattern := fmt.Sprintf("%s/(.*)%s$", testDir, testSfx)
		resultPattern := fmt.Sprintf("%s/(.*)%s$", corrDir, corrSfx)
		solutionPattern := fmt.Sprintf("%s/(.*)$", solDir)

		PanicIf(archive.LoadTests(p, testPattern, resultPattern))
		PanicIf(archive.LoadSolutions(p, solutionPattern))

		statementFile, err := archive.Open(statementPath)
		PanicIf(err, "Cannot open statement file")
		defer statementFile.Close()

		problem := new(Problem)
		utils.NewXmlDecoder(statementFile).Decode(problem)

		p.Samples = make([]model.Sample, len(problem.Examples.Items))
		for i, example := range problem.Examples.Items {
			p.Samples[i].Input = example.Input
			p.Samples[i].Output = example.Output
		}

		p.Title = problem.Statement.Title
		p.Statement = parseHTML(problem.Statement.Description.Text)
		p.InputFormat = parseHTML(problem.Statement.InputFormat.Text)
		p.OutputFormat = parseHTML(problem.Statement.OutputFormat.Text)

		problems = append(problems, p)
	}

	return problems, nil
}