示例#1
0
文件: tgf.go 项目: th0br0/carneades-4
func Import(inFile io.Reader) (af dung.AF, err error) {
	reader := bufio.NewReader(inFile)
	args := make([]dung.Arg, 0, 50)
	atks := make(map[dung.Arg][]dung.Arg, 50)
	nodeList := true // false if reading the list of edges has begun
	var line, token1, token2 string
	var n int
	eof := false
	for !eof {
		token1, token2 = "", ""
		line, err = reader.ReadString('\n')
		if err == io.EOF {
			err = nil // io.EOF isn't really an error
			eof = true
		} else if err != nil {
			return af, err // finish immediately for real errors
		}
		n, _ = fmt.Sscan(line, &token1, &token2)
		if nodeList && n >= 1 {
			if token1 == "#" {
				nodeList = false // start of edges list
				continue
			}
			args = append(args, dung.Arg(token1))
		} else if !nodeList && n >= 2 { // edges list
			atks[dung.Arg(token2)] = append(atks[dung.Arg(token2)], dung.Arg(token1))
		} else {
			continue // skip empty and invalid lines
		}
	}
	return dung.NewAF(args, atks), err
}
示例#2
0
func main() {
	if len(os.Args) == 1 {
		fmt.Printf("%s %s\n%s\n", name, version, author)
		return
	}

	formatsFlag := flag.Bool("formats", false, "print supported formats")
	problemsFlag := flag.Bool("problems", false, "print supported problems")
	problemFlag := flag.String("p", "DC-GR", "the problem to solve")
	fileFlag := flag.String("f", "", "the source file for the AF")
	formatFlag := flag.String("fo", "tgf", "the format of the source file")
	argFlag := flag.String("a", "", "the id of the argument to check")
	outputFlag := flag.String("o", "", "the name of a directory to create to output GraphML")

	flag.Parse()

	arg := *argFlag

	checkArgFlag := func() {
		if arg == "" {
			log.Fatal(fmt.Errorf("no -a flag"))
		}
	}

	if *formatsFlag {
		fmt.Printf("%s\n", formats)
		return
	}

	if *problemsFlag {
		fmt.Printf("%s\n", problems)
		return
	}

	if *formatFlag != "tgf" {
		log.Fatal(fmt.Errorf("unsupported format: %s\n", *formatFlag))
		return
	}

	var inFile *os.File
	var err error
	var af dung.AF
	var extensions []dung.ArgSet

	if *fileFlag == "" {
		log.Fatal(fmt.Errorf("no file flag (-f)"))
		return
	}

	if *formatFlag == "tgf" {
		inFile, err = os.Open(*fileFlag)
		if err != nil {
			log.Fatal(err)
		}
		af, err = tgf.Import(inFile)
		if err != nil {
			log.Fatal(err)
		}
	}

	printExtensions := func(extensions []dung.ArgSet) {
		s := []string{}
		for _, E := range extensions {
			s = append(s, E.String())
		}
		fmt.Printf("[%s]\n", strings.Join(s, ","))
	}

	printExtension := func(E dung.ArgSet, exists bool) {
		if exists {
			fmt.Printf("%s\n", E)
		} else {
			fmt.Printf("NO\n")
		}
	}

	printBool := func(b bool) {
		if b {
			fmt.Printf("YES\n")
		} else {
			fmt.Printf("NO\n")
		}
	}

	// Grounded Semantics
	if *problemFlag == "DC-GR" {
		checkArgFlag()
		printBool(af.CredulouslyInferred(dung.Grounded, dung.Arg(arg)))
	} else if *problemFlag == "DS-GR" {
		checkArgFlag()
		printBool(af.SkepticallyInferred(dung.Grounded, dung.Arg(arg)))
	} else if *problemFlag == "EE-GR" {
		E := af.GroundedExtension()
		extensions = []dung.ArgSet{E}
		fmt.Printf("[%s]\n", E)
	} else if *problemFlag == "SE-GR" {
		E := af.GroundedExtension()
		extensions = []dung.ArgSet{E}
		fmt.Printf("%s\n", E)

		// Preferred Semantics
	} else if *problemFlag == "DC-PR" {
		checkArgFlag()
		printBool(af.CredulouslyInferred(dung.Preferred, dung.Arg(arg)))
	} else if *problemFlag == "DS-PR" {
		checkArgFlag()
		printBool(af.SkepticallyInferred(dung.Preferred, dung.Arg(arg)))
	} else if *problemFlag == "EE-PR" {
		extensions = af.PreferredExtensions()
		printExtensions(extensions)
	} else if *problemFlag == "SE-PR" {
		E, ok := af.SomeExtension(dung.Preferred)
		printExtension(E, ok)

		// Complete Semantics
	} else if *problemFlag == "DC-CO" {
		checkArgFlag()
		printBool(af.CredulouslyInferred(dung.Complete, dung.Arg(arg)))
	} else if *problemFlag == "DS-CO" {
		checkArgFlag()
		printBool(af.SkepticallyInferred(dung.Complete, dung.Arg(arg)))
	} else if *problemFlag == "EE-CO" {
		extensions = af.CompleteExtensions()
		printExtensions(extensions)
	} else if *problemFlag == "SE-CO" {
		E, ok := af.SomeExtension(dung.Complete)
		printExtension(E, ok)

		// Stable Semantics
	} else if *problemFlag == "DC-ST" {
		checkArgFlag()
		printBool(af.CredulouslyInferred(dung.Stable, dung.Arg(arg)))
	} else if *problemFlag == "DS-ST" {
		checkArgFlag()
		printBool(af.SkepticallyInferred(dung.Stable, dung.Arg(arg)))
	} else if *problemFlag == "EE-ST" {
		extensions = af.StableExtensions()
		printExtensions(extensions)
	} else if *problemFlag == "SE-ST" {
		E, ok := af.SomeExtension(dung.Stable)
		printExtension(E, ok)
	} else if *problemFlag == "traverse" {
		af.Traverse(func(E dung.ArgSet) {
			fmt.Printf("%v\n", E)
		})
	} else {
		log.Fatal(fmt.Errorf("unsupported problem: %s\n", *problemFlag))
		return
	}
	if *outputFlag != "" && extensions != nil {
		if _, err := os.Stat(*outputFlag); err == nil {
			log.Fatal(fmt.Errorf("The output directory, %s, should not already exist\n", *outputFlag))
			return
		}
		if err = os.MkdirAll(*outputFlag, 0755); err != nil {
			log.Fatal(fmt.Errorf("%s\n", err))
			return
		}
		for i, ext := range extensions {
			filename := "e" + fmt.Sprintf("%d", i) + ".graphml"
			f, err := os.Create(filepath.Join(*outputFlag, filename))
			if err != nil {
				log.Fatal(fmt.Errorf("%s\n", err))
				return
			}
			graphml.Export(f, af, ext)
			f.Close()
		}
	}
}
示例#3
0
func dungCmd() {
	dungFlags := flag.NewFlagSet("dung", flag.ContinueOnError)
	problemFlag := dungFlags.String("p", "EE", "the problem to solve")
	semanticsFlag := dungFlags.String("s", "GR", "the semantics to use")
	formatFlag := dungFlags.String("f", "tgf", "the format of the source file")
	argFlag := dungFlags.String("a", "", "the id of the argument to check")
	outputFlag := dungFlags.String("o", "", "the name of a new directory to create for outputting GraphML files")

	if err := dungFlags.Parse(os.Args[2:]); err != nil {
		log.Fatal(err)
	}

	arg := *argFlag

	checkArgFlag := func() {
		if arg == "" {
			log.Fatal(fmt.Errorf("no -a flag"))
		}
	}

	if *formatFlag != "tgf" {
		log.Fatal(fmt.Errorf("unsupported format: %s\n", *formatFlag))
		return
	}

	var inFile *os.File
	var err error
	var af dung.AF
	var extensions []dung.ArgSet

	switch dungFlags.NArg() {
	case 0:
		inFile = os.Stdin
	case 1:
		inFile, err = os.Open(dungFlags.Args()[0])
		if err != nil {
			log.Fatal(err)
		}
	default:
		log.Fatal(fmt.Errorf("incorrect number of arguments after the command flags; should be 0, to read from stdin, or 1, naming the input file\n"))
		return
	}

	if *formatFlag == "tgf" {
		af, err = tgf.Import(inFile)
		if err != nil {
			log.Fatal(err)
		}
	}

	printExtensions := func(extensions []dung.ArgSet) {
		s := []string{}
		for _, E := range extensions {
			s = append(s, E.String())
		}
		fmt.Printf("[%s]\n", strings.Join(s, ","))
	}

	printExtension := func(E dung.ArgSet, exists bool) {
		if exists {
			fmt.Printf("%s\n", E)
		} else {
			fmt.Printf("NO\n")
		}
	}

	printBool := func(b bool) {
		if b {
			fmt.Printf("YES\n")
		} else {
			fmt.Printf("NO\n")
		}
	}

	problem := *problemFlag + "-" + *semanticsFlag

	// Grounded Semantics
	if problem == "DC-GR" {
		checkArgFlag()
		printBool(af.CredulouslyInferred(dung.Grounded, dung.Arg(arg)))
	} else if problem == "DS-GR" {
		checkArgFlag()
		printBool(af.SkepticallyInferred(dung.Grounded, dung.Arg(arg)))
	} else if problem == "EE-GR" {
		E := af.GroundedExtension()
		extensions = []dung.ArgSet{E}
		fmt.Printf("[%s]\n", E)
	} else if problem == "SE-GR" {
		E := af.GroundedExtension()
		extensions = []dung.ArgSet{E}
		fmt.Printf("%s\n", E)

		// Preferred Semantics
	} else if problem == "DC-PR" {
		checkArgFlag()
		printBool(af.CredulouslyInferred(dung.Preferred, dung.Arg(arg)))
	} else if problem == "DS-PR" {
		checkArgFlag()
		printBool(af.SkepticallyInferred(dung.Preferred, dung.Arg(arg)))
	} else if problem == "EE-PR" {
		extensions = af.PreferredExtensions()
		printExtensions(extensions)
	} else if problem == "SE-PR" {
		E, ok := af.SomeExtension(dung.Preferred)
		printExtension(E, ok)

		// Complete Semantics
	} else if problem == "DC-CO" {
		checkArgFlag()
		printBool(af.CredulouslyInferred(dung.Complete, dung.Arg(arg)))
	} else if problem == "DS-CO" {
		checkArgFlag()
		printBool(af.SkepticallyInferred(dung.Complete, dung.Arg(arg)))
	} else if problem == "EE-CO" {
		extensions = af.CompleteExtensions()
		printExtensions(extensions)
	} else if problem == "SE-CO" {
		E, ok := af.SomeExtension(dung.Complete)
		printExtension(E, ok)

		// Stable Semantics
	} else if problem == "DC-ST" {
		checkArgFlag()
		printBool(af.CredulouslyInferred(dung.Stable, dung.Arg(arg)))
	} else if problem == "DS-ST" {
		checkArgFlag()
		printBool(af.SkepticallyInferred(dung.Stable, dung.Arg(arg)))
	} else if problem == "EE-ST" {
		extensions = af.StableExtensions()
		printExtensions(extensions)
	} else if problem == "SE-ST" {
		E, ok := af.SomeExtension(dung.Stable)
		printExtension(E, ok)
	} else if problem == "traverse" {
		af.Traverse(func(E dung.ArgSet) {
			fmt.Printf("%v\n", E)
		})
	} else {
		log.Fatal(fmt.Errorf("unsupported problem: %s\n", problem))
		return
	}
	if *outputFlag != "" && extensions != nil {
		if _, err := os.Stat(*outputFlag); err == nil {
			log.Fatal(fmt.Errorf("The output directory, %s, should not already exist\n", *outputFlag))
			return
		}
		if err = os.MkdirAll(*outputFlag, 0755); err != nil {
			log.Fatal(fmt.Errorf("%s\n", err))
			return
		}
		for i, ext := range extensions {
			filename := "e" + fmt.Sprintf("%d", i) + ".graphml"
			f, err := os.Create(filepath.Join(*outputFlag, filename))
			if err != nil {
				log.Fatal(fmt.Errorf("%s\n", err))
				return
			}
			graphml.Export(f, af, ext)
			f.Close()
		}
	}
}
示例#4
0
// Mozilla Public License, v. 2.0. If a copy of the MPL
// was not distributed with this file, You can obtain one
// at http://mozilla.org/MPL/2.0/.

package test

import (
	"github.com/carneades/carneades-4/internal/engine/dung"
	"github.com/carneades/carneades-4/internal/engine/dung/encoding/tgf"
	// "fmt"
	"log"
	"os"
	"testing"
)

const a1 = dung.Arg("1")
const a2 = dung.Arg("2")
const a3 = dung.Arg("3")

func TestUnattackedArg(t *testing.T) {
	af := dung.NewAF([]dung.Arg{a1},
		map[dung.Arg][]dung.Arg{})
	l := af.GroundedExtension()
	expected := true
	actual := l.Contains(a1)
	if actual != expected {
		t.Errorf("expected extension to contain 1")
	}
}

func TestSelfAttack(t *testing.T) {