run

package
v0.6.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 4, 2026 License: Apache-2.0 Imports: 8 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RunCmd = &cobra.Command{
	Use:   "run",
	Short: "Perform batch inference (Logic ETL)",
	Long:  `Perform batch inference by loading raw data, applying Datalog rules, and exporting derived facts.`,
	RunE: func(cmd *cobra.Command, args []string) error {

		eng, err := engine.New()
		if err != nil {
			return fmt.Errorf("failed to initialize engine: %w", err)
		}

		policyBytes, err := os.ReadFile(policyPath)
		if err != nil {
			return fmt.Errorf("failed to read policy file: %w", err)
		}
		if err := eng.LoadPolicy(cmd.Context(), string(policyBytes)); err != nil {
			return fmt.Errorf("failed to load policy: %w", err)
		}

		// 3. Load Data
		var facts []string
		ext := strings.ToLower(filepath.Ext(dataPath))
		if ext == ".json" {
			dataBytes, err := os.ReadFile(dataPath)
			if err != nil {
				return fmt.Errorf("failed to read data file: %w", err)
			}
			var input any
			if err := json.Unmarshal(dataBytes, &input); err != nil {
				return fmt.Errorf("failed to unmarshal JSON: %w", err)
			}

			facts, err = engine.Flatten("root", input)
			if err != nil {
				return fmt.Errorf("failed to flatten JSON data: %w", err)
			}
		} else if ext == ".nq" || ext == ".nt" || ext == ".ttl" {

			triples, err := knowledge.ParseGraphFile(dataPath)
			if err != nil {
				return fmt.Errorf("failed to parse graph file: %w", err)
			}

			facts = knowledge.TriplesToFacts(triples)
		} else {
			return fmt.Errorf("unsupported data file extension: %s", ext)
		}

		if err := eng.LoadFacts(cmd.Context(), facts); err != nil {
			return fmt.Errorf("failed to load facts: %w", err)
		}

		f, err := os.Create(outputPath)
		if err != nil {
			return fmt.Errorf("failed to create output file: %w", err)
		}
		defer f.Close()

		targetList := strings.Split(targets, ",")
		ctx := cmd.Context()

		for _, target := range targetList {
			target = strings.TrimSpace(target)

			query := fmt.Sprintf("%s(S, O)", target)
			results, err := eng.Query(ctx, nil, query)

			if err == nil && len(results) > 0 {
				for _, row := range results {

					s := row["S"]
					o := row["O"]

					line := fmt.Sprintf("<%s> <%s> \"%v\" .\n", s, target, o)
					f.WriteString(line)
				}
				continue
			}

			query = fmt.Sprintf("%s(S)", target)
			results, err = eng.Query(ctx, nil, query)
			if err == nil && len(results) > 0 {
				for _, row := range results {
					s := row["S"]

					line := fmt.Sprintf("<%s> <%s> \"true\" .\n", s, target)
					f.WriteString(line)
				}
				continue
			}
		}

		fmt.Printf("Inference complete. Results written to %s\n", outputPath)
		return nil
	},
}

Functions

func AddCommands

func AddCommands(rootCmd *cobra.Command)

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL