private Uri TryParseContext(ITokenQueue tokens)
        {
            IToken next = tokens.Dequeue();
            if (next.TokenType == Token.DOT)
            {
                return null;
            }
            else
            {
                INode context;
                switch (next.TokenType)
                {
                    case Token.BLANKNODEWITHID:
                        context = new BlankNode(null, next.Value.Substring(2));
                        break;
                    case Token.URI:
                        context = new UriNode(null, new Uri(next.Value));
                        break;
                    case Token.LITERAL:
                        //Check for Datatype/Language
                        IToken temp = tokens.Peek();
                        if (temp.TokenType == Token.LANGSPEC)
                        {
                            tokens.Dequeue();
                            context = new LiteralNode(null, next.Value, temp.Value);
                        }
                        else if (temp.TokenType == Token.DATATYPE)
                        {
                            tokens.Dequeue();
                            context = new LiteralNode(null, next.Value, new Uri(temp.Value.Substring(1, temp.Value.Length - 2)));
                        }
                        else
                        {
                            context = new LiteralNode(null, next.Value);
                        }
                        break;
                    default:
                        throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a Blank Node/Literal/URI as the Context of the Triple", next);
                }

                //Ensure we then see a . to terminate the Quad
                next = tokens.Dequeue();
                if (next.TokenType != Token.DOT)
                {
                    throw ParserHelper.Error("Unexpected Token '" + next.GetType().ToString() + "' encountered, expected a Dot Token (Line Terminator) to terminate a Triple", next);
                }

                //Finally return the Context URI
                if (context.NodeType == NodeType.Uri)
                {
                    return ((IUriNode)context).Uri;
                }
                else if (context.NodeType == NodeType.Blank)
                {
                    return new Uri("nquads:bnode:" + context.GetHashCode());
                }
                else if (context.NodeType == NodeType.Literal)
                {
                    return new Uri("nquads:literal:" + context.GetHashCode());
                }
                else
                {
                    throw ParserHelper.Error("Cannot turn a Node of type '" + context.GetType().ToString() + "' into a Context URI for a Triple", next);
                }
            }
        }
        private void DirectoryTreeView_Load(object sender, EventArgs e)
        {
            treeView1.DrawMode = TreeViewDrawMode.OwnerDrawAll;

            if(!DesignMode)
            {
                textBox1.Parent = treeView1;
                BlankNode bn = new BlankNode(treeView1);

                //if(File.Exists(rootpath))
                //    LoadData(rootpath, ContainHtmFile);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Creates a new Blank Node for this Context
        /// </summary>
        /// <param name="id">ID</param>
        /// <returns></returns>
        /// <remarks>
        /// <para>
        /// If the same Blank Node ID is used multiple times in this Context you will always get the same Blank Node for that ID
        /// </para>
        /// </remarks>
        public INode GetBlankNode(String id)
        {
            if (this._bnodeMap == null) this._bnodeMap = new Dictionary<string, INode>();

            if (this._bnodeMap.ContainsKey(id)) return this._bnodeMap[id];

            INode temp;
            if (this._g != null)
            {
                temp = this._g.CreateBlankNode();
            }
            else if (this._factory != null)
            {
                temp = this._factory.CreateBlankNode();
            }
            else if (this._s != null)
            {
                temp = new BlankNode(this._g, id.Substring(2) + "-" + this._s.ID);
            }
            else
            {
                temp = new BlankNode(this._g, id.Substring(2));
            }
            this._bnodeMap.Add(id, temp);
            return temp;
        }
        public ITreeNode Program()
        {
            ITreeNode programNode = new ErrorNode();

            switch (curTokenType)
            {
                case TokenType.BEGINBL:
                    Match(ref matchToken, TokenType.BEGINBL);
                    ITreeNode someStatement = Program();
                    BlockNode block = new BlockNode();
                    block.addStatement(someStatement);
                    while (curTokenType != TokenType.ENDBL)
                    {
                        someStatement = Program();
                        block.addStatement(someStatement);
                    }
                    Match(ref matchToken, TokenType.ENDBL);
                    programNode = block;
                    break;
                case TokenType.LABEL:
                    Match(ref matchToken, TokenType.LABEL);
                    LabelNode labeledBlock = new LabelNode(matchToken.getValue());
                    ITreeNode labeledStatement;
                    do
                    {
                        labeledStatement = Program();
                        labeledBlock.addStatement(labeledStatement);
                    }
                    while (curTokenType != TokenType.EOF);
                    programNode = labeledBlock;
                    break;
                case TokenType.INTDEC:
                    Match(ref matchToken, TokenType.INTDEC);
                    Match(ref matchToken, TokenType.ID);
                    programNode = new IdentifierDeclarationNode(IdentifierType.INT, matchToken.getValue());
                    Match(ref matchToken, TokenType.EOS);
                    break;
                case TokenType.BOOLDEC:
                    Match(ref matchToken, TokenType.BOOLDEC);
                    Match(ref matchToken, TokenType.ID);
                    programNode = new IdentifierDeclarationNode(IdentifierType.BOOL, matchToken.getValue());
                    Match(ref matchToken, TokenType.EOS);
                    break;
                case TokenType.FOR:
                    Match(ref matchToken, TokenType.FOR);
                    Match(ref matchToken, TokenType.LPAREN);
                    AssignmentNode init = (AssignmentNode)Program();
                    AssignmentNode step = (AssignmentNode)Program();
                    BooleanExpressionNode condition = (BooleanExpressionNode)BooleanExpression();
                    Match(ref matchToken, TokenType.RPAREN);
                    BlockNode forBody = (BlockNode)Program();
                    programNode = new ForNode(init, step, condition, forBody);
                    break;
                /*case TokenType.FUN:
                    Match(ref matchToken, TokenType.FUN);
                    IdentifierNode id = (IdentifierNode)Factor();
                    programNode = new FunctionNode(id);
                    Match(ref matchToken, TokenType.EOS);
                    break;*/
                case TokenType.GOTO:
                    Match(ref matchToken, TokenType.GOTO);
                    Match(ref matchToken, TokenType.ID);
                    IdentifierNode gotoLabel = new IdentifierNode(matchToken.getValue(), IdentifierType.LABEL);
                    programNode = new GotoNode(gotoLabel);
                    Match(ref matchToken, TokenType.EOS);
                    break;
                case TokenType.ID:
                    Match(ref matchToken, TokenType.ID);
                    IdentifierNode assignId = new IdentifierNode(matchToken.getValue(), IdentifierType.UNKNOWN);
                    Match(ref matchToken, TokenType.ASSIGN);
                    BooleanExpressionNode assignValue = (BooleanExpressionNode)BooleanExpression();
                    programNode = new AssignmentNode(assignId, assignValue);
                    Match(ref matchToken, TokenType.EOS);
                    break;
                case TokenType.IF:
                    Match(ref matchToken, TokenType.IF);
                    ITreeNode thenBranch;
                    ITreeNode elseBranch;

                    Match(ref matchToken, TokenType.LPAREN);
                    BooleanExpressionNode ifCondition = (BooleanExpressionNode)BooleanExpression();
                    Match(ref matchToken, TokenType.RPAREN);
                    Match(ref matchToken, TokenType.THEN);
                    thenBranch = (BlockNode)Program();
                    if (curTokenType == TokenType.ELSE)
                    {
                        Match(ref matchToken, TokenType.ELSE);
                        elseBranch = (BlockNode)Program();
                    }
                    else
                    {
                        elseBranch = new BlankNode();
                    }
                    programNode = new IfNode(ifCondition, thenBranch, elseBranch);
                    break;
                /*case TokenType.LET:
                    Match(ref matchToken, TokenType.LET);
                    IdentifierNode shortId = (IdentifierNode)Factor();
                    Match(ref matchToken, TokenType.ASSIGN);
                    BooleanExpressionNode subst = (BooleanExpressionNode)BooleanExpression();
                    Match(ref matchToken, TokenType.IN);
                    BooleanExpressionNode target = (BooleanExpressionNode)BooleanExpression();
                    programNode = new LetNode(shortId, subst, target);
                    Match(ref matchToken, TokenType.EOS);
                    break;*/
                case TokenType.PRINT:
                    Match(ref matchToken, TokenType.PRINT);
                    Match(ref matchToken, TokenType.LPAREN);
                    BooleanExpressionNode printArgument = (BooleanExpressionNode)BooleanExpression();
                    Match(ref matchToken, TokenType.RPAREN);
                    programNode = new PrintNode(printArgument);
                    Match(ref matchToken, TokenType.EOS);
                    break;
                case TokenType.WHILE:
                    Match(ref matchToken, TokenType.WHILE);
                    Match(ref matchToken, TokenType.LPAREN);
                    BooleanExpressionNode whileCondition = (BooleanExpressionNode)BooleanExpression();
                    Match(ref matchToken, TokenType.RPAREN);
                    BlockNode whileBody = (BlockNode)Program();
                    programNode = new WhileNode(whileCondition, whileBody);
                    break;
                case TokenType.EOF:
                    programNode = new BlankNode();
                    break;
                default:
                    Expect(TokenType.UNKNOWN, lookAheadToken);
                    break;
            }
            return programNode;
        }
        public IArithmeticNode Factor()
        {
            IArithmeticNode factorNode = new BlankNode();

            switch (curTokenType)
            {
                case TokenType.LPAREN:
                    Match(ref matchToken, TokenType.LPAREN);
                    factorNode = BooleanExpression();
                    Match(ref matchToken, TokenType.RPAREN);
                    break;
                case TokenType.ID:
                    String idName = lookAheadToken.getValue();
                    factorNode = new IdentifierNode(idName, IdentifierType.UNKNOWN);
                    Match(ref matchToken, TokenType.ID);
                    /*if (lookAheadToken.getType() == TokenType.LPAREN)
                    {
                        Match(ref matchToken, TokenType.LPAREN);
                        ITreeNode argument = BooleanExpression();
                        Match(ref matchToken, TokenType.RPAREN);
                        factorNode = new FunctionCallNode(idName, argument);
                    }*/
                    break;
                case TokenType.INTEGER:
                    factorNode = new IntegerNode(int.Parse(lookAheadToken.getValue()));
                    Match(ref matchToken, TokenType.INTEGER);
                    break;
                case TokenType.MINUS:       //unary minus
                    Match(ref matchToken, TokenType.MINUS);
                    IArithmeticNode tempNode = Expression();
                    tempNode.setNegative();
                    factorNode = tempNode;
                    break;
                default:
                    Expect(TokenType.UNKNOWN, lookAheadToken);
                    break;
            }
            return factorNode;
        }
Beispiel #6
0
        /// <summary>
        /// Copies a Node so it can be used in another Graph since by default Triples cannot contain Nodes from more than one Graph
        /// </summary>
        /// <param name="original">Node to Copy</param>
        /// <param name="target">Graph to Copy into</param>
        /// <returns></returns>
        /// <remarks>
        /// <para>
        /// <strong>Warning:</strong> Copying Blank Nodes may lead to unforseen circumstances since no remapping of IDs between Graphs is done
        /// </para>
        /// </remarks>
        public static INode CopyNode(INode original, IGraph target)
        {
            //No need to copy if it's already in the relevant Graph
            if (ReferenceEquals(original.Graph, target)) return original;

            if (original.NodeType == NodeType.Uri)
            {
                IUriNode u = (IUriNode)original;
                IUriNode u2 = new UriNode(target, u.Uri);

                return u2;
            }
            else if (original.NodeType == NodeType.Literal)
            {
                ILiteralNode l = (ILiteralNode)original;
                ILiteralNode l2;
                if (l.Language.Equals(String.Empty))
                {
                    if (!(l.DataType == null))
                    {
                        l2 = new LiteralNode(target, l.Value, l.DataType);
                    }
                    else
                    {
                        l2 = new LiteralNode(target, l.Value);
                    }
                }
                else
                {
                    l2 = new LiteralNode(target, l.Value, l.Language);
                }

                return l2;
            }
            else if (original.NodeType == NodeType.Blank)
            {
                IBlankNode b = (IBlankNode)original;
                IBlankNode b2;

                b2 = new BlankNode(target, b.InternalID);
                return b2;
            }
            else if (original.NodeType == NodeType.Variable)
            {
                IVariableNode v = (IVariableNode)original;
                return new VariableNode(target, v.VariableName);
            }
            else
            {
                throw new RdfException("Unable to Copy '" + original.GetType().ToString() + "' Nodes between Graphs");
            }
        }
Beispiel #7
0
        /// <summary>
        /// Gets the list of Search Results which match the given search term
        /// </summary>
        /// <param name="text">Search Term</param>
        /// <returns>A list of Search Results representing Nodes in the Knowledge Base that match the search term</returns>
        public List<SearchServiceResult> Search(String text)
        {
            String search = this._searchUri + "?search=" + Uri.EscapeDataString(text);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(search);
            request.Method = this.Endpoint.HttpMethods.First();
            request.Accept = "text/json";

#if DEBUG
            if (Options.HttpDebugging)
            {
                Tools.HttpDebugRequest(request);
            }
#endif

            String jsonText;
            JArray json;
            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
#if DEBUG
                if (Options.HttpDebugging)
                {
                    Tools.HttpDebugResponse(response);
                }
#endif
                jsonText = new StreamReader(response.GetResponseStream()).ReadToEnd();
                json = JArray.Parse(jsonText);

                response.Close();
            }

            //Parse the Response into Search Results
            try
            {
                List<SearchServiceResult> results = new List<SearchServiceResult>();

                foreach (JToken result in json.Children())
                {
                    JToken hit = result.SelectToken("hit");
                    String type = (String)hit.SelectToken("type");
                    INode node;
                    if (type.ToLower().Equals("uri"))
                    {
                        node = new UriNode(null, new Uri((String)hit.SelectToken("value")));
                    }
                    else
                    {
                        node = new BlankNode(null, (String)hit.SelectToken("value"));
                    }
                    double score = (double)result.SelectToken("score");

                    results.Add(new SearchServiceResult(node, score));
                }

                return results;
            }
            catch (WebException webEx)
            {
#if DEBUG
                if (Options.HttpDebugging)
                {
                    if (webEx.Response != null) Tools.HttpDebugResponse((HttpWebResponse)webEx.Response);
                }
#endif
                throw new RdfReasoningException("A HTTP error occurred while communicating with Pellet Server", webEx);
            }
            catch (Exception ex)
            {
                throw new RdfReasoningException("Error occurred while parsing Search Results from the Search Service", ex);
            }
        }
Beispiel #8
0
        /// <summary>
        /// Gets the list of Search Results which match the given search term
        /// </summary>
        /// <param name="text">Search Term</param>
        /// <param name="callback">Callback to invoke when the operation completes</param>
        /// <param name="state">State to pass to the callback</param>
        public void Search(String text, PelletSearchServiceCallback callback, Object state)
        {
            String search = this._searchUri + "?search=" + Uri.EscapeDataString(text);

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(search);
            request.Method = this.Endpoint.HttpMethods.First();
            request.Accept = "text/json";

#if DEBUG
            if (Options.HttpDebugging)
            {
                Tools.HttpDebugRequest(request);
            }
#endif

            String jsonText;
            JArray json;
            request.BeginGetResponse(result =>
                {
                    using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(result))
                    {
#if DEBUG
                        if (Options.HttpDebugging)
                        {
                            Tools.HttpDebugResponse(response);
                        }
#endif
                        jsonText = new StreamReader(response.GetResponseStream()).ReadToEnd();
                        json = JArray.Parse(jsonText);

                        response.Close();

                        //Parse the Response into Search Results

                        List<SearchServiceResult> results = new List<SearchServiceResult>();

                        foreach (JToken res in json.Children())
                        {
                            JToken hit = res.SelectToken("hit");
                            String type = (String)hit.SelectToken("type");
                            INode node;
                            if (type.ToLower().Equals("uri"))
                            {
                                node = new UriNode(null, new Uri((String)hit.SelectToken("value")));
                            }
                            else
                            {
                                node = new BlankNode(null, (String)hit.SelectToken("value"));
                            }
                            double score = (double)res.SelectToken("score");

                            results.Add(new SearchServiceResult(node, score));
                        }

                        callback(results, state);
                    }
                }, null);
        }