Example #1
0
func (store *MySQLStore) id2literal(id uint64) (term argo.Term) {
	cachedLit, ok := store.literalRevLookup[id]
	if !ok {
		row, ok := store.selectOne("SELECT value, language, datatype FROM %s_literals WHERE id = ?", id)
		if !ok || row == nil {
			return nil
		}

		cachedLit = &cachedLiteral{
			Value:      row[0].(string),
			Language:   row[1].(string),
			DatatypeID: row[2].(uint64),
		}

		store.literalRevLookup[id] = cachedLit
	}

	var datatype argo.Term = nil

	if cachedLit.DatatypeID != 0 {
		datatypeURI := store.id2node(cachedLit.DatatypeID)
		datatype = argo.NewResource(datatypeURI)
	}

	return argo.NewLiteralWithLanguageAndDatatype(cachedLit.Value, cachedLit.Language, datatype)
}
Example #2
0
func (rewrite Rewrite) Apply(termPtr *argo.Term) {
	term := *termPtr
	termStr := ""

	switch realTerm := term.(type) {
	case *argo.Resource:
		termStr = realTerm.URI
	case *argo.BlankNode:
		termStr = "_:" + realTerm.ID
	case *argo.Literal:
		return
	}

	match := rewrite.Regexp.FindStringSubmatchIndex(termStr)
	if match != nil {
		resBytes := rewrite.Regexp.ExpandString(nil, rewrite.Template, termStr, match)
		resStr := string(resBytes)

		if len(resStr) >= 2 && resStr[0] == '_' && resStr[1] == ':' {
			*termPtr = argo.NewBlankNode(resStr[2:])
		} else {
			*termPtr = argo.NewResource(resStr)
		}

		Rewritten++
	}
}
Example #3
0
func main() {
	loop.Debug = true

	graph := loop.NewInterpreter()

	if len(os.Args) >= 3 {
		url := os.Args[2]
		if strings.HasPrefix(url, "http:") {
			graph.ParseHTTP(argo.ParseRDFXML, url, "application/rdf+xml")

		} else {
			graph.ParseFile(argo.ParseRDFXML, url)
		}
	}

	graph.Serialize(argo.SerializeNTriples, os.Stdout)

	funcTerm := argo.NewResource(os.Args[1])
	value, err := graph.Evaluate(funcTerm, nil)
	if err != nil {
		fmt.Fprintf(os.Stderr, "Error: %s\n", err.Error())
	}

	fmt.Printf("%v\n", value)
}
Example #4
0
func node2term(node string) (term argo.Term) {
	if len(node) >= 2 && node[0] == '_' && node[1] == ':' {
		return argo.NewBlankNode(node[2:])
	} else {
		return argo.NewResource(node)
	}

	return nil
}
Example #5
0
func parseUri(rp *ResultParser, itok xml.Token) (newState stateFunc, err error) {
	switch tok := itok.(type) {
	case xml.Comment:
		return parseUri, nil

	case xml.CharData:
		rp.currentResult[rp.currentBinding] = argo.NewResource(string(tok))
		rp.currentBinding = ""

		return parseUri, nil

	case xml.EndElement: // </uri>
		return parseBinding, nil
	}

	return nil, fmt.Errorf("Unexpected %T in parseUri", itok)
}
Example #6
0
func parseLiteral(rp *ResultParser, itok xml.Token) (newState stateFunc, err error) {
	switch tok := itok.(type) {
	case xml.Comment:
		return parseLiteral, nil

	case xml.CharData:
		var datatype argo.Term

		if rp.literalDatatype != "" {
			datatype = argo.NewResource(rp.literalDatatype)
		}

		rp.currentResult[rp.currentBinding] = argo.NewLiteralWithLanguageAndDatatype(string(tok), rp.literalLanguage, datatype)
		rp.currentBinding = ""

		return parseLiteral, nil

	case xml.EndElement: // </literal>
		return parseBinding, nil
	}

	return nil, fmt.Errorf("Unexpected %T in parseLiteral", itok)
}
Example #7
0
func (store *MySQLStore) decodePredicate(predicatePrefixID uint64, predicateLocal string) (predicate argo.Term) {
	predicatePrefix := store.id2prefix(predicatePrefixID)
	return argo.NewResource(predicatePrefix + predicateLocal)
}
Example #8
0
func (graph *Interpreter) Evaluate(term argo.Term, ctx map[string]interface{}) (value interface{}, err error) {
	if ctx == nil {
		ctx = make(map[string]interface{})
	}

	lit, isLit := term.(*argo.Literal)
	if isLit {
		switch lit.Datatype {
		case XSD.Get("boolean"):
			switch lit.Value {
			case "true", "1":
				return true, nil

			case "false", "0":
				return false, nil

			default:
				return nil, fmt.Errorf("Invalid boolean value: %s", lit.Value)
			}

		case XSD.Get("base64Binary"):
			return base64.StdEncoding.DecodeString(lit.Value)

		case XSD.Get("hexBinary"):
			return hex.DecodeString(lit.Value)

		case XSD.Get("float"), XSD.Get("decimal"), XSD.Get("double"):
			return strconv.ParseFloat(lit.Value, 64)

		case XSD.Get("integer"), XSD.Get("nonPositiveInteger"), XSD.Get("negativeInteger"), XSD.Get("long"), XSD.Get("int"), XSD.Get("short"), XSD.Get("byte"), XSD.Get("nonNegativeInteger"), XSD.Get("unsignedLong"), XSD.Get("unsignedInt"), XSD.Get("unsignedShort"), XSD.Get("unsignedByte"), XSD.Get("positiveInteger"):
			return strconv.ParseInt(lit.Value, 10, 64)

		case XSD.Get("anyURI"):
			return Resource(argo.NewResource(lit.Value)), nil

		case XSD.Get("QName"):
			colonPos := strings.Index(lit.Value, ":")
			if colonPos < 0 {
				return nil, fmt.Errorf("No colon found in QName value: %s", lit.Value)
			}

			b := lit.Value[colonPos+1:]
			a, ok := graph.Prefixes[lit.Value[:colonPos]]

			if !ok {
				return nil, fmt.Errorf("Namespace identifier not found in graph prefix map when parsing QName: %s", lit.Value)
			}

			return Resource(argo.NewResource(a + b)), nil

		default:
			return lit.Value, nil
		}
	}

	graph.FetchIfNeeded(term)
	t := graph.Get(term, argo.A)

	switch t {
	case LOOP.Get("Variable"), LOOP.Get("Argument"):
		uri := term2str(term)
		value, ok := ctx[uri]
		if !ok {
			return nil, fmt.Errorf("Reference to unset variable: %s", uri)
		}

		return value, nil
	}

	graph.FetchIfNeeded(t)

	if graph.Get(t, argo.A) == LOOP.Get("Function") {
		uri := term2str(t)

		builtin, ok := builtins[uri]
		if ok {
			valargs := make([]interface{}, len(builtin.ValArgs))
			refargs := make([]Resource, len(builtin.RefArgs))

			for i, argURI := range builtin.ValArgs {
				v, err := graph.Evaluate(graph.MustGet(term, argo.NewResource(argURI)), ctx)
				if err != nil {
					return nil, err
				}

				valargs[i] = v
			}

			for i, argURI := range builtin.RefArgs {
				refargs[i] = Resource(graph.MustGet(term, argo.NewResource(argURI)))
			}

			return builtin.Func(valargs, refargs)

		} else {
			subctx := make(map[string]interface{})

			for triple := range graph.Filter(term, nil, nil) {
				arg := triple.Predicate
				argURI := term2str(arg)
				graph.FetchIfNeeded(arg)

				byRef := false

				obj := graph.Get(arg, LOOP.Get("byReference"))
				if obj != nil {
					lit, isLit = obj.(*argo.Literal)
					byRef = isLit && (lit.Value == "true" || lit.Value == "1")
				}

				if byRef {
					subctx[argURI] = Resource(triple.Object)

				} else {
					v, err := graph.Evaluate(triple.Object, ctx)
					if err != nil {
						return nil, err
					}

					subctx[argURI] = v
				}
			}

			code := graph.Get(t, LOOP.Get("code"))
			if code != nil && code != argo.RDF.Get("nil") {
				for expr := range graph.IterList(code) {
					value, err = graph.Evaluate(expr, subctx)
					if err != nil {
						return nil, err
					}
				}

				return value, nil

			} else {
				return nil, nil
			}
		}
	}

	return Resource(term), nil
}
Example #9
0
func str2term(uri string) (term argo.Term) {
	if len(uri) >= 2 && uri[0] == '_' && uri[1] == ':' {
		return argo.NewBlankNode(uri[2:])
	}
	return argo.NewResource(uri)
}
Example #10
0
func (expr ResourceConstant) ToRDF(graph *argo.Graph) (term argo.Term) {
	return argo.NewResource(string(expr))
}