<commit_before>/*
 * 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.jena.sparql.api;

import java.util.Iterator;

import org.apache.jena.atlas.iterator.Iter ;
import org.apache.jena.atlas.junit.BaseTest;
import org.apache.jena.graph.Graph ;
import org.apache.jena.graph.Node ;
import org.apache.jena.graph.Triple;
import org.apache.jena.query.* ;
import org.apache.jena.rdf.model.* ;
import org.apache.jena.sparql.core.DatasetGraph;
import org.apache.jena.sparql.core.DatasetGraphFactory;
import org.apache.jena.sparql.core.Quad;
import org.apache.jena.sparql.graph.GraphFactory;
import org.apache.jena.sparql.util.IsoMatcher;
import org.apache.jena.sparql.util.ModelUtils;
import org.apache.jena.vocabulary.OWL;
import org.apache.jena.vocabulary.RDF;
import org.junit.Test;

public class TestAPI extends BaseTest
{
    private static final String ns = "http://example/ns#" ;
    
    static Model m = GraphFactory.makeJenaDefaultModel() ;
    static Resource r1 = m.createResource() ;
    static Property p1 = m.createProperty(ns+"p1") ;
    static Property p2 = m.createProperty(ns+"p2") ;
    static Property p3 = m.createProperty(ns+"p3") ;
    static Model dft = GraphFactory.makeJenaDefaultModel() ;
    static Resource s = dft.createResource(ns+"s") ;
    static Property p = dft.createProperty(ns+"p") ;
    static Resource o = dft.createResource(ns+"o") ;
    static Resource g1 = dft.createResource(ns+"g1") ;
    static Dataset d = null;
    static  {
        m.add(r1, p1, "x1") ;
        m.add(r1, p2, "X2") ; // NB Capital
        m.add(r1, p3, "y1") ;
        dft.add(s, p, o) ;
        d = DatasetFactory.create(dft);
        d.addNamedModel(g1.getURI(), m);
    }
    
    @Test public void testInitialBindingsConstruct1()
    {
        try(QueryExecution qExec = makeQExec("CONSTRUCT {?s ?p ?z} {?s ?p 'x1'}")) {
            QuerySolutionMap init = new QuerySolutionMap() ;
            init.add("z", m.createLiteral("zzz"));
            
            qExec.setInitialBinding(init) ;
            Model r = qExec.execConstruct() ;
        
            assertTrue("Empty model", r.size() > 0 ) ;
        
            Property p1 = m.createProperty(ns+"p1") ;
        
            assertTrue("Empty model", r.contains(null,p1, init.get("z"))) ; 
        }
    }
    
    @Test public void testInitialBindingsConstruct2()
    {
        try(QueryExecution qExec = makeQExec("CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }")) {
            QuerySolutionMap init = new QuerySolutionMap() ;
            init.add("o", m.createLiteral("x1"));
            
            qExec.setInitialBinding(init) ;
            Model r = qExec.execConstruct() ;
        
            assertTrue("Empty model", r.size() > 0 ) ;
        
            Property p1 = m.createProperty(ns+"p1") ;
        
            assertTrue("Empty model", r.contains(null, p1, init.get("x1"))) ; 
        }
    }

    // This test is slightly dubious. It is testing that the model for the
    // resource in the result is the same object as the model supplied ot the
    // query.
    //
    // It happens to be true for DatasetImpl and the default model but that's
    // about it. It is not part of the contract of query/datasets.
    //
    // Left as an active test so the assumption is tested (it has been true for
    // many years). 
    //
    // Using the Resource.getXXX and Resource.listXXX operations is dubious if
    // there are named graphs and that has always been the case.
    
    @Test public void test_API1()
    {
        try(QueryExecution qExec = makeQExec("SELECT * {?s ?p ?o}")) {
            ResultSet rs = qExec.execSelect() ;
            assertTrue("No results", rs.hasNext()) ;
            QuerySolution qs = rs.nextSolution() ;
            Resource qr = qs.getResource("s") ;
            assertSame("Not the same model as queried", qr.getModel(), m) ;
        }
    }
    
    @Test public void testInitialBindings0()
    {
        QuerySolutionMap smap1 = new QuerySolutionMap() ;
        QuerySolutionMap smap2 = new QuerySolutionMap() ;
        smap1.add("o", m.createLiteral("y1"));
        smap2.addAll(smap1) ;
        assertTrue(smap2.contains("o")) ;
        smap2.clear() ;
        assertFalse(smap2.contains("o")) ;
        assertTrue(smap1.contains("o")) ;
        
        QuerySolutionMap smap3 = new QuerySolutionMap() ;
        smap2.addAll((QuerySolution)smap1) ;
        assertTrue(smap2.contains("o")) ;
    }
    
    @Test public void testInitialBindings1()
    {
        QueryExecution qExec = makeQExec("SELECT * {?s ?p ?o}") ;
        QuerySolutionMap init = new QuerySolutionMap() ;
        init.add("o", m.createLiteral("y1"));
        qExec.setInitialBinding(init) ;
        int count = queryAndCount(qExec) ;
        assertEquals("Initial binding didn't restrict query properly", 1, count) ;<commit_msg>Fix up testAPI1() to test by value, not identity<commit_after>/*
 * 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.jena.sparql.api;

import java.util.Iterator;
import java.util.Set ;

import org.apache.jena.atlas.iterator.Iter ;
import org.apache.jena.atlas.junit.BaseTest;
import org.apache.jena.graph.Graph ;
import org.apache.jena.graph.Node ;
import org.apache.jena.graph.Triple;
import org.apache.jena.query.* ;
import org.apache.jena.rdf.model.* ;
import org.apache.jena.sparql.core.DatasetGraph;
import org.apache.jena.sparql.core.DatasetGraphFactory;
import org.apache.jena.sparql.core.Quad;
import org.apache.jena.sparql.graph.GraphFactory;
import org.apache.jena.sparql.util.IsoMatcher;
import org.apache.jena.sparql.util.ModelUtils;
import org.apache.jena.vocabulary.OWL;
import org.apache.jena.vocabulary.RDF;
import org.junit.Test;

public class TestAPI extends BaseTest
{
    private static final String ns = "http://example/ns#" ;
    
    static Model m = GraphFactory.makeJenaDefaultModel() ;
    static Resource r1 = m.createResource() ;
    static Property p1 = m.createProperty(ns+"p1") ;
    static Property p2 = m.createProperty(ns+"p2") ;
    static Property p3 = m.createProperty(ns+"p3") ;
    static Model dft = GraphFactory.makeJenaDefaultModel() ;
    static Resource s = dft.createResource(ns+"s") ;
    static Property p = dft.createProperty(ns+"p") ;
    static Resource o = dft.createResource(ns+"o") ;
    static Resource g1 = dft.createResource(ns+"g1") ;
    static Dataset d = null;
    static  {
        m.add(r1, p1, "x1") ;
        m.add(r1, p2, "X2") ; // NB Capital
        m.add(r1, p3, "y1") ;
        dft.add(s, p, o) ;
        d = DatasetFactory.create(dft);
        d.addNamedModel(g1.getURI(), m);
    }
    
    @Test public void testInitialBindingsConstruct1()
    {
        try(QueryExecution qExec = makeQExec("CONSTRUCT {?s ?p ?z} {?s ?p 'x1'}")) {
            QuerySolutionMap init = new QuerySolutionMap() ;
            init.add("z", m.createLiteral("zzz"));
            
            qExec.setInitialBinding(init) ;
            Model r = qExec.execConstruct() ;
        
            assertTrue("Empty model", r.size() > 0 ) ;
        
            Property p1 = m.createProperty(ns+"p1") ;
        
            assertTrue("Empty model", r.contains(null,p1, init.get("z"))) ; 
        }
    }
    
    @Test public void testInitialBindingsConstruct2()
    {
        try(QueryExecution qExec = makeQExec("CONSTRUCT { ?s ?p ?o } WHERE { ?s ?p ?o }")) {
            QuerySolutionMap init = new QuerySolutionMap() ;
            init.add("o", m.createLiteral("x1"));
            
            qExec.setInitialBinding(init) ;
            Model r = qExec.execConstruct() ;
        
            assertTrue("Empty model", r.size() > 0 ) ;
        
            Property p1 = m.createProperty(ns+"p1") ;
        
            assertTrue("Empty model", r.contains(null, p1, init.get("x1"))) ; 
        }
    }

    // The original test (see commented out "assertSame) is test is now bogus.
    // DatasetImpl no longer caches the default model as that caused problems.
    //
    // This is testing that the model for the resource in the result is the
    // same object as the model supplied to the query.
    // "Same" here means "same contents" includign blank nodes.
    // 
    // it used to be that this tested whether they were the same object. 
    // That is dubious and no longer true even for DatasetImpl (teh default mode
    // is not cached but recreated on demand so theer are no problems with
    // transaction boundaries).    
    // 
    // Left as an active test so the assumption is tested (it has been true for
    // many years). 
    //
    // Using the Resource.getXXX and Resource.listXXX operations is dubious if
    // there are named graphs and that has always been the case.
    
    @Test public void test_API1()
    {
        try(QueryExecution qExec = makeQExec("SELECT * {?s ?p ?o}")) {
            ResultSet rs = qExec.execSelect() ;
            assertTrue("No results", rs.hasNext()) ;
            QuerySolution qs = rs.nextSolution() ;
            Resource qr = qs.getResource("s") ;
            //assertSame("Not the same model as queried", qr.getModel(), m) ;
            Set<Statement> s1 = qr.getModel().listStatements().toSet() ;
            Set<Statement> s2 = m.listStatements().toSet() ;
            assertEquals(s1,s2) ;
        }
    }
    
    @Test public void testInitialBindings0()
    {
        QuerySolutionMap smap1 = new QuerySolutionMap() ;
        QuerySolutionMap smap2 = new QuerySolutionMap() ;
        smap1.add("o", m.createLiteral("y1"));
        smap2.addAll(smap1) ;
        assertTrue(smap2.contains("o")) ;
        smap2.clear() ;
        assertFalse(smap2.contains("o")) ;
        assertTrue(smap1.contains("o")) ;
        
        QuerySolutionMap smap3 = new QuerySolutionMap() ;
        smap2.addAll((QuerySolution)smap1) ;
        assertTrue(smap2.contains("o")) ;
    }
    
    @Test public void testInitialBindings1()
    {
        QueryExecution qExec = makeQExec("SELECT * {?s ?p ?o}") ;
        QuerySolutionMap init = new QuerySolutionMap() ;
        init.add("o", m.createLiteral("y1"));
        qExec.setInitialBinding(init) ;
        int count = queryAndCount(qExec) ;
        assertEquals("Initial binding didn't restrict query properly", 1, count) ;
{'fraction_non_alphanumeric': 0.11682561307901908, 'fraction_numerical': 0.009792234332425069, 'mean_word_length': 2.5950413223140494, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4377679', 'n_tokens_mistral': 3585, 'n_tokens_neox': 3550, 'n_words': 990}