eval

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2026 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var EvalCmd = &cobra.Command{
	Use:   "eval",
	Short: "Evaluate a Datalog query against policy and data",
	RunE: func(cmd *cobra.Command, args []string) error {

		if dataPath == "" && knowledgePath == "" {
			return fmt.Errorf("must provide either --data (JSON) or --facts/--knowledge (Graph)")
		}

		policyBytes, err := os.ReadFile(policyPath)
		if err != nil {
			return fmt.Errorf("failed to read policy file: %w", err)
		}

		var dataBytes []byte
		if dataPath != "" {
			dataBytes, err = os.ReadFile(dataPath)
			if err != nil {
				return fmt.Errorf("failed to read data file: %w", err)
			}
		}

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

		if knowledgePath != "" {

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

			preds := knowledge.GetPredicates(triples)
			if len(preds) > 0 {
				var decls []string
				for _, p := range preds {

					decls = append(decls, fmt.Sprintf("Decl %s(S, O).", p))
				}
				schemaBlock := strings.Join(decls, "\n")

				if err := e.LoadPolicy(cmd.Context(), schemaBlock); err != nil {
					return fmt.Errorf("failed to inject schema declarations: %w", err)
				}
				fmt.Printf("Injected %d schema declarations.\n", len(preds))
			}

			facts := knowledge.TriplesToFacts(triples)
			if err := e.LoadFacts(facts); err != nil {
				return fmt.Errorf("failed to load knowledge facts: %w", err)
			}
			fmt.Printf("Loaded %d knowledge facts.\n", len(facts))
		}

		if err := e.LoadPolicy(cmd.Context(), string(policyBytes)); err != nil {

			return fmt.Errorf("failed to load policy: %w", err)
		}

		if dataPath != "" {
			var data any
			if err := json.Unmarshal(dataBytes, &data); err != nil {
				return fmt.Errorf("failed to parse data JSON: %w", err)
			}

			dataFacts, err := engine.Flatten("input", data)
			if err != nil {
				return fmt.Errorf("failed to flatten data: %w", err)
			}

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

		ctx := context.Background()

		results, err := e.Query(ctx, nil, queryString)
		if err != nil {
			return fmt.Errorf("query execution failed: %w", err)
		}

		if len(results) == 0 {
			fmt.Println("No results found.")
			return nil
		}

		outputBytes, err := json.MarshalIndent(results, "", "  ")
		if err != nil {
			return fmt.Errorf("failed to format output: %w", err)
		}
		fmt.Println(string(outputBytes))

		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