Package com.hp.hpl.jena.reasoner.rulesys

Examples of com.hp.hpl.jena.reasoner.rulesys.BindingEnvironment


  }
  /* (non-Javadoc)
   * @see com.hp.hpl.jena.reasoner.rulesys.Builtin#bodyCall(com.hp.hpl.jena.graph.Node[], int, com.hp.hpl.jena.reasoner.rulesys.RuleContext)
   */
  public boolean bodyCall(Node[] args, int length, RuleContext context) {
    BindingEnvironment env = context.getEnv();
    InfGraph graph = context.getGraph();
    Node s = env.getGroundVersion(args[0]);
    Node p = env.getGroundVersion(args[1]);
    Node o = env.getGroundVersion(args[2]);
    try {
      Triple t = Triple.create(s,p,o);
      Reifier r = graph.getReifier();
      Node a = Node.createAnon();
      r.reifyAs(a,t);
      env.bind(args[3],a);
      return true;
    } catch (Exception e) {
      return false;
    }
  }
View Full Code Here


      return false;
    }
  }
 
  public void headAction(Node[] args, int length, RuleContext context) {
    BindingEnvironment env = context.getEnv();
    InfGraph graph = context.getGraph();
    Node s = env.getGroundVersion(args[0]);
    Node p = env.getGroundVersion(args[1]);
    Node o = env.getGroundVersion(args[2]);
    try {
      Triple t = Triple.create(s,p,o);
      context.add(t);
      Node a = Node.createAnon();
      Reifier r = graph.getReifier();
      r.reifyAs(a,t);
      env.bind(args[3],a);
    } catch (Exception e) {
      e.printStackTrace(System.err);
    }   
  }
View Full Code Here

  public String getURI() {
    return "http://www.hp.com/gloze/extension/countValues";
  }

  public boolean bodyCall(Node[] args, int length, RuleContext context) {
    BindingEnvironment env = context.getEnv();
    InfGraph graph = context.getGraph();

    Node s = env.getGroundVersion(args[0]);
    Node p = env.getGroundVersion(args[1]);
    ExtendedIterator i = graph.find(s,p,null);
    int n = i.toSet().size();
   
    TypeMapper tm = TypeMapper.getInstance();
    RDFDatatype type = tm.getTypeByName(XSD.xint.getURI());
    Node count = Node.createLiteral(Integer.toString(n),null,type);
    env.bind(args[2],count);
    return true;
  }
View Full Code Here

  }
  /* (non-Javadoc)
   * @see com.hp.hpl.jena.reasoner.rulesys.Builtin#bodyCall(com.hp.hpl.jena.graph.Node[], int, com.hp.hpl.jena.reasoner.rulesys.RuleContext)
   */
  public boolean bodyCall(Node[] args, int length, RuleContext context) {
    BindingEnvironment env = context.getEnv();
    Node value = env.getGroundVersion(args[0]);
    TypeMapper tm = TypeMapper.getInstance();
    RDFDatatype type = tm.getTypeByName(PrintUtil.expandQname(args[2].toString()));
    Node cast = Node.createLiteral(value.getLiteral().getValue().toString(),null,type);
    env.bind(args[1],cast);
    return type.isValid(value.getLiteral().getValue().toString());
  }
View Full Code Here

  }
  /* (non-Javadoc)
   * @see com.hp.hpl.jena.reasoner.rulesys.Builtin#bodyCall(com.hp.hpl.jena.graph.Node[], int, com.hp.hpl.jena.reasoner.rulesys.RuleContext)
   */
  public boolean bodyCall(Node[] args, int length, RuleContext context) {
    BindingEnvironment env = context.getEnv();
    Node namespace = env.getGroundVersion(args[0]);
    Node localName = env.getGroundVersion(args[1]);
    try {
      String ns = namespace.getLiteral().getValue().toString();
      String ln;
      try {
        ln = localName.getLiteral().getValue().toString();
      }
      // if the value is at odds with the datatype
      catch (Exception e) { ln = localName.getLiteral().toString(); }
      ln = URLEncoder.encode(ln,"UTF-8");
      URI uri = new URI(ns+(!ns.endsWith("#")&&!ns.endsWith("/")?"#":"")+ln);
      env.bind(args[2],Node.createURI(uri.toString()));
      return true;
    } catch (Exception e) {
      return false;
    }
  }
View Full Code Here

        String pattern = getString( getArg(1, args, context), context );
        Matcher m = Pattern.compile(pattern).matcher(text);
        if ( ! m.matches()) return false;
        if (length > 2) {
            // bind any capture groups
            BindingEnvironment env = context.getEnv();
            for (int i = 0; i < Math.min(length-2, m.groupCount()); i++) {
                String gm = m.group(i+1);
                Node match =  (gm != null) ? NodeFactory.createLiteral( gm ) : NodeFactory.createLiteral("");
                if ( !env.bind(args[i+2], match) ) return false;
            }
        }
        return true;
    }
View Full Code Here

     * the current environment
     */
    @Override
    public boolean bodyCall(Node[] args, int length, RuleContext context) {
        checkArgs(length, context);
        BindingEnvironment env = context.getEnv();
        Node now = NodeFactory.createLiteral( LiteralLabelFactory.create(new XSDDateTime(Calendar.getInstance())) );
        return env.bind(args[0], now);
    }
View Full Code Here

    /**
     * Execute a single rule firing.
     */
    public static void execute(RETERuleContext context, boolean isAdd) {
        Rule rule = context.getRule();
        BindingEnvironment env = context.getEnv();
        ForwardRuleInfGraphI infGraph = (ForwardRuleInfGraphI)context.getGraph();
        if (infGraph.shouldTrace()) {
            logger.info("Fired rule: " + rule.toShortString());
        }
        RETEEngine engine = context.getEngine();
        engine.incRuleCount();
        List<Triple> matchList = null;
        if (infGraph.shouldLogDerivations() && isAdd) {
            // Create derivation record
            matchList = new ArrayList<Triple>(rule.bodyLength());
            for (int i = 0; i < rule.bodyLength(); i++) {
                Object clause = rule.getBodyElement(i);
                if (clause instanceof TriplePattern) {
                    matchList.add(env.instantiate((TriplePattern)clause));
                }
            }
        }
        for (int i = 0; i < rule.headLength(); i++) {
            Object hClause = rule.getHeadElement(i);
            if (hClause instanceof TriplePattern) {
                Triple t = env.instantiate((TriplePattern) hClause);
                // Used to filter out triples with literal subjects
                // but this is not necessary
                // if (!t.getSubject().isLiteral()) {
                    // Only add the result if it is legal at the RDF level.
                    // E.g. RDFS rules can create assertions about literals
View Full Code Here

     * the current environment
     */
    @Override
    public boolean bodyCall(Node[] args, int length, RuleContext context) {
        checkArgs(length, context);
        BindingEnvironment env = context.getEnv();
        Node n1 = getArg(0, args, context);
        Node n2 = getArg(1, args, context);
        if (n1.isLiteral() && n2.isLiteral()) {
            Object v1 = n1.getLiteralValue();
            Object v2 = n2.getLiteralValue();
            Node sum = null;
            if (v1 instanceof Number && v2 instanceof Number) {
                Number nv1 = (Number)v1;
                Number nv2 = (Number)v2;
                if (v1 instanceof Float || v1 instanceof Double
                ||  v2 instanceof Float || v2 instanceof Double) {
                    sum = Util.makeDoubleNode(nv1.doubleValue() / nv2.doubleValue());
                } else {
                    sum = Util.makeLongNode(nv1.longValue() / nv2.longValue());
                }
                return env.bind(args[2], sum);
            }
        }
        // Doesn't (yet) handle partially bound cases
        return false;
    }
View Full Code Here

   
    static class DummyRuleContext implements RuleContext {

        @Override
        public BindingEnvironment getEnv() {
            return new BindingEnvironment() {
               
                @Override
                public Triple instantiate(TriplePattern pattern) {
                    // TODO Auto-generated method stub
                    return null;
View Full Code Here

TOP

Related Classes of com.hp.hpl.jena.reasoner.rulesys.BindingEnvironment

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.