[ 
https://issues.apache.org/jira/browse/RYA-417?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16320713#comment-16320713
 ] 

ASF GitHub Bot commented on RYA-417:
------------------------------------

Github user ejwhite922 commented on a diff in the pull request:

    https://github.com/apache/incubator-rya/pull/255#discussion_r160741607
  
    --- Diff: 
extras/rya.forwardchain/src/main/java/org/apache/rya/forwardchain/rule/SpinConstructRule.java
 ---
    @@ -0,0 +1,344 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *   http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.rya.forwardchain.rule;
    +
    +import java.util.Collection;
    +import java.util.Map;
    +import java.util.Set;
    +import java.util.concurrent.ConcurrentHashMap;
    +
    +import org.apache.log4j.Logger;
    +import org.apache.rya.api.RdfCloudTripleStoreConfiguration;
    +import org.apache.rya.api.domain.StatementMetadata;
    +import org.apache.rya.api.resolver.RdfToRyaConversions;
    +import org.apache.rya.forwardchain.ForwardChainConstants;
    +import org.apache.rya.forwardchain.ForwardChainException;
    +import org.apache.rya.forwardchain.strategy.AbstractRuleExecutionStrategy;
    +import org.apache.rya.sail.config.RyaSailFactory;
    +import org.openrdf.model.Literal;
    +import org.openrdf.model.Resource;
    +import org.openrdf.model.Value;
    +import org.openrdf.model.vocabulary.OWL;
    +import org.openrdf.model.vocabulary.RDF;
    +import org.openrdf.model.vocabulary.RDFS;
    +import org.openrdf.model.vocabulary.SP;
    +import org.openrdf.model.vocabulary.SPIN;
    +import org.openrdf.query.BindingSet;
    +import org.openrdf.query.MalformedQueryException;
    +import org.openrdf.query.QueryEvaluationException;
    +import org.openrdf.query.QueryLanguage;
    +import org.openrdf.query.TupleQuery;
    +import org.openrdf.query.TupleQueryResultHandlerBase;
    +import org.openrdf.query.TupleQueryResultHandlerException;
    +import org.openrdf.query.algebra.Extension;
    +import org.openrdf.query.algebra.Join;
    +import org.openrdf.query.algebra.QueryModelNode;
    +import org.openrdf.query.algebra.SingletonSet;
    +import org.openrdf.query.algebra.StatementPattern;
    +import org.openrdf.query.algebra.TupleExpr;
    +import org.openrdf.query.algebra.UnaryTupleOperator;
    +import org.openrdf.query.algebra.ValueExpr;
    +import org.openrdf.query.algebra.Var;
    +import org.openrdf.query.algebra.helpers.QueryModelVisitorBase;
    +import org.openrdf.query.parser.ParsedGraphQuery;
    +import org.openrdf.query.parser.ParsedQuery;
    +import org.openrdf.query.parser.sparql.SPARQLParser;
    +import org.openrdf.repository.RepositoryException;
    +import org.openrdf.repository.sail.SailRepository;
    +import org.openrdf.repository.sail.SailRepositoryConnection;
    +
    +import com.google.common.base.Preconditions;
    +import com.google.common.collect.Sets;
    +
    +/**
    + * Represents a SPIN Construct rule extracted from the data store, 
providing
    + * access to its associated query tree and providing methods to apply the 
rule.
    + */
    +public class SpinConstructRule extends AbstractConstructRule {
    +    private static Logger logger = 
Logger.getLogger(SpinConstructRule.class);
    +
    +    private final Resource ruleId;
    +    private final ParsedGraphQuery graphQuery;
    +    private Set<StatementPattern> antecedentStatementPatterns = null;
    +    private Set<StatementPattern> consequentStatementPatterns = null;
    +
    +    /**
    +     * Instantiate a SPIN construct rule given its associated type, URI or 
bnode
    +     * identifier, and construct query tree. Modifies the query tree to
    +     * incorporate the fact that ?this must belong to the associated type, 
and
    +     * traverses the modified tree to find antecedent and consequent triple
    +     * patterns.
    +     * @param type This rule applies to objects of this type. Should not be
    +     *  null. If the type is owl:Thing or rdfs:Resource, it will be 
applied to
    +     *  any objects. Otherwise, a statement pattern will be added that
    +     *  effectively binds ?this to members of the type. Therefore, passing
    +     *  owl:Thing or rdfs:Resource yields the intended behavior of
    +     *  sp:thisUnbound.
    +     * @param ruleId The Resource representing this rule in the RDF data;
    +     *  should not be null.
    +     * @param graphQuery The query tree corresponding to the "construct" 
text;
    +     *  should not be null.
    +     */
    +    public SpinConstructRule(Resource type, Resource ruleId,
    +            ParsedGraphQuery graphQuery) {
    +        Preconditions.checkNotNull(type);
    +        Preconditions.checkNotNull(ruleId);
    +        Preconditions.checkNotNull(graphQuery);
    +        this.ruleId = ruleId;
    +        this.graphQuery = graphQuery;
    +        // Add the type requirement: ?this must belong to the type
    +        graphQuery.getTupleExpr().visit(new TypeRequirementVisitor("this", 
type));
    +        // Find all statement patterns that could trigger this rule
    +        AntecedentVisitor aVisitor = new AntecedentVisitor();
    +        graphQuery.getTupleExpr().visit(aVisitor);
    +        antecedentStatementPatterns = aVisitor.getAntecedents();
    +        // Construct statement patterns for all possible conclusions of 
this rule
    +        ConstructConsequentVisitor cVisitor = new 
ConstructConsequentVisitor();
    +        graphQuery.getTupleExpr().visit(cVisitor);
    +        consequentStatementPatterns = cVisitor.getConsequents();
    +    }
    +
    +    /**
    +     * Get the URI or bnode associated with this rule in the data.
    +     * @return The rule's identifier.
    +     */
    +    public Resource getId() {
    +        return ruleId;
    +    }
    +
    +    @Override
    +    public String toString() {
    +        return "SpinConstructRule{" + ruleId.stringValue() + "}";
    +    }
    +
    +    @Override
    +    public ParsedGraphQuery getQuery() {
    +        return graphQuery;
    +    }
    +
    +    @Override
    +    public boolean canConclude(StatementPattern sp) {
    +        Preconditions.checkNotNull(sp);
    +        Value s1 = getVarValue(sp.getSubjectVar());
    +        Value p1 = getVarValue(sp.getPredicateVar());
    +        Value o1 = getVarValue(sp.getObjectVar());
    +        Value c1 = getVarValue(sp.getContextVar());
    +        for (StatementPattern consequent : consequentStatementPatterns) {
    +            Value s2 = getVarValue(consequent.getSubjectVar());
    +            Value p2 = getVarValue(consequent.getPredicateVar());
    +            Value o2 = getVarValue(consequent.getObjectVar());
    +            Value c2 = getVarValue(consequent.getContextVar());
    +            if ((s1 == null || s2 == null || s1.equals(s2))
    +                    && (p1 == null || p2 == null || p1.equals(p2))
    +                    && (o1 == null || o2 == null || o1.equals(o2))
    +                    && (c1 == null || c2 == null || c1.equals(c2))) {
    +                return true;
    +            }
    +        }
    +        return false;
    +    }
    +
    +    @Override
    +    public Collection<StatementPattern> getAntecedentPatterns() {
    +        return antecedentStatementPatterns;
    +    }
    +
    +    @Override
    +    public Collection<StatementPattern> getConsequentPatterns() {
    +        return consequentStatementPatterns;
    +    }
    +
    +    @Override
    +    public long execute(AbstractRuleExecutionStrategy strategy,
    +            StatementMetadata metadata) throws ForwardChainException {
    +        metadata.addMetadata(ForwardChainConstants.RYA_DERIVATION_RULE,
    +                RdfToRyaConversions.convertResource(ruleId));
    +        return super.execute(strategy, metadata);
    +    }
    +
    +    private static Value getVarValue(Var var) {
    +        return var == null ? null : var.getValue();
    +    }
    +
    +    private static class TypeRequirementVisitor extends 
QueryModelVisitorBase<RuntimeException> {
    +        private static final Var RDF_TYPE_VAR = new Var("-const-" + 
RDF.TYPE.stringValue(), RDF.TYPE);
    --- End diff --
    
    The hardcoded "-const-"'s will need to go away once RDF4J is added.  But 
that should be handled by 
[RYA-405](https://github.com/apache/incubator-rya/pull/245) so no worries here.


> Implement a forward-chaining rules engine (SPIN)
> ------------------------------------------------
>
>                 Key: RYA-417
>                 URL: https://issues.apache.org/jira/browse/RYA-417
>             Project: Rya
>          Issue Type: New Feature
>            Reporter: Jesse Hatfield
>            Assignee: Jesse Hatfield
>
> Implement a forward-chaining reasoner that:
> * Runs as a batch process
> * Operates on user-defined [SPIN|http://spinrdf.org] rules
> * Inserts derived information back into Rya
> * Iterates until no new information can be derived



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Reply via email to