use of com.inova8.intelligentgraph.intelligentGraphRepository.Graph in project com.inova8.intelligentgraph by peterjohnlawrence.
the class Remote_TutorialTests method test_1.
/**
* Test 1.
*/
@Test
@Order(1)
void test_1() {
try {
IntelligentGraphRepository source = IntelligentGraphRepository.create("http://localhost:8080/rdf4j-server", "tutorial");
// IntelligentGraphRepository source = IntelligentGraphRepository.create("http://host.docker.internal:8080/rdf4j-server","tutorial");
source.prefix("<http://inova8.com/intelligentgraph/example1/>");
source.prefix(RDFS.PREFIX, RDFS.NAMESPACE);
source.removeGraph("<http://inova8.com/intelligentgraph/example1>");
Graph graph = source.addGraph("<http://inova8.com/intelligentgraph/example1>");
Thing aPerson = graph.getThing(":aPerson");
Thing Person = graph.getThing(":Person");
aPerson.addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.7", XSD.DOUBLE).addFact(":hasWeight", "62", XSD.DOUBLE);
assertEquals("http://inova8.com/intelligentgraph/example1/aPerson", aPerson.stringValue());
aPerson.addFact(":hasBMI", "height=_this.getFact(':hasHeight').doubleValue(); _this.getFact(':hasWeight').doubleValue()/(height*height)", SCRIPT.GROOVY);
assertEquals(" 1. Getting facts ':hasBMI' of aPerson <http://inova8.com/intelligentgraph/example1/aPerson>\n" + " 2. ...within contexts: [http://inova8.com/intelligentgraph/example1]\n" + " 1. Evaluating predicate hasBMI <http://inova8.com/intelligentgraph/example1/hasBMI> of aPerson <http://inova8.com/intelligentgraph/example1/aPerson> , by invoking groovy script\n" + " 2. height=_this.getFact(':hasHeight').doubleValue(); _this.getFact(':hasWeight').doubleValue()/(height*height)\n" + "\n" + " 1. Getting facts ':hasHeight' of aPerson <http://inova8.com/intelligentgraph/example1/aPerson>\n" + " 2. Next fact 'http://inova8.com/intelligentgraph/example1/hasHeight' of aPerson <http://inova8.com/intelligentgraph/example1/aPerson> = 1.7\n" + " 3. Returned fact ':hasHeight' of aPerson <http://inova8.com/intelligentgraph/example1/aPerson> = 1.7^^double <http://www.w3.org/2001/XMLSchema#double>\n" + "\n" + " 4. Getting facts ':hasWeight' of aPerson <http://inova8.com/intelligentgraph/example1/aPerson>\n" + " 5. Next fact 'http://inova8.com/intelligentgraph/example1/hasWeight' of aPerson <http://inova8.com/intelligentgraph/example1/aPerson> = 62\n" + " 6. Returned fact ':hasWeight' of aPerson <http://inova8.com/intelligentgraph/example1/aPerson> = 62^^double <http://www.w3.org/2001/XMLSchema#double>\n" + "\n" + " 3. Evaluated hasBMI <http://inova8.com/intelligentgraph/example1/hasBMI> of aPerson <http://inova8.com/intelligentgraph/example1/aPerson> = 21.453287197231838^^double <http://www.w3.org/2001/XMLSchema#double>\n" + " 3. Calculated hasBMI <http://inova8.com/intelligentgraph/example1/hasBMI> of aPerson <http://inova8.com/intelligentgraph/example1/aPerson> = 21.453287197231838^^double <http://www.w3.org/2001/XMLSchema#double>\n" + " 4. Retrieved cached value hasBMI <http://inova8.com/intelligentgraph/example1/hasBMI> of aPerson <http://inova8.com/intelligentgraph/example1/aPerson> = 21.453287197231838^^double <http://www.w3.org/2001/XMLSchema#double>\n" + " 5. Returned fact 'http://inova8.com/intelligentgraph/example1/hasBMI' of aPerson <http://inova8.com/intelligentgraph/example1/aPerson> = 21.453287197231838^^double <http://www.w3.org/2001/XMLSchema#double>", aPerson.traceFact(":hasBMI").asText());
assertEquals("21.453287197231838", aPerson.getFact(":hasBMI").stringValue());
assertEquals(" 1. Getting facts ':hasBMI' of aPerson <http://inova8.com/intelligentgraph/example1/aPerson>\n" + " 2. ...within contexts: [http://inova8.com/intelligentgraph/example1]\n" + " 3. Retrieved cached value hasBMI <http://inova8.com/intelligentgraph/example1/hasBMI> of aPerson <http://inova8.com/intelligentgraph/example1/aPerson> = 21.453287197231838^^double <http://www.w3.org/2001/XMLSchema#double>\n" + " 4. Retrieved cached value hasBMI <http://inova8.com/intelligentgraph/example1/hasBMI> of aPerson <http://inova8.com/intelligentgraph/example1/aPerson> = 21.453287197231838^^double <http://www.w3.org/2001/XMLSchema#double>\n" + " 5. Returned fact 'http://inova8.com/intelligentgraph/example1/hasBMI' of aPerson <http://inova8.com/intelligentgraph/example1/aPerson> = 21.453287197231838^^double <http://www.w3.org/2001/XMLSchema#double>", aPerson.traceFact(":hasBMI").asText());
} catch (Exception e) {
assertEquals("", e.getCause().getMessage());
}
}
use of com.inova8.intelligentgraph.intelligentGraphRepository.Graph in project com.inova8.intelligentgraph by peterjohnlawrence.
the class Query method createNativeLuceneIntelligentGraphRepository.
/**
* Creates the native lucene intelligent graph repository.
*
* @param dir the dir
* @return the org.eclipse.rdf 4 j.repository. repository
* @throws IOException Signals that an I/O exception has occurred.
* @throws SailConfigException the sail config exception
*/
public static org.eclipse.rdf4j.repository.Repository createNativeLuceneIntelligentGraphRepository(String dir) throws IOException, SailConfigException {
File dataDir = new File(dir);
FileUtils.deleteDirectory(dataDir);
IntelligentGraphConfig intelligentGraphConfig = new IntelligentGraphConfig();
IntelligentGraphFactory intelligentGraphFactory = new IntelligentGraphFactory();
IntelligentGraphSail intelligentGraphSail = (IntelligentGraphSail) intelligentGraphFactory.getSail(intelligentGraphConfig);
// IntelligentGraphSail intelligentGraphSail = new IntelligentGraphSail();
LuceneSail lucenesail = new LuceneSail();
lucenesail.setParameter(LuceneSail.LUCENE_RAMDIR_KEY, "true");
Sail baseSail = new NativeStore(dataDir);
lucenesail.setBaseSail(baseSail);
intelligentGraphSail.setBaseSail(lucenesail);
org.eclipse.rdf4j.repository.Repository workingRep = new SailRepository(intelligentGraphSail);
return workingRep;
}
use of com.inova8.intelligentgraph.intelligentGraphRepository.Graph in project com.inova8.intelligentgraph by peterjohnlawrence.
the class Query method createNativeIntelligentGraphRepository.
/**
* Creates the native intelligent graph repository.
*
* @param dir the dir
* @return the org.eclipse.rdf 4 j.repository. repository
* @throws IOException Signals that an I/O exception has occurred.
* @throws SailConfigException the sail config exception
*/
public static org.eclipse.rdf4j.repository.Repository createNativeIntelligentGraphRepository(String dir) throws IOException, SailConfigException {
File dataDir = new File(dir);
FileUtils.deleteDirectory(dataDir);
IntelligentGraphConfig intelligentGraphConfig = new IntelligentGraphConfig();
IntelligentGraphFactory intelligentGraphFactory = new IntelligentGraphFactory();
IntelligentGraphSail intelligentGraphSail = (IntelligentGraphSail) intelligentGraphFactory.getSail(intelligentGraphConfig);
// IntelligentGraphSail intelligentGraphSail = new IntelligentGraphSail();
Sail baseSail = new NativeStore(dataDir);
intelligentGraphSail.setBaseSail(baseSail);
org.eclipse.rdf4j.repository.Repository workingRep = new SailRepository(intelligentGraphSail);
return workingRep;
}
use of com.inova8.intelligentgraph.intelligentGraphRepository.Graph in project com.inova8.intelligentgraph by peterjohnlawrence.
the class Local_TutorialTests method test_2.
/**
* Test 2.
*/
@SuppressWarnings("resource")
@Test
@Order(2)
void test_2() {
try {
Repository workingRep = Query.createNativeLuceneIntelligentGraphRepository("src/test/resources/datadir/Local_TutorialTests2/");
RepositoryConnection conn = workingRep.getConnection();
conn.setNamespace("", "http://inova8.com/intelligentgraph/example2/");
conn.setNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
conn.setNamespace("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
conn.setNamespace("xsd", "http://www.w3.org/2001/XMLSchema#");
IntelligentGraphRepository source = IntelligentGraphRepository.create(workingRep);
source.removeGraph("<http://inova8.com/intelligentgraph/example2>");
Graph graph = source.addGraph("<http://inova8.com/intelligentgraph/example2>");
Thing Person = graph.getThing(":Person");
graph.getThing(":aPerson").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.7", XSD.DOUBLE).addFact(":hasWeight", "62", XSD.DOUBLE);
graph.getThing(":Another1").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.9", XSD.DOUBLE).addFact(":hasWeight", "72", XSD.DOUBLE);
graph.getThing(":Another2").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.7", XSD.DOUBLE).addFact(":hasWeight", "65", XSD.DOUBLE);
graph.getThing(":Another3").addFact(RDF.TYPE, Person).addFact(":hasHeight", "2", XSD.DOUBLE).addFact(":hasWeight", "59", XSD.DOUBLE);
graph.getThing(":Another4").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.8", XSD.DOUBLE).addFact(":hasWeight", "47", XSD.DOUBLE);
graph.getThing(":Another5").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.5", XSD.DOUBLE).addFact(":hasWeight", "70", XSD.DOUBLE);
graph.getThing(":Another6").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.5", XSD.DOUBLE).addFact(":hasWeight", "56", XSD.DOUBLE);
graph.getThing(":Another7").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.7", XSD.DOUBLE).addFact(":hasWeight", "63", XSD.DOUBLE);
graph.getThing(":Another8").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.6", XSD.DOUBLE).addFact(":hasWeight", "66", XSD.DOUBLE);
graph.getThing(":Another9").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.7", XSD.DOUBLE).addFact(":hasWeight", "46", XSD.DOUBLE);
graph.getThing(":Another10").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.9", XSD.DOUBLE).addFact(":hasWeight", "61", XSD.DOUBLE);
graph.getThing(":Another11").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.5", XSD.DOUBLE).addFact(":hasWeight", "61", XSD.DOUBLE);
graph.getThing(":Another12").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.8", XSD.DOUBLE).addFact(":hasWeight", "75", XSD.DOUBLE);
@SuppressWarnings("unused") Thing BMI = graph.getThing(":BMI").addFact(SCRIPT.SCRIPTCODE, "height=_this.getFact(':hasHeight').doubleValue(); _this.getFact(':hasWeight').doubleValue()/(height*height)", SCRIPT.GROOVY);
@SuppressWarnings("unused") Thing relativeBMI = graph.getThing(":RelativeBMI").addFact(SCRIPT.SCRIPTCODE, "double averageBMI = _this.getFact('rdf:type/:averageBMI').doubleValue(); _this.getFact(':hasBMI').doubleValue()/averageBMI;", SCRIPT.GROOVY);
for (Resource person : graph.getThing(":Person").getFacts("^rdf:type")) {
// ((Thing) person)
person.addFact(":hasBMI", "<:BMI>", SCRIPT.GROOVY).addFact(":hasRelativeBMI", "<:RelativeBMI>", SCRIPT.GROOVY);
}
Person.addFact(":averageBMI", "_this.getFacts('^rdf:type/:hasBMI').average()", SCRIPT.GROOVY);
assertEquals("21.523052847377123", Person.getFact(":averageBMI").stringValue());
assertEquals("0.9967585615925397", graph.getThing(":aPerson").getFact(":hasRelativeBMI").stringValue());
conn.setNamespace("xsd", "http://www.w3.org/2001/XMLSchema#");
Person.addFact(":average1.7-1.8BMI", "_this.getFacts(\"^rdf:type[:hasHeight [ ge '1.7'^^xsd:double ; le '1.8'^^xsd:double ]]/:hasBMI\").average()", SCRIPT.GROOVY);
assertEquals("19.885870106938924", Person.getFact(":average1.7-1.8BMI").stringValue());
} catch (Exception e) {
assertEquals("", e.getMessage());
}
}
use of com.inova8.intelligentgraph.intelligentGraphRepository.Graph in project com.inova8.intelligentgraph by peterjohnlawrence.
the class Local_TutorialTests method test_3.
/**
* Test 3.
*/
@SuppressWarnings("resource")
@Test
@Order(3)
void test_3() {
try {
org.eclipse.rdf4j.repository.Repository workingRep = Query.createNativeLuceneIntelligentGraphRepository("src/test/resources/datadir/Local_TutorialTests3/");
RepositoryConnection conn = workingRep.getConnection();
conn.setNamespace("", "http://inova8.com/intelligentgraph/example3/");
conn.setNamespace("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
conn.setNamespace("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
IntelligentGraphRepository source = IntelligentGraphRepository.create(workingRep);
source.removeGraph("<http://inova8.com/intelligentgraph/example3>");
Graph graph = source.addGraph("<http://inova8.com/intelligentgraph/example3>");
Thing Person = graph.getThing(":Person");
@SuppressWarnings("unused") Thing BMI = graph.getThing(":BMI").addFact(SCRIPT.SCRIPTCODE, "height=_this.getFact(':hasHeight').doubleValue(); _this.getFact(':hasWeight').doubleValue()/(height*height)", SCRIPT.GROOVY);
@SuppressWarnings("unused") Thing relativeBMI = graph.getThing(":RelativeBMI").addFact("<http://inova8.com/script/scriptCode>", "double averageBMI = _this.getFact('rdf:type/:averageBMI').doubleValue(); _this.getFact(':hasBMI').doubleValue()/averageBMI;", SCRIPT.GROOVY);
@SuppressWarnings("unused") Thing locationAverageBMI = graph.getThing(":LocationAverageBMI").addFact(SCRIPT.SCRIPTCODE, "_this.getFacts('^:hasLocation/:hasBMI')average()", SCRIPT.GROOVY);
Thing Maidstone = graph.getThing(":Maidstone").addFact(":averageBMI", "<:LocationAverageBMI>", SCRIPT.GROOVY);
Thing Tideswell = graph.getThing(":Tideswell").addFact(":averageBMI", "<:LocationAverageBMI>", SCRIPT.GROOVY);
graph.getThing(":aPerson").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.7", XSD.DOUBLE).addFact(":hasWeight", "62", XSD.DOUBLE).addFact(":hasLocation", Tideswell);
graph.getThing(":Another1").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.9", XSD.DOUBLE).addFact(":hasWeight", "72", XSD.DOUBLE).addFact(":hasLocation", Tideswell);
graph.getThing(":Another2").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.7", XSD.DOUBLE).addFact(":hasWeight", "65", XSD.DOUBLE).addFact(":hasLocation", Maidstone);
graph.getThing(":Another3").addFact(RDF.TYPE, Person).addFact(":hasHeight", "2", XSD.DOUBLE).addFact(":hasWeight", "59", XSD.DOUBLE).addFact(":hasLocation", Tideswell);
graph.getThing(":Another4").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.8", XSD.DOUBLE).addFact(":hasWeight", "47", XSD.DOUBLE).addFact(":hasLocation", Maidstone);
graph.getThing(":Another5").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.5", XSD.DOUBLE).addFact(":hasWeight", "70", XSD.DOUBLE).addFact(":hasLocation", Tideswell);
graph.getThing(":Another6").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.5", XSD.DOUBLE).addFact(":hasWeight", "56", XSD.DOUBLE).addFact(":hasLocation", Maidstone);
graph.getThing(":Another7").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.7", XSD.DOUBLE).addFact(":hasWeight", "63", XSD.DOUBLE).addFact(":hasLocation", Maidstone);
graph.getThing(":Another8").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.6", XSD.DOUBLE).addFact(":hasWeight", "66", XSD.DOUBLE).addFact(":hasLocation", Tideswell);
graph.getThing(":Another9").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.7", XSD.DOUBLE).addFact(":hasWeight", "46", XSD.DOUBLE).addFact(":hasLocation", Maidstone);
graph.getThing(":Another10").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.9", XSD.DOUBLE).addFact(":hasWeight", "61", XSD.DOUBLE).addFact(":hasLocation", Tideswell);
graph.getThing(":Another11").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.5", XSD.DOUBLE).addFact(":hasWeight", "61", XSD.DOUBLE).addFact(":hasLocation", Maidstone);
graph.getThing(":Another12").addFact(RDF.TYPE, Person).addFact(":hasHeight", "1.8", XSD.DOUBLE).addFact(":hasWeight", "75", XSD.DOUBLE).addFact(":hasLocation", Tideswell);
Person.addFact(":averageBMI", "_this.getFacts('^rdf:type/:hasBMI').average()", SCRIPT.GROOVY);
for (Resource person : graph.getThing(":Person").getFacts("^rdf:type")) {
((Thing) person).addFact(":hasBMI", "<:BMI>", SCRIPT.GROOVY).addFact(":hasRelativeBMI", "<:RelativeBMI>", SCRIPT.GROOVY);
}
assertEquals("21.86941453137843", Tideswell.getFact(":averageBMI").stringValue());
} catch (Exception e) {
assertEquals("", e.getMessage());
}
}
Aggregations